[jboss-cvs] JBossAS SVN: r86099 - in projects/ejb3/trunk/endpoint: src/main/java/org/jboss/ejb3/endpoint/reflect and 5 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Mar 19 06:29:34 EDT 2009
Author: wolfc
Date: 2009-03-19 06:29:34 -0400 (Thu, 19 Mar 2009)
New Revision: 86099
Added:
projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/equality/
projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/equality/unit/
projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/equality/unit/EqualityTestCase.java
projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/javassist/
projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/javassist/MethodHandlerAdapter.java
Modified:
projects/ejb3/trunk/endpoint/pom.xml
projects/ejb3/trunk/endpoint/src/main/java/org/jboss/ejb3/endpoint/reflect/EndpointInvocationHandler.java
projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/invocation/unit/InvocationTestCase.java
Log:
EJBTHREE-1772: implement equals, hashCode and toString on invocation handler
Modified: projects/ejb3/trunk/endpoint/pom.xml
===================================================================
--- projects/ejb3/trunk/endpoint/pom.xml 2009-03-19 08:22:03 UTC (rev 86098)
+++ projects/ejb3/trunk/endpoint/pom.xml 2009-03-19 10:29:34 UTC (rev 86099)
@@ -21,6 +21,13 @@
<dependencies>
<dependency>
+ <groupId>javassist</groupId>
+ <artifactId>javassist</artifactId>
+ <version>3.10.0.GA</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
Modified: projects/ejb3/trunk/endpoint/src/main/java/org/jboss/ejb3/endpoint/reflect/EndpointInvocationHandler.java
===================================================================
--- projects/ejb3/trunk/endpoint/src/main/java/org/jboss/ejb3/endpoint/reflect/EndpointInvocationHandler.java 2009-03-19 08:22:03 UTC (rev 86098)
+++ projects/ejb3/trunk/endpoint/src/main/java/org/jboss/ejb3/endpoint/reflect/EndpointInvocationHandler.java 2009-03-19 10:29:34 UTC (rev 86099)
@@ -24,6 +24,7 @@
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
import org.jboss.ejb3.endpoint.Endpoint;
@@ -35,6 +36,28 @@
*/
public class EndpointInvocationHandler implements InvocationHandler
{
+ private static final Method METHOD_EQUALS;
+ private static final Method METHOD_HASH_CODE;
+ private static final Method METHOD_TO_STRING;
+
+ static
+ {
+ try
+ {
+ METHOD_EQUALS = Object.class.getDeclaredMethod("equals", Object.class);
+ METHOD_HASH_CODE = Object.class.getDeclaredMethod("hashCode");
+ METHOD_TO_STRING = Object.class.getDeclaredMethod("toString");
+ }
+ catch (SecurityException e)
+ {
+ throw new RuntimeException(e);
+ }
+ catch (NoSuchMethodException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
private Endpoint endpoint;
private Serializable session;
private Class<?> invokedBusinessInterface;
@@ -51,8 +74,88 @@
this.invokedBusinessInterface = invokedBusinessInterface; // can be null;
}
+ /* (non-Javadoc)
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj)
+ {
+ if(obj == this)
+ return true;
+
+ if(obj == null)
+ return false;
+
+ if(Proxy.isProxyClass(obj.getClass()))
+ return equals(Proxy.getInvocationHandler(obj));
+
+ // It might not be a JDK proxy that's handling this, so here we do a little trick.
+ // Normally you would do:
+ // if(!(obj instanceof EndpointInvocationHandler))
+ // return false;
+ if(!(obj instanceof EndpointInvocationHandler))
+ return obj.equals(this);
+
+ EndpointInvocationHandler other = (EndpointInvocationHandler) obj;
+
+ if(!(other.endpoint.equals(this.endpoint)))
+ return false;
+
+ if(!equals(other.session, this.session))
+ return false;
+
+ if(!equals(other.invokedBusinessInterface, this.invokedBusinessInterface))
+ return false;
+
+ return true;
+ }
+
+ private static boolean equals(Object obj1, Object obj2)
+ {
+ if(obj1 == obj2)
+ return true;
+
+ if(obj1 == null || obj2 == null)
+ return false;
+
+ return obj1.equals(obj2);
+ }
+
+ @Override
+ public int hashCode()
+ {
+ int hashCode = endpoint.hashCode();
+ if(session != null)
+ hashCode += (session.hashCode() << 2);
+ if(invokedBusinessInterface != null)
+ hashCode += (invokedBusinessInterface.hashCode() << 4);
+ return hashCode;
+ }
+
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
+ if(method.equals(METHOD_EQUALS))
+ return equals(args[0]);
+ if(method.equals(METHOD_HASH_CODE))
+ return hashCode();
+ if(method.equals(METHOD_TO_STRING))
+ return toProxyString();
return endpoint.invoke(session, invokedBusinessInterface, method, args);
}
+
+ public String toProxyString()
+ {
+ return "Proxy on " + toString();
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuffer sb = new StringBuffer(super.toString());
+ sb.append("{endpoint=" + endpoint);
+ sb.append(",invokedBusinessInterface=" + invokedBusinessInterface);
+ sb.append(",session=" + session);
+ sb.append("}");
+ return sb.toString();
+ }
}
Added: projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/equality/unit/EqualityTestCase.java
===================================================================
--- projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/equality/unit/EqualityTestCase.java (rev 0)
+++ projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/equality/unit/EqualityTestCase.java 2009-03-19 10:29:34 UTC (rev 86099)
@@ -0,0 +1,272 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.endpoint.test.equality.unit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.Serializable;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.UUID;
+
+import javassist.util.proxy.ProxyFactory;
+
+import org.jboss.ejb3.endpoint.Endpoint;
+import org.jboss.ejb3.endpoint.reflect.EndpointInvocationHandler;
+import org.jboss.ejb3.endpoint.reflect.EndpointProxy;
+import org.jboss.ejb3.endpoint.test.javassist.MethodHandlerAdapter;
+import org.junit.Test;
+
+/**
+ * Invoking equals, hashCode and toString on a proxy must not result in
+ * a call to the Endpoint.
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class EqualityTestCase
+{
+ private class SimpleEndpoint implements Endpoint
+ {
+ public Object invoke(Serializable session, Class<?> invokedBusinessInterface, Method method, Object[] args)
+ throws Throwable
+ {
+ if(!method.getName().equals("sayHi"))
+ fail("Invalid call to method " + method);
+ return "Hi " + args[0];
+ }
+ }
+
+ private interface SimpleInterface
+ {
+ String sayHi(String name);
+ }
+
+ private static int createHashCode(Endpoint endpoint, Serializable session, Class<?> invokedBusinessInterface)
+ {
+ int hashCode = endpoint.hashCode();
+ if(session != null)
+ hashCode += (session.hashCode() << 2);
+ if(invokedBusinessInterface != null)
+ hashCode += (invokedBusinessInterface.hashCode() << 4);
+ return hashCode;
+ }
+
+ @Test
+ public void testEquals() throws Exception
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = SimpleInterface.class;
+ SimpleInterface proxy1 = EndpointProxy.newProxyInstance(loader, session, businessInterface, endpoint);
+ SimpleInterface proxy2 = EndpointProxy.newProxyInstance(loader, session, businessInterface, endpoint);
+ assertTrue(proxy1.equals(proxy2));
+ }
+
+ @Test
+ public void testEqualsDifferentBusinessInterface() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = UUID.randomUUID();
+ Class<SimpleInterface> businessInterface1 = SimpleInterface.class;
+ Class<?> businessInterface2 = null;
+ InvocationHandler handler1 = new EndpointInvocationHandler(endpoint, session, businessInterface1);
+ InvocationHandler handler2 = new EndpointInvocationHandler(endpoint, session, businessInterface2);
+ assertFalse(handler1.equals(handler2));
+ }
+
+ @Test
+ public void testEqualsDifferentProxyProviders() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = SimpleInterface.class;
+ InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+
+ ProxyFactory factory = new ProxyFactory();
+ factory.setHandler(new MethodHandlerAdapter(handler));
+ factory.setInterfaces(new Class[] { businessInterface });
+ SimpleInterface javassistProxy = (SimpleInterface) factory.create(null, null);
+
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ SimpleInterface proxy = EndpointProxy.newProxyInstance(loader, session, businessInterface, endpoint);
+
+ assertTrue(javassistProxy.equals(proxy));
+ assertTrue(proxy.equals(javassistProxy));
+ }
+
+ @Test
+ public void testEqualsDifferentSession() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session1 = UUID.randomUUID();
+ Serializable session2 = UUID.randomUUID();
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler1 = new EndpointInvocationHandler(endpoint, session1, businessInterface);
+ InvocationHandler handler2 = new EndpointInvocationHandler(endpoint, session2, businessInterface);
+ assertFalse(handler1.equals(handler2));
+ }
+
+ @Test
+ public void testEqualsDummyObject() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+ assertFalse(handler.equals(new Object()));
+ }
+
+ @Test
+ public void testEqualsNull() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = null;
+ EndpointInvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+ assertFalse(handler.equals(null));
+ }
+
+ @Test
+ public void testEqualsOtherEndpoint() throws Exception
+ {
+ Endpoint endpoint1 = new SimpleEndpoint();
+ Endpoint endpoint2 = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler1 = new EndpointInvocationHandler(endpoint1, session, businessInterface);
+ InvocationHandler handler2 = new EndpointInvocationHandler(endpoint2, session, businessInterface);
+ assertFalse(handler1.equals(handler2));
+ }
+
+ @Test
+ public void testEqualsSame() throws Exception
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = SimpleInterface.class;
+ SimpleInterface proxy = EndpointProxy.newProxyInstance(loader, session, businessInterface, endpoint);
+ assertTrue(proxy.equals(proxy));
+ }
+
+ @Test
+ public void testEqualsSameSession() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session1 = UUID.randomUUID();
+ Serializable session2 = session1;
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler1 = new EndpointInvocationHandler(endpoint, session1, businessInterface);
+ InvocationHandler handler2 = new EndpointInvocationHandler(endpoint, session2, businessInterface);
+ assertTrue(handler1.equals(handler2));
+ }
+
+ @Test
+ public void testEqualsWithAndWithoutSession() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session1 = UUID.randomUUID();
+ Serializable session2 = null;
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler1 = new EndpointInvocationHandler(endpoint, session1, businessInterface);
+ InvocationHandler handler2 = new EndpointInvocationHandler(endpoint, session2, businessInterface);
+ assertFalse(handler1.equals(handler2));
+ }
+
+ @Test
+ public void testEqualsWithoutAndWithSession() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session1 = null;
+ Serializable session2 = UUID.randomUUID();
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler1 = new EndpointInvocationHandler(endpoint, session1, businessInterface);
+ InvocationHandler handler2 = new EndpointInvocationHandler(endpoint, session2, businessInterface);
+ assertFalse(handler1.equals(handler2));
+ }
+
+ @Test
+ public void testHashCode() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+ int result = handler.hashCode();
+ assertEquals(createHashCode(endpoint, session, businessInterface), result);
+ }
+
+ @Test
+ public void testHashCodeWithBusinessInterface() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = SimpleInterface.class;
+ InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+ int result = handler.hashCode();
+ assertEquals(createHashCode(endpoint, session, businessInterface), result);
+ assertFalse(createHashCode(endpoint, session, null) == result);
+ }
+
+ @Test
+ public void testHashCodeWithSession() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = UUID.randomUUID();
+ Class<SimpleInterface> businessInterface = null;
+ InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+ int result = handler.hashCode();
+ assertEquals(createHashCode(endpoint, session, businessInterface), result);
+ assertFalse(createHashCode(endpoint, null, businessInterface) == result);
+ }
+
+ @Test
+ public void testHashCodeWithSessionAndBusinessInterface() throws Exception
+ {
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = UUID.randomUUID();
+ Class<SimpleInterface> businessInterface = SimpleInterface.class;
+ InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, businessInterface);
+ int result = handler.hashCode();
+ assertEquals(createHashCode(endpoint, session, businessInterface), result);
+ assertFalse(createHashCode(endpoint, null, businessInterface) == result);
+ assertFalse(createHashCode(endpoint, session, null) == result);
+ assertFalse(createHashCode(endpoint, null, null) == result);
+ }
+
+ @Test
+ public void testToString() throws Exception
+ {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ Endpoint endpoint = new SimpleEndpoint();
+ Serializable session = null;
+ Class<SimpleInterface> businessInterface = SimpleInterface.class;
+ SimpleInterface proxy = EndpointProxy.newProxyInstance(loader, session, businessInterface, endpoint);
+ String result = proxy.toString();
+ assertTrue(result.startsWith("Proxy on " + EndpointInvocationHandler.class.getName()));
+ }
+}
Modified: projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/invocation/unit/InvocationTestCase.java
===================================================================
--- projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/invocation/unit/InvocationTestCase.java 2009-03-19 08:22:03 UTC (rev 86098)
+++ projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/invocation/unit/InvocationTestCase.java 2009-03-19 10:29:34 UTC (rev 86099)
@@ -55,7 +55,8 @@
Class<?> invokedBusinessInterface = null;
InvocationHandler handler = new EndpointInvocationHandler(endpoint, session, invokedBusinessInterface);
Object proxy = null;
- Method method = null;
+ // just make sure method is not null
+ Method method = InvocationTestCase.class.getDeclaredMethod("testInvocation");
Date now = new Date();
Object args[] = { now };
Object result = handler.invoke(proxy, method, args);
Added: projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/javassist/MethodHandlerAdapter.java
===================================================================
--- projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/javassist/MethodHandlerAdapter.java (rev 0)
+++ projects/ejb3/trunk/endpoint/src/test/java/org/jboss/ejb3/endpoint/test/javassist/MethodHandlerAdapter.java 2009-03-19 10:29:34 UTC (rev 86099)
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.endpoint.test.javassist;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+
+import javassist.util.proxy.MethodHandler;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class MethodHandlerAdapter implements MethodHandler
+{
+ private InvocationHandler handler;
+
+ public MethodHandlerAdapter(InvocationHandler handler)
+ {
+ this.handler = handler;
+ }
+
+ public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
+ {
+ return handler.invoke(self, thisMethod, args);
+ }
+}
More information about the jboss-cvs-commits
mailing list