< prev index next >

test/jdk/java/lang/ClassLoader/getResource/GetResource.java

Print this page

  1 /*
  2  * Copyright (c) 2014, 2018, 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.Map;
 44 import java.util.stream.Collectors;
 45 import java.util.stream.Stream;
 46 
 47 import jdk.test.lib.JDKToolFinder;
 48 import static jdk.test.lib.process.ProcessTools.*;
 49 
 50 import org.testng.annotations.BeforeTest;
 51 import org.testng.annotations.DataProvider;
 52 import org.testng.annotations.Test;
 53 
 54 public class GetResource {
 55     private static final Path CWD = Paths.get(System.getProperty("user.dir"));
 56     private static final String DIR_A = "a";
 57     private static final String DIR_B = "b";
 58 
 59     private static final String RESOURCE_NAME = "test.properties";
 60     private static final String GETRESOURCE_CLASS = "GetResource.class";
 61 
 62     public static void main(String... args) {
 63         String expect = args[0] + "/" + RESOURCE_NAME;
 64         URL url = GetResource.class.getResource(RESOURCE_NAME);
 65         System.out.println("getResource found: " + url);
 66         if (!url.toString().endsWith(expect)) {
 67             throw new RuntimeException(url + " != expected resource " + expect);

127 
128             new Object[] { List.of("-Xbootclasspath/a:" + dirB), "b"},
129             // empty path in first element
130             new Object[] { List.of("-Xbootclasspath/a:" + File.pathSeparator + dirB), "b"},
131 
132             new Object[] { List.of("-cp", File.pathSeparator), "a"},
133             new Object[] { List.of("-cp", dirB), "b"},
134             new Object[] { List.of("-cp", File.pathSeparator + dirB), "a"},
135         };
136     }
137 
138     @Test(dataProvider = "dirA")
139     public void testCurrentDirA(List<String> options, String expected) throws Throwable {
140         // current working directory is "a"
141         runTest(CWD.resolve(DIR_A), options, expected);
142     }
143 
144     private void runTest(Path dir, List<String> options, String expected)
145         throws Throwable
146     {
147         String javapath = JDKToolFinder.getJDKTool("java");
148 
149         List<String> cmdLine = new ArrayList<>();
150         cmdLine.add(javapath);
151         options.forEach(cmdLine::add);
152 
153         cmdLine.add("GetResource");
154         cmdLine.add(expected);
155 
156         System.out.println("Command line: " + cmdLine);
157         ProcessBuilder pb =
158             new ProcessBuilder(cmdLine.stream().toArray(String[]::new));
159 
160         // change working directory
161         pb.directory(dir.toFile());
162 
163         // remove CLASSPATH environment variable
164         Map<String,String> env = pb.environment();
165         String value = env.remove("CLASSPATH");
166 
167         executeCommand(pb).shouldHaveExitValue(0);
168     }
169 
170 }

  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);

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 }
< prev index next >