16
16
package com .google .testing .compile ;
17
17
18
18
import static com .google .common .base .Preconditions .checkArgument ;
19
+ import static javax .tools .JavaFileObject .Kind .CLASS ;
19
20
20
21
import com .google .common .base .Function ;
21
22
import com .google .common .base .Joiner ;
28
29
import com .google .common .collect .Iterables ;
29
30
import com .google .common .collect .Lists ;
30
31
import com .google .common .collect .Maps ;
32
+ import com .google .common .io .ByteSource ;
31
33
import com .google .common .io .ByteStreams ;
32
34
import com .google .testing .compile .Compilation .Result ;
33
35
@@ -92,14 +94,58 @@ private CompilationClause(Iterable<? extends Processor> processors) {
92
94
this .processors = ImmutableSet .copyOf (processors );
93
95
}
94
96
97
+ /** Returns a {@code String} report describing the contents of a given generated file. */
98
+ private String reportFileGenerated (JavaFileObject generatedFile ) {
99
+ try {
100
+ StringBuilder entry =
101
+ new StringBuilder ().append (String .format ("\n %s:\n " , generatedFile .toUri ().getPath ()));
102
+ if (generatedFile .getKind ().equals (CLASS )) {
103
+ entry .append (String .format ("[generated class file (%s bytes)]" ,
104
+ ByteSource .wrap (ByteStreams .toByteArray (generatedFile .openInputStream ()))
105
+ .size ()));
106
+ } else {
107
+ entry .append (generatedFile .getCharContent (true ));
108
+ }
109
+ return entry .append ("\n " ).toString ();
110
+ } catch (IOException e ) {
111
+ throw new IllegalStateException ("Couldn't read from JavaFileObject when it was "
112
+ + "already in memory." , e );
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Returns a {@code String} report describing what files were generated in the given
118
+ * {@link Compilation.Result}
119
+ */
120
+ private String reportFilesGenerated (Compilation .Result result ) {
121
+ FluentIterable <JavaFileObject > generatedFiles =
122
+ FluentIterable .from (result .generatedSources ());
123
+ StringBuilder message = new StringBuilder ("\n \n " );
124
+ if (generatedFiles .isEmpty ()) {
125
+ return message .append ("(No files were generated.)\n " ).toString ();
126
+ } else {
127
+ message .append ("Generated Files\n " )
128
+ .append ("===============\n " );
129
+ for (JavaFileObject generatedFile : generatedFiles ) {
130
+ message .append (reportFileGenerated (generatedFile ));
131
+ }
132
+ return message .toString ();
133
+ }
134
+ }
135
+
95
136
@ Override
96
137
public SuccessfulCompilationClause compilesWithoutError () {
97
138
Compilation .Result result = Compilation .compile (processors , getSubject ());
98
139
if (!result .successful ()) {
99
140
ImmutableList <Diagnostic <? extends JavaFileObject >> errors =
100
141
result .diagnosticsByKind ().get (Kind .ERROR );
101
142
StringBuilder message = new StringBuilder ("Compilation produced the following errors:\n " );
102
- Joiner .on ('\n' ).appendTo (message , errors );
143
+ for (Diagnostic <? extends JavaFileObject > error : errors ) {
144
+ message .append ('\n' );
145
+ message .append (error );
146
+ }
147
+ message .append ('\n' );
148
+ message .append (reportFilesGenerated (result ));
103
149
failureStrategy .fail (message .toString ());
104
150
}
105
151
return new SuccessfulCompilationBuilder (result );
@@ -109,7 +155,11 @@ public SuccessfulCompilationClause compilesWithoutError() {
109
155
public UnsuccessfulCompilationClause failsToCompile () {
110
156
Result result = Compilation .compile (processors , getSubject ());
111
157
if (result .successful ()) {
112
- failureStrategy .fail ("Compilation was expected to fail, but contained no errors" );
158
+ String message = Joiner .on ('\n' ).join (
159
+ "Compilation was expected to fail, but contained no errors." ,
160
+ "" ,
161
+ reportFilesGenerated (result ));
162
+ failureStrategy .fail (message );
113
163
}
114
164
return new UnsuccessfulCompilationBuilder (result );
115
165
}
0 commit comments