Skip to content
Browse files

Added CountTests

  • Loading branch information...
1 parent b14c275 commit 66b820fb2f0e47a4ca5461e02b5854a8cd5b1e89 @belaban committed Feb 9, 2012
View
8 bin/count-tests.sh
@@ -0,0 +1,8 @@
+
+# Author: Bela Ban
+
+#!/bin/bash
+
+JG=$HOME/JGroups
+
+jgroups.sh org.jgroups.util.CountTests $*
View
115 src/org/jgroups/util/CountTests.java
@@ -0,0 +1,115 @@
+package org.jgroups.util;
+
+import org.jgroups.util.Util;
+import org.testng.annotations.Test;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+/**
+ * Counts and dumps all tests annotated with @Test. Tests with enabled=false are excluded. Takes invocationCount
+ * into account, e.g. a method with @Test(invocationCount=5) is counted 5 times.
+ * @author Bela Ban
+ * @since 3.1
+ */
+public class CountTests {
+
+ public static void main(String[] args) throws ClassNotFoundException, IOException {
+ List<Class<?>> list=Util.findClassesAnnotatedWith("org.jgroups",Test.class);
+ int total=0, disabled=0;
+ Map<String,Integer> group_count=new HashMap<String,Integer>(list.size());
+ String group=null;
+ boolean details=false;
+
+ for(int i=0; i < args.length; i++) {
+ if(args[i].equals("-group")) {
+ group=args[++i];
+ continue;
+ }
+ if(args[i].equals("-details")) {
+ details=true;
+ continue;
+ }
+
+ System.out.println("CountTests [-group groupname] [-details]");
+ return;
+ }
+
+ for(Class<?> clazz: list) {
+ Test annotation=clazz.getAnnotation(Test.class);
+ if(!annotation.enabled()) {
+ disabled++;
+ continue;
+ }
+
+ String name=clazz.getSimpleName();
+ if(details && group != null && hasGroup(annotation, group))
+ System.out.println(Util.bold("\n" + name + ":"));
+ Method[] methods=clazz.getDeclaredMethods();
+ int cnt=0;
+ for(Method method: methods) {
+ int mods=method.getModifiers();
+ if(!Modifier.isPublic(mods))
+ continue;
+ Test method_annotation=method.getAnnotation(Test.class);
+ if(method_annotation != null) {
+ if(method_annotation.enabled() == false) {
+ disabled++;
+ continue;
+ }
+ int invocation_cnt=method_annotation.invocationCount();
+ if(invocation_cnt > 1) {
+ if(details && group != null && hasGroup(annotation, group))
+ System.out.println(" " + method.getName() + " [invocationCount=" + invocation_cnt + "]");
+ total+=invocation_cnt;
+ cnt+=invocation_cnt;
+ continue;
+ }
+ }
+ if(details && group != null && hasGroup(annotation, group))
+ System.out.println(" " + method.getName());
+ total++;
+ cnt++;
+ }
+ if(details && group != null && hasGroup(annotation, group))
+ System.out.println(" (" + cnt + ")");
+
+ String[] groups=annotation.groups();
+ if(groups != null) {
+ for(String grp: groups) {
+ if(group != null && !grp.equals(group))
+ continue;
+ Integer count=group_count.get(grp);
+ if(count == null)
+ group_count.put(grp, cnt);
+ else
+ group_count.put(grp, count+cnt);
+ }
+ }
+ }
+
+
+ System.out.println("\nFound " + total + " classes annotated with @Test: " + disabled + " disabled, groups:");
+ for(Map.Entry<String,Integer> entry: group_count.entrySet()) {
+ String key=entry.getKey();
+ Integer val=entry.getValue();
+ System.out.println(key + ": " + val);
+ }
+ }
+
+ protected static boolean hasGroup(Test annotation, String grp) {
+ String[] groups=annotation.groups();
+ if(groups != null) {
+ for(String group: groups) {
+ if(group.equals(grp))
+ return true;
+ }
+ }
+ return false;
+ }
+}
View
38 src/org/jgroups/util/JUnitXMLReporter.java
@@ -4,6 +4,8 @@
import java.io.*;
import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
/**
* Listener generating XML output suitable to be processed by JUnitReport.
@@ -27,9 +29,10 @@
protected PrintStream old_stdout=System.out;
protected PrintStream old_stderr=System.err;
+ protected final ConcurrentMap<Class<?>, DataOutputStream> tests=new ConcurrentHashMap<Class<?>,DataOutputStream>(100);
+
public static final InheritableThreadLocal<PrintStream> stdout=new InheritableThreadLocal<PrintStream>();
public static final InheritableThreadLocal<PrintStream> stderr=new InheritableThreadLocal<PrintStream>();
- public static final InheritableThreadLocal<DataOutputStream> tests=new InheritableThreadLocal<DataOutputStream>();
@@ -52,6 +55,9 @@ public void onStart(ITestContext context) {
/** Invoked after all test classes in this test have been run */
public void onFinish(ITestContext context) {
try {
+ for(DataOutputStream out: tests.values())
+ Util.close(out);
+ tests.clear();
generateReports();
}
catch(IOException e) {
@@ -109,9 +115,9 @@ 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));
- closeStreams();
+ addTest(real_class,tr);
+ print(out,message,real_class.getName(),getMethodName(tr));
+ closeStreams(real_class);
}
protected void setupStreams(ITestResult result, boolean printMethodName) {
@@ -121,8 +127,16 @@ protected void setupStreams(ITestResult result, boolean printMethodName) {
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)));
+ Class<?> clazz=result.getTestClass().getRealClass();
+ if(!tests.containsKey(clazz)) {
+ DataOutputStream output=new DataOutputStream(new FileOutputStream(_tests,true));
+ DataOutputStream tmp=tests.putIfAbsent(clazz, output);
+ if(tmp != null) {
+ Util.close(output);
+ output=tmp;
+ }
+ }
+
if(stdout.get() == null)
stdout.set(new PrintStream(new FileOutputStream(_stdout, true)));
if(stderr.get() == null)
@@ -135,13 +149,14 @@ protected void setupStreams(ITestResult result, boolean printMethodName) {
}
}
- protected static void closeStreams() {
+ protected static void closeStreams(Class<?> clazz) {
Util.close(stdout.get());
stdout.set(null);
Util.close(stderr.get());
stderr.set(null);
- Util.close(tests.get());
- tests.set(null);
+ // DataOutputStream output=tests.get(clazz);
+ // Util.close(output);
+ // tests.remove(clazz);
}
protected static void print(PrintStream out, String msg, String classname, String method_name) {
@@ -176,7 +191,8 @@ protected void addTest(Class<?> clazz, ITestResult result) {
}
synchronized(this) { // handle concurrent access by different threads, if test methods are run in parallel
- test_case.writeTo(tests.get());
+ DataOutputStream output=tests.get(clazz);
+ test_case.writeTo(output);
}
}
catch(Exception e) {
@@ -579,7 +595,7 @@ public String toString() {
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);
+ sb.append("\n" + failure_type).append(" msg=" + failure_msg).append("\n").append(stack_trace);
return sb.toString();
}
View
7 tests/junit-functional/org/jgroups/protocols/NAKACK_StressTest.java
@@ -6,10 +6,7 @@
import org.jgroups.protocols.pbcast.NakAckHeader2;
import org.jgroups.stack.Protocol;
import org.jgroups.util.MutableDigest;
-import org.jgroups.util.TimeScheduler;
-import org.jgroups.util.TimeScheduler2;
import org.jgroups.util.Util;
-import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.ArrayList;
@@ -194,7 +191,7 @@ public void run() {
}
- @Test(enabled=false)
+ /*@Test(enabled=false)
public static void main(String[] args) {
int num_threads=10;
int num_msgs=1000000;
@@ -217,5 +214,5 @@ public static void main(String[] args) {
return;
}
start(num_threads, num_msgs, oob);
- }
+ }*/
}
View
11 tests/junit-functional/org/jgroups/tests/UNICAST_Test.java
@@ -61,17 +61,6 @@ public void testReceptionOfAllMessagesWithDISCARD(Protocol prot) throws Throwabl
};
}
- //@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) {
ByteBuffer buf=ByteBuffer.allocate(size);
View
3 tests/junit/org/jgroups/blocks/ExecutingServiceTest.java
@@ -53,7 +53,7 @@
*/
@Test(groups=Global.STACK_DEPENDENT,sequential=true)
public class ExecutingServiceTest extends ChannelTestBase {
- protected static Log logger= LogFactory.getLog(ExecutingServiceTest.class);
+ protected static Log logger=null;
protected ExposedExecutingProtocol exposedProtocol;
protected JChannel c1, c2, c3;
@@ -62,6 +62,7 @@
@BeforeClass
protected void init() throws Exception {
+ logger = LogFactory.getLog(ExecutingServiceTest.class);
c1=createChannel(true, 3, "A");
// Add the exposed executing protocol
View
15 tests/junit/org/jgroups/blocks/RpcDispatcherUnicastMethodExceptionTest.java
@@ -29,28 +29,23 @@ protected void tearDown() throws Exception {
channel.close();
}
- @Test(enabled=false)
- public static Object foo() {
+ static Object foo() {
return "foo(): OK";
}
- @Test(enabled=false)
- public static Object bar() throws Exception {
+ static Object bar() throws Exception {
throw new TimeoutException("this is an exception");
}
- @Test(enabled=false)
- public static Object foobar() {
+ static Object foobar() {
throw new IllegalArgumentException("bla bla bla from foobar");
}
- @Test(enabled=false)
- public static Object foofoobar() {
+ static Object foofoobar() {
throw new AssertionError("bla bla bla from foofoobar");
}
- @Test(enabled=false)
- public static void fooWithThrowable() throws Throwable {
+ static void fooWithThrowable() throws Throwable {
throw new Throwable("this is an exception");
}
View
4 tests/junit/org/jgroups/tests/FlushTest.java
@@ -165,8 +165,8 @@ public void testFlushWithCrashedFlushCoordinator() throws Exception {
// will be started
Util.shutdown(c2);
- c1.getProtocolStack().findProtocol(FLUSH.class).setLevel("trace");
- c3.getProtocolStack().findProtocol(FLUSH.class).setLevel("trace");
+ c1.getProtocolStack().findProtocol(FLUSH.class).setLevel("debug");
+ c3.getProtocolStack().findProtocol(FLUSH.class).setLevel("debug");
Util.waitUntilAllChannelsHaveSameSize(10000, 500, c1, c3);
View
4 tests/other/org/jgroups/tests/PingPongDatagram.java
@@ -24,8 +24,8 @@
static final byte PING = 1;
static final byte PONG = 2;
- static final byte[] PING_REQ=new byte[]{PING};
- static final byte[] PONG_RSP=new byte[]{PONG};
+ static final byte[] PING_REQ={PING};
+ static final byte[] PONG_RSP={PONG};
long start=0;

0 comments on commit 66b820f

Please sign in to comment.
Something went wrong with that request. Please try again.