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