1
1
/*******************************************************************************
2
- * Copyright (c) 2024 GK Software SE, and others.
2
+ * Copyright (c) 2025 GK Software SE, and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
21
21
import java .io .File ;
22
22
import java .io .FileWriter ;
23
23
import java .io .IOException ;
24
- import java .nio .file .DirectoryNotEmptyException ;
25
24
import java .nio .file .FileVisitResult ;
26
25
import java .nio .file .Files ;
27
26
import java .nio .file .Path ;
31
30
import java .util .HashSet ;
32
31
import java .util .List ;
33
32
import java .util .Set ;
33
+ import java .util .stream .Collectors ;
34
34
import junit .framework .AssertionFailedError ;
35
+ import org .eclipse .jdt .core .util .ClassFormatException ;
35
36
import org .eclipse .jdt .internal .compiler .classfmt .ClassFileConstants ;
36
37
37
38
public abstract class AbstractModuleCompilationTest extends AbstractBatchCompilerTest {
@@ -45,7 +46,7 @@ public AbstractModuleCompilationTest(String name) {
45
46
46
47
class Runner extends AbstractRegressionTest .Runner {
47
48
StringBuilder commandLine = new StringBuilder ();
48
- String outputDir = OUTPUT_DIR + File . separator + "javac" ;
49
+ String outputDir = OUTPUT_DIR ;
49
50
List <String > fileNames = new ArrayList <>();
50
51
/** will replace any -8, -9 ... option for javac */
51
52
String javacVersionOptions ;
@@ -72,11 +73,22 @@ Set<String> runConformModuleTest() {
72
73
String javacCommandLine = adjustForJavac (commandLineString , this .javacVersionOptions );
73
74
return AbstractModuleCompilationTest .this .runConformModuleTest (this .testFiles , commandLineString ,
74
75
this .expectedOutputString , this .expectedErrorString ,
75
- this .shouldFlushOutputDirectory , this .outputDir ,
76
- this . javacTestOptions , javacCommandLine );
76
+ this .shouldFlushOutputDirectory , this .javacTestOptions ,
77
+ javacCommandLine );
77
78
}
78
79
}
79
80
81
+ protected String getSourceDir () {
82
+ return OUTPUT_DIR + File .separatorChar + "src" ;
83
+ }
84
+
85
+ protected String getEcjOutputDir () {
86
+ return OUTPUT_DIR + File .separatorChar + "bin" ;
87
+ }
88
+ protected String getJavacOutputDir () {
89
+ return OUTPUT_DIR + File .separatorChar + "javac" ;
90
+ }
91
+
80
92
protected void writeFileCollecting (List <String > collectedFiles , String directoryName , String fileName , String source ) {
81
93
writeFile (directoryName , fileName , source );
82
94
collectedFiles .add (directoryName +File .separator +fileName );
@@ -104,44 +116,47 @@ protected void writeFile(String directoryName, String fileName, String source) {
104
116
105
117
protected void runConformModuleTest (List <String > testFileNames , StringBuilder commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString ) {
106
118
runConformModuleTest (testFileNames , commandLine ,
107
- expectedFailureOutOutputString , expectedFailureErrOutputString , OUTPUT_DIR + File .separator + "javac" );
108
- }
109
-
110
- protected void runConformModuleTest (List <String > testFileNames , StringBuilder commandLine ,
111
- String expectedFailureOutOutputString , String expectedFailureErrOutputString ,
112
- String output ) {
113
- runConformModuleTest (testFileNames , commandLine ,
114
- expectedFailureOutOutputString , expectedFailureErrOutputString , output ,
115
- JavacTestOptions .DEFAULT );
119
+ expectedFailureOutOutputString , expectedFailureErrOutputString , JavacTestOptions .DEFAULT );
116
120
}
117
121
protected void runConformModuleTest (List <String > testFileNames , StringBuilder commandLine ,
118
122
String expectedFailureOutOutputString , String expectedFailureErrOutputString ,
119
- String output , JavacTestOptions javacTestOptions ) {
123
+ JavacTestOptions javacTestOptions ) {
120
124
for (String file : testFileNames )
121
125
commandLine .append (" \" " ).append (file ).append ("\" " );
122
126
runConformModuleTest (new String [0 ], commandLine .toString (),
123
127
expectedFailureOutOutputString , expectedFailureErrOutputString , false ,
124
- output , javacTestOptions , null );
128
+ javacTestOptions , null );
125
129
}
126
130
127
131
protected Set <String > runConformModuleTest (String [] testFiles , String commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , boolean shouldFlushOutputDirectory ) {
128
132
return runConformModuleTest (testFiles , commandLine , expectedFailureErrOutputString , expectedFailureErrOutputString ,
129
- shouldFlushOutputDirectory , OUTPUT_DIR , JavacTestOptions .DEFAULT , null );
133
+ shouldFlushOutputDirectory , JavacTestOptions .DEFAULT , null );
130
134
}
131
135
132
- protected Set <String > runConformModuleTest (String [] testFiles , String commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , boolean shouldFlushOutputDirectory , String output ,
133
- JavacTestOptions options , String javacCommandLine ) {
134
- runConformTest (testFiles , commandLine , expectedFailureOutOutputString , expectedFailureErrOutputString , shouldFlushOutputDirectory );
136
+ protected Set <String > runConformModuleTest (String [] testFiles , String commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , boolean shouldFlushOutputDirectory , JavacTestOptions options ,
137
+ String javacCommandLine ) {
138
+ String ecjOutput = getEcjOutputDir ();
139
+ File ecjOutFile = new File (ecjOutput );
140
+ if (!ecjOutFile .exists ()) {
141
+ ecjOutFile .mkdir ();
142
+ }
143
+ String ecjCommandLine = commandLine .contains ("-d " )
144
+ ? commandLine
145
+ : " -d " + ecjOutput + ' ' + commandLine ;
146
+ runConformTest (testFiles , ecjCommandLine , expectedFailureOutOutputString , expectedFailureErrOutputString , shouldFlushOutputDirectory );
135
147
if (shouldRunJavac ()) {
136
- File outputDir = new File (output );
137
148
final Set <String > outFiles = new HashSet <>();
138
- walkOutFiles (output , outFiles , true );
149
+ walkOutFiles (ecjOutput , outFiles , true );
150
+
151
+ String javacOutput = getJavacOutputDir ();
152
+ File javacOutputDir = new File (javacOutput );
153
+
139
154
String [] testFileNames = new String [testFiles .length /2 ];
140
155
for (int i = 0 ; i < testFileNames .length ; i ++) {
141
156
testFileNames [i ] = testFiles [i *2 ];
142
157
}
143
158
if (javacCommandLine == null ) {
144
- javacCommandLine = adjustForJavac (commandLine , null );
159
+ javacCommandLine = " -d " + javacOutput + ' ' + adjustForJavac (commandLine , null );
145
160
}
146
161
for (JavacCompiler javacCompiler : javacCompilers ) {
147
162
if (javacCompiler .compliance < ClassFileConstants .JDK9 )
@@ -153,7 +168,7 @@ protected Set<String> runConformModuleTest(String[] testFiles, String commandLin
153
168
StringBuilder log = new StringBuilder ();
154
169
try {
155
170
long compileResult = javacCompiler .compile (
156
- outputDir , /* directory */
171
+ javacOutputDir , /* directory */
157
172
javacCommandLine /* options */ ,
158
173
testFileNames /* source file names */ ,
159
174
log ,
@@ -172,8 +187,8 @@ protected Set<String> runConformModuleTest(String[] testFiles, String commandLin
172
187
e .printStackTrace ();
173
188
throw new AssertionFailedError (e .getMessage ());
174
189
}
175
- final Set <String > expectedFiles = new HashSet <>( outFiles );
176
- walkOutFiles (output , expectedFiles , false );
190
+ final Set <String > expectedFiles = outFiles . stream (). map ( s -> s . replace ( ecjOutput , javacOutput )). collect ( Collectors . toSet () );
191
+ walkOutFiles (javacOutput , expectedFiles , false );
177
192
for (String missingFile : expectedFiles )
178
193
System .err .println ("Missing output file from javac: " +missingFile );
179
194
}
@@ -188,78 +203,88 @@ protected StringBuilder trimJavacLog(StringBuilder log) {
188
203
}
189
204
190
205
protected void runNegativeModuleTest (List <String > testFileNames , StringBuilder commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , String javacErrorMatch ) {
191
- runNegativeModuleTest (testFileNames , commandLine , expectedFailureOutOutputString ,
192
- expectedFailureErrOutputString , javacErrorMatch , OUTPUT_DIR + File .separator + "javac" );
193
- }
194
-
195
- protected void runNegativeModuleTest (List <String > testFileNames , StringBuilder commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , String javacErrorMatch ,
196
- String output ) {
197
- runNegativeModuleTest (testFileNames , commandLine , expectedFailureOutOutputString , expectedFailureErrOutputString ,
198
- javacErrorMatch , output , JavacTestOptions .DEFAULT );
199
- }
206
+ runNegativeModuleTest (testFileNames , commandLine , expectedFailureOutOutputString , expectedFailureErrOutputString ,
207
+ javacErrorMatch , JavacTestOptions .DEFAULT );
208
+ }
200
209
201
210
protected void runNegativeModuleTest (List <String > testFileNames , StringBuilder commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , String javacErrorMatch ,
202
- String output , JavacTestOptions options ) {
203
- for (String file : testFileNames )
204
- commandLine .append (" \" " ).append (file ).append ("\" " );
205
- runNegativeModuleTest (new String [0 ], commandLine .toString (),
206
- expectedFailureOutOutputString , expectedFailureErrOutputString , false , javacErrorMatch , output ,
207
- options );
208
- }
211
+ JavacTestOptions options ) {
212
+ for (String file : testFileNames )
213
+ commandLine .append (" \" " ).append (file ).append ("\" " );
214
+ runNegativeModuleTest (new String [0 ], commandLine .toString (),
215
+ expectedFailureOutOutputString , expectedFailureErrOutputString , false , javacErrorMatch , options );
216
+ }
209
217
210
218
protected void runNegativeModuleTest (String [] testFiles , String commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , boolean shouldFlushOutputDirectory ,
211
219
String javacErrorMatch ) {
212
- runNegativeModuleTest (testFiles , commandLine , expectedFailureOutOutputString , expectedFailureErrOutputString ,
213
- shouldFlushOutputDirectory , javacErrorMatch , OUTPUT_DIR , JavacTestOptions .DEFAULT );
214
- }
220
+ String prefix = "src" + File .separatorChar ;
221
+ for (int i = 0 ; i + 1 < testFiles .length ; i +=2 ) {
222
+ testFiles [i ] = prefix + testFiles [i ];
223
+ }
224
+ runNegativeModuleTest (testFiles , commandLine , expectedFailureOutOutputString , expectedFailureErrOutputString ,
225
+ shouldFlushOutputDirectory , javacErrorMatch , JavacTestOptions .DEFAULT );
226
+ }
215
227
216
228
void runNegativeModuleTest (String [] testFiles , String commandLine , String expectedFailureOutOutputString , String expectedFailureErrOutputString , boolean shouldFlushOutputDirectory , String javacErrorMatch ,
217
- String output , JavacTestOptions options ) {
218
- runNegativeTest (testFiles , commandLine , expectedFailureOutOutputString , expectedFailureErrOutputString , shouldFlushOutputDirectory );
219
- if (shouldRunJavac ()) {
220
- String [] testFileNames = new String [testFiles .length /2 ];
221
- for (int i = 0 ; i < testFileNames .length ; i ++) {
222
- testFileNames [i ] = testFiles [i *2 ];
223
- }
224
- File outputDir = new File (OUTPUT_DIR );
225
- final Set <String > outFiles = new HashSet <>();
226
- walkOutFiles (output , outFiles , true );
227
- for (JavacCompiler javacCompiler : javacCompilers ) {
228
- if (javacCompiler .compliance < ClassFileConstants .JDK9 )
229
- continue ;
230
- JavacTestOptions .Excuse excuse = options .excuseFor (javacCompiler );
229
+ JavacTestOptions options ) {
230
+ String ecjOutput = getEcjOutputDir ();
231
+ File ecjOutFile = new File (ecjOutput );
232
+ if (!ecjOutFile .exists ()) {
233
+ ecjOutFile .mkdir ();
234
+ }
235
+ String ecjCommandLine = commandLine .contains ("-d " )
236
+ ? commandLine
237
+ : " -d " + ecjOutput + ' ' + commandLine ;
238
+ runNegativeTest (testFiles , ecjCommandLine , expectedFailureOutOutputString , expectedFailureErrOutputString , shouldFlushOutputDirectory );
239
+ if (shouldRunJavac ()) {
240
+ final Set <String > outFiles = new HashSet <>();
241
+ walkOutFiles (ecjOutput , outFiles , true );
231
242
232
- commandLine = adjustForJavac (commandLine , null );
233
- StringBuilder log = new StringBuilder ();
234
- int mismatch = 0 ;
235
- try {
236
- long compileResult = javacCompiler .compile (
237
- outputDir , /* directory */
238
- commandLine /* options */ ,
239
- testFileNames /* source file names */ ,
240
- log );
241
- if (compileResult == 0 ) {
242
- mismatch = JavacTestOptions .MismatchType .EclipseErrorsJavacNone ;
243
- javacErrorMatch = expectedFailureErrOutputString ;
244
- System .err .println ("Previous error was from " +testName ());
245
- } else if (!log .toString ().contains (javacErrorMatch )) {
246
- mismatch = JavacTestOptions .MismatchType .CompileErrorMismatch ;
247
- System .err .println (testName ()+": Error match " + javacErrorMatch + " not found in \n " +log .toString ());
248
- }
249
- } catch (IOException | InterruptedException e ) {
250
- e .printStackTrace ();
251
- throw new AssertionFailedError (e .getMessage ());
252
- }
253
- handleMismatch (javacCompiler , testName (), testFiles , javacErrorMatch ,
254
- "" , "" , log , "" , "" ,
255
- excuse , mismatch );
256
- final Set <String > expectedFiles = new HashSet <>(outFiles );
257
- walkOutFiles (output , expectedFiles , false );
258
- for (String missingFile : expectedFiles )
259
- System .err .println ("Missing output file from javac: " +missingFile );
243
+ String javacOutput = getJavacOutputDir ();
244
+ File outputDir = new File (javacOutput );
245
+
246
+ String [] testFileNames = new String [testFiles .length /2 ];
247
+ for (int i = 0 ; i < testFileNames .length ; i ++) {
248
+ testFileNames [i ] = testFiles [i *2 ];
249
+ }
250
+
251
+ for (JavacCompiler javacCompiler : javacCompilers ) {
252
+ if (javacCompiler .compliance < ClassFileConstants .JDK9 )
253
+ continue ;
254
+ JavacTestOptions .Excuse excuse = options .excuseFor (javacCompiler );
255
+
256
+ String javacCommandLine = " -d " + javacOutput + ' ' + adjustForJavac (commandLine , null );
257
+ StringBuilder log = new StringBuilder ();
258
+ int mismatch = 0 ;
259
+ try {
260
+ long compileResult = javacCompiler .compile (
261
+ outputDir , /* directory */
262
+ javacCommandLine /* options */ ,
263
+ testFileNames /* source file names */ ,
264
+ log ,
265
+ false );
266
+ if (compileResult == 0 ) {
267
+ mismatch = JavacTestOptions .MismatchType .EclipseErrorsJavacNone ;
268
+ javacErrorMatch = expectedFailureErrOutputString ;
269
+ System .err .println ("Previous error was from " +testName ());
270
+ } else if (!log .toString ().contains (javacErrorMatch )) {
271
+ mismatch = JavacTestOptions .MismatchType .CompileErrorMismatch ;
272
+ System .err .println (testName ()+": Error match " + javacErrorMatch + " not found in \n " +log .toString ());
260
273
}
274
+ } catch (IOException | InterruptedException e ) {
275
+ e .printStackTrace ();
276
+ throw new AssertionFailedError (e .getMessage ());
261
277
}
278
+ handleMismatch (javacCompiler , testName (), testFiles , javacErrorMatch ,
279
+ "" , "" , log , "" , "" ,
280
+ excuse , mismatch );
281
+ final Set <String > expectedFiles = outFiles .stream ().map (s -> s .replace (ecjOutput , javacOutput )).collect (Collectors .toSet ());
282
+ walkOutFiles (javacOutput , expectedFiles , false );
283
+ for (String missingFile : expectedFiles )
284
+ System .out .println ("Missing output file from javac in negative test: " +missingFile );
262
285
}
286
+ }
287
+ }
263
288
264
289
/**
265
290
* @param commandLine command line arguments as used for ecj
@@ -329,21 +354,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
329
354
if (!fileNames .remove (file .toString ()))
330
355
System .err .println ("Unexpected output file from javac: " +file .toString ());
331
356
}
332
- Files .delete (file );
333
357
}
334
358
return FileVisitResult .CONTINUE ;
335
359
}
336
- @ Override
337
- public FileVisitResult postVisitDirectory (Path dir , IOException exc ) throws IOException {
338
- if (!dir .toString ().equals (outputLocation )) {
339
- try {
340
- Files .delete (dir );
341
- } catch (DirectoryNotEmptyException ex ) {
342
- // expected
343
- }
344
- }
345
- return FileVisitResult .CONTINUE ;
346
- }
347
360
});
348
361
} catch (IOException e ) {
349
362
e .printStackTrace ();
@@ -358,5 +371,15 @@ protected void assertClassFile(String msg, String fileName, Set<String> classFil
358
371
assertTrue (msg , (new File (fileName ).exists ()));
359
372
}
360
373
}
361
-
374
+ @ Override
375
+ protected void verifyClassFile (String expectedOutput , String classFileName , int mode ) throws IOException , ClassFormatException {
376
+ verifyClassFile (expectedOutput , classFileName , mode , false );
377
+ }
378
+ protected void verifyClassFile (String expectedOutput , String classFileName , int mode , boolean skipJavac )
379
+ throws IOException , ClassFormatException {
380
+ verifyClassFile (expectedOutput , null , getEcjOutputDir ()+File .separatorChar +classFileName , mode );
381
+ if (!skipJavac && shouldRunJavac ()) {
382
+ verifyClassFile (expectedOutput , null , getJavacOutputDir ()+File .separatorChar +classFileName , mode );
383
+ }
384
+ }
362
385
}
0 commit comments