1 /*
  2  * Copyright (c) 2014, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 6760902
 27  * @library /test/lib
 28  * @build jdk.test.lib.process.ProcessTools
 29  * @run testng GetResource
 30  * @summary Empty path on bootclasspath is not default to current working
 31  *          directory for both class lookup and resource lookup whereas
 32  *          empty path on classpath is default to current working directory.
 33  */
 34 
 35 import java.io.File;
 36 import java.io.IOException;
 37 import java.net.URL;
 38 import java.nio.file.Files;
 39 import java.nio.file.Path;
 40 import java.nio.file.Paths;
 41 import java.util.ArrayList;
 42 import java.util.List;
 43 import java.util.stream.Collectors;
 44 import java.util.stream.Stream;
 45 
 46 import static jdk.test.lib.process.ProcessTools.*;
 47 
 48 import org.testng.annotations.BeforeTest;
 49 import org.testng.annotations.DataProvider;
 50 import org.testng.annotations.Test;
 51 
 52 public class GetResource {
 53     private static final Path CWD = Paths.get(System.getProperty("user.dir"));
 54     private static final String DIR_A = "a";
 55     private static final String DIR_B = "b";
 56 
 57     private static final String RESOURCE_NAME = "test.properties";
 58     private static final String GETRESOURCE_CLASS = "GetResource.class";
 59 
 60     public static void main(String... args) {
 61         String expect = args[0] + "/" + RESOURCE_NAME;
 62         URL url = GetResource.class.getResource(RESOURCE_NAME);
 63         System.out.println("getResource found: " + url);
 64         if (!url.toString().endsWith(expect)) {
 65             throw new RuntimeException(url + " != expected resource " + expect);
 66         }
 67 
 68         url = ClassLoader.getSystemResource(RESOURCE_NAME);
 69         System.out.println("getSystemResource found: " + url);
 70         if (!url.toString().endsWith(expect)) {
 71             throw new RuntimeException(url + " != expected resource " + expect);
 72         }
 73     }
 74 
 75     @BeforeTest
 76     public void setup() throws IOException {
 77         // setup two directories "a" and "b"
 78         // each directory contains both test.properties and this test class
 79         Path testSrc = Paths.get(System.getProperty("test.src"));
 80         Path testClasses = Paths.get(System.getProperty("test.classes"));
 81 
 82         Files.createDirectories(Paths.get(DIR_A));
 83         Files.createDirectories(Paths.get(DIR_B));
 84 
 85         Files.copy(testSrc.resolve(RESOURCE_NAME),
 86                    Paths.get(DIR_A, RESOURCE_NAME));
 87         Files.copy(testSrc.resolve(RESOURCE_NAME),
 88                    Paths.get(DIR_B, RESOURCE_NAME));
 89 
 90         Files.copy(testClasses.resolve(GETRESOURCE_CLASS),
 91                    Paths.get(DIR_A, GETRESOURCE_CLASS));
 92         Files.copy(testClasses.resolve(GETRESOURCE_CLASS),
 93                    Paths.get(DIR_B, GETRESOURCE_CLASS));
 94     }
 95 
 96     private String concat(String... dirs) {
 97         return Stream.of(dirs).collect(Collectors.joining(File.pathSeparator));
 98     }
 99 
100     @DataProvider
101     public Object[][] options() {
102         return new Object[][] {
103             new Object[] { List.of("-Xbootclasspath/a:a"), "a"},
104             new Object[] { List.of("-Xbootclasspath/a:b"), "b"},
105             new Object[] { List.of("-Xbootclasspath/a:" + concat("a", "b")), "a"},
106             new Object[] { List.of("-Xbootclasspath/a:" + concat("b", "a")), "b"},
107 
108             new Object[] { List.of("-cp", "a"), "a"},
109             new Object[] { List.of("-cp", "b"), "b"},
110             new Object[] { List.of("-cp", concat("a", "b")), "a"},
111             new Object[] { List.of("-cp", concat("b", "a")), "b"},
112         };
113     }
114 
115     @Test(dataProvider = "options")
116     public void test(List<String> options, String expected) throws Throwable {
117         runTest(CWD, options, expected);
118     }
119 
120     @DataProvider
121     public Object[][] dirA() {
122         String dirB = ".." + File.separator + "b";
123         return new Object[][] {
124             new Object[] { List.of("-Xbootclasspath/a:."), "a"},
125 
126             new Object[] { List.of("-Xbootclasspath/a:" + dirB), "b"},
127             // empty path in first element
128             new Object[] { List.of("-Xbootclasspath/a:" + File.pathSeparator + dirB), "b"},
129 
130             new Object[] { List.of("-cp", File.pathSeparator), "a"},
131             new Object[] { List.of("-cp", dirB), "b"},
132             new Object[] { List.of("-cp", File.pathSeparator + dirB), "a"},
133         };
134     }
135 
136     @Test(dataProvider = "dirA")
137     public void testCurrentDirA(List<String> options, String expected) throws Throwable {
138         // current working directory is "a"
139         runTest(CWD.resolve(DIR_A), options, expected);
140     }
141 
142     private void runTest(Path dir, List<String> options, String expected)
143         throws Throwable
144     {
145         List<String> cmdLine = new ArrayList<>();
146         options.forEach(cmdLine::add);
147 
148         cmdLine.add("GetResource");
149         cmdLine.add(expected);
150         ProcessBuilder pb = createTestJavaProcessBuilder(cmdLine);
151         pb.directory(dir.toFile()); // change working directory
152         pb.environment().remove("CLASSPATH"); // remove CLASSPATH environment variable
153         executeCommand(pb).shouldHaveExitValue(0);
154     }
155 
156 }