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