From d73fe880a2b8ebfe6c11a358a25a51106f8cc684 Mon Sep 17 00:00:00 2001 From: Bela Ban Date: Wed, 8 Feb 2012 17:12:53 +0100 Subject: [PATCH] First impl of new JUnitXmlReporter --- src/org/jgroups/util/JUnitXMLReporter.java | 706 ++++++++++-------- .../org/jgroups/tests/UNICAST_Test.java | 15 + .../org/jgroups/tests/ChannelTestBase.java | 5 - 3 files changed, 415 insertions(+), 311 deletions(-) diff --git a/src/org/jgroups/util/JUnitXMLReporter.java b/src/org/jgroups/util/JUnitXMLReporter.java index 68bc2ddcc1d..ac007cd0dc0 100644 --- a/src/org/jgroups/util/JUnitXMLReporter.java +++ b/src/org/jgroups/util/JUnitXMLReporter.java @@ -1,19 +1,11 @@ package org.jgroups.util; import org.testng.ITestContext; -import org.testng.ITestNGMethod; +import org.testng.ITestListener; import org.testng.ITestResult; -import org.testng.IInvokedMethod; -import org.testng.IInvokedMethodListener; -import org.testng.TestListenerAdapter; -import org.testng.annotations.Test; import java.io.*; -import java.lang.reflect.Method; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; -import java.util.concurrent.ConcurrentMap; /** * Listener generating XML output suitable to be processed by JUnitReport. @@ -21,279 +13,240 @@ * * @author Bela Ban */ -public class JUnitXMLReporter extends TestListenerAdapter implements IInvokedMethodListener { - private String output_dir=null; - private String suffix=null; +public class JUnitXMLReporter implements ITestListener { + protected String output_dir=null; - private static final String SUFFIX="test.suffix"; - private static final String XML_DEF=""; - private static final String CDATA="![CDATA["; - private static final String LT="<"; - private static final String GT=">"; - private static final String SYSTEM_OUT="system-out"; - private static final String SYSTEM_ERR="system-err"; + protected static final String XML_DEF=""; + protected static final String CDATA="![CDATA["; + protected static final String LT="<"; + protected static final String GT=">"; + protected static final String SYSTEM_OUT="system-out"; + protected static final String SYSTEM_ERR="system-err"; + protected static final String TESTS="tests.data"; + protected static final String STDOUT="stdout.txt"; + protected static final String STDERR="stderr.txt"; - private PrintStream old_stdout=System.out; - private PrintStream old_stderr=System.err; + protected PrintStream old_stdout=System.out; + protected PrintStream old_stderr=System.err; - private final ConcurrentMap,Collection> classes=new ConcurrentHashMap,Collection>(); + public static final InheritableThreadLocal stdout=new InheritableThreadLocal(); + public static final InheritableThreadLocal stderr=new InheritableThreadLocal(); + public static final InheritableThreadLocal tests=new InheritableThreadLocal(); - /** Map to keep systemout and systemerr associated with a class */ - private final ConcurrentMap,Tuple> outputs=new ConcurrentHashMap,Tuple>(); - // Keeps track of already gnerated reports, so we don't generate the same report multiple times - protected final Set> generated_reports=new HashSet>(); - public static InheritableThreadLocal> local=new InheritableThreadLocal>(); - - class MyOutput extends PrintStream { - final int type; + /** Invoked at the start of the test, before any of the classes in the test are run */ + public void onStart(ITestContext context) { + output_dir=context.getOutputDirectory(); + // Uncomment to delete dir created by previous run of this testsuite + // File dir=new File(output_dir); + // if(dir.exists()) + // deleteContents(dir); - public MyOutput(String fileName,int type) throws FileNotFoundException { - super(fileName); - this.type=type; - if(type != 1 && type != 2) - throw new IllegalArgumentException("index has to be 1 or 2"); + try { + System.setOut(new MyOutput(1)); + System.setErr(new MyOutput(2)); } - - public void write(final byte[] b) { - String s = new String(b) ; - append(s.trim(), false) ; + catch(FileNotFoundException e) { } + } - public void write(final byte[] b, final int off, final int len) { - String s = new String(b, off, len) ; - append(s.trim(), false) ; + /** Invoked after all test classes in this test have been run */ + public void onFinish(ITestContext context) { + try { + generateReports(); } - - public void write(final int b) { - Integer i = new Integer(b) ; - append(i.toString(), false) ; + catch(IOException e) { + error(e.toString()); } - - public void println(String s) { - append(s, true); + finally { + System.setOut(old_stdout); + System.setErr(old_stderr); } + } - public void print(String s) { - append(s, false); - } - public void print(Object obj) { - if(obj != null) - append(obj.toString(), false); - else - append("null", false); + /* Invoked at the start of each test method in a test class */ + public void onTestStart(ITestResult result) { + String test_name=result.getTestClass().getName(); + File dir=new File(output_dir + File.separator + test_name); + if(!dir.exists()) + dir.mkdirs(); + File _tests=new File(dir, TESTS), _stdout=new File(dir, STDOUT), _stderr=new File(dir, STDERR); + try { + tests.set(new DataOutputStream(new FileOutputStream(_tests, true))); + stdout.set(new PrintStream(new FileOutputStream(_stdout, true))); + stderr.set(new PrintStream(new FileOutputStream(_stderr, true))); + stdout.get().println("\n\n------------- " + getMethodName(result) + " -----------"); } - - public void println(Object obj) { - if(obj != null) - append(obj.toString(), true); - else - append("null", true); - } - - private synchronized void append(String x, boolean newline) { - Class clazz=local.get(); - if(clazz != null) { - Tuple tuple=outputs.get(clazz); - if(tuple == null) { - tuple=new Tuple(new StringBuffer(), new StringBuffer()); - Tuple tmp=outputs.putIfAbsent(clazz,tuple); - if(tmp != null) - tuple=tmp; - } - StringBuffer sb=type == 1? tuple.getVal1() : tuple.getVal2(); - if(sb.length() == 0) { - sb.append("\n" + clazz.getName() + ":"); - } - sb.append("\n").append(x); - return; - } - PrintStream stream=type == 2? old_stderr : old_stdout; - if(newline) - stream.println(x); - else - stream.print(x); + catch(IOException e) { + error(e.toString()); } } - /** Invoked before any method (configuration or test) is invoked */ - public void beforeInvocation(IInvokedMethod method, ITestResult tr) { - Class real_class=tr.getTestClass().getRealClass(); - - local.set(real_class); - - Collection results=classes.get(real_class); - if(results == null) { - results=new LinkedList(); - classes.putIfAbsent(real_class,results); - } - outputs.putIfAbsent(real_class, new Tuple(new StringBuffer(), new StringBuffer())) ; + /** Invoked each time a test method succeeds */ + public void onTestSuccess(ITestResult tr) { + onTestCompleted(tr, "OK: ", old_stdout); } - /** Invoked after any method (configuration or test) is invoked */ - public void afterInvocation(IInvokedMethod method, ITestResult tr) { + public void onTestFailedButWithinSuccessPercentage(ITestResult tr) { + onTestCompleted(tr,"OK: ",old_stdout); } - /* Moved code from onTestStart() to beforeInvocation() to avoid output leaks (JGRP-850) */ - public void onTestStart(ITestResult result) { + /** Invoked each time a test method fails */ + public void onTestFailure(ITestResult tr) { + onTestCompleted(tr,"FAIL: ",old_stderr); + } + /** Invoked each time a test method is skipped */ + public void onTestSkipped(ITestResult tr) { + onTestCompleted(tr,"SKIP: ",old_stderr); } + public String toString() { + return "bla"; + } - /** Invoked each time a test succeeds */ - public void onTestSuccess(ITestResult tr) { + protected void onTestCompleted(ITestResult tr, String message, PrintStream out) { Class real_class=tr.getTestClass().getRealClass(); addTest(real_class, tr); + print(out, message , real_class.getName(), getMethodName(tr)); + stdout.get().close(); + stderr.get().close(); + Util.close(tests.get()); + } - String method_name=tr.getName(); - Object[] params=tr.getParameters(); - if(params != null && params.length > 0) { - String tmp=params[0] != null? params[0].getClass().getSimpleName() : null; - method_name=method_name + "-" + tmp; - } - print(old_stdout,"OK: ",real_class.getName(),method_name); + protected static void print(PrintStream out, String msg, String classname, String method_name) { + out.println(msg + "[" + Thread.currentThread().getId() + "] " + classname + "." + method_name + "()"); } - public void onTestFailedButWithinSuccessPercentage(ITestResult tr) { - Class real_class=tr.getTestClass().getRealClass(); - addTest(tr.getTestClass().getRealClass(), tr); - print(old_stdout, "OK: ", real_class.getName(), tr.getName()); + protected void error(String msg) { + old_stderr.println(msg); } - /** - * Invoked each time a test fails. - */ - public void onTestFailure(ITestResult tr) { - Class real_class=tr.getTestClass().getRealClass(); - addTest(tr.getTestClass().getRealClass(), tr); - print(old_stderr, "FAIL: ", real_class.getName(), tr.getName()); + protected void println(String msg) { + old_stdout.println(msg); } - /** - * Invoked each time a test is skipped. - */ - public void onTestSkipped(ITestResult tr) { - Class real_class=tr.getTestClass().getRealClass(); - addTest(tr.getTestClass().getRealClass(), tr); - print(old_stdout, "SKIP: ", real_class.getName(), tr.getName()); - } - - private static void print(PrintStream out, String msg, String classname, String method_name) { - out.println(msg + "[" - + Thread.currentThread().getId() - + "] " - + classname - + "." - + method_name - + "()"); - } - - private void addTest(Class clazz, ITestResult result) { - Collection results=classes.get(clazz); - if(results == null) { - results=new ConcurrentLinkedQueue(); - Collection tmp=classes.putIfAbsent(clazz,results); - if(tmp != null) - results=tmp; - } - results.add(result); - - ITestNGMethod[] testMethods=result.getMethod().getTestClass().getTestMethods(); - int enabledCount = enabledMethods(testMethods); - boolean allTestsInClassCompleted = results.size() >= enabledCount; - if(allTestsInClassCompleted) { - boolean do_generate=false; - synchronized(generated_reports) { - do_generate=generated_reports.add(clazz); - } - try { - if(do_generate) - generateReport(clazz, results); - } - catch(IOException e) { - print(old_stderr, "Failed generating report: ", clazz.getName(), ""); + protected void addTest(Class clazz, ITestResult result) { + try { + TestCase test_case=new TestCase(result.getStatus(), clazz.getName(), getMethodName(result), + result.getStartMillis(), result.getEndMillis()); + switch(result.getStatus()) { + case ITestResult.FAILURE: + case ITestResult.SKIP: + Throwable ex=result.getThrowable(); + if(ex != null) { + String failure_type=ex.getClass().getName(); + String failure_msg=ex.getMessage(); + String stack_trace=printException(ex); + test_case.setFailure(failure_type, failure_msg, stack_trace); + } + else + test_case.setFailure("SKIP", null, null); + break; } - } - } - - // Basically all of the test methods of a test, minus the ones with @Test(enabled=false) - /* private static int enabledMethods(ITestNGMethod[] testMethods) { - int count = testMethods.length; - for(ITestNGMethod testNGMethod: testMethods) { - Method m = testNGMethod.getConstructorOrMethod().getMethod(); - if(m != null && m.isAnnotationPresent(Test.class)){ - Test annotation=m.getAnnotation(Test.class); - if(!annotation.enabled()){ - count --; - } + + synchronized(this) { // handle concurrent access by different threads, if test methods are run in parallel + test_case.writeTo(tests.get()); } } - return count; - }*/ + catch(Exception e) { + error(e.toString()); + } + } - // Basically all of the test methods of a test, minus the ones with @Test(enabled=false). Takes - // @Test(invocationCount=N) into account by counting the test N times - private static int enabledMethods(ITestNGMethod[] testMethods) { - int count=0; - for(ITestNGMethod testNGMethod: testMethods) { - if(testNGMethod.getEnabled()) - count+=testNGMethod.getInvocationCount(); + protected static String getMethodName(ITestResult tr) { + String method_name=tr.getName(); + Object[] params=tr.getParameters(); + if(params != null && params.length > 0) { + String tmp=params[0] != null? params[0].getClass().getSimpleName() : null; + method_name=method_name + "-" + tmp; } - return count; + return method_name; } - /** - * Invoked after the test class is instantiated and before any configuration - * method is called. - */ - public void onStart(ITestContext context) { - suffix=System.getProperty(SUFFIX); - if(suffix != null) - suffix=suffix.trim(); - output_dir=context.getOutputDirectory(); // + File.separator + context.getName() + suffix + ".xml"; + /** Generate the XML report from all the test results */ + protected void generateReports() throws IOException { + File root_dir=new File(output_dir); + if(!root_dir.exists()) + throw new IOException(root_dir + " not found"); + File[] subdirs=root_dir.listFiles(new FileFilter() {public boolean accept(File f) {return f.isDirectory();}}); + if(subdirs != null) { + for(File dir: subdirs) { + try { + process(dir); + } + catch(Throwable e) { + error(e.toString()); + } + } + } + } + + + protected static void process(File dir) throws IOException { + File file=new File(dir, TESTS); + if(!file.exists()) + throw new IOException(file + " not found"); + List test_cases=new ArrayList(); + DataInputStream input=new DataInputStream(new FileInputStream(file)); try { - System.setOut(new MyOutput("/tmp/tmp.txt", 1)); + for(;;) { + TestCase test_case=new TestCase(); + try { + test_case.readFrom(input); + test_cases.add(test_case); + } + catch(Exception e) { + break; + } + } } - catch(FileNotFoundException e) { + finally { + Util.close(input); } + if(test_cases.isEmpty()) + return; + + Reader stdout_reader=null, stderr_reader=null; + File tmp=new File(dir, STDOUT); + if(tmp.exists() && tmp.length() > 0) + stdout_reader=new FileReader(tmp); + + tmp=new File(dir, STDERR); + if(tmp.exists() && tmp.length() > 0) + stderr_reader=new FileReader(tmp); + File parent=dir.getParentFile(); + File xml_file=new File(parent, "TESTS-" + dir.getName() + "-" + parent.getName() + ".xml"); + Writer out=new FileWriter(xml_file); try { - System.setErr(new MyOutput("/tmp/tmp.txt", 2)); + generateReport(out, dir.getName(), test_cases, stdout_reader, stderr_reader); } - catch(FileNotFoundException e) { + finally { + out.close(); + if(stdout_reader != null) + stdout_reader.close(); + if(stderr_reader != null) + stderr_reader.close(); } } - /** - * Invoked after all the tests have run and all their Configuration methods - * have been called. - */ - public void onFinish(ITestContext context) { - System.setOut(old_stdout); - System.setErr(old_stderr); - } - - /** - * generate the XML report given what we know from all the test results - */ - protected void generateReport(Class clazz, Collection results) throws IOException { + + /** Generate the XML report from all the test results */ + protected static void generateReport(Writer out, String classname, List results, + Reader stdout, Reader stderr) throws IOException { int num_failures=getFailures(results); int num_skips=getSkips(results); int num_errors=getErrors(results); long total_time=getTotalTime(results); - String file_name=output_dir + File.separator + "TEST-" + clazz.getName(); - if(suffix != null) - file_name=file_name + "-" + suffix; - file_name=file_name + ".xml"; - Writer out=new FileWriter(file_name, false); // don't append, overwrite try { out.write(XML_DEF + "\n"); @@ -304,9 +257,7 @@ protected void generateReport(Class clazz, Collection results) t + "\" skips=\"" + num_skips + "\" name=\"" - + clazz.getName()); - if(suffix != null) - out.write(" (" + suffix + ")"); + + classname); out.write("\" tests=\"" + results.size() + "\" time=\"" + (total_time / 1000.0) + "\">"); out.write("\n"); @@ -321,110 +272,80 @@ protected void generateReport(Class clazz, Collection results) t } out.write("\n\n"); - for(ITestResult result: results) { + for(TestCase result: results) { if(result == null) continue; try { - String testcase=writeTestCase(result,clazz,suffix); - out.write(testcase); + writeTestCase(out,result); } catch(Throwable t) { t.printStackTrace(); } } - Tuple stdout=outputs.get(clazz); - if(stdout != null) { - StringBuffer system_out=stdout.getVal1(); - StringBuffer system_err=stdout.getVal2(); - writeOutput(out, system_out.toString(), 1); - out.write("\n"); - writeOutput(out, system_err.toString(), 2); - } + if(stdout != null) + writeOutput(1, stdout, out); + if(stderr != null) + writeOutput(2, stderr, out); } finally { out.write("\n\n"); - out.close(); } } - protected static String writeTestCase(ITestResult result, Class clazz, String suffix) throws IOException { - if(result == null) - return null; - long time=result.getEndMillis() - result.getStartMillis(); - StringBuilder sb=new StringBuilder(); - sb.append("\n "); - Throwable ex=result.getThrowable(); + protected static void writeTestCase(Writer out, TestCase result) throws IOException { + long time=result.stop_time - result.start_time; + // StringBuilder sb=new StringBuilder(); + out.write("\n "); - switch(result.getStatus()) { - case ITestResult.SUCCESS: - case ITestResult.SUCCESS_PERCENTAGE_FAILURE: - case ITestResult.STARTED: - break; + switch(result.status) { case ITestResult.FAILURE: - String failure=writeFailure("failure", - result.getMethod().getConstructorOrMethod().getMethod(), - ex, - "exception"); + String failure=writeFailure("failure", result.failure_type, result.failure_msg, result.stack_trace); if(failure != null) - sb.append(failure); + out.write(failure); break; case ITestResult.SKIP: - failure=writeFailure("error", result.getMethod().getConstructorOrMethod().getMethod(), ex, "SKIPPED"); + failure=writeFailure("error", result.failure_type, result.failure_msg, result.stack_trace); if(failure != null) - sb.append(failure); + out.write(failure); break; - default: - failure=writeFailure("error", result.getMethod().getConstructorOrMethod().getMethod(), ex, "exception"); - if(failure != null) - sb.append(failure); } - - sb.append("\n \n"); - return sb.toString(); + out.write("\n \n"); } - private static void writeOutput(Writer out, String s, int type) throws IOException { - if(s != null && s.length() > 0) { - out.write("\n<" + (type == 2? SYSTEM_ERR : SYSTEM_OUT) + "><" + CDATA + "\n"); - out.write(s); - out.write("\n]]>"); - out.write("\n"); - } + protected static void writeOutput(int type, Reader in, Writer out) throws IOException { + out.write("\n<" + (type == 2? SYSTEM_ERR : SYSTEM_OUT) + "><" + CDATA + "\n"); + copy(in, out); + out.write("\n]]>"); + out.write("\n"); } - private static String writeFailure(String type, Method method, Throwable ex, String msg) throws IOException { - Test annotation=method != null? method.getAnnotation(Test.class) : null; - if(annotation != null && ex != null) { - Class[] expected_exceptions=annotation.expectedExceptions(); - for(int i=0;i < expected_exceptions.length;i++) { - Class expected_exception=expected_exceptions[i]; - if(expected_exception.equals(ex.getClass())) { - return null; - } - } + + protected static String getStatus(TestCase tr) { + switch(tr.status) { + case ITestResult.SUCCESS: + case ITestResult.SUCCESS_PERCENTAGE_FAILURE: + return "OK"; + case ITestResult.FAILURE: return "FAIL"; + case ITestResult.SKIP: return "SKIP"; + default: return "UNKNOWN"; } + } + protected static String writeFailure(String type, String failure_type, String failure_msg, String stack_trace) throws IOException { StringBuilder sb=new StringBuilder(); - sb.append("\n <" + type + " type=\""); - if(ex != null) { - sb.append(ex.getClass().getName() + "\" message=\"" + escape(ex.getMessage()) + "\">"); - String ex_str=printException(ex); - if(ex_str != null) - sb.append(ex_str); - } - else - sb.append("exception\" message=\"" + msg + "\">"); + sb.append("\n <" + type + " type=\"").append(failure_type); + sb.append("\" message=\"" + escape(failure_msg) + "\">"); + if(stack_trace != null) + sb.append(stack_trace); sb.append("\n "); return sb.toString(); } - private static String printException(Throwable ex) throws IOException { + protected static String printException(Throwable ex) throws IOException { if(ex == null) return null; StackTraceElement[] stack_trace=ex.getStackTrace(); @@ -439,16 +360,16 @@ private static String printException(Throwable ex) throws IOException { return sb.toString(); } - private static String escape(String message) { + protected static String escape(String message) { return message != null? message.replaceAll("<", LT).replaceAll(">", GT) : message; } - private static long getTotalTime(Collection results) { + protected static long getTotalTime(Collection results) { long start=0, stop=0; - for(ITestResult result:results) { + for(TestCase result: results) { if(result == null) continue; - long tmp_start=result.getStartMillis(), tmp_stop=result.getEndMillis(); + long tmp_start=result.start_time, tmp_stop=result.stop_time; if(start == 0) start=tmp_start; else { @@ -464,33 +385,206 @@ private static long getTotalTime(Collection results) { return stop - start; } - private static int getFailures(Collection results) { + protected static int getFailures(Collection results) { int retval=0; - for(ITestResult result:results) { - if(result != null && result.getStatus() == ITestResult.FAILURE) + for(TestCase result: results) { + if(result != null && result.status == ITestResult.FAILURE) retval++; } return retval; } - private static int getErrors(Collection results) { + protected static int getErrors(Collection results) { int retval=0; - for(ITestResult result:results) { - if(result != null && result.getStatus() != ITestResult.SUCCESS - && result.getStatus() != ITestResult.SUCCESS_PERCENTAGE_FAILURE - && result.getStatus() != ITestResult.FAILURE) + for(TestCase result: results) { + if(result != null && result.status != ITestResult.SUCCESS + && result.status != ITestResult.SUCCESS_PERCENTAGE_FAILURE + && result.status != ITestResult.FAILURE) retval++; } return retval; } - private static int getSkips(Collection results) { + protected static int getSkips(Collection results) { int retval=0; - for(ITestResult result:results) { - if(result != null && result.getStatus() == ITestResult.SKIP) + for(TestCase result: results) { + if(result != null && result.status == ITestResult.SKIP) retval++; } return retval; } + /** Deletes all files and dirs in dir, but not dir itself */ + protected static void deleteContents(File dir) { + File[] contents=dir.listFiles(); + if(contents != null) { + for(File file: contents) { + if(file.isDirectory()) { + deleteContents(file); + file.delete(); + } + else + file.delete(); + } + } + } + + /** Copies the contents of in into out */ + protected static int copy(Reader in, Writer out) { + int count=0; + + char[] buf=new char[1024]; + + while(true) { + try { + int num=in.read(buf, 0, buf.length); + if(num == -1) + break; + out.write(buf, 0, num); + count+=num; + } + catch(IOException e) { + break; + } + } + + return count; + } + + protected static class MyOutput extends PrintStream { + final int type; + + public MyOutput(int type) throws FileNotFoundException { + super("/tmp/tmp.txt"); // dummy name + this.type=type; + if(type != 1 && type != 2) + throw new IllegalArgumentException("index has to be 1 or 2"); + } + + public void write(final byte[] b) { + String s = new String(b) ; + append(s.trim(), false) ; + } + + public void write(final byte[] b, final int off, final int len) { + String s = new String(b, off, len) ; + append(s.trim(), false) ; + } + + public void write(final int b) { + Integer i = new Integer(b) ; + append(i.toString(), false) ; + } + + public void println(String s) { + append(s, true); + } + + public void print(String s) { + append(s, false); + } + + public void print(Object obj) { + if(obj != null) + append(obj.toString(), false); + else + append("null", false); + } + + public void println(Object obj) { + if(obj != null) + append(obj.toString(), true); + else + append("null", true); + } + + protected synchronized void append(String x, boolean newline) { + PrintStream tmp=type == 1? stdout.get() : stderr.get(); + if(newline) + tmp.println(x); + else + tmp.print(x); + } + } + + public static void main(String[] args) throws IOException { + JUnitXMLReporter reporter=new JUnitXMLReporter(); + reporter.output_dir="/home/bela/JGroups/tmp/test-results/xml/tcp"; + reporter.generateReports(); + } + + + protected static class TestCase implements Streamable { + protected int status; + protected String classname; + protected String name; + protected long start_time; + protected long stop_time; + protected String failure_type; // null: no failure, class of the Throwable + protected String failure_msg; // result of Throwable.getMessage() + protected String stack_trace; + + + public TestCase() { // needed for externalization + } + + protected TestCase(int status, String classname, String name, long start_time, long stop_time) { + this.status=status; + this.classname=classname; + this.name=name; + this.start_time=start_time; + this.stop_time=stop_time; + } + + public void setFailure(String failure_type, String failure_msg, String stack_trace) { + this.failure_type=failure_type; + this.failure_msg=failure_msg; + this.stack_trace=stack_trace; + } + + public long getTime() {return stop_time-start_time;} + + public String toString() { + StringBuilder sb=new StringBuilder(); + sb.append(statusToString(status)).append(" ").append(classname).append(".").append(name).append(" in ") + .append(getTime()).append(" ms"); + if(failure_type != null) + sb.append("\n" + failure_type).append("msg=" + failure_msg).append("\n").append(stack_trace); + return sb.toString(); + } + + protected static String statusToString(int status) { + switch(status) { + case ITestResult.SUCCESS: + case ITestResult.SUCCESS_PERCENTAGE_FAILURE: + return "OK"; + case ITestResult.FAILURE: return "FAIL"; + case ITestResult.SKIP: return "SKIP"; + default: return "N/A"; + } + } + + public void writeTo(DataOutput out) throws Exception { + out.writeInt(status); + Util.writeString(classname,out); + Util.writeString(name, out); + out.writeLong(start_time); + out.writeLong(stop_time); + Util.writeString(failure_type, out); + Util.writeString(failure_msg, out); + Util.writeString(stack_trace, out); + } + + public void readFrom(DataInput in) throws Exception { + status=in.readInt(); + classname=Util.readString(in); + name=Util.readString(in); + start_time=in.readLong(); + stop_time=in.readLong(); + failure_type=Util.readString(in); + failure_msg=Util.readString(in); + stack_trace=Util.readString(in); + } + } + } diff --git a/tests/junit-functional/org/jgroups/tests/UNICAST_Test.java b/tests/junit-functional/org/jgroups/tests/UNICAST_Test.java index 25124ea30c6..265530e29a5 100644 --- a/tests/junit-functional/org/jgroups/tests/UNICAST_Test.java +++ b/tests/junit-functional/org/jgroups/tests/UNICAST_Test.java @@ -11,6 +11,7 @@ import org.jgroups.util.Util; import org.testng.Assert; import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @@ -29,6 +30,10 @@ public class UNICAST_Test { static final int SIZE=1000; // bytes static final int NUM_MSGS=10000; + @BeforeMethod + void before() { + throw new NullPointerException("booom"); + } @AfterMethod void tearDown() throws Exception { @@ -61,6 +66,16 @@ public static Object[][] configProvider() { }; } + //@Test(expectedExceptions=RuntimeException.class) + public static void foo() { + throw new RuntimeException("booooom"); + } + + public static void bar() { + int num=43; + System.err.println("bar will throw an assertion error"); + assert num > 100; + } private static byte[] createPayload(int size, int seqno) { diff --git a/tests/junit/org/jgroups/tests/ChannelTestBase.java b/tests/junit/org/jgroups/tests/ChannelTestBase.java index e40aba699fb..ac588111ea9 100644 --- a/tests/junit/org/jgroups/tests/ChannelTestBase.java +++ b/tests/junit/org/jgroups/tests/ChannelTestBase.java @@ -72,11 +72,6 @@ protected void initializeBase(@Optional("udp.xml") String chconf, @Optional("fal // bind_addr = Util.getBindAddress(null).getHostAddress(); } - @BeforeMethod - protected static void startTestHeader(java.lang.reflect.Method m) { - System.out.println("\n================ Starting test " + m.getName() - + " ================\n"); - } @AfterClass(alwaysRun = true) protected void nullifyInstanceFields() {