Skip to content

Commit 741ae06

Browse files
Qing Xiaosormuras
authored andcommitted
8313613: Use JUnit in langtools/lib tests
Reviewed-by: cstein, asotona
1 parent 8c1bb2b commit 741ae06

17 files changed

+471
-346
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# This file identifies root(s) of the test-ng hierarchy.
1+
# This file identifies root(s) of the JUnit hierarchy.
22

3-
TestNG.dirs = .
3+
JUnit.dirs = .
44

55
modules = \
66
jdk.compiler/com.sun.tools.javac.util
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package tools.javac.combo;
2+
3+
import java.util.Collections;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
import org.junit.jupiter.api.extension.AfterAllCallback;
8+
import org.junit.jupiter.api.extension.ExtensionContext;
9+
import org.junit.jupiter.api.extension.TestWatcher;
10+
11+
public class ComboWatcher implements TestWatcher, AfterAllCallback {
12+
private final Set<String> errors = Collections.synchronizedSet(new HashSet<>());
13+
14+
@Override
15+
public void testFailed(ExtensionContext context, Throwable cause) {
16+
if (context.getRequiredTestInstance() instanceof JavacTemplateTestBase instance) {
17+
errors.addAll(instance.diags.errorKeys());
18+
if (instance instanceof CompilationTestCase) {
19+
// Make sure offending template ends up in log file on failure
20+
System.err.printf("Diagnostics: %s%nTemplate: %s%n", instance.diags.errorKeys(),
21+
instance.sourceFiles.stream().map(SourceFile::template).toList());
22+
}
23+
}
24+
}
25+
26+
@Override
27+
public void afterAll(ExtensionContext extensionContext) {
28+
if (errors.isEmpty()) return;
29+
System.err.println("Errors found in tests: " + errors);
30+
}
31+
}

test/langtools/lib/combo/tools/javac/combo/CompilationTestCase.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,30 +31,14 @@
3131

3232
import javax.tools.Diagnostic;
3333

