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