1 /*
   2  * Copyright (c) 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  * @build OverlappingPackagesTest CompilerUtils jdk.testlibrary.OutputAnalyzer
  28  * @run testng OverlappingPackagesTest
  29  * @summary Basic test to ensure that startup fails if two or more modules
  30  *          in the boot Layer have the same package. Startup is not supposed
  31  *          to fail if there is a package name duplication with/on the unnamed
  32  *          module.
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Path;
  37 import java.nio.file.Paths;
  38 import java.util.Arrays;
  39 import java.util.List;
  40 import java.util.regex.Pattern;
  41 
  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 import jdk.testlibrary.OutputAnalyzer;
  49 
  50 @Test
  51 public class OverlappingPackagesTest {
  52 
  53     private static final String TEST_SRC = System.getProperty("test.src");
  54 
  55     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  56     private static final Path MODS_DIR = Paths.get("mods");
  57 
  58     // the names of the modules in this test
  59     private static final List<String> modules = Arrays.asList("m1", "m2",
  60             "misc", "test");
  61 
  62     /**
  63      * Compiles all modules used by the test
  64      */
  65     @BeforeTest
  66     public void compileAll() throws Exception {
  67         for (String mn : modules) {
  68             Path src = SRC_DIR.resolve(mn);
  69             Path mods = MODS_DIR.resolve(mn);
  70             assertTrue(CompilerUtils.compile(src, mods));
  71         }
  72     }
  73 
  74     /**
  75      * Sanity check that the test runs without error.
  76      */
  77     public void testNoOverlappingPackages() throws Exception {
  78         int exitValue
  79             = executeTestJava("-mp", MODS_DIR.toString(),
  80                               "-m", "test/test.Main")
  81                 .outputTo(System.out)
  82                 .errorTo(System.err)
  83                 .getExitValue();
  84         assertTrue(exitValue == 0);
  85     }
  86 
  87 
  88     /**
  89      * Run the test with "-addmods misc", the misc module has package
  90      * sun.misc and so should overlap with the base module.
  91      */
  92     public void testOverlapWithBaseModule() throws Exception {
  93         OutputAnalyzer output
  94             = executeTestJava("-mp", MODS_DIR.toString(),
  95                               "-addmods", "misc",
  96                               "-m", "test/test.Main")
  97                 .outputTo(System.out)
  98                 .errorTo(System.err);
  99         output.shouldContain("Module misc contains package sun.misc, " +
 100             "module java.base exports package sun.misc to misc");
 101         assertTrue(output.getExitValue() != 0);
 102     }
 103 
 104     /**
 105      * Run the test with "-addmods m1,m2". Both modules have package p.
 106      */
 107     public void testOverlap() throws Exception {
 108         OutputAnalyzer output
 109             = executeTestJava("-mp", MODS_DIR.toString(),
 110                               "-addmods", "m1,m2",
 111                               "-m", "test/test.Main")
 112                 .outputTo(System.out)
 113                 .errorTo(System.err);
 114         String errorText = "Package p in both module m. and module m.";
 115         if(!Pattern.compile(errorText).matcher(output.getOutput()).find()) {
 116             fail("Output does not contain expected error: \"" +
 117                     errorText + "\"");
 118         }
 119         assertTrue(output.getExitValue() != 0);
 120     }
 121 
 122     /**
 123      * Run the test with m1 being on the modulepath. m1 module has package p.
 124      * Package p is also present on the classpath.
 125      */
 126     public void testOverlapWithClasspath() throws Exception {
 127         int exitValue
 128                 = executeTestJava("-mp", MODS_DIR.toString(),
 129                         "-classpath", MODS_DIR.resolve("m1").toString(),
 130                         "-addmods", "m2",
 131                         "-m", "test/test.Main")
 132                 .outputTo(System.out)
 133                 .errorTo(System.err)
 134                 .getExitValue();
 135         assertTrue(exitValue == 0);
 136     }
 137 
 138     /**
 139      * Run with package p present two times on the classpath.
 140      */
 141     public void testOverlapWithDupsOnClasspath() throws Exception {
 142         String cp = MODS_DIR.resolve("m1").toString() + File.pathSeparator +
 143                     MODS_DIR.resolve("m2");
 144         int exitValue
 145             = executeTestJava("-mp", MODS_DIR.toString(),
 146                               "-classpath", cp,
 147                               "-m", "test/test.Main")
 148                 .outputTo(System.out)
 149                 .errorTo(System.err)
 150                 .getExitValue();
 151         assertTrue(exitValue == 0);
 152     }
 153 }