1 /*
2 * Copyright (c) 2017, 2024, 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 * @modules java.base/jdk.internal.module
27 * @library /test/lib
28 * @run testng ClassFileVersionsTest
29 * @summary Test parsing of module-info.class with different class file versions
30 */
31
32 import java.lang.classfile.ClassFile;
33 import java.lang.module.InvalidModuleDescriptorException;
34 import java.lang.module.ModuleDescriptor;
35 import java.lang.module.ModuleDescriptor.Requires.Modifier;
36 import java.nio.ByteBuffer;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Set;
40
41 import static java.lang.module.ModuleDescriptor.Requires.Modifier.*;
42
43 import jdk.test.lib.util.ModuleInfoWriter;
44
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.Test;
47 import static org.testng.Assert.*;
48
49 public class ClassFileVersionsTest {
50 private static final int PREVIEW_MINOR_VERSION =
51 ClassFile.PREVIEW_MINOR_VERSION;
52 private static final int FEATURE;
53 static {
54 FEATURE = Runtime.version().feature();
55 assert FEATURE >= 10;
56 }
57
58 // major, minor, modifiers for requires java.base
59 @DataProvider(name = "supported")
60 public Object[][] supported() {
61 /*
62 * There are four test cases for JDK 9, one test case
63 * for each subsequent JDK version from JDK 10 to the current
64 * feature release, and two tests for the current release with
65 * a preview flag set, for a total of (4 + (FEATURE - 9) + 2)
66 * rows.
67 */
68 List<Object[]> result = new ArrayList<>(4 + (FEATURE - 9) + 2);
69
70 // Class file version of JDK 9 is 53.0
71 result.add(new Object[]{ 53, 0, Set.of()});
72 result.add(new Object[]{ 53, 0, Set.of(STATIC) });
73 result.add(new Object[]{ 53, 0, Set.of(TRANSITIVE) });
74 result.add(new Object[]{ 53, 0, Set.of(STATIC, TRANSITIVE) });
75
76 // Major class file version of JDK N is 44 + n. Create rows
77 // for JDK 10 through FEATURE.
78 for (int i = 10; i <= FEATURE; i++) {
79 result.add(new Object[]{ 44 + i, 0, Set.of()});
80 result.add(new Object[]{ 44 + i, 0, Set.of(TRANSITIVE)});
81 }
82
83 result.add(new Object[]{ 44 + FEATURE,
84 PREVIEW_MINOR_VERSION,
85 Set.of()});
86 result.add(new Object[]{ 44 + FEATURE,
87 PREVIEW_MINOR_VERSION,
88 Set.of(TRANSITIVE) });
89
90 return result.toArray(s -> new Object[s][]);
91 }
92
93 // major, minor, modifiers for requires java.base
94 @DataProvider(name = "unsupported")
95 public Object[][] unsupported() {
96 /*
97 * There are three test cases for releases prior to JDK 9,
98 * three test cases for each JDK version from JDK 10 to the
99 * current feature release, two tests for the current release with
100 * the preview flag set, plus one addition test case for
101 * the next release for a total of (3 + (FEATURE - 9) * 3 + 2 + 1)
102 * rows.
103 */
104 List<Object[]> result = new ArrayList<>(3 + (FEATURE - 9) * 3 + 2 + 1);
105
106 result.add(new Object[]{50, 0, Set.of()}); // JDK 6
107 result.add(new Object[]{51, 0, Set.of()}); // JDK 7
108 result.add(new Object[]{52, 0, Set.of()}); // JDK 8
109
110 for (int i = 10; i <= FEATURE ; i++) {
111 // Major class file version of JDK N is 44+n
112 result.add(new Object[]{i + 44, 0, Set.of(STATIC)});
113 result.add(new Object[]{i + 44, 0, Set.of(STATIC, TRANSITIVE)});
114 }
115
116 result.add(new Object[]{FEATURE + 44,
117 PREVIEW_MINOR_VERSION,
118 Set.of(STATIC)});
119 result.add(new Object[]{FEATURE + 44,
120 PREVIEW_MINOR_VERSION,
121 Set.of(STATIC, TRANSITIVE)});
122
123 result.add(new Object[]{FEATURE+1+44, 0, Set.of()});
124
125 return result.toArray(s -> new Object[s][]);
126 }
127
128 @Test(dataProvider = "supported")
129 public void testSupported(int major, int minor, Set<Modifier> ms) {
130 ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo")
131 .requires(ms, "java.base")
132 .build();
133 ByteBuffer bb = ModuleInfoWriter.toByteBuffer(descriptor);
134 classFileVersion(bb, major, minor);
135 descriptor = ModuleDescriptor.read(bb);
136 assertEquals(descriptor.name(), "foo");
137 }
138
139 @Test(dataProvider = "unsupported",
140 expectedExceptions = InvalidModuleDescriptorException.class)
141 public void testUnsupported(int major, int minor, Set<Modifier> ms) {
142 ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo")
143 .requires(ms, "java.base")
144 .build();
145 ByteBuffer bb = ModuleInfoWriter.toByteBuffer(descriptor);
146 classFileVersion(bb, major, minor);
147
148 // throws InvalidModuleDescriptorException
149 ModuleDescriptor.read(bb);
150 }
151
152 private void classFileVersion(ByteBuffer bb, int major, int minor) {
153 bb.putShort(4, (short) minor);
154 bb.putShort(6, (short) major);
155 }
156 }
--- EOF ---