1414 *******************************************************************************/
1515package org .eclipse .cdt .core .parser .tests .rewrite ;
1616
17+ import static org .junit .jupiter .api .Assertions .assertEquals ;
18+
1719import java .io .BufferedReader ;
1820import java .io .FileReader ;
1921import java .io .IOException ;
20- import java .lang .reflect .Constructor ;
21- import java .lang .reflect .InvocationTargetException ;
2222import java .util .ArrayList ;
23- import java .util .Iterator ;
2423import java .util .List ;
2524import java .util .regex .Matcher ;
2625import java .util .regex .Pattern ;
2726
27+ import org .eclipse .cdt .core .parser .tests .rewrite .comenthandler .CommentHandlingTest ;
2828import org .eclipse .cdt .core .testplugin .CTestPlugin ;
2929import org .eclipse .core .runtime .FileLocator ;
3030import org .eclipse .core .runtime .Path ;
31- import org .eclipse . jface . text . TextSelection ;
31+ import org .junit . jupiter . params . provider . Arguments ;
3232import 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}
0 commit comments