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