/* * 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); } } }