Skip to content

Commit c2f2a8c

Browse files
author
David Saff
committed
Merge pull request #994 from marcphilipp/serialization-compatibility
Ensure serialization compatibility where possible
2 parents a5727fc + 86b9346 commit c2f2a8c

9 files changed

+130
-86
lines changed

src/main/java/org/junit/ComparisonFailure.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ public class ComparisonFailure extends AssertionError {
1818
private static final int MAX_CONTEXT_LENGTH = 20;
1919
private static final long serialVersionUID = 1L;
2020

21-
private String expected;
22-
private String actual;
21+
/*
22+
* We have to use the f prefix until the next major release to ensure
23+
* serialization compatibility.
24+
* See https://github.com/junit-team/junit/issues/976
25+
*/
26+
private String fExpected;
27+
private String fActual;
2328

2429
/**
2530
* Constructs a comparison failure.
@@ -30,8 +35,8 @@ public class ComparisonFailure extends AssertionError {
3035
*/
3136
public ComparisonFailure(String message, String expected, String actual) {
3237
super(message);
33-
this.expected = expected;
34-
this.actual = actual;
38+
this.fExpected = expected;
39+
this.fActual = actual;
3540
}
3641

3742
/**
@@ -41,7 +46,7 @@ public ComparisonFailure(String message, String expected, String actual) {
4146
*/
4247
@Override
4348
public String getMessage() {
44-
return new ComparisonCompactor(MAX_CONTEXT_LENGTH, expected, actual).compact(super.getMessage());
49+
return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage());
4550
}
4651

4752
/**
@@ -50,7 +55,7 @@ public String getMessage() {
5055
* @return the actual string value
5156
*/
5257
public String getActual() {
53-
return actual;
58+
return fActual;
5459
}
5560

5661
/**
@@ -59,7 +64,7 @@ public String getActual() {
5964
* @return the expected string value
6065
*/
6166
public String getExpected() {
62-
return expected;
67+
return fExpected;
6368
}
6469

6570
private static class ComparisonCompactor {

src/main/java/org/junit/experimental/max/MaxHistory.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,44 @@ private static MaxHistory readHistory(File storedResults)
6161
}
6262
}
6363

64-
private final Map<String, Long> durations = new HashMap<String, Long>();
65-
66-
private final Map<String, Long> failureTimestamps = new HashMap<String, Long>();
67-
68-
private final File historyStore;
64+
/*
65+
* We have to use the f prefix until the next major release to ensure
66+
* serialization compatibility.
67+
* See https://github.com/junit-team/junit/issues/976
68+
*/
69+
private final Map<String, Long> fDurations = new HashMap<String, Long>();
70+
private final Map<String, Long> fFailureTimestamps = new HashMap<String, Long>();
71+
private final File fHistoryStore;
6972

7073
private MaxHistory(File storedResults) {
71-
historyStore = storedResults;
74+
fHistoryStore = storedResults;
7275
}
7376

7477
private void save() throws IOException {
7578
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
76-
historyStore));
79+
fHistoryStore));
7780
stream.writeObject(this);
7881
stream.close();
7982
}
8083

8184
Long getFailureTimestamp(Description key) {
82-
return failureTimestamps.get(key.toString());
85+
return fFailureTimestamps.get(key.toString());
8386
}
8487

8588
void putTestFailureTimestamp(Description key, long end) {
86-
failureTimestamps.put(key.toString(), end);
89+
fFailureTimestamps.put(key.toString(), end);
8790
}
8891

8992
boolean isNewTest(Description key) {
90-
return !durations.containsKey(key.toString());
93+
return !fDurations.containsKey(key.toString());
9194
}
9295

9396
Long getTestDuration(Description key) {
94-
return durations.get(key.toString());
97+
return fDurations.get(key.toString());
9598
}
9699

97100
void putTestDuration(Description description, long duration) {
98-
durations.put(description.toString(), duration);
101+
fDurations.put(description.toString(), duration);
99102
}
100103

