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