8259395: Patching automatic module with additional packages re-creates module without "requires java.base"

Reviewed-by: attila, alanb
This commit is contained in:
Johannes Kuhn 2021-02-01 08:09:36 +00:00 committed by Alan Bateman
parent 039affc8bc
commit cf942081a5
11 changed files with 454 additions and 1 deletions

View File

@ -152,7 +152,7 @@ public final class ModulePatcher {
packages.removeAll(descriptor.packages());
if (!packages.isEmpty()) {
Builder builder = JLMA.newModuleBuilder(descriptor.name(),
/*strict*/ false,
/*strict*/ descriptor.isAutomatic(),
descriptor.modifiers());
if (!descriptor.isAutomatic()) {
descriptor.requires().forEach(builder::requires);

View File

@ -0,0 +1,173 @@
/*
* Copyright (c) 2021, 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 /test/lib
* @modules jdk.compiler
* @build PatchTest
* jdk.test.lib.compiler.CompilerUtils
* jdk.test.lib.util.JarUtils
* jdk.test.lib.process.ProcessTools
* @run testng PatchTest
* @bug 8259395
* @summary Tests patching an automatic module
*/
import java.io.File;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Path;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.util.JarUtils;
import static jdk.test.lib.process.ProcessTools.*;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import static org.testng.Assert.*;
public class PatchTest {
private static final String APP_NAME = "myapp";
private static final String MODULE_NAME = "somelib";
private static final String EXTEND_PATCH_NAME = "patch1";
private static final String AUGMENT_PATCH_NAME = "patch2";
private static final String APP_MAIN = "myapp.Main";
private static final String EXTEND_PATCH_MAIN = "somelib.test.TestMain";
private static final String AUGMENT_PATCH_MAIN = "somelib.Dummy";
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path APP_SRC = Path.of(TEST_SRC, APP_NAME);
private static final Path APP_CLASSES = Path.of("classes", APP_NAME);
private static final Path SOMELIB_SRC = Path.of(TEST_SRC, MODULE_NAME);
private static final Path SOMELIB_EXTEND_PATCH_SRC = Path.of(TEST_SRC, EXTEND_PATCH_NAME);
private static final Path SOMELIB_AUGMENT_PATCH_SRC = Path.of(TEST_SRC, AUGMENT_PATCH_NAME);
private static final Path SOMELIB_CLASSES = Path.of("classes", MODULE_NAME);
private static final Path SOMELIB_EXTEND_PATCH_CLASSES = Path.of("classes", EXTEND_PATCH_NAME);
private static final Path SOMELIB_AUGMENT_PATCH_CLASSES = Path.of("classes", AUGMENT_PATCH_NAME);
private static final Path SOMELIB_JAR = Path.of("mods", MODULE_NAME + "-0.19.jar");
private static final String MODULE_PATH = String.join(File.pathSeparator, SOMELIB_JAR.toString(), APP_CLASSES.toString());
/**
* The test consists of 2 modules:
*
* somelib - dummy automatic module.
* myapp - explicit module, uses somelib
*
* And two patches:
*
* patch1 - adds an additional package. (extend)
* patch2 - only replaces existing classes. (augment)
*
*/
@BeforeClass
public void compile() throws Exception {
boolean compiled;
// create mods/somelib-0.19.jar
compiled = CompilerUtils.compile(SOMELIB_SRC, SOMELIB_CLASSES);
assertTrue(compiled);
JarUtils.createJarFile(SOMELIB_JAR, SOMELIB_CLASSES);
// compile patch 1
compiled = CompilerUtils.compile(SOMELIB_EXTEND_PATCH_SRC, SOMELIB_EXTEND_PATCH_CLASSES,
"--module-path", SOMELIB_JAR.toString(),
"--add-modules", MODULE_NAME,
"--patch-module", MODULE_NAME + "=" + SOMELIB_EXTEND_PATCH_SRC);
assertTrue(compiled);
// compile patch 2
compiled = CompilerUtils.compile(SOMELIB_AUGMENT_PATCH_SRC, SOMELIB_AUGMENT_PATCH_CLASSES,
"--module-path", SOMELIB_JAR.toString(),
"--add-modules", MODULE_NAME,
"--patch-module", MODULE_NAME + "=" + SOMELIB_AUGMENT_PATCH_SRC);
assertTrue(compiled);
// compile app
compiled = CompilerUtils.compile(APP_SRC, APP_CLASSES,
"--module-path", SOMELIB_JAR.toString());
assertTrue(compiled);
}
@Test
public void testExtendAutomaticModuleOnModulePath() throws Exception {
int exitValue
= executeTestJava("--module-path", MODULE_PATH,
"--patch-module", MODULE_NAME + "=" + SOMELIB_EXTEND_PATCH_CLASSES,
"-m", APP_NAME + "/" + APP_MAIN, "patch1")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
assertTrue(exitValue == 0);
}
@Test
public void testAugmentAutomaticModuleOnModulePath() throws Exception {
int exitValue
= executeTestJava("--module-path", MODULE_PATH,
"--patch-module", MODULE_NAME + "=" + SOMELIB_AUGMENT_PATCH_CLASSES,
"-m", APP_NAME + "/" + APP_MAIN, "patch2")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
assertTrue(exitValue == 0);
}
@Test
public void testExtendAutomaticModuleAsInitialModule() throws Exception {
int exitValue
= executeTestJava("--module-path", SOMELIB_JAR.toString(),
"--patch-module", MODULE_NAME + "=" + SOMELIB_EXTEND_PATCH_CLASSES,
"-m", MODULE_NAME + "/" + EXTEND_PATCH_MAIN)
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
assertTrue(exitValue == 0);
}
@Test
public void testAugmentAutomaticModuleAsInitialModule() throws Exception {
int exitValue
= executeTestJava("--module-path", SOMELIB_JAR.toString(),
"--patch-module", MODULE_NAME + "=" + SOMELIB_AUGMENT_PATCH_CLASSES,
"-m", MODULE_NAME + "/" + AUGMENT_PATCH_MAIN)
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
assertTrue(exitValue == 0);
}
}

View File

@ -0,0 +1,3 @@
module myapp {
requires somelib;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2021, 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.
*/
package myapp;
import somelib.Invariants;
/**
* This test is modelled to use --patch-module to gain access to non-exported internals.
*/
public class Main {
public static void main(String[] args) {
Invariants.test(args[0]);
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2015, 2021, 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.
*/
package somelib;
// This class will be patched
public class PatchInfo {
public static String patchName() {
return "patch1";
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (c) 2021, 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.
*/
package somelib.test;
import somelib.Invariants;
/**
* This test is modelled to use --patch-module to gain access to non-exported internals.
*/
public class TestMain {
public static void main(String[] args) {
Invariants.test("patch1");
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2021, 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.
*/
package somelib;
public class Dummy {
public static boolean returnTrue() {
return true;
}
public static void main(String[] args) {
Invariants.test("patch2");
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2015, 2021, 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.
*/
package somelib;
// This class will be patched
public class PatchInfo {
public static String patchName() {
return "patch2";
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2021, 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.
*/
package somelib;
public class Dummy {
public static boolean returnTrue() {
return true;
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2021, 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.
*/
package somelib;
import java.lang.module.ModuleDescriptor;
public class Invariants {
public static void test(String expectPatch) {
ModuleDescriptor ownDesc = Invariants.class.getModule().getDescriptor();
assertThat(ownDesc.isAutomatic(), "Expected to be executed in an automatic module");
assertThat(ownDesc.requires().stream().anyMatch(
r -> r.name().equals("java.base") && r.modifiers().contains(ModuleDescriptor.Requires.Modifier.MANDATED)),
"requires mandated java.base");
assertThat(Dummy.returnTrue(), "Dummy.returnTrue returns true");
assertThat(expectPatch.equals(PatchInfo.patchName()), "Module is patched with the right patch");
}
private static void assertThat(boolean expected, String message) {
if (!expected) {
throw new AssertionError(message);
}
}
}

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2015, 2021, 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.
*/
package somelib;
// This class will be patched
public class PatchInfo {
public static String patchName() {
return "original";
}
}