/* * 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 Test API and runtime behavior 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 java.util.Set; import java.util.regex.Pattern; import java.util.stream.Stream; 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 DUP_2_MODULES = "Modules m. and m. 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()); String exceptionMessage = assertAPIFail(root); assertMatches(DUP_2_MODULES, exceptionMessage); OutputAnalyzer output = runCommandLine(root, M1, M2); assertNotEquals(output.getExitValue(), 0); assertMatches(DUP_2_MODULES, output.getStdout()); } /** * Triplicate package p. */ public void testOverlap3() throws Exception { Path root = Paths.get("overlap"); write(root, init(M1).exports(P).build()); write(root, init(M2).exports(P).build()); write(root, init(M3).exports(P).build()); assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST), root.resolve(TEST))); write(root, init(TEST).requires(M1).requires(M2).requires(M3).build()); String exceptionMessage = assertAPIFail(root); assertMatches(DUP_2_MODULES, exceptionMessage); OutputAnalyzer output = runCommandLine(root, M1, M2); assertNotEquals(output.getExitValue(), 0); assertMatches(DUP_2_MODULES, output.getStdout()); } /** * 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()); String exceptionMessage = assertAPIFail(root); assertMatches(DUP_2_MODULES, exceptionMessage); OutputAnalyzer output = runCommandLine(root, M1, M2); assertNotEquals(output.getExitValue(), 0); assertMatches(DUP_2_MODULES, output.getStdout()); } /** * 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()); String exceptionMessage = assertAPIFail(root); assertMatches(DUP_2_MODULES, exceptionMessage); OutputAnalyzer output = runCommandLine(root, M1, M2); assertNotEquals(output.getExitValue(), 0); assertMatches(DUP_2_MODULES, output.getStdout()); } /** * 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"; String exceptionMessage = assertAPIFail(root); assertEquals(error, exceptionMessage); OutputAnalyzer output = runCommandLine(root, M1); assertNotEquals(output.getExitValue(), 0); assertTrue(output.getStdout().contains(error)); } /** * 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).requires(M1).build()); write(root, init(M4).requires(M2).build()); CompilerUtils.compile(SRC_DIR.resolve(TEST), root.resolve(TEST)); write(root, init(TEST).requires(M3).requires(M4).build()); assertAPIPass(root, M1, M2, M3, M4); OutputAnalyzer output = runCommandLine(root, M1, M2, M3, M4); assertNotEquals(output.getExitValue(), 0); assertTrue(output.getStdout(). contains("Package p in both module m2 and module m1")); } private Configuration doRunAPI(Path root) { ModuleFinder finder = ModuleFinder.of(root); return Configuration.resolve(ModuleFinder.empty(), Layer.boot(), finder, TEST).bind(); } private void assertAPIPass(Path root, String... modules) { Set descriptors = doRunAPI(root).descriptors(); assertTrue(Stream.of(modules). allMatch(m -> descriptors.stream(). anyMatch(d -> d.name().equals(m)))); } private String assertAPIFail(Path root) { try { doRunAPI(root); throw new RuntimeException("No resolution exception"); } catch(ResolutionException e) { System.out.println("Exception as expected"); e.printStackTrace(System.out); return e.getMessage(); } } private OutputAnalyzer runCommandLine(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 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); } } private static void assertMatches(String regex, CharSequence text) { if(!Pattern.compile(regex).matcher(text).find()) { fail("Regex: \"" + regex + "\" is not matched by text: \n" + text); } } }