Path: blob/master/test/jdk/tools/launcher/modules/patch/automatic/PatchTest.java
41159 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* @library /test/lib26* @modules jdk.compiler27* @build PatchTest28* jdk.test.lib.compiler.CompilerUtils29* jdk.test.lib.util.JarUtils30* jdk.test.lib.process.ProcessTools31* @run testng PatchTest32* @bug 825939533* @summary Tests patching an automatic module34*/3536import java.io.File;37import java.util.List;38import java.nio.file.Files;39import java.nio.file.Path;4041import jdk.test.lib.compiler.CompilerUtils;42import jdk.test.lib.util.JarUtils;43import static jdk.test.lib.process.ProcessTools.*;4445import org.testng.annotations.Test;46import org.testng.annotations.BeforeClass;47import static org.testng.Assert.*;4849public class PatchTest {5051private static final String APP_NAME = "myapp";5253private static final String MODULE_NAME = "somelib";5455private static final String EXTEND_PATCH_NAME = "patch1";56private static final String AUGMENT_PATCH_NAME = "patch2";5758private static final String APP_MAIN = "myapp.Main";59private static final String EXTEND_PATCH_MAIN = "somelib.test.TestMain";60private static final String AUGMENT_PATCH_MAIN = "somelib.Dummy";6162private static final String TEST_SRC = System.getProperty("test.src");6364private static final Path APP_SRC = Path.of(TEST_SRC, APP_NAME);65private static final Path APP_CLASSES = Path.of("classes", APP_NAME);66private static final Path SOMELIB_SRC = Path.of(TEST_SRC, MODULE_NAME);67private static final Path SOMELIB_EXTEND_PATCH_SRC = Path.of(TEST_SRC, EXTEND_PATCH_NAME);68private static final Path SOMELIB_AUGMENT_PATCH_SRC = Path.of(TEST_SRC, AUGMENT_PATCH_NAME);69private static final Path SOMELIB_CLASSES = Path.of("classes", MODULE_NAME);70private static final Path SOMELIB_EXTEND_PATCH_CLASSES = Path.of("classes", EXTEND_PATCH_NAME);71private static final Path SOMELIB_AUGMENT_PATCH_CLASSES = Path.of("classes", AUGMENT_PATCH_NAME);72private static final Path SOMELIB_JAR = Path.of("mods", MODULE_NAME + "-0.19.jar");7374private static final String MODULE_PATH = String.join(File.pathSeparator, SOMELIB_JAR.toString(), APP_CLASSES.toString());7576/**77* The test consists of 2 modules:78*79* somelib - dummy automatic module.80* myapp - explicit module, uses somelib81*82* And two patches:83*84* patch1 - adds an additional package. (extend)85* patch2 - only replaces existing classes. (augment)86*87*/88@BeforeClass89public void compile() throws Exception {90boolean compiled;9192// create mods/somelib-0.19.jar9394compiled = CompilerUtils.compile(SOMELIB_SRC, SOMELIB_CLASSES);95assertTrue(compiled);9697JarUtils.createJarFile(SOMELIB_JAR, SOMELIB_CLASSES);9899100// compile patch 1101compiled = CompilerUtils.compile(SOMELIB_EXTEND_PATCH_SRC, SOMELIB_EXTEND_PATCH_CLASSES,102"--module-path", SOMELIB_JAR.toString(),103"--add-modules", MODULE_NAME,104"--patch-module", MODULE_NAME + "=" + SOMELIB_EXTEND_PATCH_SRC);105assertTrue(compiled);106107// compile patch 2108compiled = CompilerUtils.compile(SOMELIB_AUGMENT_PATCH_SRC, SOMELIB_AUGMENT_PATCH_CLASSES,109"--module-path", SOMELIB_JAR.toString(),110"--add-modules", MODULE_NAME,111"--patch-module", MODULE_NAME + "=" + SOMELIB_AUGMENT_PATCH_SRC);112assertTrue(compiled);113114// compile app115compiled = CompilerUtils.compile(APP_SRC, APP_CLASSES,116"--module-path", SOMELIB_JAR.toString());117assertTrue(compiled);118}119120@Test121public void testExtendAutomaticModuleOnModulePath() throws Exception {122int exitValue123= executeTestJava("--module-path", MODULE_PATH,124"--patch-module", MODULE_NAME + "=" + SOMELIB_EXTEND_PATCH_CLASSES,125"-m", APP_NAME + "/" + APP_MAIN, "patch1")126.outputTo(System.out)127.errorTo(System.out)128.getExitValue();129130assertTrue(exitValue == 0);131}132133@Test134public void testAugmentAutomaticModuleOnModulePath() throws Exception {135int exitValue136= executeTestJava("--module-path", MODULE_PATH,137"--patch-module", MODULE_NAME + "=" + SOMELIB_AUGMENT_PATCH_CLASSES,138"-m", APP_NAME + "/" + APP_MAIN, "patch2")139.outputTo(System.out)140.errorTo(System.out)141.getExitValue();142143assertTrue(exitValue == 0);144}145146@Test147public void testExtendAutomaticModuleAsInitialModule() throws Exception {148int exitValue149= executeTestJava("--module-path", SOMELIB_JAR.toString(),150"--patch-module", MODULE_NAME + "=" + SOMELIB_EXTEND_PATCH_CLASSES,151"-m", MODULE_NAME + "/" + EXTEND_PATCH_MAIN)152.outputTo(System.out)153.errorTo(System.out)154.getExitValue();155156assertTrue(exitValue == 0);157}158159@Test160public void testAugmentAutomaticModuleAsInitialModule() throws Exception {161int exitValue162= executeTestJava("--module-path", SOMELIB_JAR.toString(),163"--patch-module", MODULE_NAME + "=" + SOMELIB_AUGMENT_PATCH_CLASSES,164"-m", MODULE_NAME + "/" + AUGMENT_PATCH_MAIN)165.outputTo(System.out)166.errorTo(System.out)167.getExitValue();168169assertTrue(exitValue == 0);170}171172}173174175