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