|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | +* @test |
| 26 | +# @bug 8296329 |
| 27 | +* @summary Tests for version validator. |
| 28 | +* @library /test/lib |
| 29 | +* @modules java.base/jdk.internal.misc |
| 30 | +* jdk.compiler |
| 31 | +* jdk.jartool |
| 32 | +* @build jdk.test.lib.Utils |
| 33 | +* jdk.test.lib.Asserts |
| 34 | +* jdk.test.lib.JDKToolFinder |
| 35 | +* jdk.test.lib.JDKToolLauncher |
| 36 | +* jdk.test.lib.Platform |
| 37 | +* jdk.test.lib.process.* |
| 38 | +* MRTestBase |
| 39 | +* @run testng/timeout=1200 VersionValidatorTest |
| 40 | +*/ |
| 41 | + |
| 42 | +import org.testng.annotations.BeforeMethod; |
| 43 | +import org.testng.annotations.DataProvider; |
| 44 | +import org.testng.annotations.Test; |
| 45 | + |
| 46 | +import java.lang.reflect.Method; |
| 47 | +import java.nio.file.Files; |
| 48 | +import java.nio.file.Path; |
| 49 | +import java.nio.file.Paths; |
| 50 | +import java.util.List; |
| 51 | + |
| 52 | +public class VersionValidatorTest extends MRTestBase { |
| 53 | + private Path root; |
| 54 | + |
| 55 | + @BeforeMethod |
| 56 | + void testInit(Method method) { |
| 57 | + root = Paths.get(method.getName()); |
| 58 | + } |
| 59 | + |
| 60 | + @Test(dataProvider = "differentMajorVersions") |
| 61 | + public void onlyCompatibleVersionIsAllowedInMultiReleaseJar(String baseMajorVersion, String otherMajorVersion, |
| 62 | + boolean enablePreviewForBaseVersion, boolean enablePreviewForOtherVersion, boolean isAcceptable) |
| 63 | + throws Throwable { |
| 64 | + Path baseVersionClassesDir = compileLibClass(baseMajorVersion, enablePreviewForBaseVersion); |
| 65 | + Path otherVersionClassesDir = compileLibClass(otherMajorVersion, enablePreviewForOtherVersion); |
| 66 | + |
| 67 | + var result = jar("--create", "--file", "lib.jar", "-C", baseVersionClassesDir.toString(), "Lib.class", |
| 68 | + "--release", otherMajorVersion, "-C", otherVersionClassesDir.toString(), "Lib.class"); |
| 69 | + |
| 70 | + if (isAcceptable) { |
| 71 | + result.shouldHaveExitValue(SUCCESS) |
| 72 | + .shouldBeEmptyIgnoreVMWarnings(); |
| 73 | + } else { |
| 74 | + result.shouldNotHaveExitValue(SUCCESS) |
| 75 | + .shouldContain("has a class version incompatible with an earlier version"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private Path compileLibClass(String majorVersion, boolean enablePreview) throws Throwable { |
| 80 | + String classTemplate = """ |
| 81 | + public class Lib { |
| 82 | + public static int version = $VERSION; |
| 83 | + } |
| 84 | + """; |
| 85 | + |
| 86 | + Path sourceFile = Files.createDirectories(root.resolve("src").resolve(majorVersion)).resolve("Lib.java"); |
| 87 | + Files.write(sourceFile, classTemplate.replace("$VERSION", majorVersion).getBytes()); |
| 88 | + |
| 89 | + Path classesDir = root.resolve("classes").resolve(majorVersion); |
| 90 | + |
| 91 | + javac(classesDir, List.of("--release", majorVersion), sourceFile); |
| 92 | + if (enablePreview) { |
| 93 | + rewriteMinorVersionForEnablePreviewClass(classesDir.resolve("Lib.class")); |
| 94 | + } |
| 95 | + return classesDir; |
| 96 | + } |
| 97 | + |
| 98 | + private void rewriteMinorVersionForEnablePreviewClass(Path classFile) throws Throwable { |
| 99 | + byte[] classBytes = Files.readAllBytes(classFile); |
| 100 | + classBytes[4] = -1; |
| 101 | + classBytes[5] = -1; |
| 102 | + Files.write(classFile, classBytes); |
| 103 | + } |
| 104 | + |
| 105 | + @DataProvider |
| 106 | + Object[][] differentMajorVersions() { |
| 107 | + return new Object[][] { |
| 108 | + { "16", "17", false, true, true }, |
| 109 | + { "16", "17", false, false, true }, |
| 110 | + { "16", "17", true, true, true }, |
| 111 | + { "16", "17", true, false, true }, |
| 112 | + { "17", "16", false, true, false }, |
| 113 | + { "17", "16", false, false, false }, |
| 114 | + { "17", "16", true, true, false }, |
| 115 | + { "17", "16", true, false, false }, |
| 116 | + }; |
| 117 | + } |
| 118 | +} |
0 commit comments