/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @library ../../lib /lib/testlibrary * @build OverlappingPackagesTest CompilerUtils jdk.testlibrary.OutputAnalyzer * @run testng OverlappingPackagesTest * @summary Basic test to ensure that startup fails if two or more modules * in the boot Layer have the same package */ import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.Files; import java.util.Arrays; import java.util.List; import static jdk.testlibrary.ProcessTools.*; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import static org.testng.Assert.*; import jdk.testlibrary.OutputAnalyzer; @Test public class OverlappingPackagesTest { private static final String TEST_SRC = System.getProperty("test.src"); private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); private static final Path MODS_DIR = Paths.get("mods"); // the names of the modules in this test private static List modules = Arrays.asList("m1", "m2", "misc", "test"); private static final String DUP_P_MSG = "Package p in both module m2 and " + "module m1"; /** * Compiles all modules used by the test */ @BeforeTest public void compileAll() throws Exception { for (String mn : modules) { Path src = SRC_DIR.resolve(mn); Path mods = MODS_DIR.resolve(mn); assertTrue(CompilerUtils.compile(src, mods)); } } /** * Sanity check that the test runs without error. */ public void testNoOverlappingPackages() throws Exception { int exitValue = executeTestJava("-mp", MODS_DIR.toString(), "-m", "test/test.Main") .outputTo(System.out) .errorTo(System.err) .getExitValue(); assertTrue(exitValue == 0); } /** * Run the test with "-addmods misc", the misc module has package * sun.misc and so should overlap with the base module. */ public void testOverlapWithBaseModule() throws Exception { OutputAnalyzer output = executeTestJava("-mp", MODS_DIR.toString(), "-addmods", "misc", "-m", "test/test.Main") .outputTo(System.out) .errorTo(System.err); output.shouldContain("Module misc contains package sun.misc, " + "module java.base exports package sun.misc to misc"); assertTrue(output.getExitValue() != 0); } /** * Run the test with "-addmods m1,m2". Both modules have package p. */ public void testOverlap() throws Exception { OutputAnalyzer output = executeTestJava("-mp", MODS_DIR.toString(), "-addmods", "m1,m2", "-m", "test/test.Main") .outputTo(System.out) .errorTo(System.err); output.shouldContain(DUP_P_MSG); assertTrue(output.getExitValue() != 0); } /** * Run the test with m1 being on the modulepath. m1 module has package p. * Package p is also present on the classpath. */ public void testOverlapWithClasspath() throws Exception { int exitValue = executeTestJava("-mp", MODS_DIR.toString(), "-classpath", MODS_DIR.resolve("m1").toString(), "-addmods", "m2", "-m", "test/test.Main") .outputTo(System.out) .errorTo(System.err) .getExitValue(); assertTrue(exitValue == 0); } /** * Run with package p present two times on the classpath. */ public void testOverlapWithDupsOnClasspath() throws Exception { String cp = MODS_DIR.resolve("m1").toString() + File.pathSeparator + MODS_DIR.resolve("m2"); int exitValue = executeTestJava("-mp", MODS_DIR.toString(), "-classpath", cp, "-m", "test/test.Main") .outputTo(System.out) .errorTo(System.err) .getExitValue(); assertTrue(exitValue == 0); } /** * Run the test with "-addmods m1,m2" with one of the modules being * on the module path and the other one - on the upgrade module path. * Both modules have package p. */ public void testOverlapUpgratePath() throws Exception { Path upgradePath = Paths.get("up"); Path dest = upgradePath.resolve("m1"); Files.createDirectories(upgradePath); Path src = MODS_DIR.resolve("m1"); Files.move(src, dest); try { OutputAnalyzer output = executeTestJava("-mp", MODS_DIR.toString(), "-upgrademodulepath", upgradePath.toString(), "-addmods", "m1,m2", "-m", "test/test.Main") .outputTo(System.out) .errorTo(System.err); output.shouldContain(DUP_P_MSG); assertTrue(output.getExitValue() != 0); } finally { Files.move(dest, src); } } }