34-
import org.testng.ITestResult;
35-
import org.testng.annotations.AfterMethod;
36-
import org.testng.annotations.Test;
37-
38-
import static java.util.stream.Collectors.toList;
39-
4034
/**
4135
* Base class for negative and positive compilation tests.
4236
*/
43-
@Test
4437
public class CompilationTestCase extends JavacTemplateTestBase {
45-
private String[] compileOptions = new String[] { };
38+
private String[] compileOptions = new String[]{};
4639
private String defaultFileName = "Source.java";
4740
private String programShell = "#";
4841

49-
@AfterMethod
50-
public void dumpTemplateIfError(ITestResult result) {
51-
// Make sure offending template ends up in log file on failure
52-
if (!result.isSuccess()) {
53-
System.err.printf("Diagnostics: %s%nTemplate: %s%n", diags.errorKeys(),
54-
sourceFiles.stream().map(p -> p.snd).collect(toList()));
55-
}
56-
}
57-
5842
protected void setProgramShell(String shell) {
5943
programShell = shell;
6044
}
@@ -81,7 +65,7 @@ protected void removeLastCompileOptions(int i) {
8165
throw new AssertionError("unexpected negative value " + i);
8266
}
8367
if (i >= compileOptions.length) {
84-
compileOptions = new String[] {};
68+
compileOptions = new String[]{};
8569
} else {
8670
compileOptions = Arrays.copyOf(compileOptions, compileOptions.length - i);
8771
}
@@ -105,8 +89,7 @@ private File assertCompile(String program, Runnable postTest, boolean generate)
10589
File dir = null;
10690
try {
10791
dir = compile(generate);
108-
}
109-
catch (IOException e) {
92+
} catch (IOException e) {
11093
throw new RuntimeException(e);
11194
}
11295
postTest.run();

test/langtools/lib/combo/tools/javac/combo/JavacTemplateTestBase.java

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,10 +33,8 @@
3333
import java.util.Arrays;
3434
import java.util.Collections;
3535
import java.util.HashMap;
36-
import java.util.HashSet;
3736
import java.util.List;
3837
import java.util.Map;
39-
import java.util.Set;
4038
import java.util.concurrent.atomic.AtomicInteger;
4139
import java.util.function.Consumer;
4240
import javax.tools.Diagnostic;
@@ -48,17 +46,13 @@
4846
import javax.tools.ToolProvider;
4947

5048
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;
5751

58-
import static org.testng.Assert.fail;
52+
import static org.junit.jupiter.api.Assertions.fail;
5953

6054
/**
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
6256
* source file generation, compilation, classloading, execution, and separate
6357
* compilation.
6458
*
@@ -70,16 +64,15 @@
7064
*
7165
* @author Brian Goetz
7266
*/
73-
@Test
67+
@ExtendWith(ComboWatcher.class)
7468
public abstract class JavacTemplateTestBase {
75-
private static final Set<String> suiteErrors = Collections.synchronizedSet(new HashSet<>());
7669
private static final AtomicInteger counter = new AtomicInteger();
7770
private static final File root = new File("gen");
7871
private static final File nullDir = new File("empty");
7972

8073
protected final Map<String, Template> templates = new HashMap<>();
8174
protected final Diagnostics diags = new Diagnostics();
82-
protected final List<Pair<String, String>> sourceFiles = new ArrayList<>();
75+
protected final List<SourceFile> sourceFiles = new ArrayList<>();
8376
protected final List<String> compileOptions = new ArrayList<>();
8477
protected final List<File> classpaths = new ArrayList<>();
8578

@@ -95,7 +88,7 @@ protected void addTemplate(String name, String s) {
9588

9689
/** Add a source file */
9790
protected void addSourceFile(String name, String template) {
98-
sourceFiles.add(new Pair<>(name, template));
91+
sourceFiles.add(new SourceFile(name, template));
9992
}
10093

10194
/** Add a File to the class path to be used when loading classes; File values
@@ -130,7 +123,7 @@ protected void addCompileOptions(String... opts) {
130123
protected void resetClassPaths() { classpaths.clear(); }
131124

132125
// Before each test method, reset everything
133-
@BeforeMethod
126+
@BeforeEach
134127
public void reset() {
135128
resetCompileOptions();
136129
resetDiagnostics();
@@ -139,38 +132,6 @@ public void reset() {
139132
resetClassPaths();
140133
}
141134

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-
174135
/** Assert that all previous calls to compile() succeeded */
175136
protected void assertCompileSucceeded() {
176137
if (diags.errorsFound())
@@ -258,23 +219,19 @@ protected void compile() throws IOException {
258219
/** Compile all registered source files, optionally generating class files
259220
* and returning a File describing the directory to which they were written */
260221
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();
264223
return compile(classpaths, files, generate);
265224
}
266225

267226
/** Compile all registered source files, using the provided list of class paths
268227
* for finding required classfiles, optionally generating class files
269228
* and returning a File describing the directory to which they were written */
270229
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();
274231
return compile(classpaths, files, generate);
275232
}
276233

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 {
278235
JavaCompiler systemJavaCompiler = ToolProvider.getSystemJavaCompiler();
279236
try (StandardJavaFileManager fm = systemJavaCompiler.getStandardFileManager(null, null, null)) {
280237
if (classpaths.size() > 0)
@@ -327,9 +284,9 @@ public String toString() {
327284
private class FileAdapter extends SimpleJavaFileObject {
328285
private final String templateString;
329286

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();
333290
}
334291

335292
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2023, 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+
package tools.javac.combo;
25+
26+
public record SourceFile(String name, String template) {}

test/langtools/lib/combo/tools/javac/combo/TemplateTest.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,34 +23,34 @@
2323

2424
package tools.javac.combo;
2525

26-
import org.testng.annotations.BeforeTest;
27-
import org.testng.annotations.Test;
26+
import static org.junit.jupiter.api.Assertions.*;
27+
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.BeforeEach;
2830

2931
import java.util.HashMap;
3032
import java.util.Map;
3133

32-
import static org.testng.Assert.assertEquals;
33-
3434
/**
3535
* TemplateTest
3636
*/
37-
@Test
38-
public class TemplateTest {
39-
Map<String, Template> vars = new HashMap<>();
37+
class TemplateTest {
38+
final Map<String, Template> vars = new HashMap<>();
4039

41-
@BeforeTest
40+
@BeforeEach
4241
void before() { vars.clear(); }
4342

4443
private void assertTemplate(String expected, String template) {
4544
String result = Template.expandTemplate(template, vars);
46-
assertEquals(result, expected, "for " + template);
45+
assertEquals(expected, result, "for " + template);
4746
}
4847

4948
private String dotIf(String s) {
5049
return s == null || s.isEmpty() ? "" : "." + s;
5150
}
5251

53-
public void testTemplateExpansion() {
52+
@Test
53+
void testTemplateExpansion() {
5454
vars.put("A", s -> "a" + dotIf(s));
5555
vars.put("B", s -> "b" + dotIf(s));
5656
vars.put("C", s -> "#{A}#{B}");
@@ -72,7 +72,8 @@ public void testTemplateExpansion() {
7272
assertTemplate("#{A", "#{A");
7373
}
7474

75-
public void testIndexedTemplate() {
75+
@Test
76+
void testIndexedTemplate() {
7677
vars.put("A[0]", s -> "a" );
7778
vars.put("A[1]", s -> "b" );
7879
vars.put("A[2]", s -> "c" );
@@ -82,13 +83,14 @@ public void testIndexedTemplate() {
8283
assertTemplate("c", "#{A[2]}");
8384
}
8485

85-
public void testAngleBrackets() {
86+
@Test
87+
void testAngleBrackets() {
8688
vars.put("X", s -> "xyz");
8789
assertTemplate("List<String> ls = xyz;", "List<String> ls = #{X};");
8890
}
8991

90-
@Test(expectedExceptions = IllegalStateException.class )
91-
public void testUnknownKey() {
92-
assertTemplate("#{Q}", "#{Q}");
92+
@Test
93+
void testUnknownKey() {
94+
assertThrows(IllegalStateException.class, () -> assertTemplate("#{Q}", "#{Q}"));
9395
}
9496
}

0 commit comments

Comments
 (0)