1 /* 2 * Copyright (c) 2020, 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 8223260 27 * @summary NamingManager should cache InitialContextFactory 28 * @library /test/lib /lib/testlibrary 29 * @build JarUtils jdk.test.lib.process.* 30 * FactoryCacheTest 31 * DummyContextFactory 32 * DummyContextFactory2 33 * @run main FactoryCacheTest 34 */ 35 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.util.ArrayList; 39 import java.util.List; 40 41 import jdk.test.lib.JDKToolFinder; 42 import jdk.test.lib.Utils; 43 import jdk.test.lib.process.ProcessTools; 44 45 import static java.nio.file.StandardOpenOption.CREATE; 46 import static java.util.Arrays.asList; 47 48 import static jdk.test.lib.Utils.TEST_CLASSES; 49 50 public class FactoryCacheTest { 51 52 private static final Path SPIJAR = Path.of("testDir", "ContextFactory.jar"); 53 private static final String SPIJAR_CP = SPIJAR.toAbsolutePath().toString(); 54 main(String[] args)55 public static void main(String[] args) throws Throwable { 56 List<String> argLine = new ArrayList<>(); 57 argLine.add(JDKToolFinder.getJDKTool("java")); 58 argLine.addAll(asList(Utils.getTestJavaOpts())); 59 argLine.addAll(List.of("-cp", TEST_CLASSES)); 60 argLine.addAll(List.of("-Durl.dir=" + TEST_CLASSES)); 61 argLine.add("DummyContextFactory"); 62 63 ProcessTools.executeCommand(argLine.stream() 64 .filter(t -> !t.isEmpty()) 65 .toArray(String[]::new)) 66 .shouldHaveExitValue(0); 67 68 // now test the ServiceLoader approach 69 setupService(); 70 argLine = new ArrayList<>(); 71 argLine.add(JDKToolFinder.getJDKTool("java")); 72 argLine.addAll(asList(Utils.getTestJavaOpts())); 73 argLine.addAll(List.of("-cp", SPIJAR_CP)); 74 argLine.addAll(List.of("-Durl.dir=" + TEST_CLASSES)); 75 argLine.add("DummyContextFactory"); 76 77 ProcessTools.executeCommand(argLine.stream() 78 .filter(t -> !t.isEmpty()) 79 .toArray(String[]::new)) 80 .shouldHaveExitValue(0); 81 } 82 setupService()83 private static void setupService() throws Exception { 84 Path xdir = Path.of(TEST_CLASSES); 85 Path config = xdir.resolve(Path.of(TEST_CLASSES,"META-INF/services/javax.naming.spi.InitialContextFactory")); 86 Files.createDirectories(config.getParent()); 87 Files.write(config, "DummyContextFactory".getBytes(), CREATE); 88 JarUtils.createJarFile(SPIJAR, xdir); 89 } 90 } 91