Path: blob/master/test/jdk/java/lang/module/ClassFileVersionsTest.java
41149 views
/*1* Copyright (c) 2017, 2020, 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* @modules java.base/jdk.internal.module26* @run testng ClassFileVersionsTest27* @summary Test parsing of module-info.class with different class file versions28*/2930import java.lang.module.InvalidModuleDescriptorException;31import java.lang.module.ModuleDescriptor;32import java.lang.module.ModuleDescriptor.Requires.Modifier;33import java.nio.ByteBuffer;34import java.util.Set;3536import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;3738import jdk.internal.module.ModuleInfoWriter;3940import org.testng.annotations.DataProvider;41import org.testng.annotations.Test;42import static org.testng.Assert.*;4344public class ClassFileVersionsTest {4546// major, minor, modifiers for requires java.base47@DataProvider(name = "supported")48public Object[][] supported() {49return new Object[][]{50{ 53, 0, Set.of() }, // JDK 951{ 53, 0, Set.of(STATIC) },52{ 53, 0, Set.of(TRANSITIVE) },53{ 53, 0, Set.of(STATIC, TRANSITIVE) },5455{ 54, 0, Set.of() }, // JDK 1056{ 55, 0, Set.of() }, // JDK 1157{ 56, 0, Set.of() }, // JDK 1258{ 57, 0, Set.of() }, // JDK 1359{ 58, 0, Set.of() }, // JDK 1460{ 59, 0, Set.of() }, // JDK 1561{ 60, 0, Set.of() }, // JDK 1662{ 61, 0, Set.of() }, // JDK 1763};64}6566// major, minor, modifiers for requires java.base67@DataProvider(name = "unsupported")68public Object[][] unsupported() {69return new Object[][]{70{ 50, 0, Set.of()}, // JDK 671{ 51, 0, Set.of()}, // JDK 772{ 52, 0, Set.of()}, // JDK 87374{ 54, 0, Set.of(STATIC) }, // JDK 1075{ 54, 0, Set.of(TRANSITIVE) },76{ 54, 0, Set.of(STATIC, TRANSITIVE) },7778{ 55, 0, Set.of(STATIC) }, // JDK 1179{ 55, 0, Set.of(TRANSITIVE) },80{ 55, 0, Set.of(STATIC, TRANSITIVE) },8182{ 56, 0, Set.of(STATIC) }, // JDK 1283{ 56, 0, Set.of(TRANSITIVE) },84{ 56, 0, Set.of(STATIC, TRANSITIVE) },8586{ 57, 0, Set.of(STATIC) }, // JDK 1387{ 57, 0, Set.of(TRANSITIVE) },88{ 57, 0, Set.of(STATIC, TRANSITIVE) },8990{ 58, 0, Set.of(STATIC) }, // JDK 1491{ 58, 0, Set.of(TRANSITIVE) },92{ 58, 0, Set.of(STATIC, TRANSITIVE) },9394{ 59, 0, Set.of(STATIC) }, // JDK 1595{ 59, 0, Set.of(TRANSITIVE) },96{ 59, 0, Set.of(STATIC, TRANSITIVE) },9798{ 60, 0, Set.of(STATIC) }, // JDK 1699{ 60, 0, Set.of(TRANSITIVE) },100{ 60, 0, Set.of(STATIC, TRANSITIVE) },101102{ 61, 0, Set.of(STATIC) }, // JDK 17103{ 61, 0, Set.of(TRANSITIVE) },104{ 61, 0, Set.of(STATIC, TRANSITIVE) },105106{ 62, 0, Set.of()}, // JDK 18107};108}109110@Test(dataProvider = "supported")111public void testSupported(int major, int minor, Set<Modifier> ms) {112ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo")113.requires(ms, "java.base")114.build();115ByteBuffer bb = ModuleInfoWriter.toByteBuffer(descriptor);116classFileVersion(bb, major, minor);117descriptor = ModuleDescriptor.read(bb);118assertEquals(descriptor.name(), "foo");119}120121@Test(dataProvider = "unsupported",122expectedExceptions = InvalidModuleDescriptorException.class)123public void testUnsupported(int major, int minor, Set<Modifier> ms) {124ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo")125.requires(ms, "java.base")126.build();127ByteBuffer bb = ModuleInfoWriter.toByteBuffer(descriptor);128classFileVersion(bb, major, minor);129130// throws InvalidModuleDescriptorException131ModuleDescriptor.read(bb);132}133134private void classFileVersion(ByteBuffer bb, int major, int minor) {135bb.putShort(4, (short) minor);136bb.putShort(6, (short) major);137}138}139140141