8315485: (fs) Move java/nio/file/Path/Misc.java tests into java/nio/file/Path/PathOps.java

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2023-09-15 15:15:46 +00:00
parent 3c743cfea0
commit 8dfde28b28
2 changed files with 61 additions and 91 deletions
test/jdk/java/nio/file/Path

@ -1,88 +0,0 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 4313887 6838333 7029979
* @summary Unit test for miscellenous java.nio.file.Path methods
* @library .. /test/lib
* @build jdk.test.lib.Platform
* @run main Misc
*/
import java.io.*;
import java.nio.file.*;
import jdk.test.lib.Platform;
public class Misc {
public static void main(String[] args) throws IOException {
Path dir = TestUtil.createTemporaryDirectory();
try {
// equals and hashCode methods
testEqualsAndHashCode();
// toFile method
testToFile(dir);
} finally {
TestUtil.removeAll(dir);
}
}
/**
* Exercise equals and hashCode methods
*/
static void testEqualsAndHashCode() {
Path thisFile = Paths.get("this");
Path thatFile = Paths.get("that");
assertTrue(thisFile.equals(thisFile));
assertTrue(!thisFile.equals(thatFile));
assertTrue(!thisFile.equals(null));
assertTrue(!thisFile.equals(new Object()));
Path likeThis = Paths.get("This");
if (Platform.isWindows()) {
// case insensitive
assertTrue(thisFile.equals(likeThis));
assertTrue(thisFile.hashCode() == likeThis.hashCode());
} else {
// case senstive
assertTrue(!thisFile.equals(likeThis));
}
}
/**
* Exercise toFile method
*/
static void testToFile(Path dir) throws IOException {
File d = dir.toFile();
assertTrue(d.toString().equals(dir.toString()));
assertTrue(d.toPath().equals(dir));
}
static void assertTrue(boolean okay) {
if (!okay)
throw new RuntimeException("Assertion Failed");
}
}

@ -22,16 +22,22 @@
*/
/* @test
* @bug 4313887 6838333 6925932 7006126 8037945 8072495 8140449 8254876 8262742
* 8298478
* @bug 4313887 6838333 6925932 7006126 7029979 8037945 8072495 8140449
* 8254876 8262742 8298478
* @summary Unit test for java.nio.file.Path path operations
* @library .. /test/lib
* @build jdk.test.lib.Platform
* @run main PathOps
*/
import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import jdk.test.lib.Platform;
public class PathOps {
static final java.io.PrintStream out = System.out;
@ -239,6 +245,34 @@ public class PathOps {
return this;
}
PathOps equals(String other) {
out.format("test equals %s\n", other);
checkPath();
Path that = Path.of(other);
check(that, path.toString());
check(path.hashCode() == that.hashCode(), true);
return this;
}
PathOps notEquals(Object other) {
out.format("test not equals %s\n", other);
checkPath();
check(path.equals(other), false);
return this;
}
PathOps toFile() {
out.println("check toFile");
checkPath();
File file = path.toFile();
check(file.toString(), path.toString());
check(file.toPath().equals(path), true);
return this;
}
PathOps string(String expected) {
out.println("check string representation");
checkPath();
@ -1444,6 +1478,18 @@ public class PathOps {
.parent(null)
.name(null);
// equals
test("this")
.equals("this")
.notEquals(Path.of("that"))
.notEquals(null)
.notEquals(new Object())
.equals(Path.of("This"));
// toFile
test("C:\\foo\\bar\\gus")
.toFile();
// invalid
test(":\\foo")
.invalid();
@ -2121,6 +2167,18 @@ public class PathOps {
test("/foo/bar/gus/../..")
.normalize("/foo");
// equals
test("this")
.equals("this")
.notEquals(Path.of("that"))
.notEquals(null)
.notEquals(new Object())
.notEquals(Path.of("This"));
// toFile
test("/foo/bar/gus")
.toFile();
// invalid
test("foo\u0000bar")
.invalid();
@ -2198,7 +2256,7 @@ public class PathOps {
// operating system specific
String osname = System.getProperty("os.name");
if (osname.startsWith("Windows")) {
if (Platform.isWindows()) {
doWindowsTests();
} else {
doUnixTests();