1 /*
   2  * Copyright (c) 2016, 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  * @build jdk.test.lib.process.ProcessTools
  28  *        ModuleTest jdk.test.lib.compiler.CompilerUtils JarUtils
  29  * @run testng ModuleTest
  30  * @summary Basic tests for using rmi in module world
  31  */
  32 
  33 import static jdk.test.lib.process.ProcessTools.executeTestJava;
  34 import static org.testng.Assert.assertEquals;
  35 import static org.testng.Assert.assertTrue;
  36 
  37 import java.io.File;
  38 import java.nio.file.Paths;
  39 import jdk.test.lib.compiler.CompilerUtils;
  40 
  41 import org.testng.annotations.BeforeTest;
  42 import org.testng.annotations.Test;
  43 
  44 public class ModuleTest {
  45 
  46     static String fileJoin(String... names) {
  47         return String.join(File.separator, names);
  48     }
  49 
  50     static String pathJoin(String... paths) {
  51         return String.join(File.pathSeparator, paths);
  52     }
  53 
  54     private static final String TEST_SRC = System.getProperty("test.src");
  55     private static final String CLIENT_EXP = fileJoin("exploded", "mclient");
  56     private static final String SERVER_EXP = fileJoin("exploded", "mserver");
  57     private static final String MTEST_EXP  = fileJoin("exploded", "mtest");
  58     private static final String CLIENT_JAR = fileJoin("mods", "mclient.jar");
  59     private static final String SERVER_JAR = fileJoin("mods", "mserver.jar");
  60     private static final String MTEST_JAR  = fileJoin("mods", "mtest.jar");
  61 
  62     private static final String DUMMY_MAIN = "testpkg.DummyApp";
  63 
  64     /**
  65      * Compiles all sample classes
  66      */
  67     @BeforeTest
  68     public void compileAll() throws Exception {
  69         assertTrue(CompilerUtils.compile(
  70                 Paths.get(TEST_SRC, "src", "mserver"),
  71                 Paths.get(SERVER_EXP)));
  72 
  73         JarUtils.createJarFile(
  74                 Paths.get(SERVER_JAR),
  75                 Paths.get(SERVER_EXP));
  76 
  77         assertTrue(CompilerUtils.compile(
  78                 Paths.get(TEST_SRC, "src", "mclient"),
  79                 Paths.get(CLIENT_EXP),
  80                 "-cp", SERVER_JAR));
  81 
  82         JarUtils.createJarFile(
  83                 Paths.get(CLIENT_JAR),
  84                 Paths.get(CLIENT_EXP));
  85 
  86         assertTrue(CompilerUtils.compile(Paths.get(TEST_SRC, "src", "mtest"),
  87                 Paths.get(MTEST_EXP),
  88                 "-cp", pathJoin(CLIENT_JAR, SERVER_JAR)));
  89 
  90         JarUtils.createJarFile(
  91                 Paths.get(MTEST_JAR),
  92                 Paths.get(MTEST_EXP));
  93     }
  94 
  95     /**
  96      * Test the client, server and dummy application in different modules
  97      * @throws Exception
  98      */
  99     @Test
 100     public void testAllInModule() throws Exception {
 101         assertEquals(executeTestJava("--module-path", pathJoin(MTEST_JAR, CLIENT_JAR, SERVER_JAR),
 102                 "--add-modules", "mclient,mserver",
 103                 "-m", "mtest/" + DUMMY_MAIN)
 104                 .outputTo(System.out)
 105                 .errorTo(System.out)
 106                 .getExitValue(),
 107                 0);
 108     }
 109 
 110     /**
 111      * Test the client and server in unnamed modules,
 112      * while the dummy application is in automatic module
 113      * @throws Exception
 114      */
 115     @Test
 116     public void testAppInModule() throws Exception {
 117         assertEquals(executeTestJava("--module-path", MTEST_JAR,
 118                 "-cp", pathJoin(CLIENT_JAR, SERVER_JAR),
 119                 "-m", "mtest/" + DUMMY_MAIN)
 120                 .outputTo(System.out)
 121                 .errorTo(System.out)
 122                 .getExitValue(),
 123                 0);
 124     }
 125 
 126     /**
 127      * Test the client and server in automatic modules,
 128      * while the dummy application is in unnamed module
 129      * @throws Exception
 130      */
 131     @Test
 132     public void testAppInUnnamedModule() throws Exception {
 133         assertEquals(executeTestJava("--module-path", pathJoin(CLIENT_JAR, SERVER_JAR),
 134                 "--add-modules", "mclient,mserver",
 135                 "-cp", MTEST_JAR,
 136                 DUMMY_MAIN)
 137                 .outputTo(System.out)
 138                 .errorTo(System.out)
 139                 .getExitValue(),
 140                 0);
 141     }
 142 
 143     /**
 144      * Test the server and test application in automatic modules,
 145      * with client in unnamed module
 146      * @throws Exception
 147      */
 148     @Test
 149     public void testClientInUnamedModule() throws Exception {
 150         assertEquals(executeTestJava("--module-path", pathJoin(MTEST_JAR, SERVER_JAR),
 151                 "--add-modules", "mserver",
 152                 "-cp", CLIENT_JAR,
 153                 "-m", "mtest/" + DUMMY_MAIN)
 154                 .outputTo(System.out)
 155                 .errorTo(System.out)
 156                 .getExitValue(),
 157                 0);
 158     }
 159 }
 160