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