--- old/test/jdk/jigsaw/scenarios/overlappingpackages/OverlappingPackagesTest.java 2015-09-21 15:55:52.000000000 -0700 +++ new/test/jdk/jigsaw/scenarios/overlappingpackages/OverlappingPackagesTest.java 2015-09-21 15:55:52.000000000 -0700 @@ -24,14 +24,16 @@ /** * @test * @library ../../lib /lib/testlibrary - * @build OverlappingPackagesTest CompilerUtils + * @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; @@ -41,6 +43,8 @@ import org.testng.annotations.Test; import static org.testng.Assert.*; +import jdk.testlibrary.OutputAnalyzer; + @Test public class OverlappingPackagesTest { @@ -50,7 +54,11 @@ 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 List modules = Arrays.asList("m1", "m2", "misc", + "test"); + + private static final String DUP_P_MSG = "Package p in both module m2 and " + + "module m1"; /** @@ -85,31 +93,86 @@ * sun.misc and so should overlap with the base module. */ public void testOverlapWithBaseModule() throws Exception { - int exitValue + OutputAnalyzer output = executeTestJava("-mp", MODS_DIR.toString(), "-addmods", "misc", "-m", "test/test.Main") .outputTo(System.out) - .errorTo(System.err) - .getExitValue(); - - assertTrue(exitValue != 0); + .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 { - int exitValue + 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); + 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); + } + } } --- /dev/null 2015-09-21 15:55:53.000000000 -0700 +++ new/test/jdk/jigsaw/scenarios/overlappingpackages/OverlappingExportedPackagesTest.java 2015-09-21 15:55:53.000000000 -0700 @@ -0,0 +1,194 @@ +/* + * 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 + * @modules java.base/jdk.internal.module + * @build OverlappingExportedPackagesTest CompilerUtils + * @build jdk.testlibrary.OutputAnalyzer jdk.testlibrary.ProcessTools + * @run testng OverlappingExportedPackagesTest + * @summary Basic test to ensure that startup fails if two or more modules + * export the same package. + */ + +import java.io.OutputStream; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.stream.Collectors; + +import java.lang.module.Configuration; +import java.lang.module.ModuleDescriptor; +import java.lang.module.ModuleFinder; +import java.lang.module.ResolutionException; + +import java.lang.reflect.Layer; + +import jdk.testlibrary.OutputAnalyzer; +import static jdk.testlibrary.ProcessTools.*; + +import jdk.internal.module.ModuleInfoWriter; + +import org.testng.annotations.Test; +import static org.testng.Assert.*; + +@Test +public class OverlappingExportedPackagesTest { + + private static final String TEST_SRC = System.getProperty("test.src"); + + private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); + + private static final String ERROR_MESSAGE_M1_M2 = + "Modules m1 and m2 export package p to module test"; + private static final String ERROR_MESSAGE_M2_M1 = + "Modules m2 and m1 export package p to module test"; + + //a package name + private static final String P = "p"; + //some module names + private static final String M1 = "m1", M2 = "m2", M3 = "m3", M4 = "m4", + TEST = "test"; + + /** + * Duplicate package p. + */ + public void testOverlap() throws Exception { + Path root = Paths.get("overlap"); + write(root, init(M1).exports(P).build()); + write(root, init(M2).exports(P).build()); + assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST), + root.resolve(TEST))); + write(root, init(TEST).requires(M1).requires(M2).build()); + assertAPIFail(ERROR_MESSAGE_M2_M1, root); + assertCommandLineFail(ERROR_MESSAGE_M2_M1, root, M1, M2); + } + + /** + * Duplicate package p is available directly and through "requires public". + */ + public void testOverlapRequiresPublic() throws Exception { + Path root = Paths.get("overlap_requires_public"); + write(root, init(M1).exports(P).build()); + write(root, init(M2).exports(P).build()); + write(root, init(M3).requires(ModuleDescriptor.Requires.Modifier.PUBLIC, + M2).build()); + CompilerUtils.compile(SRC_DIR.resolve(TEST), root.resolve(TEST)); + write(root, init(TEST).requires(M1).requires(M3).build()); + assertAPIFail(ERROR_MESSAGE_M2_M1, root); + assertCommandLineFail(ERROR_MESSAGE_M2_M1, root, M1, M2); + } + + /** + * Duplicate package p is available directly and through a qualified export. + */ + public void testOverlapExportsTo() throws Exception { + Path root = Paths.get("overlap_exports_to"); + write(root, init(M1).exports(P).build()); + write(root, init(M2).exports(P, TEST).build()); + CompilerUtils.compile(SRC_DIR.resolve(TEST), root.resolve(TEST)); + write(root, init(TEST).requires(M1).requires(M2).build()); + assertAPIFail(ERROR_MESSAGE_M1_M2, root); + assertCommandLineFail(ERROR_MESSAGE_M1_M2, root, M1, M2); + } + + /** + * Duplicate package p is available from a required module and also present + * in the module. + */ + public void testOverlapSelf() throws Exception { + Path root = Paths.get("overlap_itself"); + write(root, init(M1).exports(P).build()); + CompilerUtils.compile(SRC_DIR.resolve(TEST), root.resolve(TEST)); + write(root, init(TEST).exports(P).requires(M1).build()); + String error = "Module test contains package p, "+ + "module m1 exports package p to test"; + assertAPIFail(error, root); + assertCommandLineFail(error, root, M1); + } + + /** + * Duplicate package p is exported to different modules. + */ + public void testOverlapQualified() throws Exception { + Path root = Paths.get("overlap_qualified"); + write(root, init(M1).exports(P, M3).build()); + write(root, init(M2).exports(P, M4).build()); + write(root, init(M3).build()); + write(root, init(M4).build()); + CompilerUtils.compile(SRC_DIR.resolve(TEST), root.resolve(TEST)); + write(root, init(TEST).requires(M3).requires(M4).build()); + assertAPIPass(root); + assertCommandLineFail("Package p in both module m2 and module m1", root, + M1, M2); + } + + private void assertAPIPass(Path root) { + ModuleFinder finder = ModuleFinder.of(root); + Configuration.resolve(ModuleFinder.empty(), + Layer.boot(), finder, TEST).bind(); + } + + private void assertAPIFail(String message, Path root) { + try { + assertAPIPass(root); + } catch(ResolutionException e) { + System.out.println("Exception as expected"); + e.printStackTrace(System.out); + assertEquals(message, e.getMessage()); + } + } + + private OutputAnalyzer doRunCommandLine(Path root, String... modules) + throws Exception { + return executeTestJava("-mp", root.toString(), + "-addmods", Arrays.stream(modules). + collect(Collectors.joining(",")), + "-m", "test/test.Main") + .outputTo(System.out) + .errorTo(System.err); + } + + private void assertCommandLineFail(String message, Path root, + String... modules) throws Exception { + OutputAnalyzer output = doRunCommandLine(root, modules); + output.shouldContain(message); + assertNotEquals(output.getExitValue(), 0); + } + + private static ModuleDescriptor.Builder init(String name) { + return new ModuleDescriptor.Builder(name).requires("java.base"); + } + + private static void write(Path root, ModuleDescriptor descriptor) + throws Exception { + Path mRoot = root.resolve(descriptor.name()); + Files.createDirectories(mRoot); + Path mi = mRoot.resolve("module-info.class"); + try (OutputStream out = Files.newOutputStream(mi)) { + ModuleInfoWriter.write(descriptor, out); + } + } +}