1 /*
   2  * Copyright (c) 2014, 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  * @modules jdk.jartool/sun.tools.jar
  28  *          jdk.compiler
  29  * @build ContainerTest CompilerUtils jdk.testlibrary.*
  30  * @run testng ContainerTest
  31  * @summary Starts a simple container that uses dynamic configurations
  32  *          and launches two applications in the same VM
  33  */
  34 
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 
  39 import static jdk.testlibrary.ProcessTools.*;
  40 
  41 import org.testng.annotations.BeforeTest;
  42 import org.testng.annotations.Test;
  43 import static org.testng.Assert.*;
  44 
  45 @Test
  46 public class ContainerTest {
  47 
  48     private static final String TEST_SRC = System.getProperty("test.src");
  49 
  50     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  51     private static final Path MODS_DIR = Paths.get("mods");
  52 
  53     private static final Path MLIB_DIR = Paths.get("mlib");
  54     private static final Path APPLIB_DIR = Paths.get("applib");
  55 
  56     private static final String CONTAINER_MODULE = "container";
  57     private static final String CONTAINER_MAIN_CLASS = "container.Main";
  58 
  59 
  60     /**
  61      * Creates the container module in mlib/container@1.0.jmod
  62      */
  63     void buildContainer() throws Exception {
  64 
  65         Path src = SRC_DIR.resolve(CONTAINER_MODULE);
  66         Path output = MODS_DIR.resolve(CONTAINER_MODULE);
  67 
  68         boolean compiled = CompilerUtils.compile(src, output);
  69         assertTrue(compiled);
  70 
  71         // jar --create ...
  72         Path mlib = Files.createDirectories(MLIB_DIR);
  73         String classes = output.toString();
  74         String jar = mlib.resolve(CONTAINER_MODULE + "@1.0.jar").toString();
  75         String[] args = {
  76             "--create",
  77             "--file=" + jar,
  78             "--main-class=" + CONTAINER_MAIN_CLASS,
  79             "-C", classes, "."
  80         };
  81         boolean success
  82             = new sun.tools.jar.Main(System.out, System.out, "jar")
  83                 .run(args);
  84         assertTrue(success);
  85     }
  86 
  87     /**
  88      * Creates app1 and its bundled libraries in applib.
  89      */
  90     void buildApp1() throws Exception {
  91         Path dir = Files.createDirectories(APPLIB_DIR);
  92 
  93         // app1 uses its own copy of JAX-WS
  94         boolean compiled
  95             = CompilerUtils.compile(SRC_DIR.resolve("java.xml.ws"),
  96                                     dir.resolve("java.xml.ws"));
  97         assertTrue(compiled);
  98 
  99         compiled = CompilerUtils.compile(SRC_DIR.resolve("app1"),
 100                                          dir.resolve("app1"),
 101                                          "-upgrademodulepath", dir.toString());
 102         assertTrue(compiled);
 103     }
 104 
 105     /**
 106      * Creates app2 and its bundled libraries in applib.
 107      */
 108     void buildApp2() throws Exception {
 109         Path dir = Files.createDirectories(APPLIB_DIR);
 110 
 111         // app2 uses JAX-RS
 112         boolean compiled
 113             = CompilerUtils.compile(SRC_DIR.resolve("java.ws.rs"),
 114                                     dir.resolve("java.ws.rs"));
 115         assertTrue(compiled);
 116 
 117         compiled = CompilerUtils.compile(SRC_DIR.resolve("app2"),
 118                                          dir.resolve("app2"),
 119                                          "-mp", dir.toString());
 120         assertTrue(compiled);
 121     }
 122 
 123 
 124     @BeforeTest
 125     public void setup() throws Exception {
 126         buildContainer();
 127         buildApp1();
 128         buildApp2();
 129     }
 130 
 131     /**
 132      * Launches the container
 133      */
 134     public void testContainer() throws Exception {
 135 
 136         int exitValue
 137             = executeTestJava("-mp", MLIB_DIR.toString(),
 138                               "-m", CONTAINER_MODULE)
 139                 .outputTo(System.out)
 140                 .errorTo(System.err)
 141                 .getExitValue();
 142 
 143         assertTrue(exitValue == 0);
 144     }
 145 
 146 }