Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.sf.webcat.plugins.javatddplugin;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidationMessageFormatter {

public static String formatMessage(Throwable error) {
if (error == null) {
return null;
}

if (error instanceof NoSuchMethodError) {
return "Your test has called a method that does not exist in the interface. "
+ "The reference implementation is not guaranteed to implement this method "
+ "and as such cannot validate this test case. "
+ "The method is: " + error.getMessage();
}
else if (error instanceof AssertionError) {
String original = error.getMessage();
String msg;

if (original != null) {
Pattern p1 = Pattern.compile(
".*expected:<?([^>]*)>? but was:<?([^>]*)>?");
Matcher m1 = p1.matcher(original);
if (m1.matches()) {
String expected = m1.group(1).trim();
String actual = m1.group(2).trim();
msg = "Your test expected: <" + expected + "> "
+ "while the reference implementation returned: <"
+ actual + ">";
}
else if (original.contains("expected null, but was:")) {
Pattern p2 = Pattern.compile(
".*expected null, but was:<?([^>]*)>?");
Matcher m2 = p2.matcher(original);
if (m2.matches()) {
String actual = m2.group(1).trim();
msg = "Your test expected: <null> "
+ "while the reference implementation returned: <"
+ actual + ">";
}
else {
msg = original;
}
}
else if (original.contains("expected same:")) {
Pattern p3 = Pattern.compile(
".*expected same:<?([^>]*)>? was not:<?([^>]*)>?");
Matcher m3 = p3.matcher(original);
if (m3.matches()) {
String expected = m3.group(1).trim();
String actual = m3.group(2).trim();
msg = "Your test expected the *same* object: <"
+ expected + "> "
+ "while the reference implementation returned a different object: <"
+ actual + ">";
}
else {
msg = original;
}
}
else {
msg = original;
}
}
else {
msg = original;
}
return msg;
}

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.sf.webcat.plugins.javatddplugin;

import junit.framework.Test;

public class ValidationPlistJUnitResultFormatter
extends PlistJUnitResultFormatter {
@Override
protected TestResultDescriptor describe(Test test, Throwable error) {
String customMsg = ValidationMessageFormatter.formatMessage(error);

if (customMsg != null) {
int code = codeOf(error);
int level = levelOf(code);
String stackTrace = stackTraceMessage(error, false);
return new TestResultDescriptor(currentSuite, test, error, code,
level, customMsg, stackTrace);
}
else {
return super.describe(test, error);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package net.sf.webcat.plugins.javatddplugin;

import junit.framework.Test;
import junit.framework.AssertionFailedError;

public class ValidationTextJUnitResultFormatter
extends BasicJUnitResultFormatter {

private static class FormattedThrowable extends Throwable {
private static final long serialVersionUID = 1L;
private final Throwable original;
private final String message;

public FormattedThrowable(String message, Throwable original) {
super(message, original);
this.message = message;
this.original = original;
this.setStackTrace(original.getStackTrace());
}


@Override
public String toString() {
return original.getClass().getName() + ": " + message;
}
}

@Override
public void addError(Test test, Throwable error) {
String msg = formatMessage(error);
Throwable newError = new FormattedThrowable(msg, error);
super.addError(test, newError);
}


@Override
public void addFailure(Test test, AssertionFailedError error) {
String msg = formatMessage(error);
Throwable newFailure = new FormattedThrowable(msg, error);
super.addFailure(test, newFailure);
}


private String formatMessage(Throwable error) {
String customMsg = ValidationMessageFormatter.formatMessage(error);

if (customMsg != null) {
return customMsg;
}

if (error != null) {
if (error.getMessage() != null) {
return error.getMessage();
}
else {
return error.toString();
}
}
return "";
}
}
Loading