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