1 /*
   2  * Copyright (c) 2015, 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.compiler
  28  * @build OverlappingPackagesTest jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.*
  29  * @run testng OverlappingPackagesTest
  30  * @summary Basic test to ensure that startup fails if two or more modules
  31  *          in the boot Layer have the same package
  32  */
  33 
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.util.Arrays;
  37 import java.util.List;
  38 
  39 import jdk.test.lib.compiler.CompilerUtils;
  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 OverlappingPackagesTest {
  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     // the names of the modules in this test
  55     private static List<String> modules = Arrays.asList("m1", "m2", "test");
  56 
  57 
  58     /**
  59      * Compiles all modules used by the test
  60      */
  61     @BeforeTest
  62     public void compileAll() throws Exception {
  63         for (String mn : modules) {
  64             Path src = SRC_DIR.resolve(mn);
  65             Path mods = MODS_DIR.resolve(mn);
  66             assertTrue(CompilerUtils.compile(src, mods));
  67         }
  68         Path srcMisc = SRC_DIR.resolve("misc");
  69         Path modsMisc = MODS_DIR.resolve("misc");
  70         assertTrue(CompilerUtils.compile(srcMisc, modsMisc));
  71     }
  72 
  73     /**
  74      * Sanity check that the test runs without error.
  75      */
  76     public void testNoOverlappingPackages() throws Exception {
  77         int exitValue
  78             = executeTestJava("--module-path", MODS_DIR.toString(),
  79                               "-m", "test/test.Main")
  80                 .outputTo(System.out)
  81                 .errorTo(System.err)
  82                 .getExitValue();
  83 
  84         assertTrue(exitValue == 0);
  85     }
  86 
  87 
  88     /**
  89      * Run the test with "--add-modules misc", the misc module has package
  90      * jdk.internal.misc and so should overlap with the base module.
  91      */
  92     public void testOverlapWithBaseModule() throws Exception {
  93         int exitValue
  94             = executeTestJava("--module-path", MODS_DIR.toString(),
  95                               "-add-modules", "misc",
  96                               "-m", "test/test.Main")
  97                 .outputTo(System.out)
  98                 .errorTo(System.err)
  99                 .getExitValue();
 100 
 101         assertTrue(exitValue != 0);
 102     }
 103 
 104     /**
 105      * Run the test with "--add-modules m1,m2". Both modules have package p.
 106      */
 107     public void testOverlap() throws Exception {
 108         int exitValue
 109             = executeTestJava("--module-path", MODS_DIR.toString(),
 110                               "--add-modules", "m1,m2",
 111                               "-m", "test/test.Main")
 112                 .outputTo(System.out)
 113                 .errorTo(System.err)
 114                 .getExitValue();
 115 
 116         assertTrue(exitValue != 0);
 117     }
 118 
 119 
 120 }