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  * @run main FactoryCacheTest
  33  */
  34 
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 import jdk.test.lib.JDKToolFinder;
  41 import jdk.test.lib.Utils;
  42 import jdk.test.lib.process.ProcessTools;
  43 import jdk.test.lib.util.JarUtils;
  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 
  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.add("DummyContextFactory");
  61 
  62         ProcessTools.executeCommand(argLine.stream()
  63                 .filter(t -> !t.isEmpty())
  64                 .toArray(String[]::new))
  65                 .shouldHaveExitValue(0);
  66 
  67         // now test the ServiceLoader approach
  68         setupService();
  69         argLine = new ArrayList<>();
  70         argLine.add(JDKToolFinder.getJDKTool("java"));
  71         argLine.addAll(asList(Utils.getTestJavaOpts()));
  72         argLine.addAll(List.of("-cp", SPIJAR_CP));
  73         argLine.add("DummyContextFactory");
  74 
  75         ProcessTools.executeCommand(argLine.stream()
  76                 .filter(t -> !t.isEmpty())
  77                 .toArray(String[]::new))
  78                 .shouldHaveExitValue(0);
  79     }
  80 
  81     private static void setupService() throws Exception {
  82         Path xdir = Path.of(TEST_CLASSES);
  83         Path config = xdir.resolve(Path.of(TEST_CLASSES,"META-INF/services/javax.naming.spi.InitialContextFactory"));
  84         Files.createDirectories(config.getParent());
  85         Files.write(config, "DummyContextFactory".getBytes(), CREATE);
  86         JarUtils.createJarFile(SPIJAR, xdir);
  87     }
  88 }