17
17
package com .google .errorprone ;
18
18
19
19
import static com .google .common .truth .Truth .assertWithMessage ;
20
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
20
21
import static org .junit .Assert .assertTrue ;
21
22
import static org .junit .Assert .fail ;
22
23
30
31
import com .sun .tools .javac .api .JavacTool ;
31
32
import com .sun .tools .javac .main .Main .Result ;
32
33
import com .sun .tools .javac .util .Context ;
34
+ import java .io .BufferedWriter ;
33
35
import java .io .ByteArrayOutputStream ;
34
36
import java .io .IOError ;
35
37
import java .io .IOException ;
36
38
import java .io .OutputStream ;
39
+ import java .io .OutputStreamWriter ;
37
40
import java .io .PrintWriter ;
38
41
import java .util .ArrayList ;
39
42
import java .util .Arrays ;
43
+ import java .util .Collections ;
40
44
import java .util .List ;
41
45
import java .util .Locale ;
42
46
import javax .tools .Diagnostic ;
43
47
import javax .tools .JavaCompiler ;
44
48
import javax .tools .JavaCompiler .CompilationTask ;
45
49
import javax .tools .JavaFileObject ;
50
+ import javax .tools .StandardLocation ;
46
51
47
52
/**
48
53
* Helps test Error Prone bug checkers and compilations.
@@ -69,12 +74,20 @@ public class CompilationTestHelper {
69
74
70
75
private CompilationTestHelper (ScannerSupplier scannerSupplier , String checkName , Class <?> clazz ) {
71
76
this .fileManager = new ErrorProneInMemoryFileManager (clazz );
77
+ try {
78
+ fileManager .setLocation (StandardLocation .SOURCE_PATH , Collections .emptyList ());
79
+ } catch (IOException e ) {
80
+ e .printStackTrace ();
81
+ }
72
82
this .diagnosticHelper = new DiagnosticTestHelper (checkName );
73
83
this .outputStream = new ByteArrayOutputStream ();
74
84
this .compiler =
75
85
BaseErrorProneCompiler .builder ()
76
86
.report (scannerSupplier )
77
- .redirectOutputTo (new PrintWriter (outputStream , /*autoFlush=*/ true ))
87
+ .redirectOutputTo (
88
+ new PrintWriter (
89
+ new BufferedWriter (new OutputStreamWriter (outputStream , UTF_8 )),
90
+ /*autoFlush=*/ true ))
78
91
.listenToDiagnostics (diagnosticHelper .collector )
79
92
.build ();
80
93
}
@@ -178,7 +191,7 @@ public CompilationTestHelper expectNoDiagnostics() {
178
191
this .expectNoDiagnostics = true ;
179
192
return this ;
180
193
}
181
-
194
+
182
195
/**
183
196
* By default, the compilation helper will not run Error Prone on
184
197
* compilations that fail with javac errors. This behaviour can be
@@ -210,7 +223,7 @@ public CompilationTestHelper expectResult(Result result) {
210
223
expectedResult = Optional .of (result );
211
224
return this ;
212
225
}
213
-
226
+
214
227
/**
215
228
* Expects an error message matching {@code matcher} at the line below a comment matching the key.
216
229
* For example, given the source
@@ -256,7 +269,7 @@ public void doTest() {
256
269
} catch (IOException e ) {
257
270
throw new IOError (e );
258
271
}
259
- }
272
+ }
260
273
assertTrue ("Unused error keys: " + diagnosticHelper .getUnusedLookupKeys (),
261
274
diagnosticHelper .getUnusedLookupKeys ().isEmpty ());
262
275
}
@@ -286,13 +299,16 @@ private void checkWellFormed(Iterable<JavaFileObject> sources, String[] args) {
286
299
} catch (InvalidCommandLineOptionException e ) {
287
300
fail ("Exception during argument processing: " + e );
288
301
}
289
- CompilationTask task = compiler .getTask (
290
- new PrintWriter (outputStream , /*autoFlush=*/ true ),
291
- fileManager ,
292
- null ,
293
- buildArguments (Arrays .asList (remainingArgs )),
294
- null ,
295
- sources );
302
+ CompilationTask task =
303
+ compiler .getTask (
304
+ new PrintWriter (
305
+ new BufferedWriter (new OutputStreamWriter (outputStream , UTF_8 )),
306
+ /*autoFlush=*/ true ),
307
+ fileManager ,
308
+ null ,
309
+ buildArguments (Arrays .asList (remainingArgs )),
310
+ null ,
311
+ sources );
296
312
boolean result = task .call ();
297
313
assertWithMessage (String .format (
298
314
"Test program failed to compile with non Error Prone error: %s" ,
0 commit comments