Skip to content

Commit 9d78040

Browse files
committed
Convert org.eclipse.cdt.core.tests.BaseTestFramework to JUnit5
BaseTestFramework is the base class for a hierarchy of tests and all those test updates are included in this commit.
1 parent c8316a3 commit 9d78040

File tree

21 files changed

+378
-370
lines changed

21 files changed

+378
-370
lines changed

core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteBaseTest.java

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,63 +14,28 @@
1414
*******************************************************************************/
1515
package org.eclipse.cdt.core.parser.tests.rewrite;
1616

17+
import static org.junit.jupiter.api.Assertions.fail;
18+
1719
import java.io.BufferedReader;
1820
import java.io.InputStreamReader;
19-
import java.util.List;
20-
import java.util.Map;
21-
import java.util.TreeMap;
2221

2322
import org.eclipse.cdt.core.tests.BaseTestFramework;
2423
import org.eclipse.core.resources.IFile;
2524
import org.eclipse.core.runtime.ILogListener;
2625
import org.eclipse.core.runtime.IStatus;
2726
import org.eclipse.core.runtime.NullProgressMonitor;
28-
import org.eclipse.core.runtime.Path;
2927
import org.eclipse.jface.text.TextSelection;
28+
import org.junit.jupiter.api.AfterEach;
3029

