Path: blob/master/test/langtools/tools/jdeps/multiVersion/MultiVersionError.java
64474 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 827716526* @library ../lib27* @build CompilerUtils28* @run testng MultiVersionError29* @summary Tests multiple versions of the same class file30*/3132import java.nio.file.Path;33import java.nio.file.Paths;34import java.util.Set;35import java.util.spi.ToolProvider;3637import org.testng.annotations.BeforeTest;38import org.testng.annotations.Test;3940import static org.testng.Assert.assertTrue;4142public class MultiVersionError {43private static final String TEST_SRC = System.getProperty("test.src");44private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");4546private static final Path MODS_DIR = Paths.get("mods");4748private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar").orElseThrow();49private static final Set<String> modules = Set.of("m1", "m2");5051/**52* Compiles classes used by the test53*/54@BeforeTest55public void compileAll() throws Exception {56CompilerUtils.cleanDir(MODS_DIR);57modules.forEach(mn ->58assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn)));5960// create a modular multi-release m1.jar61Path m1 = MODS_DIR.resolve("m1");62Path m2 = MODS_DIR.resolve("m2");63jar("cf", "m1.jar", "-C", m1.toString(), "p/Test.class",64"--release", "9", "-C", m1.toString(), "module-info.class",65"--release", "11", "-C", m1.toString(), "p/internal/P.class");66jar("cf", "m2.jar", "-C", m2.toString(), "q/Q.class",67"--release", "10", "-C", m2.toString(), "module-info.class");6869// package private p/internal/P.class in m1 instead70jar("cf", "m3.jar", "-C", m2.toString(), "q/Q.class",71"--release", "12", "-C", m2.toString(), "module-info.class",72"-C", m1.toString(), "p/internal/P.class");73}7475/*76* multiple module-info.class from different versions should be excluded77* from multiple version check.78*/79@Test80public void noMultiVersionClass() {81// skip parsing p.internal.P to workaround JDK-827768182JdepsRunner jdepsRunner = new JdepsRunner("--print-module-deps", "--multi-release", "10",83"--ignore-missing-deps",84"--module-path", "m1.jar", "m2.jar");85int rc = jdepsRunner.run(true);86assertTrue(rc == 0);87assertTrue(jdepsRunner.outputContains("java.base,m1"));88}8990/*91* Detect multiple versions of p.internal.P class92*/93@Test94public void classInMultiVersions() {95JdepsRunner jdepsRunner = new JdepsRunner("--print-module-deps", "--multi-release", "13",96"--module-path", "m1.jar", "m3.jar");97int rc = jdepsRunner.run(true);98assertTrue(rc != 0);99assertTrue(jdepsRunner.outputContains("class p.internal.P already associated with version"));100}101102private static void jar(String... options) {103int rc = JAR_TOOL.run(System.out, System.err, options);104assertTrue(rc == 0);105}106}107108109