21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.io.File; |
24 import java.io.File; |
25 import java.io.IOException; |
25 import java.io.IOException; |
26 import java.io.PrintStream; |
26 import java.util.List; |
27 import java.util.*; |
27 import java.util.Objects; |
28 import java.util.function.Function; |
28 import java.util.function.Function; |
29 import java.util.stream.Stream; |
29 import java.util.stream.Stream; |
30 import javax.tools.*; |
30 import javax.tools.DiagnosticCollector; |
|
31 import javax.tools.JavaCompiler; |
|
32 import javax.tools.JavaFileObject; |
|
33 import javax.tools.ToolProvider; |
31 |
34 |
32 import static java.lang.String.format; |
35 import static java.lang.String.format; |
33 import static java.lang.System.lineSeparator; |
36 import static java.lang.System.lineSeparator; |
34 import static java.util.Arrays.asList; |
37 import static java.util.Arrays.asList; |
35 import static java.util.Collections.emptyList; |
38 import static java.util.Collections.emptyList; |
36 import static java.util.stream.Collectors.joining; |
39 import static java.util.stream.Collectors.joining; |
37 import static java.util.stream.Collectors.toList; |
40 import static java.util.stream.Collectors.toList; |
38 |
41 |
|
42 /** |
|
43 * Base class for class file attribute tests. |
|
44 * Contains methods for compiling generated sources in memory, |
|
45 * for reading files from disk and a lot of assert* methods. |
|
46 */ |
39 public class TestBase { |
47 public class TestBase { |
40 |
48 |
41 public static final String LINE_SEPARATOR = lineSeparator(); |
49 public static final String LINE_SEPARATOR = lineSeparator(); |
42 |
50 |
43 private <S> InMemoryFileManager compile( |
51 private <S> InMemoryFileManager compile( |
64 } |
72 } |
65 return fileManager; |
73 return fileManager; |
66 } |
74 } |
67 } |
75 } |
68 |
76 |
|
77 /** |
|
78 * Compiles sources in memory. |
|
79 * |
|
80 * @param sources to compile. |
|
81 * @return memory file manager which contains class files and class loader. |
|
82 */ |
69 public InMemoryFileManager compile(String... sources) |
83 public InMemoryFileManager compile(String... sources) |
70 throws IOException, CompilationException { |
84 throws IOException, CompilationException { |
71 return compile(emptyList(), sources); |
85 return compile(emptyList(), sources); |
72 } |
86 } |
73 |
87 |
74 /** |
88 /** |
75 * @param options - compiler options |
89 * Compiles sources in memory. |
76 * @param sources |
90 * |
|
91 * @param options compiler options. |
|
92 * @param sources sources to compile. |
77 * @return map where key is className, value is corresponding ClassFile. |
93 * @return map where key is className, value is corresponding ClassFile. |
78 * @throws IOException |
94 */ |
79 */ |
95 public InMemoryFileManager compile(List<String> options, String... sources) |
80 public InMemoryFileManager compile(List<String> options, String...sources) |
|
81 throws IOException, CompilationException { |
96 throws IOException, CompilationException { |
82 return compile(options, ToolBox.JavaSource::new, asList(sources)); |
97 return compile(options, ToolBox.JavaSource::new, asList(sources)); |
83 } |
98 } |
84 |
99 |
|
100 /** |
|
101 * Compiles sources in memory. |
|
102 * |
|
103 * @param sources sources[i][0] - name of file, sources[i][1] - sources. |
|
104 * @return map where key is className, value is corresponding ClassFile. |
|
105 */ |
85 public InMemoryFileManager compile(String[]... sources) throws IOException, |
106 public InMemoryFileManager compile(String[]... sources) throws IOException, |
86 CompilationException { |
107 CompilationException { |
87 return compile(emptyList(), sources); |
108 return compile(emptyList(), sources); |
88 } |
109 } |
89 |
110 |
90 /** |
111 /** |
91 * @param options - compiler options |
112 * Compiles sources in memory. |
92 * @param sources - sources[i][0] - name of file, sources[i][1] - sources |
113 * |
|
114 * @param options compiler options |
|
115 * @param sources sources[i][0] - name of file, sources[i][1] - sources. |
93 * @return map where key is className, value is corresponding ClassFile. |
116 * @return map where key is className, value is corresponding ClassFile. |
94 * @throws IOException |
117 */ |
95 * @throws CompilationException |
118 public InMemoryFileManager compile(List<String> options, String[]... sources) |
96 */ |
|
97 public InMemoryFileManager compile(List<String> options, String[]...sources) |
|
98 throws IOException, CompilationException { |
119 throws IOException, CompilationException { |
99 return compile(options, src -> new ToolBox.JavaSource(src[0], src[1]), asList(sources)); |
120 return compile(options, src -> new ToolBox.JavaSource(src[0], src[1]), asList(sources)); |
100 } |
121 } |
101 |
122 |
102 public void assertEquals(Object actual, Object expected, String message) { |
123 public void assertEquals(Object actual, Object expected, String message) { |
140 |
161 |
141 public File getClassFile(Class clazz) { |
162 public File getClassFile(Class clazz) { |
142 return getClassFile(clazz.getName().replace(".", "/") + ".class"); |
163 return getClassFile(clazz.getName().replace(".", "/") + ".class"); |
143 } |
164 } |
144 |
165 |
|
166 /** |
|
167 * Prints message to standard error. New lines are converted to system dependent NL. |
|
168 * |
|
169 * @param message string to print. |
|
170 */ |
145 public void echo(String message) { |
171 public void echo(String message) { |
146 System.err.println(message.replace("\n", LINE_SEPARATOR)); |
172 System.err.println(message.replace("\n", LINE_SEPARATOR)); |
147 } |
173 } |
148 |
174 |
149 public void printf(String template, Object...args) { |
175 /** |
|
176 * Substitutes args in template and prints result to standard error. New lines are converted to system dependent NL. |
|
177 * |
|
178 * @param template template in standard String.format(...) format. |
|
179 * @param args arguments to substitute in template. |
|
180 */ |
|
181 public void printf(String template, Object... args) { |
150 System.err.printf(template, Stream.of(args) |
182 System.err.printf(template, Stream.of(args) |
151 .map(Objects::toString) |
183 .map(Objects::toString) |
152 .map(m -> m.replace("\n", LINE_SEPARATOR)) |
184 .map(m -> m.replace("\n", LINE_SEPARATOR)) |
153 .collect(toList()) |
185 .collect(toList()) |
154 .toArray()); |
186 .toArray()); |