3130
/**
3231
* @author Guido Zgraggen IFS
3332
*/
3433
public abstract class RewriteBaseTest extends BaseTestFramework implements ILogListener {
3534
protected static final NullProgressMonitor NULL_PROGRESS_MONITOR = new NullProgressMonitor();
3635

37-
protected TreeMap<String, TestSourceFile> fileMap = new TreeMap<>();
3836
protected String fileWithSelection;
3937
protected TextSelection selection;
4038

41-
protected RewriteBaseTest(String name) {
42-
super(name);
43-
}
44-
45-
public RewriteBaseTest(String name, List<TestSourceFile> files) {
46-
super(name);
47-
for (TestSourceFile file : files) {
48-
fileMap.put(file.getName(), file);
49-
}
50-
}
51-
52-
@Override
53-
protected abstract void runTest() throws Throwable;
54-
55-
@Override
56-
protected void setUp() throws Exception {
57-
super.setUp();
58-
for (TestSourceFile testFile : fileMap.values()) {
59-
if (testFile.getSource().length() > 0) {
60-
importFile(testFile.getName(), testFile.getSource());
61-
}
62-
}
63-
}
64-
65-
protected void compareFiles(Map<String, TestSourceFile> testResourceFiles) throws Exception {
66-
for (String fileName : testResourceFiles.keySet()) {
67-
TestSourceFile file = testResourceFiles.get(fileName);
68-
IFile iFile = project.getFile(new Path(fileName));
69-
StringBuilder code = getCodeFromFile(iFile);
70-
assertEquals(TestHelper.unifyNewLines(file.getExpectedSource()), TestHelper.unifyNewLines(code.toString()));
71-
}
72-
}
73-
7439
protected StringBuilder getCodeFromFile(IFile file) throws Exception {
7540
BufferedReader br = new BufferedReader(new InputStreamReader(file.getContents()));
7641
StringBuilder code = new StringBuilder();
@@ -83,11 +48,10 @@ protected StringBuilder getCodeFromFile(IFile file) throws Exception {
8348
return code;
8449
}
8550

86-
@Override
87-
protected void tearDown() throws Exception {
51+
@AfterEach
52+
protected void closeAllFiles() throws Exception {
8853
System.gc();
8954
fileManager.closeAllFiles();
90-
super.tearDown();
9155
}
9256

9357
@Override

core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/RewriteTester.java

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,32 @@
1414
*******************************************************************************/
1515
package org.eclipse.cdt.core.parser.tests.rewrite;
1616

17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
1719
import java.io.BufferedReader;
1820
import java.io.FileReader;
1921
import java.io.IOException;
20-
import java.lang.reflect.Constructor;
21-
import java.lang.reflect.InvocationTargetException;
2222
import java.util.ArrayList;
23-
import java.util.Iterator;
2423
import java.util.List;
2524
import java.util.regex.Matcher;
2625
import java.util.regex.Pattern;
2726

27+
import org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest;
2828
import org.eclipse.cdt.core.testplugin.CTestPlugin;
2929
import org.eclipse.core.runtime.FileLocator;
3030
import org.eclipse.core.runtime.Path;
31-
import org.eclipse.jface.text.TextSelection;
31+
import org.junit.jupiter.params.provider.Arguments;
3232
import org.osgi.framework.Bundle;
3333

34-
import junit.framework.Test;
35-
import junit.framework.TestSuite;
36-
3734
/**
35+
* This is not actually a test, but a test provider. See loadTests and its uses
36+
*
37+
* The possibly unusual structure here is a result of migrating this JUnit 3 test
38+
* suite creator to JUnit 5
39+
*
3840
* @author Emanuel Graf
3941
*/
40-
public class RewriteTester extends TestSuite {
42+
public class RewriteTester {
4143
enum MatcherState {
4244
skip, inTest, inSource, inExpectedResult
4345
}
@@ -47,12 +49,12 @@ enum MatcherState {
4749
private static final String fileRegexp = "//@(.*)\\s*(\\w*)*$"; //$NON-NLS-1$
4850
private static final String resultRegexp = "//=.*$"; //$NON-NLS-1$
4951

50-
public static Test suite(String name, String file) throws Exception {
52+
public static List<Arguments> loadTests(Class<? extends CommentHandlingTest> clazz, String file) throws Exception {
5153
BufferedReader in = createReader(file);
5254

53-
ArrayList<RewriteBaseTest> testCases = createTests(in);
55+
List<Arguments> testCases = createTests(clazz, in);
5456
in.close();
55-
return createSuite(testCases, name);
57+
return testCases;
5658
}
5759

5860
protected static BufferedReader createReader(String file) throws IOException {
@@ -62,20 +64,21 @@ protected static BufferedReader createReader(String file) throws IOException {
6264
return new BufferedReader(new FileReader(file2));
6365
}
6466

65-
private static ArrayList<RewriteBaseTest> createTests(BufferedReader inputReader) throws Exception {
67+
private static List<Arguments> createTests(Class<? extends CommentHandlingTest> clazz, BufferedReader inputReader)
68+
throws Exception {
6669
String line;
6770
List<TestSourceFile> files = new ArrayList<>();
6871
TestSourceFile actFile = null;
6972
MatcherState matcherState = MatcherState.skip;
70-
ArrayList<RewriteBaseTest> testCases = new ArrayList<>();
73+
List<Arguments> testCases = new ArrayList<>();
7174
String testName = null;
7275
String className = null;
7376
boolean bevorFirstTest = true;
7477

7578
while ((line = inputReader.readLine()) != null) {
7679
if (lineMatchesBeginOfTest(line)) {
7780
if (!bevorFirstTest) {
78-
RewriteBaseTest test = createTestClass(className, testName, files);
81+
Arguments test = createTestClass(clazz, className, testName, files);
7982
testCases.add(test);
8083
files = new ArrayList<>();
8184
className = null;
@@ -113,42 +116,17 @@ private static ArrayList<RewriteBaseTest> createTests(BufferedReader inputReader
113116
break;
114117
}
115118
}
116-
RewriteBaseTest test = createTestClass(className, testName, files);
119+
Arguments test = createTestClass(clazz, className, testName, files);
117120
testCases.add(test);
118121
return testCases;
119122
}
120123

121-
private static RewriteBaseTest createTestClass(String className, String testName, List<TestSourceFile> files)
122-
throws Exception {
123-
try {
124-
Class<?> refClass = Class.forName(className);
125-
Constructor<?> ct = refClass.getConstructor(new Class[] { String.class, List.class });
126-
RewriteBaseTest test = (RewriteBaseTest) ct.newInstance(new Object[] { testName, files });
127-
for (TestSourceFile file : files) {
128-
TextSelection sel = file.getSelection();
129-
if (sel != null) {
130-
test.setFileWithSelection(file.getName());
131-
test.setSelection(sel);
132-
break;
133-
}
134-
}
135-
return test;
136-
} catch (ClassNotFoundException e) {
137-
throw new Exception("Unknown TestClass: " + e.getMessage()
138-
+ ". Make sure the test's sourcefile specifies a valid test class.");
139-
} catch (SecurityException e) {
140-
throw new Exception("Security Exception during Test creation", e);
141-
} catch (NoSuchMethodException e) {
142-
throw new Exception("Test class does not provied required constructor.");
143-
} catch (IllegalArgumentException e) {
144-
throw new Exception("IllegalArgumentException during Test creation", e);
145-
} catch (InstantiationException e) {
146-
throw new Exception("InstantiationException during Test creation", e);
147-
} catch (IllegalAccessException e) {
148-
throw new Exception("IllegalAccessException during Test creation", e);
149-
} catch (InvocationTargetException e) {
150-
throw new Exception("InvocationTargetException during Test creation", e);
151-
}
124+
private static Arguments createTestClass(Class<? extends CommentHandlingTest> clazz, String className,
125+
String testName, List<TestSourceFile> files) throws Exception {
126+
// For historical reasons, the Java classname of the test exists in the rts files.
127+
// This is a check that the rts file matches the test currently being loaded.
128+
assertEquals(clazz.getName(), className);
129+
return Arguments.argumentSet(testName, files);
152130
}
153131

154132
private static String getFileName(String line) {
@@ -191,14 +169,4 @@ private static String getNameOfTest(String line) {
191169
private static boolean lineMatchesBeginOfResult(String line) {
192170
return createMatcherFromString(resultRegexp, line).find();
193171
}
194-
195-
private static TestSuite createSuite(ArrayList<RewriteBaseTest> testCases, String name) {
196-
TestSuite suite = new TestSuite(name);
197-
Iterator<RewriteBaseTest> it = testCases.iterator();
198-
while (it.hasNext()) {
199-
RewriteBaseTest subject = it.next();
200-
suite.addTest(subject);
201-
}
202-
return suite;
203-
}
204172
}

core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/astwriter/ASTWriterTester.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
*******************************************************************************/
1515
package org.eclipse.cdt.core.parser.tests.rewrite.astwriter;
1616

17-
import java.util.Map;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
19+
import java.util.List;
1820

1921
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
2022
import org.eclipse.cdt.core.dom.parser.ISourceCodeParser;
@@ -43,47 +45,47 @@
4345
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter;
4446
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
4547
import org.eclipse.core.resources.IFile;
48+
import org.eclipse.jface.text.TextSelection;
49+
import org.junit.jupiter.params.ParameterizedTest;
50+
import org.junit.jupiter.params.provider.Arguments;
51+
import org.junit.jupiter.params.provider.MethodSource;
4652

4753
/**
4854
* @author Guido Zgraggen
4955
*/
50-
public abstract class ASTWriterTester extends RewriteBaseTest {
56+
public class ASTWriterTester extends RewriteBaseTest {
57+
5158
private static final IParserLogService NULL_LOG = new NullLogService();
5259

5360
private IFile file;
5461

55-
public ASTWriterTester(String name, ASTWriterTestSourceFile file) {
56-
super(name);
57-
fileMap.put(file.getName(), file);
62+
public static List<Arguments> loadTests() throws Exception {
63+
return SourceRewriteTest.loadTests();
5864
}
5965

60-
@Override
61-
protected void setUp() throws Exception {
62-
super.setUp();
63-
for (TestSourceFile testFile : fileMap.values()) {
64-
if (testFile.getSource().length() > 0) {
65-
file = importFile(testFile.getName(), testFile.getSource());
66-
}
66+
@ParameterizedTest
67+
@MethodSource("loadTests")
68+
protected void test(ASTWriterTestSourceFile testFile) throws Throwable {
69+
if (testFile.getSource().length() > 0) {
70+
importFile(testFile.getName(), testFile.getSource());
71+
}
72+
TextSelection sel = testFile.getSelection();
73+
if (sel != null) {
74+
setFileWithSelection(testFile.getName());
75+
setSelection(sel);
6776
}
68-
}
6977

70-
@Override
71-
protected void runTest() throws Throwable {
7278
file = project.getFile("ASTWritterTest.h"); //$NON-NLS-1$
73-
compareFiles(fileMap);
79+
compareFiles(testFile);
7480
}
7581

76-
@Override
77-
protected void compareFiles(Map<String, TestSourceFile> testResourceFiles) throws Exception {
78-
for (String fileName : testResourceFiles.keySet()) {
79-
TestSourceFile testFile = testResourceFiles.get(fileName);
80-
String code = generateSource(testFile);
81-
assertEquals(TestHelper.unifyNewLines(testFile.getExpectedSource()),
82-
TestHelper.unifyNewLines(code + System.getProperty("line.separator"))); //$NON-NLS-1$
83-
}
82+
private void compareFiles(ASTWriterTestSourceFile testFile) throws Exception {
83+
String code = generateSource(testFile);
84+
assertEquals(TestHelper.unifyNewLines(testFile.getExpectedSource()),
85+
TestHelper.unifyNewLines(code + System.getProperty("line.separator"))); //$NON-NLS-1$
8486
}
8587

86-
public String generateSource(TestSourceFile testFile) throws Exception {
88+
private String generateSource(TestSourceFile testFile) throws Exception {
8789
IASTTranslationUnit unit = getParser(testFile).parse();
8890
NodeCommentMap commentMap = ASTCommenter.getCommentedNodeMap(unit);
8991
ASTModificationMap map = new ASTModificationMap();
@@ -92,7 +94,7 @@ public String generateSource(TestSourceFile testFile) throws Exception {
9294
return writer.write(unit, commentMap);
9395
}
9496

95-
protected ISourceCodeParser getParser(TestSourceFile testFile) throws Exception {
97+
private ISourceCodeParser getParser(TestSourceFile testFile) throws Exception {
9698
FileContent codeReader = FileContent.create(file);
9799

98100
ParserLanguage language = getLanguage(testFile);

0 commit comments

Comments
 (0)