Path: blob/master/test/jdk/javax/naming/spi/FactoryCacheTest.java
41152 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 822326026* @summary NamingManager should cache InitialContextFactory27* @library /test/lib28* @build jdk.test.lib.util.JarUtils jdk.test.lib.process.*29* FactoryCacheTest30* DummyContextFactory31* DummyContextFactory232* @run main FactoryCacheTest33*/3435import java.nio.file.Files;36import java.nio.file.Path;37import java.util.ArrayList;38import java.util.List;3940import jdk.test.lib.JDKToolFinder;41import jdk.test.lib.Utils;42import jdk.test.lib.process.ProcessTools;43import jdk.test.lib.util.JarUtils;4445import static java.nio.file.StandardOpenOption.CREATE;46import static java.util.Arrays.asList;4748import static jdk.test.lib.Utils.TEST_CLASSES;4950public class FactoryCacheTest {5152private static final Path SPIJAR = Path.of("testDir", "ContextFactory.jar");53private static final String SPIJAR_CP = SPIJAR.toAbsolutePath().toString();5455public static void main(String[] args) throws Throwable {56List<String> argLine = new ArrayList<>();57argLine.add(JDKToolFinder.getJDKTool("java"));58argLine.addAll(asList(Utils.getTestJavaOpts()));59argLine.addAll(List.of("-cp", TEST_CLASSES));60argLine.addAll(List.of("-Durl.dir=" + TEST_CLASSES));61argLine.add("DummyContextFactory");6263ProcessTools.executeCommand(argLine.stream()64.filter(t -> !t.isEmpty())65.toArray(String[]::new))66.shouldHaveExitValue(0);6768// now test the ServiceLoader approach69setupService();70argLine = new ArrayList<>();71argLine.add(JDKToolFinder.getJDKTool("java"));72argLine.addAll(asList(Utils.getTestJavaOpts()));73argLine.addAll(List.of("-cp", SPIJAR_CP));74argLine.addAll(List.of("-Durl.dir=" + TEST_CLASSES));75argLine.add("DummyContextFactory");7677ProcessTools.executeCommand(argLine.stream()78.filter(t -> !t.isEmpty())79.toArray(String[]::new))80.shouldHaveExitValue(0);81}8283private static void setupService() throws Exception {84Path xdir = Path.of(TEST_CLASSES);85Path config = xdir.resolve(Path.of(TEST_CLASSES,"META-INF/services/javax.naming.spi.InitialContextFactory"));86Files.createDirectories(config.getParent());87Files.write(config, "DummyContextFactory".getBytes(), CREATE);88JarUtils.createJarFile(SPIJAR, xdir);89}90}919293