101104
private final class RememberingListener extends RunListener {

src/main/java/org/junit/internal/ArrayComparisonFailure.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ public class ArrayComparisonFailure extends AssertionError {
1414

1515
private static final long serialVersionUID = 1L;
1616

17-
private final List<Integer> indices = new ArrayList<Integer>();
18-
private final String message;
17+
/*
18+
* We have to use the f prefix until the next major release to ensure
19+
* serialization compatibility.
20+
* See https://github.com/junit-team/junit/issues/976
21+
*/
22+
private final List<Integer> fIndices = new ArrayList<Integer>();
23+
private final String fMessage;
1924

2025
/**
2126
* Construct a new <code>ArrayComparisonFailure</code> with an error text and the array's
@@ -26,23 +31,23 @@ public class ArrayComparisonFailure extends AssertionError {
2631
* @see Assert#assertArrayEquals(String, Object[], Object[])
2732
*/
2833
public ArrayComparisonFailure(String message, AssertionError cause, int index) {
29-
this.message = message;
34+
this.fMessage = message;
3035
initCause(cause);
3136
addDimension(index);
3237
}
3338

3439
public void addDimension(int index) {
35-
indices.add(0, index);
40+
fIndices.add(0, index);
3641
}
3742

3843
@Override
3944
public String getMessage() {
4045
StringBuilder sb = new StringBuilder();
41-
if (message != null) {
42-
sb.append(message);
46+
if (fMessage != null) {
47+
sb.append(fMessage);
4348
}
4449
sb.append("arrays first differed at element ");
45-
for (int each : indices) {
50+
for (int each : fIndices) {
4651
sb.append("[");
4752
sb.append(each);
4853
sb.append("]");

src/main/java/org/junit/internal/AssumptionViolatedException.java

+20-17
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
public class AssumptionViolatedException extends RuntimeException implements SelfDescribing {
1919
private static final long serialVersionUID = 2L;
2020

21-
private final String assumption;
22-
23-
private final boolean valueMatcher;
24-
private final Object value;
25-
26-
private final Matcher<?> matcher;
21+
/*
22+
* We have to use the f prefix until the next major release to ensure
23+
* serialization compatibility.
24+
* See https://github.com/junit-team/junit/issues/976
25+
*/
26+
private final String fAssumption;
27+
private final boolean fValueMatcher;
28+
private final Object fValue;
29+
private final Matcher<?> fMatcher;
2730

2831
public AssumptionViolatedException(String assumption, boolean valueMatcher, Object value, Matcher<?> matcher) {
29-
this.assumption = assumption;
30-
this.value = value;
31-
this.matcher = matcher;
32-
this.valueMatcher = valueMatcher;
32+
this.fAssumption = assumption;
33+
this.fValue = value;
34+
this.fMatcher = matcher;
35+
this.fValueMatcher = valueMatcher;
3336

3437
if (value instanceof Throwable) {
3538
initCause((Throwable) value);
@@ -72,21 +75,21 @@ public String getMessage() {
7275
}
7376

7477
public void describeTo(Description description) {
75-
if (assumption != null) {
76-
description.appendText(assumption);
78+
if (fAssumption != null) {
79+
description.appendText(fAssumption);
7780
}
7881

79-
if (valueMatcher) {
80-
if (assumption != null) {
82+
if (fValueMatcher) {
83+
if (fAssumption != null) {
8184
description.appendText(": ");
8285
}
8386

8487
description.appendText("got: ");
85-
description.appendValue(value);
88+
description.appendValue(fValue);
8689

87-
if (matcher != null) {
90+
if (fMatcher != null) {
8891
description.appendText(", expected: ");
89-
description.appendDescriptionOf(matcher);
92+
description.appendDescriptionOf(fMatcher);
9093
}
9194
}
9295
}

src/main/java/org/junit/internal/runners/InitializationError.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
@Deprecated
1212
public class InitializationError extends Exception {
1313
private static final long serialVersionUID = 1L;
14-
private final List<Throwable> errors;
14+
15+
/*
16+
* We have to use the f prefix until the next major release to ensure
17+
* serialization compatibility.
18+
* See https://github.com/junit-team/junit/issues/976
19+
*/
20+
private final List<Throwable> fErrors;
1521

1622
public InitializationError(List<Throwable> errors) {
17-
this.errors = errors;
23+
this.fErrors = errors;
1824
}
1925

2026
public InitializationError(Throwable... errors) {
@@ -26,6 +32,6 @@ public InitializationError(String string) {
2632
}
2733

2834
public List<Throwable> getCauses() {
29-
return errors;
35+
return fErrors;
3036
}
3137
}

src/main/java/org/junit/runner/Description.java

+29-24
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,16 @@ public static Description createSuiteDescription(Class<?> testClass) {
136136
*/
137137
public static final Description TEST_MECHANISM = new Description(null, "Test mechanism");
138138

139-
private final Collection<Description> children = new ConcurrentLinkedQueue<Description>();
140-
private final String displayName;
141-
private final Serializable uniqueId;
142-
private final Annotation[] annotations;
143-
private volatile /* write-once */ Class<?> testClass;
139+
/*
140+
* We have to use the f prefix until the next major release to ensure
141+
* serialization compatibility.
142+
* See https://github.com/junit-team/junit/issues/976
143+
*/
144+
private final Collection<Description> fChildren = new ConcurrentLinkedQueue<Description>();
145+
private final String fDisplayName;
146+
private final Serializable fUniqueId;
147+
private final Annotation[] fAnnotations;
148+
private volatile /* write-once */ Class<?> fTestClass;
144149

145150
private Description(Class<?> clazz, String displayName, Annotation... annotations) {
146151
this(clazz, displayName, displayName, annotations);
@@ -155,17 +160,17 @@ private Description(Class<?> testClass, String displayName, Serializable uniqueI
155160
throw new IllegalArgumentException(
156161
"The unique id must not be null.");
157162
}
158-
this.testClass = testClass;
159-
this.displayName = displayName;
160-
this.uniqueId = uniqueId;
161-
this.annotations = annotations;
163+
this.fTestClass = testClass;
164+
this.fDisplayName = displayName;
165+
this.fUniqueId = uniqueId;
166+
this.fAnnotations = annotations;
162167
}
163168

164169
/**
165170
* @return a user-understandable label
166171
*/
167172
public String getDisplayName() {
168-
return displayName;
173+
return fDisplayName;
169174
}
170175

171176
/**
@@ -174,15 +179,15 @@ public String getDisplayName() {
174179
* @param description the soon-to-be child.
175180
*/
176181
public void addChild(Description description) {
177-
children.add(description);
182+
fChildren.add(description);
178183
}
179184

180185
/**
181186
* Gets the copy of the children of this {@code Description}.
182187
* Returns an empty list if there are no children.
183188
*/
184189
public ArrayList<Description> getChildren() {
185-
return new ArrayList<Description>(children);
190+
return new ArrayList<Description>(fChildren);
186191
}
187192

188193
/**
@@ -196,7 +201,7 @@ public boolean isSuite() {
196201
* @return <code>true</code> if the receiver is an atomic test
197202
*/
198203
public boolean isTest() {
199-
return children.isEmpty();
204+
return fChildren.isEmpty();
200205
}
201206

202207
/**
@@ -207,15 +212,15 @@ public int testCount() {
207212
return 1;
208213
}
209214
int result = 0;
210-
for (Description child : children) {
215+
for (Description child : fChildren) {
211216
result += child.testCount();
212217
}
213218
return result;
214219
}
215220

216221
@Override
217222
public int hashCode() {
218-
return uniqueId.hashCode();
223+
return fUniqueId.hashCode();
219224
}
220225

221226
@Override
@@ -224,7 +229,7 @@ public boolean equals(Object obj) {
224229
return false;
225230
}
226231
Description d = (Description) obj;
227-
return uniqueId.equals(d.uniqueId);
232+
return fUniqueId.equals(d.fUniqueId);
228233
}
229234

230235
@Override
@@ -244,15 +249,15 @@ public boolean isEmpty() {
244249
* children will be added back)
245250
*/
246251
public Description childlessCopy() {
247-
return new Description(testClass, displayName, annotations);
252+
return new Description(fTestClass, fDisplayName, fAnnotations);
248253
}
249254

250255
/**
251256
* @return the annotation of type annotationType that is attached to this description node,
252257
* or null if none exists
253258
*/
254259
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
255-
for (Annotation each : annotations) {
260+
for (Annotation each : fAnnotations) {
256261
if (each.annotationType().equals(annotationType)) {
257262
return annotationType.cast(each);
258263
}
@@ -264,24 +269,24 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
264269
* @return all of the annotations attached to this description node
265270
*/
266271
public Collection<Annotation> getAnnotations() {
267-
return Arrays.asList(annotations);
272+
return Arrays.asList(fAnnotations);
268273
}
269274

270275
/**
271276
* @return If this describes a method invocation,
272277
* the class of the test instance.
273278
*/
274279
public Class<?> getTestClass() {
275-
if (testClass != null) {
276-
return testClass;
280+
if (fTestClass != null) {
281+
return fTestClass;
277282
}
278283
String name = getClassName();
279284
if (name == null) {
280285
return null;
281286
}
282287
try {
283-
testClass = Class.forName(name, false, getClass().getClassLoader());
284-
return testClass;
288+
fTestClass = Class.forName(name, false, getClass().getClassLoader());
289+
return fTestClass;
285290
} catch (ClassNotFoundException e) {
286291
return null;
287292
}
@@ -292,7 +297,7 @@ public Class<?> getTestClass() {
292297
* the name of the class of the test instance
293298
*/
294299
public String getClassName() {
295-
return testClass != null ? testClass.getName() : methodAndClassNamePatternGroupOrDefault(2, toString());
300+
return fTestClass != null ? fTestClass.getName() : methodAndClassNamePatternGroupOrDefault(2, toString());
296301
}
297302

298303
/**

0 commit comments

Comments
 (0)