--- /dev/null 2018-09-07 16:39:11.960000000 -0400 +++ new/test/jdk/java/nio/file/Path/ParsePathTest.java 2018-09-10 14:12:44.911490752 -0400 @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2018, 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. + */ + +import java.lang.System; +import java.io.File; +import java.nio.file.Path; +import java.util.List; +import java.util.stream.Collectors; + +import java.nio.file.Paths; + +import jdk.internal.util.PathParser; +import org.testng.Assert; +import org.testng.IMethodInstance; +import org.testng.IMethodInterceptor; +import org.testng.TestListenerAdapter; +import org.testng.TestNG; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +/* + * @test + * @bug 4463345 4244670 8030781 + * @summary Tests of parsing paths via public and internal APIs + * @modules java.base/jdk.internal.util:+open + * @run testng/othervm ParsePathTest + */ + +@Test +public class ParsePathTest { + + static final String SP = File.pathSeparator; + + @DataProvider(name = "pathData") + static Object[][] pathData() { + if (System.getProperty("os.name").contains("Windows")) { + return new Object[][]{ + // Common path lists + {"", List.of(".")}, + {SP, List.of(".", ".")}, + {"a" + SP, List.of("a", ".")}, + {SP + "b", List.of(".", "b")}, + {"a" + SP + SP + "b", List.of("a", ".", "b")}, + + // on Windows parts of paths may be quoted + {"\"\"", List.of(".")}, + {"\"\"" + SP, List.of(".", ".")}, + {SP + "\"\"", List.of(".", ".")}, + {"a" + SP + "\"b\"" + SP, List.of("a", "b", ".")}, + {SP + "\"a\"" + SP + SP + "b", List.of(".", "a", ".", "b")}, + {"\"a\"" + SP + "\"b\"", List.of("a", "b")}, + {"\"/a/\"b" + SP + "c", List.of("/a/b", "c")}, + {"\"/a;b\"" + SP + "c", List.of("/a;b", "c")}, + {"\"/a" + SP + "b\"" + SP + "c", List.of("/a" + SP + "b", "c")}, + {"/\"a\"\";\"\"b\"" + SP + "\"c\"", List.of("/a;b", "c")}, + + // Unmatched trailing quotes + {"\"c\"" + SP + "/\"a\"\";\"\"b", List.of("c", "/a;b")}, + {"\"c\"" + SP + "/\"a\"\";b", List.of("c", "/a;b")}, + + }; + } else { + return new Object[][]{ + {"", List.of(".")}, + {SP, List.of(".", ".")}, + {"a" + SP, List.of("a", ".")}, + {SP + "b", List.of(".", "b")}, + {"a" + SP + SP + "b", List.of("a", ".", "b")}, + }; + } + } + + /** + * Test public API that produces lists of Path. + */ + @Test(dataProvider = "pathData") + static void checkPathToPaths(String path, Listexpected) { + List newExpected = expected.stream() + .filter(s -> !s.equals(".")) + .map( s -> Path.of(s)) + .collect(Collectors.toList()); + List s = Paths.pathToPaths(path); + Assert.assertEquals(s, newExpected, path + " as " + newExpected.toString()); + } + + /** + * Test public API that produces lists of String. + */ + @Test(dataProvider = "pathData") + static void checkpathToStrings(String path, Listexpected) { + List newExpected = expected.stream() + .filter(s -> !s.equals(".")) + .collect(Collectors.toList()); + List s = Paths.pathToStrings(path); + Assert.assertEquals(s, newExpected, path + " as " + newExpected.toString()); + } + + /** + * Test internal API that parses to arrays of String with replaced empty path. + * @param path a path + * @param expected a list of the expected path strings + */ + @Test(dataProvider = "pathData") + static void checkpathToStringsRepl(String path, List expected) { + List s = List.of(PathParser.parsePath(path, ".")); + Assert.assertEquals(s, expected, path + " as " + expected.toString()); + } + + /** + * Test internal API that parses to arrays of String omitting empty paths. + * @param path a path + * @param expected a list of the expected path strings + */ + @Test(dataProvider = "pathData") + static void checkpathToStringsNoRepl(String path, Listexpected) { + List newExpected = expected.stream() + .filter(s -> !s.equals(".")) + .collect(Collectors.toList()); + List s = List.of(PathParser.parsePath(path, null)); + Assert.assertEquals(s, newExpected, path + " as " + newExpected.toString()); + } + + + /** + * Main to allow stand alone execution. + * @param args command line args - none expected + */ + @SuppressWarnings("raw_types") + @Test(enabled=false) + public static void main(String[] args) { + TestListenerAdapter tla = new TestListenerAdapter(); + + Class[] testclass = {ParsePathTest.class}; + TestNG testng = new TestNG(); + testng.setTestClasses(testclass); + testng.addListener(tla); + if (args.length > 0) { + IMethodInterceptor intercept = (m, c) -> { + List x = m.stream() + .filter(m1 -> m1.getMethod().getMethodName().contains(args[0])) + .collect(Collectors.toList()); + return x; + }; + testng.setMethodInterceptor(intercept); + } + testng.run(); + tla.getPassedTests() + .stream().forEach(t -> System.out.printf("Passed: %s%s%n", t.getName(), + List.of(t.getParameters()))); + tla.getFailedTests() + .stream().forEach(t -> System.out.printf("Failed: %s%s%n", t.getName(), + List.of(t.getParameters()))); + } +}