Skip to content

Commit

Permalink
Ensure serialization compatibility where possible
Browse files Browse the repository at this point in the history
Not possible for org.junit.runner.Result
  • Loading branch information
marcphilipp committed Sep 21, 2014
1 parent a5727fc commit 86b9346
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 86 deletions.
19 changes: 12 additions & 7 deletions src/main/java/org/junit/ComparisonFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ public class ComparisonFailure extends AssertionError {
private static final int MAX_CONTEXT_LENGTH = 20;
private static final long serialVersionUID = 1L;

private String expected;
private String actual;
/*
* We have to use the f prefix until the next major release to ensure
* serialization compatibility.
* See https://github.com/junit-team/junit/issues/976
*/
private String fExpected;
private String fActual;

/**
* Constructs a comparison failure.
Expand All @@ -30,8 +35,8 @@ public class ComparisonFailure extends AssertionError {
*/
public ComparisonFailure(String message, String expected, String actual) {
super(message);
this.expected = expected;
this.actual = actual;
this.fExpected = expected;
this.fActual = actual;
}

/**
Expand All @@ -41,7 +46,7 @@ public ComparisonFailure(String message, String expected, String actual) {
*/
@Override
public String getMessage() {
return new ComparisonCompactor(MAX_CONTEXT_LENGTH, expected, actual).compact(super.getMessage());
return new ComparisonCompactor(MAX_CONTEXT_LENGTH, fExpected, fActual).compact(super.getMessage());
}

/**
Expand All @@ -50,7 +55,7 @@ public String getMessage() {
* @return the actual string value
*/
public String getActual() {
return actual;
return fActual;
}

/**
Expand All @@ -59,7 +64,7 @@ public String getActual() {
* @return the expected string value
*/
public String getExpected() {
return expected;
return fExpected;
}

private static class ComparisonCompactor {
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/org/junit/experimental/max/MaxHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,44 @@ private static MaxHistory readHistory(File storedResults)
}
}

private final Map<String, Long> durations = new HashMap<String, Long>();

private final Map<String, Long> failureTimestamps = new HashMap<String, Long>();

private final File historyStore;
/*
* We have to use the f prefix until the next major release to ensure
* serialization compatibility.
* See https://github.com/junit-team/junit/issues/976
*/
private final Map<String, Long> fDurations = new HashMap<String, Long>();
private final Map<String, Long> fFailureTimestamps = new HashMap<String, Long>();
private final File fHistoryStore;

private MaxHistory(File storedResults) {
historyStore = storedResults;
fHistoryStore = storedResults;
}

private void save() throws IOException {
ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream(
historyStore));
fHistoryStore));
stream.writeObject(this);
stream.close();
}

Long getFailureTimestamp(Description key) {
return failureTimestamps.get(key.toString());
return fFailureTimestamps.get(key.toString());
}

void putTestFailureTimestamp(Description key, long end) {
failureTimestamps.put(key.toString(), end);
fFailureTimestamps.put(key.toString(), end);
}

boolean isNewTest(Description key) {
return !durations.containsKey(key.toString());
return !fDurations.containsKey(key.toString());
}

Long getTestDuration(Description key) {
return durations.get(key.toString());
return fDurations.get(key.toString());
}

void putTestDuration(Description description, long duration) {
durations.put(description.toString(), duration);
fDurations.put(description.toString(), duration);
}

