1 /*
   2  * Copyright (c) 2015, 2017, 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  * @library /lib/testlibrary /test/lib
  27  * @modules jdk.compiler
  28  * @build RunWithAutomaticModules jdk.test.lib.compiler.CompilerUtils JarUtils
  29  *        jdk.testlibrary.ProcessTools
  30  * @run testng RunWithAutomaticModules
  31  * @summary Runs tests that make use of automatic modules
  32  */
  33 
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 
  38 import jdk.test.lib.compiler.CompilerUtils;
  39 import static jdk.testlibrary.ProcessTools.*;
  40 
  41 import org.testng.annotations.Test;
  42 import static org.testng.Assert.*;
  43 
  44 @Test
  45 public class RunWithAutomaticModules {
  46 
  47     private static final String TEST_SRC = System.getProperty("test.src");
  48 
  49     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  50     private static final Path CLASSES_DIR = Paths.get("classes");
  51     private static final Path MODS_DIR = Paths.get("mods");
  52 
  53     /**
  54      * Basic test that consists of 3 modules:
  55      *
  56      * basictest - the test itself
  57      * httpserver - a JAR file (automatic module) with a dummy HTTP server
  58      * logging - a JAR file (automatic module) with a dummy logging library
  59      *
  60      * The test runs the dummy HTTP server and checks that has the expected
  61      * reads and exported packages.
  62      *
  63      * The HTTP server uses the logging library.
  64      */
  65 
  66     public void testBasic() throws Exception {
  67         boolean compiled;
  68 
  69         Path loggingSrc = SRC_DIR.resolve("logging");
  70         Path loggingClasses = CLASSES_DIR.resolve("logging");
  71 
  72         Path httpServerSrc = SRC_DIR.resolve("httpserver");
  73         Path httpServerClasses = CLASSES_DIR.resolve("httpserver");
  74 
  75         String testModule = "basictest";
  76         String mainClass = "test.Main";
  77 
  78 
  79         // compile + create mods/logging-1.0.jar
  80 
  81         compiled = CompilerUtils.compile(loggingSrc, loggingClasses);
  82         assertTrue(compiled);
  83 
  84         JarUtils.createJarFile(MODS_DIR.resolve("logging-1.0.jar"),
  85                                loggingClasses);
  86 
  87 
  88         // compile + create mods/httpserver-9.0.0.jar
  89 
  90         compiled = CompilerUtils.compile(httpServerSrc,
  91                 httpServerClasses,
  92                 "-cp", loggingClasses.toString());
  93         assertTrue(compiled);
  94 
  95         JarUtils.createJarFile(MODS_DIR.resolve("httpserver-9.0.0.jar"),
  96                 httpServerClasses);
  97 
  98 
  99         // compile basictest to mods/basictest
 100 
 101         compiled = CompilerUtils
 102             .compile(SRC_DIR.resolve(testModule),
 103                     MODS_DIR.resolve(testModule),
 104                     "--module-path", MODS_DIR.toString());
 105         assertTrue(compiled);
 106 
 107 
 108         // launch the test. Need --add-mdoules because nothing explicitly depends on logging
 109 
 110         int exitValue
 111             = executeTestJava("--module-path", MODS_DIR.toString(),
 112                               "--add-modules", "logging",
 113                               "-m", testModule + "/" + mainClass)
 114                 .outputTo(System.out)
 115                 .errorTo(System.out)
 116                 .getExitValue();
 117 
 118         assertTrue(exitValue == 0);
 119     }
 120 
 121 
 122 
 123     /**
 124      * Test using a JAR file with a service provider as an automatic module.
 125      *
 126      * The consists of 2 modules:
 127      *
 128      * sptest - the test itself
 129      * bananascript - a JAR file (automatic module) with a dummy ScriptEngineFactory
 130      *
 131      * The test uses ServiceLoader to locate and load ScriptEngineFactory
 132      * implementations. It checks that bananascript is located.
 133      */
 134 
 135     public void testServiceProvider() throws Exception {
 136         boolean compiled;
 137 
 138         Path providerSrc = SRC_DIR.resolve("bananascript");
 139         Path providerClasses = CLASSES_DIR.resolve("bananascript");
 140 
 141         String testModule = "sptest";
 142         String mainClass = "test.Main";
 143 
 144 
 145         // create mods/bananascript-0.9.jar
 146 
 147         compiled = CompilerUtils.compile(providerSrc, providerClasses);
 148         assertTrue(compiled);
 149 
 150         String config = "META-INF/services/javax.script.ScriptEngineFactory";
 151         Path services = providerClasses.resolve(config).getParent();
 152         Files.createDirectories(services);
 153         Files.copy(providerSrc.resolve(config), providerClasses.resolve(config));
 154 
 155         JarUtils.createJarFile(MODS_DIR.resolve("bananascript-0.9.jar"), providerClasses);
 156 
 157 
 158         // compile sptest to mods/sptest
 159 
 160         compiled = CompilerUtils
 161                 .compile(SRC_DIR.resolve(testModule),
 162                         MODS_DIR.resolve(testModule),
 163                         "--module-path", MODS_DIR.toString());
 164 
 165         assertTrue(compiled);
 166 
 167 
 168         // launch the test
 169 
 170         int exitValue
 171             = executeTestJava("--module-path", MODS_DIR.toString(),
 172                               "-m", testModule + "/" + mainClass)
 173                 .outputTo(System.out)
 174                 .errorTo(System.out)
 175                 .getExitValue();
 176 
 177         assertTrue(exitValue == 0);
 178 
 179     }
 180 
 181 }