From: erik Date: Sat, 17 Jul 2010 20:32:05 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.3~26 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=b66272d6899e88bb3990149b4b01ad8cd5b4a168;p=utils --- diff --git a/test/enterprise/pom.xml b/test/enterprise/pom.xml index 60676401..1d9cd680 100644 --- a/test/enterprise/pom.xml +++ b/test/enterprise/pom.xml @@ -25,7 +25,6 @@ org.wamblee wamblee-support-general 0.2.5-SNAPSHOT - test javax.transaction diff --git a/test/enterprise/src/main/java/org/wamblee/test/transactions/ThreadSpecificProxyFactory.java b/test/enterprise/src/main/java/org/wamblee/test/transactions/ThreadSpecificProxyFactory.java deleted file mode 100644 index 78f44367..00000000 --- a/test/enterprise/src/main/java/org/wamblee/test/transactions/ThreadSpecificProxyFactory.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.test.transactions; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -/** - * Thread-specific proxy is used to create implementations of interfaces that - * delegate to a thread-specific implementation of the service. - * - * It is used for instance to pass a transaction scoped entity manager around. - * - * The {@link #set(Object)} method sets the current service instance for the current thread. - * The {@link #get()} method gets the current service instance for the current thread. - * The {@link #getProxy()} method gets a proxy that will delegate at runtime to the thread-specific - * instance. The result from this method can be passed at construction of an object that will be used - * by multiple threads. - * - * This class is mostly used by other test tools. - * - * @param T - * Interface to proxy. - * @author Erik Brakkee - * - */ -public class ThreadSpecificProxyFactory { - private class ThreadSpecificInvocationHandler implements InvocationHandler { - - @Override - public Object invoke(Object aProxy, Method aMethod, Object[] aArgs) - throws Throwable { - try { - return aMethod.invoke(svc.get(), aArgs); - } catch (InvocationTargetException e) { - throw e.getCause(); - } - } - } - - private ThreadLocal svc = new ThreadLocal(); - private Class clazz; - - /** - * Constructs the factory. - * - * @param aClass - * Interface class of the service to proxy. - */ - public ThreadSpecificProxyFactory(Class aClass) { - if (!aClass.isInterface()) { - throw new IllegalArgumentException("Class " + aClass.getName() + - " is not an interface"); - } - clazz = aClass; - } - - /** - * Sets the thread-specific service. - * - * @param aService - * Service, use null value to reset. - */ - public void set(T aService) { - svc.set(aService); - } - - /** - * Gets the current thread-specific service. - * @return Service. - */ - public T get() { - return svc.get(); - } - - /** - * Gets the proxy that delegates to the thread-specific instance set by - * {@link #set(Object)} - * - * @return Proxy. - */ - public T getProxy() { - InvocationHandler handler = new ThreadSpecificInvocationHandler(); - Class proxyClass = Proxy.getProxyClass(clazz.getClassLoader(), - new Class[] { clazz }); - T proxy; - try { - proxy = (T) proxyClass.getConstructor( - new Class[] { InvocationHandler.class }).newInstance( - new Object[] { handler }); - return proxy; - } catch (Exception e) { - throw new RuntimeException("Could not create proxy for " + - clazz.getName(), e); - } - } -} diff --git a/test/enterprise/src/main/java/org/wamblee/test/transactions/TransactionProxyFactory.java b/test/enterprise/src/main/java/org/wamblee/test/transactions/TransactionProxyFactory.java index 9aa5cef8..e8d08483 100644 --- a/test/enterprise/src/main/java/org/wamblee/test/transactions/TransactionProxyFactory.java +++ b/test/enterprise/src/main/java/org/wamblee/test/transactions/TransactionProxyFactory.java @@ -23,6 +23,7 @@ import java.lang.reflect.Proxy; import javax.persistence.EntityManager; +import org.wamblee.general.ThreadSpecificProxyFactory; import org.wamblee.test.persistence.JpaBuilder; import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork; diff --git a/test/enterprise/src/test/java/org/wamblee/test/transactions/ThreadSpecificProxyFactoryTest.java b/test/enterprise/src/test/java/org/wamblee/test/transactions/ThreadSpecificProxyFactoryTest.java deleted file mode 100644 index c2f87977..00000000 --- a/test/enterprise/src/test/java/org/wamblee/test/transactions/ThreadSpecificProxyFactoryTest.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.test.transactions; - -import static junit.framework.Assert.*; -import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.wamblee.test.transactions.ThreadSpecificProxyFactory; - -public class ThreadSpecificProxyFactoryTest { - - private static interface Service { - int execute(int aX) throws Exception; - } - - private ThreadSpecificProxyFactory factory; - private Service proxy; - - @Before - public void setUp() { - factory = new ThreadSpecificProxyFactory(Service.class); - proxy = factory.getProxy(); - } - - @After - public void tearDown() { - // Empty. - } - - @Test(expected = NullPointerException.class) - public void testNoSvcDefined() throws Exception { - proxy.execute(10); - } - - @Test - public void testInvokeThroughProxy() throws Exception { - Service svc = mock(Service.class); - when(svc.execute(anyInt())).thenReturn(50); - factory.set(svc); - assertEquals(50, proxy.execute(10)); - verify(svc).execute(10); - } - - @Test - public void testInvokeThroughProxyWithException() throws Exception { - Service svc = mock(Service.class); - try { - when(svc.execute(anyInt())).thenThrow( - new RuntimeException("exception thrown")); - factory.set(svc); - svc.execute(10); - fail(); - } catch (RuntimeException e) { - assertEquals("exception thrown", e.getMessage()); - } - } - - private int returnFromThread; - - @Test - public void testVerifyThreadSpecificUsingTwoThreads() throws Exception { - Service svc1 = mock(Service.class); - final Service svc2 = mock(Service.class); - when(svc1.execute(anyInt())).thenReturn(10); - when(svc2.execute(anyInt())).thenReturn(20); - - factory.set(svc1); - assertEquals(10, svc1.execute(10)); - Thread t = new Thread() { - public void run() { - factory.set(svc2); - try { - returnFromThread = proxy.execute(100); - } catch (Exception e) { - returnFromThread = 100000; - } - }; - }; - t.start(); - t.join(); - assertEquals(20, returnFromThread); - assertEquals(10, proxy.execute(100)); - - } - - @Test(expected = IllegalArgumentException.class) - public void testNotAnInterface() { - ThreadSpecificProxyFactory f = new ThreadSpecificProxyFactory(String.class); - } -}