jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1144388 - in /jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3: TestBase.java TestLock.java TestNodeTypeRegistry.java
Date Fri, 08 Jul 2011 17:15:22 GMT
Author: thomasm
Date: Fri Jul  8 17:15:22 2011
New Revision: 1144388

URL: http://svn.apache.org/viewvc?rev=1144388&view=rev
Log:
assertThrows(myObject).methodCall()

Modified:
    jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestBase.java
    jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestLock.java
    jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestNodeTypeRegistry.java

Modified: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestBase.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestBase.java?rev=1144388&r1=1144387&r2=1144388&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestBase.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestBase.java
Fri Jul  8 17:15:22 2011
@@ -17,6 +17,10 @@
 package org.apache.jackrabbit.j3;
 
 import java.io.File;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.util.HashMap;
 import java.util.Map;
 import javax.jcr.LoginException;
@@ -88,4 +92,64 @@ public abstract class TestBase extends T
         return Integer.parseInt(System.getProperty("test.scale", "1"));
     }
 
+    protected <T> T assertThrows(final Class<?> exceptionClass, final T obj)
{
+        return assertThrows(new Thread.UncaughtExceptionHandler() {
+            public void uncaughtException(Thread t, Throwable e) {
+                if (!exceptionClass.isAssignableFrom(e.getClass())) {
+                    e.printStackTrace();
+                    fail("Expected: " + exceptionClass + ", got: " + e);
+                }
+            }
+        }, exceptionClass.toString(), obj);
+    }
+
+    @SuppressWarnings("unchecked")
+    protected
+    <T> T assertThrows(final Thread.UncaughtExceptionHandler handler, final String
expected, final T obj) {
+        Class<?> c = obj.getClass();
+        return (T) Proxy.newProxyInstance(c.getClassLoader(),
+                c.getInterfaces(), new InvocationHandler() {
+            private Exception called = new Exception("No method called");
+            public void finalize() {
+                if (called != null) {
+                    called.printStackTrace(System.err);
+                }
+            }
+            public Object invoke(Object proxy, Method method, Object[] args) throws Exception
{
+                try {
+                    called = null;
+                    Object ret = method.invoke(obj, args);
+                    fail(method.getDeclaringClass().getName() + "." + method.getName() +
+                            " did not throw an exception of type " + expected +
+                            ", but returned " + ret);
+                    return ret;
+                } catch (InvocationTargetException e) {
+                    handler.uncaughtException(null, e.getTargetException());
+                    Class<?> retClass = method.getReturnType();
+                    if (!retClass.isPrimitive()) {
+                        return null;
+                    }
+                    if (retClass == boolean.class) {
+                        return false;
+                    } else if (retClass == byte.class) {
+                        return (byte) 0;
+                    } else if (retClass == char.class) {
+                        return (char) 0;
+                    } else if (retClass == short.class) {
+                        return (short) 0;
+                    } else if (retClass == int.class) {
+                        return 0;
+                    } else if (retClass == long.class) {
+                        return 0L;
+                    } else if (retClass == float.class) {
+                        return 0F;
+                    } else if (retClass == double.class) {
+                        return 0D;
+                    }
+                    return null;
+                }
+            }
+        });
+    }
+
 }

Modified: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestLock.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestLock.java?rev=1144388&r1=1144387&r2=1144388&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestLock.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestLock.java
Fri Jul  8 17:15:22 2011
@@ -34,14 +34,8 @@ public class TestLock extends TestBase {
         lockManager.lock("/testLock", false, false, 0, "test");
         Session session2 = openSession();
         Node n2 = session2.getNode("/testLock");
-        try {
-            n2.addNode("test");
-            fail();
-        } catch (LockException e) {
-            // expected
-        }
+        assertThrows(LockException.class, n2).addNode("test");
         session2.logout();
-
         n.remove();
         session.save();
     }

Modified: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestNodeTypeRegistry.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestNodeTypeRegistry.java?rev=1144388&r1=1144387&r2=1144388&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestNodeTypeRegistry.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/TestNodeTypeRegistry.java
Fri Jul  8 17:15:22 2011
@@ -31,12 +31,7 @@ public class TestNodeTypeRegistry extend
 
     public void testUndefineBuiltIn() throws RepositoryException {
         NodeTypeManager m = session.getWorkspace().getNodeTypeManager();
-        try {
-            m.unregisterNodeType("nt:base");
-            fail();
-        } catch (RepositoryException e) {
-            // expected
-        }
+        assertThrows(RepositoryException.class, m).unregisterNodeType("nt:base");
     }
 
     public void testCndParser() throws Exception {



Mime
View raw message