1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* Copyright (C) 2009-2014 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import lombok.DirectoryRunner.FileTester;
import lombok.core.AST;
import lombok.core.LombokConfiguration;
import lombok.core.LombokImmutableList;
import lombok.core.configuration.ConfigurationKeysLoader;
import lombok.core.configuration.ConfigurationResolver;
import lombok.core.configuration.ConfigurationResolverFactory;
import lombok.javac.CapturingDiagnosticListener.CompilerMessage;
public abstract class AbstractRunTests {
private final File dumpActualFilesHere;
public AbstractRunTests() {
this.dumpActualFilesHere = findPlaceToDumpActualFiles();
}
public final FileTester createTester(final DirectoryRunner.TestParams params, final File file, String platform, int version) throws IOException {
ConfigurationKeysLoader.LoaderLoader.loadAllConfigurationKeys();
AssertionError directiveFailure = null;
LombokTestSource sourceDirectives = null;
try {
sourceDirectives = LombokTestSource.readDirectives(file);
if (sourceDirectives.isIgnore()) return null;
if (!sourceDirectives.versionWithinLimit(version)) return null;
if (!sourceDirectives.runOnPlatform(platform)) return null;
} catch (AssertionError ae) {
directiveFailure = ae;
}
String fileName = file.getName();
final LombokTestSource expected = LombokTestSource.read(params.getAfterDirectory(), params.getMessagesDirectory(), fileName);
if (expected.isIgnore()) return null;
if (!expected.versionWithinLimit(params.getVersion())) return null;
if (!expected.versionWithinLimit(version)) return null;
final LombokTestSource sourceDirectives_ = sourceDirectives;
final AssertionError directiveFailure_ = directiveFailure;
return new FileTester() {
@Override public void runTest() throws Throwable {
if (directiveFailure_ != null) throw directiveFailure_;
LinkedHashSet<CompilerMessage> messages = new LinkedHashSet<CompilerMessage>();
StringWriter writer = new StringWriter();
LombokConfiguration.overrideConfigurationResolverFactory(new ConfigurationResolverFactory() {
@Override public ConfigurationResolver createResolver(AST<?, ?, ?> ast) {
return sourceDirectives_.getConfiguration();
}
});
boolean changed = transformCode(messages, writer, file, sourceDirectives_.getSpecifiedEncoding(), sourceDirectives_.getFormatPreferences());
boolean forceUnchanged = sourceDirectives_.forceUnchanged() || sourceDirectives_.isSkipCompareContent();
if (params.expectChanges() && !forceUnchanged && !changed) messages.add(new CompilerMessage(-1, -1, true, "not flagged modified"));
if (!params.expectChanges() && changed) messages.add(new CompilerMessage(-1, -1, true, "unexpected modification"));
compare(file.getName(), expected, writer.toString(), messages, params.printErrors(), sourceDirectives_.isSkipCompareContent() || expected.isSkipCompareContent());
}
};
}
protected abstract boolean transformCode(Collection<CompilerMessage> messages, StringWriter result, File file, String encoding, Map<String, String> formatPreferences) throws Throwable;
protected String readFile(File file) throws IOException {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
return null;
}
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
result.append("\n");
}
reader.close();
return result.toString();
}
private static File findPlaceToDumpActualFiles() {
String location = System.getProperty("lombok.tests.dump_actual_files");
if (location != null) {
File dumpActualFilesHere = new File(location);
dumpActualFilesHere.mkdirs();
return dumpActualFilesHere;
}
return null;
}
private static void dumpToFile(File file, String content) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
fos.write(content.getBytes("UTF-8"));
} finally {
fos.close();
}
}
private static void dumpToFile(File file, Collection<CompilerMessage> content) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
for (CompilerMessage message : content) {
fos.write(CompilerMessageMatcher.asCompilerMessageMatcher(message).toString().getBytes("UTF-8"));
fos.write('\n');
}
} finally {
fos.close();
}
}
private void compare(String name, LombokTestSource expected, String actualFile, LinkedHashSet<CompilerMessage> actualMessages, boolean printErrors, boolean skipCompareContent) throws Throwable {
if (!skipCompareContent) try {
compareContent(name, expected.getContent(), actualFile);
} catch (Throwable e) {
if (printErrors) {
System.out.println("***** " + name + " *****");
System.out.println(e.getMessage());
System.out.println("**** Expected ******");
System.out.println(expected.getContent());
System.out.println("**** Actual ******");
System.out.println(actualFile);
if (actualMessages != null && !actualMessages.isEmpty()) {
System.out.println("**** Actual Errors *****");
for (CompilerMessage actualMessage : actualMessages) {
System.out.println(actualMessage);
}
}
System.out.println("*******************");
}
if (dumpActualFilesHere != null) {
dumpToFile(new File(dumpActualFilesHere, name), actualFile);
}
throw e;
}
try {
compareMessages(name, expected.getMessages(), actualMessages);
} catch (Throwable e) {
if (printErrors) {
System.out.println("***** " + name + " *****");
System.out.println(e.getMessage());
System.out.println("**** Expected ******");
for (CompilerMessageMatcher expectedMessage : expected.getMessages()) {
System.out.println(expectedMessage);
}
System.out.println("**** Actual ******");
for (CompilerMessage actualMessage : actualMessages) {
System.out.println(actualMessage);
}
System.out.println("*******************");
}
if (dumpActualFilesHere != null) {
dumpToFile(new File(dumpActualFilesHere, name + ".messages"), actualMessages);
}
throw e;
}
}
@SuppressWarnings("null") /* eclipse bug; it falsely thinks stuffAc will always be null or some such hogwash. */
private static void compareMessages(String name, LombokImmutableList<CompilerMessageMatcher> expected, LinkedHashSet<CompilerMessage> actual) {
Iterator<CompilerMessageMatcher> expectedIterator = expected.iterator();
Iterator<CompilerMessage> actualIterator = actual.iterator();
CompilerMessage stuffAc = null;
while (true) {
boolean exHasNext = expectedIterator.hasNext();
boolean acHasNext = stuffAc != null || actualIterator.hasNext();
if (!exHasNext && !acHasNext) break;
if (exHasNext && acHasNext) {
CompilerMessageMatcher cmm = expectedIterator.next();
CompilerMessage cm = stuffAc == null ? actualIterator.next() : stuffAc;
if (cmm.matches(cm)) continue;
if (cmm.isOptional()) stuffAc = cm;
fail(String.format("[%s] Expected message '%s' but got message '%s'", name, cmm, cm));
throw new AssertionError("fail should have aborted already.");
}
while (expectedIterator.hasNext()) {
CompilerMessageMatcher next = expectedIterator.next();
if (next.isOptional()) continue;
fail(String.format("[%s] Expected message '%s' but ran out of actual messages", name, next));
}
if (acHasNext) fail(String.format("[%s] Unexpected message: %s", name, actualIterator.next()));
break;
}
}
private static void compareContent(String name, String expectedFile, String actualFile) {
String[] expectedLines = expectedFile.split("(\\r?\\n)");
String[] actualLines = actualFile.split("(\\r?\\n)");
for (int i = 0; i < expectedLines.length; i++) {
if (expectedLines[i].isEmpty() || expectedLines[i].startsWith("//")) expectedLines[i] = "";
else break;
}
for (int i = 0; i < actualLines.length; i++) {
if (actualLines[i].isEmpty() || actualLines[i].startsWith("//")) actualLines[i] = "";
else break;
}
expectedLines = removeBlanks(expectedLines);
actualLines = removeBlanks(actualLines);
int size = Math.min(expectedLines.length, actualLines.length);
if (size == 0 && expectedLines.length + actualLines.length > 0) {
Assert.fail("Missing / empty expected file.");
}
for (int i = 0; i < size; i++) {
String expected = trimRight(expectedLines[i]);
String actual = trimRight(actualLines[i]);
assertEquals(String.format("Difference in %s on line %d", name, i + 1), expected, actual);
}
if (expectedLines.length > actualLines.length) {
fail(String.format("Missing line %d in generated %s: %s", size + 1, name, expectedLines[size]));
}
if (expectedLines.length < actualLines.length) {
fail(String.format("Extra line %d in generated %s: %s", size + 1, name, actualLines[size]));
}
}
private static String trimRight(String in) {
int endIdx = in.length() - 1;
while (endIdx > -1 && Character.isWhitespace(in.charAt(endIdx))) {
endIdx--;
}
return in.substring(0, endIdx);
}
private static String[] removeBlanks(String[] in) {
List<String> out = new ArrayList<String>();
for (String s : in) {
if (!s.trim().isEmpty()) out.add(s);
}
return out.toArray(new String[0]);
}
}
|