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