Permalink
Browse files

ns

  • Loading branch information...
1 parent d73fe88 commit 21ccc5a66e12fc84659c7ff9c1867629ade22d21 @belaban committed Feb 9, 2012
@@ -1,8 +1,6 @@
package org.jgroups.util;
-import org.testng.ITestContext;
-import org.testng.ITestListener;
-import org.testng.ITestResult;
+import org.testng.*;
import java.io.*;
import java.util.*;
@@ -13,7 +11,7 @@
*
* @author Bela Ban
*/
-public class JUnitXMLReporter implements ITestListener {
+public class JUnitXMLReporter implements ITestListener, IConfigurationListener2 {
protected String output_dir=null;
protected static final String XML_DEF="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
@@ -68,20 +66,7 @@ public void onFinish(ITestContext context) {
/* 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) + " -----------");
- }
- catch(IOException e) {
- error(e.toString());
- }
+ setupStreams(result, true);
}
@@ -92,35 +77,75 @@ public void onTestSuccess(ITestResult tr) {
public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
- onTestCompleted(tr,"OK: ",old_stdout);
+ onTestCompleted(tr, "OK: ",old_stdout);
}
/** Invoked each time a test method fails */
public void onTestFailure(ITestResult tr) {
- onTestCompleted(tr,"FAIL: ",old_stderr);
+ onTestCompleted(tr, "FAIL: ",old_stderr);
}
/** Invoked each time a test method is skipped */
public void onTestSkipped(ITestResult tr) {
- onTestCompleted(tr,"SKIP: ",old_stderr);
+ onTestCompleted(tr, "SKIP: ",old_stderr);
+ }
+
+ public void beforeConfiguration(ITestResult tr) {
+ setupStreams(tr, false);
+ }
+
+ public void onConfigurationSuccess(ITestResult tr) {
+
+ }
+
+ public void onConfigurationFailure(ITestResult tr) {
+ error("failed config: " + tr.getThrowable());
+ onTestCompleted(tr, "FAIL: ", old_stderr);
}
- public String toString() {
- return "bla";
+ public void onConfigurationSkip(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());
+ closeStreams();
+ }
+
+ protected void setupStreams(ITestResult result, boolean printMethodName) {
+ 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 {
+ if(tests.get() == null)
+ tests.set(new DataOutputStream(new FileOutputStream(_tests, true)));
+ if(stdout.get() == null)
+ stdout.set(new PrintStream(new FileOutputStream(_stdout, true)));
+ if(stderr.get() == null)
+ stderr.set(new PrintStream(new FileOutputStream(_stderr, true)));
+ if(printMethodName)
+ stdout.get().println("\n\n------------- " + getMethodName(result) + " -----------");
+ }
+ catch(IOException e) {
+ error(e.toString());
+ }
}
+ protected static void closeStreams() {
+ Util.close(stdout.get());
+ stdout.set(null);
+ Util.close(stderr.get());
+ stderr.set(null);
+ Util.close(tests.get());
+ tests.set(null);
+ }
protected static void print(PrintStream out, String msg, String classname, String method_name) {
- out.println(msg + "[" + Thread.currentThread().getId() + "] " + classname + "." + method_name + "()");
+ out.println(msg + "[" + Thread.currentThread().getId() + "] " + classname + "." + method_name + "()");
}
protected void error(String msg) {
@@ -146,7 +171,7 @@ protected void addTest(Class<?> clazz, ITestResult result) {
test_case.setFailure(failure_type, failure_msg, stack_trace);
}
else
- test_case.setFailure("SKIP", null, null);
+ test_case.setFailure("exception", "SKIPPED", null);
break;
}
@@ -163,8 +188,13 @@ 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;
+ String tmp=null;
+ if(params[0] instanceof Class<?>)
+ tmp=((Class<?>)params[0]).getSimpleName();
+ else if(params[0] != null)
+ tmp=params[0].getClass().getSimpleName();
+ if(tmp != null)
+ method_name=method_name + "-" + tmp;
}
return method_name;
}
@@ -250,15 +280,13 @@ protected static void generateReport(Writer out, String classname, List<TestCase
try {
out.write(XML_DEF + "\n");
- out.write("\n<testsuite " + " failures=\""
- + num_failures
- + "\" errors=\""
- + num_errors
- + "\" skips=\""
- + num_skips
- + "\" name=\""
- + classname);
- out.write("\" tests=\"" + results.size() + "\" time=\"" + (total_time / 1000.0) + "\">");
+ out.write("\n<testsuite "
+ + "name=\"" + classname + "\" "
+ + "tests=\"" + results.size() + "\" "
+ + "failures=\"" + num_failures + "\" "
+ + "errors=\"" + num_errors + "\" "
+ + "skips=\"" + num_skips + "\" "
+ + "time=\"" + (total_time / 1000.0) + "\">");
out.write("\n<properties>");
Properties props=System.getProperties();
@@ -500,6 +528,8 @@ public void println(Object obj) {
protected synchronized void append(String x, boolean newline) {
PrintStream tmp=type == 1? stdout.get() : stderr.get();
+ if(tmp == null)
+ return;
if(newline)
tmp.println(x);
else
@@ -509,7 +539,7 @@ protected synchronized void append(String x, boolean newline) {
public static void main(String[] args) throws IOException {
JUnitXMLReporter reporter=new JUnitXMLReporter();
- reporter.output_dir="/home/bela/JGroups/tmp/test-results/xml/tcp";
+ reporter.output_dir="/home/bela/JGroups/tmp/test-results/xml/udp";
reporter.generateReports();
}
@@ -5,26 +5,23 @@
import org.jgroups.logging.LogFactory;
import org.jgroups.protocols.*;
import org.jgroups.protocols.pbcast.FLUSH;
+import org.jgroups.stack.IpAddress;
import org.jgroups.stack.Protocol;
import org.jgroups.stack.ProtocolStack;
-import org.jgroups.stack.IpAddress;
import org.jgroups.util.ResourceManager;
-import org.jgroups.util.Util;
import org.jgroups.util.StackType;
+import org.jgroups.util.Util;
import org.testng.AssertJUnit;
-import org.testng.annotations.AfterClass;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Optional;
-import org.testng.annotations.Parameters;
-import org.testng.annotations.Test;
+import org.testng.annotations.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
-import java.util.*;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
@@ -46,11 +43,12 @@
private String bind_addr = null;
- protected final Log log = LogFactory.getLog(this.getClass());
+ protected Log log;
@BeforeClass
@Parameters(value = { "channel.conf", "use_blocking" })
protected void initializeBase(@Optional("udp.xml") String chconf, @Optional("false") String use_blocking) throws Exception {
+ log=LogFactory.getLog(this.getClass());
Test annotation = this.getClass().getAnnotation(Test.class);
// this should never ever happen!
if (annotation == null)
@@ -10,32 +10,28 @@
import java.io.*;
import java.util.Iterator;
+import java.util.NoSuchElementException;
/**
* Tests state transfer API (including exception handling)
* @author Bela Ban
*/
@Test(groups=Global.STACK_DEPENDENT,sequential=true)
public class StateTransferTest2 extends ChannelTestBase {
+ JChannel c1, c2;
@DataProvider(name="createChannels")
- protected Iterator<JChannel[]> createChannels() {
- return new MyIterator(new Class[]{STATE_TRANSFER.class, STATE.class,
- STATE_SOCK.class});
+ protected Iterator<Class<?>[]> createChannels() {
+ return new MyIterator(new Class<?>[]{STATE_TRANSFER.class, STATE.class, STATE_SOCK.class});
}
- /* @DataProvider(name="createChannels")
- protected Iterator<JChannel[]> createChannels() {
- return new MyIterator(new Class[]{STATE.class});
- }*/
-
-
@Test(dataProvider="createChannels")
- public void testSuccessfulStateTransfer(final JChannel c1, final JChannel c2) throws Exception {
+ public void testSuccessfulStateTransfer(final Class<?> state_transfer_class) throws Exception {
try {
+ createStateProviderAndRequesterChannels(state_transfer_class);
StateHandler sh1=new StateHandler("Bela", false, false), sh2=new StateHandler(null, false, false);
c1.setReceiver(sh1);
c2.setReceiver(sh2);
@@ -50,8 +46,9 @@ public void testSuccessfulStateTransfer(final JChannel c1, final JChannel c2) th
}
@Test(dataProvider="createChannels")
- public void testUnsuccessfulStateTransferFailureAtStateProvider(final JChannel c1, final JChannel c2) throws Exception {
+ public void testUnsuccessfulStateTransferFailureAtStateProvider(final Class<?> state_transfer_class) throws Exception {
try {
+ createStateProviderAndRequesterChannels(state_transfer_class);
StateHandler sh1=new StateHandler("Bela", true, false), sh2=new StateHandler(null, false, false);
c1.setReceiver(sh1);
c2.setReceiver(sh2);
@@ -73,7 +70,8 @@ public void testUnsuccessfulStateTransferFailureAtStateProvider(final JChannel c
@Test(dataProvider="createChannels")
- public void testUnsuccessfulStateTransferFailureAtStateRequester(final JChannel c1, final JChannel c2) throws Exception {
+ public void testUnsuccessfulStateTransferFailureAtStateRequester(final Class<?> state_transfer_class) throws Exception {
+ createStateProviderAndRequesterChannels(state_transfer_class);
StateHandler sh1=new StateHandler("Bela", false, false), sh2=new StateHandler(null, false, true);
c1.setReceiver(sh1);
c2.setReceiver(sh2);
@@ -90,62 +88,51 @@ public void testUnsuccessfulStateTransferFailureAtStateRequester(final JChannel
}
+ protected void createStateProviderAndRequesterChannels(Class state_transfer_class) throws Exception {
+ c1=createChannel(true, 2, "Provider");
+ replaceStateTransferProtocolWith(c1, state_transfer_class);
+ c2=createChannel(c1, "Requester");
+ c1.connect("StateTransferTest2");
+ c2.connect("StateTransferTest2");
+ }
- protected class MyIterator implements Iterator<JChannel[]> {
- protected final Class[] stream_transfer_prots;
- protected int index=0;
-
- public MyIterator(Class[] stream_transfer_prots) {
- this.stream_transfer_prots=stream_transfer_prots;
+ protected void replaceStateTransferProtocolWith(JChannel ch, Class<?> state_transfer_class) throws Exception {
+ ProtocolStack stack=ch.getProtocolStack();
+ if(stack.findProtocol(state_transfer_class) != null)
+ return; // protocol of the right class is already in stack
+ Protocol prot=stack.findProtocol(STATE_TRANSFER.class, StreamingStateTransfer.class);
+ Protocol new_state_transfer_protcol=(Protocol)state_transfer_class.newInstance();
+ if(prot != null) {
+ stack.replaceProtocol(prot, new_state_transfer_protcol);
+ }
+ else { // no state transfer protocol found in stack
+ Protocol flush=stack.findProtocol(FLUSH.class);
+ if(flush != null)
+ stack.insertProtocol(new_state_transfer_protcol, ProtocolStack.BELOW, FLUSH.class);
+ else
+ stack.insertProtocolAtTop(new_state_transfer_protcol);
}
+ }
- public boolean hasNext() {
- return index < stream_transfer_prots.length;
- }
- public JChannel[] next() {
- try {
- Class next_class=stream_transfer_prots[index++];
- System.out.println("State transfer protocol used: " + next_class);
- return createStateProviderAndRequesterChannels(next_class);
- }
- catch(Exception e) {
- throw new RuntimeException("failed creating a new channel", e);
- }
- }
- public void remove() {
- }
+ protected static class MyIterator implements Iterator<Class<?>[]> {
+ protected final Class<?>[] stream_transfer_prots;
+ protected int index=0;
+ public MyIterator(Class<?>[] stream_transfer_prots) {
+ this.stream_transfer_prots=stream_transfer_prots;
+ }
+ public boolean hasNext() {return index < stream_transfer_prots.length;}
- protected JChannel[] createStateProviderAndRequesterChannels(Class state_transfer_class) throws Exception {
- JChannel[] retval=new JChannel[2];
- retval[0]=createChannel(true, 2, "Provider");
- replaceStateTransferProtocolWith(retval[0], state_transfer_class);
- retval[1]=createChannel(retval[0], "Requester");
- for(JChannel ch: retval)
- ch.connect("StateTransferTest2");
- return retval;
+ public Class<?>[] next() {
+ if(index+1 > stream_transfer_prots.length)
+ throw new NoSuchElementException();
+ return new Class<?>[]{stream_transfer_prots[index++]};
}
- protected void replaceStateTransferProtocolWith(JChannel ch, Class state_transfer_class) throws Exception {
- ProtocolStack stack=ch.getProtocolStack();
- if(stack.findProtocol(state_transfer_class) != null)
- return; // protocol of the right class is already in stack
- Protocol prot=stack.findProtocol(STATE_TRANSFER.class, StreamingStateTransfer.class);
- Protocol new_state_transfer_protcol=(Protocol)state_transfer_class.newInstance();
- if(prot != null) {
- stack.replaceProtocol(prot, new_state_transfer_protcol);
- }
- else { // no state transfer protocol found in stack
- Protocol flush=stack.findProtocol(FLUSH.class);
- if(flush != null)
- stack.insertProtocol(new_state_transfer_protcol, ProtocolStack.BELOW, FLUSH.class);
- else
- stack.insertProtocolAtTop(new_state_transfer_protcol);
- }
- }
+ public void remove() {}
}

0 comments on commit 21ccc5a

Please sign in to comment.