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 import com.sun.tools.classfile.ClassFile;
25 import com.sun.tools.classfile.ConstantPool;
26 import com.sun.tools.classfile.ConstantPoolException;
27 import com.sun.tools.classfile.Module_attribute;
28 import com.sun.tools.javac.util.Pair;
29
30 import java.io.IOException;
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.lang.reflect.Method;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.regex.Pattern;
43 import java.util.stream.Collectors;
44
45 import toolbox.JavacTask;
46 import toolbox.Task;
47 import toolbox.ToolBox;
48
49 public class ModuleTestBase {
50 protected final ToolBox tb = new ToolBox();
51 private final TestResult tr = new TestResult();
52
53
54 protected void run() throws Exception {
55 boolean noTests = true;
56 for (Method method : this.getClass().getMethods()) {
57 if (method.isAnnotationPresent(Test.class)) {
110 tr.checkEquals(module.uses_count, moduleDescriptor.uses.size(), "Wrong amount of uses.");
111 List<String> actualUses = new ArrayList<>();
112 for (int usesIdx : module.uses_index) {
113 String uses = constantPool.getClassInfo(usesIdx).getBaseName().replace('/', '.');
114 actualUses.add(uses);
115 }
116 tr.checkContains(actualUses, moduleDescriptor.uses, "Lists of uses don't match");
117 }
118
119 private void testProvides(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
120 tr.checkEquals(module.provides_count, moduleDescriptor.provides.size(), "Wrong amount of provides.");
121 List<Pair<String, String>> actualProvides = new ArrayList<>();
122 for (Module_attribute.ProvidesEntry provide : module.provides) {
123 String provides = constantPool.getClassInfo(provide.provides_index).getBaseName().replace('/', '.');
124 String with = constantPool.getClassInfo(provide.with_index).getBaseName().replace('/', '.');
125 actualProvides.add(Pair.of(provides, with));
126 }
127 tr.checkContains(actualProvides, moduleDescriptor.provides, "Lists of provides don't match");
128 }
129
130 protected void compile(Path base) throws IOException {
131 new JavacTask(tb)
132 .files(findJavaFiles(base))
133 .run(Task.Expect.SUCCESS)
134 .writeAll();
135 }
136
137 private static Path[] findJavaFiles(Path src) throws IOException {
138 return Files.find(src, Integer.MAX_VALUE, (path, attr) -> path.toString().endsWith(".java"))
139 .toArray(Path[]::new);
140 }
141
142 @Retention(RetentionPolicy.RUNTIME)
143 @interface Test {
144 }
145
146 class ModuleDescriptor {
147
148 private final String name;
149 //pair is name of module and flag(public,mandated,synthetic)
150 private final List<Pair<String, Integer>> requires = new ArrayList<>();
151
|
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 import com.sun.tools.classfile.ClassFile;
25 import com.sun.tools.classfile.ConstantPool;
26 import com.sun.tools.classfile.ConstantPoolException;
27 import com.sun.tools.classfile.Module_attribute;
28 import com.sun.tools.javac.util.Pair;
29
30 import java.io.IOException;
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.lang.reflect.Method;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.regex.Pattern;
44 import java.util.stream.Collectors;
45
46 import toolbox.JavacTask;
47 import toolbox.Task;
48 import toolbox.ToolBox;
49
50 public class ModuleTestBase {
51 protected final ToolBox tb = new ToolBox();
52 private final TestResult tr = new TestResult();
53
54
55 protected void run() throws Exception {
56 boolean noTests = true;
57 for (Method method : this.getClass().getMethods()) {
58 if (method.isAnnotationPresent(Test.class)) {
111 tr.checkEquals(module.uses_count, moduleDescriptor.uses.size(), "Wrong amount of uses.");
112 List<String> actualUses = new ArrayList<>();
113 for (int usesIdx : module.uses_index) {
114 String uses = constantPool.getClassInfo(usesIdx).getBaseName().replace('/', '.');
115 actualUses.add(uses);
116 }
117 tr.checkContains(actualUses, moduleDescriptor.uses, "Lists of uses don't match");
118 }
119
120 private void testProvides(ModuleDescriptor moduleDescriptor, Module_attribute module, ConstantPool constantPool) throws ConstantPoolException {
121 tr.checkEquals(module.provides_count, moduleDescriptor.provides.size(), "Wrong amount of provides.");
122 List<Pair<String, String>> actualProvides = new ArrayList<>();
123 for (Module_attribute.ProvidesEntry provide : module.provides) {
124 String provides = constantPool.getClassInfo(provide.provides_index).getBaseName().replace('/', '.');
125 String with = constantPool.getClassInfo(provide.with_index).getBaseName().replace('/', '.');
126 actualProvides.add(Pair.of(provides, with));
127 }
128 tr.checkContains(actualProvides, moduleDescriptor.provides, "Lists of provides don't match");
129 }
130
131 protected void compile(Path base, String... options) throws IOException {
132 new JavacTask(tb)
133 .options(options)
134 .files(findJavaFiles(base))
135 .run(Task.Expect.SUCCESS)
136 .writeAll();
137 }
138
139 private static Path[] findJavaFiles(Path src) throws IOException {
140 return Files.find(src, Integer.MAX_VALUE, (path, attr) -> path.toString().endsWith(".java"))
141 .toArray(Path[]::new);
142 }
143
144 @Retention(RetentionPolicy.RUNTIME)
145 @interface Test {
146 }
147
148 class ModuleDescriptor {
149
150 private final String name;
151 //pair is name of module and flag(public,mandated,synthetic)
152 private final List<Pair<String, Integer>> requires = new ArrayList<>();
153
|