1 /*
   2  * Copyright (c) 2015, 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 import jdk.test.lib.JDKToolFinder;
  25 import jdk.test.lib.Utils;
  26 import jdk.test.lib.compiler.CompilerUtils;
  27 import jdk.test.lib.process.ProcessTools;
  28 
  29 import java.io.IOException;
  30 import java.net.InetAddress;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.time.Duration;
  34 import java.util.Collection;
  35 import java.util.Collections;
  36 import java.util.List;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 
  40 import static jdk.test.lib.Utils.TEST_SRC;
  41 
  42 /*
  43  * @test
  44  * @summary Test of JNDI factories using classes exported by third-party modules.
  45  * @library /test/lib
  46  * @modules jdk.compiler
  47  * @run main RunBasic
  48  */
  49 
  50 /*
  51  * Demonstrates Java object storage/retrieval, LDAP control and URL context
  52  * usage using an LDAP directory. The objects and their associated object
  53  * factories, state factories, control factories and URL context factories
  54  * are exported from third-party modules.
  55  *
  56  * Seven types of object are used:
  57  *   - an AWT object (Serializable) from the 'java.desktop' JDK module
  58  *   - a Person object (DirContext) from the 'person' third-party module
  59  *   - a Fruit object (Referenceable) from the 'fruit' third-party module
  60  *   - an RMI object (Remote) from the 'hello' third-party module
  61  *   - an LDAP request control (Control) from the 'foo' third-party module
  62  *   - an LDAP response control (Control) from the 'authz' third-party module
  63  *   - an ldapv4 URL (DirContext) from the 'ldapv4' third-party module
  64  */
  65 
  66 public class RunBasic {
  67 
  68     private static final List<String> JAVA_CMDS;
  69 
  70     static final String HOST_NAME = InetAddress.getLoopbackAddress().getHostName();
  71 
  72     static {
  73         String javaPath = JDKToolFinder.getJDKTool("java");
  74 
  75         JAVA_CMDS = Stream
  76                 .concat(Stream.of(javaPath), Stream.of(Utils.getTestJavaOpts()))
  77                 .collect(Collectors.collectingAndThen(Collectors.toList(),
  78                         Collections::unmodifiableList));
  79     }
  80 
  81     public static void main(String[] args) throws Throwable {
  82         // prepare all test modules
  83         prepareModule("person");
  84         prepareModule("fruit");
  85         prepareModule("hello");
  86         prepareModule("foo");
  87         prepareModule("authz");
  88         prepareModule("ldapv4");
  89         prepareModule("test", "--module-source-path",
  90                 Path.of(TEST_SRC, "src").toString());
  91 
  92         System.out.println("Hostname: [" + HOST_NAME + "]");
  93 
  94         // run tests
  95         runTest("java.desktop", "test.StoreObject");
  96         runTest("person", "test.StorePerson");
  97         runTest("fruit", "test.StoreFruit");
  98         runTest("hello", "test.StoreRemote");
  99         runTest("foo", "test.ConnectWithFoo");
 100         runTest("authz", "test.ConnectWithAuthzId");
 101         runTest("ldapv4", "test.ReadByUrl");
 102     }
 103 
 104     private static void prepareModule(String mod, String... opts)
 105             throws IOException {
 106         System.out.println("Preparing the '" + mod + "' module...");
 107         long start = System.nanoTime();
 108         makeDir("mods", mod);
 109         CompilerUtils.compile(Path.of(TEST_SRC, "src", mod),
 110                 Path.of("mods", (mod.equals("test") ? "" : mod)), opts);
 111         Duration duration = Duration.ofNanos(System.nanoTime() - start);
 112         System.out.println("completed: duration - " + duration );
 113     }
 114 
 115     private static void makeDir(String first, String... more)
 116             throws IOException {
 117         Files.createDirectories(Path.of(first, more));
 118     }
 119 
 120     private static void runTest(String desc, String clsName) throws Throwable {
 121         System.out.println("Running with the '" + desc + "' module...");
 122         runJava("-Dtest.src=" + TEST_SRC, "-p", "mods", "-m", "test/" + clsName,
 123                 "ldap://" + HOST_NAME + "/dc=ie,dc=oracle,dc=com");
 124     }
 125 
 126     private static void runJava(String... opts) throws Throwable {
 127         ProcessTools.executeCommand(
 128                 Stream.of(JAVA_CMDS, List.of(opts)).flatMap(Collection::stream)
 129                         .toArray(String[]::new)).shouldHaveExitValue(0);
 130     }
 131 }