1
1
/*
2
- * Copyright (c) 2013, 2020 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2013, 2023 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
33
33
import java .util .Arrays ;
34
34
import java .util .Collections ;
35
35
import java .util .HashMap ;
36
- import java .util .HashSet ;
37
36
import java .util .List ;
38
37
import java .util .Map ;
39
- import java .util .Set ;
40
38
import java .util .concurrent .atomic .AtomicInteger ;
41
39
import java .util .function .Consumer ;
42
40
import javax .tools .Diagnostic ;
48
46
import javax .tools .ToolProvider ;
49
47
50
48
import com .sun .source .util .JavacTask ;
51
- import com .sun .tools .javac .util .Pair ;
52
- import org .testng .ITestResult ;
53
- import org .testng .annotations .AfterMethod ;
54
- import org .testng .annotations .AfterSuite ;
55
- import org .testng .annotations .BeforeMethod ;
56
- import org .testng .annotations .Test ;
49
+ import org .junit .jupiter .api .BeforeEach ;
50
+ import org .junit .jupiter .api .extension .ExtendWith ;
57
51
58
- import static org .testng . Assert .fail ;
52
+ import static org .junit . jupiter . api . Assertions .fail ;
59
53
60
54
/**
61
- * Base class for template-driven TestNG javac tests that support on-the-fly
55
+ * Base class for template-driven JUnit javac tests that support on-the-fly
62
56
* source file generation, compilation, classloading, execution, and separate
63
57
* compilation.
64
58
*
70
64
*
71
65
* @author Brian Goetz
72
66
*/
73
- @ Test
67
+ @ ExtendWith ( ComboWatcher . class )
74
68
public abstract class JavacTemplateTestBase {
75
- private static final Set <String > suiteErrors = Collections .synchronizedSet (new HashSet <>());
76
69
private static final AtomicInteger counter = new AtomicInteger ();
77
70
private static final File root = new File ("gen" );
78
71
private static final File nullDir = new File ("empty" );
79
72
80
73
protected final Map <String , Template > templates = new HashMap <>();
81
74
protected final Diagnostics diags = new Diagnostics ();
82
- protected final List <Pair < String , String > > sourceFiles = new ArrayList <>();
75
+ protected final List <SourceFile > sourceFiles = new ArrayList <>();
83
76
protected final List <String > compileOptions = new ArrayList <>();
84
77
protected final List <File > classpaths = new ArrayList <>();
85
78
@@ -95,7 +88,7 @@ protected void addTemplate(String name, String s) {
95
88
96
89
/** Add a source file */
97
90
protected void addSourceFile (String name , String template ) {
98
- sourceFiles .add (new Pair <> (name , template ));
91
+ sourceFiles .add (new SourceFile (name , template ));
99
92
}
100
93
101
94
/** Add a File to the class path to be used when loading classes; File values
@@ -130,7 +123,7 @@ protected void addCompileOptions(String... opts) {
130
123
protected void resetClassPaths () { classpaths .clear (); }
131
124
132
125
// Before each test method, reset everything
133
- @ BeforeMethod
126
+ @ BeforeEach
134
127
public void reset () {
135
128
resetCompileOptions ();
136
129
resetDiagnostics ();
@@ -139,38 +132,6 @@ public void reset() {
139
132
resetClassPaths ();
140
133
}
141
134
142
- // After each test method, if the test failed, capture source files and diagnostics and put them in the log
143
- @ AfterMethod
144
- public void copyErrors (ITestResult result ) {
145
- if (!result .isSuccess ()) {
146
- suiteErrors .addAll (diags .errorKeys ());
147
-
148
- List <Object > list = new ArrayList <>();
149
- Collections .addAll (list , result .getParameters ());
150
- list .add ("Test case: " + getTestCaseDescription ());
151
- for (Pair <String , String > e : sourceFiles )
152
- list .add ("Source file " + e .fst + ": " + e .snd );
153
- if (diags .errorsFound ())
154
- list .add ("Compile diagnostics: " + diags .toString ());
155
- result .setParameters (list .toArray (new Object [list .size ()]));
156
- }
157
- }
158
-
159
- @ AfterSuite
160
- // After the suite is done, dump any errors to output
161
- public void dumpErrors () {
162
- if (!suiteErrors .isEmpty ())
163
- System .err .println ("Errors found in test suite: " + suiteErrors );
164
- }
165
-
166
- /**
167
- * Get a description of this test case; since test cases may be combinatorially
168
- * generated, this should include all information needed to describe the test case
169
- */
170
- protected String getTestCaseDescription () {
171
- return this .toString ();
172
- }
173
-
174
135
/** Assert that all previous calls to compile() succeeded */
175
136
protected void assertCompileSucceeded () {
176
137
if (diags .errorsFound ())
@@ -258,23 +219,19 @@ protected void compile() throws IOException {
258
219
/** Compile all registered source files, optionally generating class files
259
220
* and returning a File describing the directory to which they were written */
260
221
protected File compile (boolean generate ) throws IOException {
261
- List <JavaFileObject > files = new ArrayList <>();
262
- for (Pair <String , String > e : sourceFiles )
263
- files .add (new FileAdapter (e .fst , e .snd ));
222
+ var files = sourceFiles .stream ().map (FileAdapter ::new ).toList ();
264
223
return compile (classpaths , files , generate );
265
224
}
266
225
267
226
/** Compile all registered source files, using the provided list of class paths
268
227
* for finding required classfiles, optionally generating class files
269
228
* and returning a File describing the directory to which they were written */
270
229
protected File compile (List <File > classpaths , boolean generate ) throws IOException {
271
- List <JavaFileObject > files = new ArrayList <>();
272
- for (Pair <String , String > e : sourceFiles )
273
- files .add (new FileAdapter (e .fst , e .snd ));
230
+ var files = sourceFiles .stream ().map (FileAdapter ::new ).toList ();
274
231
return compile (classpaths , files , generate );
275
232
}
276
233
277
- private File compile (List <File > classpaths , List <JavaFileObject > files , boolean generate ) throws IOException {
234
+ private File compile (List <File > classpaths , List <? extends JavaFileObject > files , boolean generate ) throws IOException {
278
235
JavaCompiler systemJavaCompiler = ToolProvider .getSystemJavaCompiler ();
279
236
try (StandardJavaFileManager fm = systemJavaCompiler .getStandardFileManager (null , null , null )) {
280
237
if (classpaths .size () > 0 )
@@ -327,9 +284,9 @@ public String toString() {
327
284
private class FileAdapter extends SimpleJavaFileObject {
328
285
private final String templateString ;
329
286
330
- FileAdapter (String filename , String templateString ) {
331
- super (URI .create ("myfo:/" + filename ), Kind .SOURCE );
332
- this .templateString = templateString ;
287
+ FileAdapter (SourceFile file ) {
288
+ super (URI .create ("myfo:/" + file . name () ), Kind .SOURCE );
289
+ this .templateString = file . template () ;
333
290
}
334
291
335
292
public CharSequence getCharContent (boolean ignoreEncodingErrors ) {
0 commit comments