private final class RememberingListener extends RunListener {
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/org/junit/internal/ArrayComparisonFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public class ArrayComparisonFailure extends AssertionError {

private static final long serialVersionUID = 1L;

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

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

public void addDimension(int index) {
indices.add(0, index);
fIndices.add(0, index);
}

@Override
public String getMessage() {
StringBuilder sb = new StringBuilder();
if (message != null) {
sb.append(message);
if (fMessage != null) {
sb.append(fMessage);
}
sb.append("arrays first differed at element ");
for (int each : indices) {
for (int each : fIndices) {
sb.append("[");
sb.append(each);
sb.append("]");
Expand Down
37 changes: 20 additions & 17 deletions src/main/java/org/junit/internal/AssumptionViolatedException.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@
public class AssumptionViolatedException extends RuntimeException implements SelfDescribing {
private static final long serialVersionUID = 2L;

private final String assumption;

private final boolean valueMatcher;
private final Object value;

private final Matcher<?> matcher;
/*
* We have to use the f prefix until the next major release to ensure
* serialization compatibility.
* See https://github.com/junit-team/junit/issues/976
*/
private final String fAssumption;
private final boolean fValueMatcher;
private final Object fValue;
private final Matcher<?> fMatcher;

public AssumptionViolatedException(String assumption, boolean valueMatcher, Object value, Matcher<?> matcher) {
this.assumption = assumption;
this.value = value;
this.matcher = matcher;
this.valueMatcher = valueMatcher;
this.fAssumption = assumption;
this.fValue = value;
this.fMatcher = matcher;
this.fValueMatcher = valueMatcher;

if (value instanceof Throwable) {
initCause((Throwable) value);
Expand Down Expand Up @@ -72,21 +75,21 @@ public String getMessage() {
}

public void describeTo(Description description) {
if (assumption != null) {
description.appendText(assumption);
if (fAssumption != null) {
description.appendText(fAssumption);
}

if (valueMatcher) {
if (assumption != null) {
if (fValueMatcher) {
if (fAssumption != null) {
description.appendText(": ");
}

description.appendText("got: ");
description.appendValue(value);
description.appendValue(fValue);

if (matcher != null) {
if (fMatcher != null) {
description.appendText(", expected: ");
description.appendDescriptionOf(matcher);
description.appendDescriptionOf(fMatcher);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@
@Deprecated
public class InitializationError extends Exception {
private static final long serialVersionUID = 1L;
private final List<Throwable> errors;

/*
* We have to use the f prefix until the next major release to ensure
* serialization compatibility.
* See https://github.com/junit-team/junit/issues/976
*/
private final List<Throwable> fErrors;

public InitializationError(List<Throwable> errors) {
this.errors = errors;
this.fErrors = errors;
}

public InitializationError(Throwable... errors) {
Expand All @@ -26,6 +32,6 @@ public InitializationError(String string) {
}

public List<Throwable> getCauses() {
return errors;
return fErrors;
}
}
53 changes: 29 additions & 24 deletions src/main/java/org/junit/runner/Description.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,16 @@ public static Description createSuiteDescription(Class<?> testClass) {
*/
public static final Description TEST_MECHANISM = new Description(null, "Test mechanism");

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

private Description(Class<?> clazz, String displayName, Annotation... annotations) {
this(clazz, displayName, displayName, annotations);
Expand All @@ -155,17 +160,17 @@ private Description(Class<?> testClass, String displayName, Serializable uniqueI
throw new IllegalArgumentException(
"The unique id must not be null.");
}
this.testClass = testClass;
this.displayName = displayName;
this.uniqueId = uniqueId;
this.annotations = annotations;
this.fTestClass = testClass;
this.fDisplayName = displayName;
this.fUniqueId = uniqueId;
this.fAnnotations = annotations;
}

/**
* @return a user-understandable label
*/
public String getDisplayName() {
return displayName;
return fDisplayName;
}

/**
Expand All @@ -174,15 +179,15 @@ public String getDisplayName() {
* @param description the soon-to-be child.
*/
public void addChild(Description description) {
children.add(description);
fChildren.add(description);
}

/**
* Gets the copy of the children of this {@code Description}.
* Returns an empty list if there are no children.
*/
public ArrayList<Description> getChildren() {
return new ArrayList<Description>(children);
return new ArrayList<Description>(fChildren);
}

/**
Expand All @@ -196,7 +201,7 @@ public boolean isSuite() {
* @return <code>true</code> if the receiver is an atomic test
*/
public boolean isTest() {
return children.isEmpty();
return fChildren.isEmpty();
}

/**
Expand All @@ -207,15 +212,15 @@ public int testCount() {
return 1;
}
int result = 0;
for (Description child : children) {
for (Description child : fChildren) {
result += child.testCount();
}
return result;
}

@Override
public int hashCode() {
return uniqueId.hashCode();
return fUniqueId.hashCode();
}

@Override
Expand All @@ -224,7 +229,7 @@ public boolean equals(Object obj) {
return false;
}
Description d = (Description) obj;
return uniqueId.equals(d.uniqueId);
return fUniqueId.equals(d.fUniqueId);
}

@Override
Expand All @@ -244,15 +249,15 @@ public boolean isEmpty() {
* children will be added back)
*/
public Description childlessCopy() {
return new Description(testClass, displayName, annotations);
return new Description(fTestClass, fDisplayName, fAnnotations);
}

/**
* @return the annotation of type annotationType that is attached to this description node,
* or null if none exists
*/
public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
for (Annotation each : annotations) {
for (Annotation each : fAnnotations) {
if (each.annotationType().equals(annotationType)) {
return annotationType.cast(each);
}
Expand All @@ -264,24 +269,24 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationType) {
* @return all of the annotations attached to this description node
*/
public Collection<Annotation> getAnnotations() {
return Arrays.asList(annotations);
return Arrays.asList(fAnnotations);
}

/**
* @return If this describes a method invocation,
* the class of the test instance.
*/
public Class<?> getTestClass() {
if (testClass != null) {
return testClass;
if (fTestClass != null) {
return fTestClass;
}
String name = getClassName();
if (name == null) {
return null;
}
try {
testClass = Class.forName(name, false, getClass().getClassLoader());
return testClass;
fTestClass = Class.forName(name, false, getClass().getClassLoader());
return fTestClass;
} catch (ClassNotFoundException e) {
return null;
}
Expand All @@ -292,7 +297,7 @@ public Class<?> getTestClass() {
* the name of the class of the test instance
*/
public String getClassName() {
return testClass != null ? testClass.getName() : methodAndClassNamePatternGroupOrDefault(2, toString());
return fTestClass != null ? fTestClass.getName() : methodAndClassNamePatternGroupOrDefault(2, toString());
}

/**
Expand Down
Loading

0 comments on commit 86b9346

Please sign in to comment.