Skip to content

Commit

Permalink
Added equivalence tests for proxied objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Feb 20, 2013
1 parent a4c8537 commit 4447027
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
35 changes: 35 additions & 0 deletions proxy/src/main/java/org/jboss/forge/proxy/Proxies.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,41 @@ else if (type.getName().contains("_javassist_"))
typeName = type.getName();
}
return typeName;
}

/**
* This method tests if two proxied objects are equivalent.
*
* It does so by comparing the class names and the hashCode, since they may be loaded from different classloaders.
*
*/
public static boolean areEquivalent(Object proxiedObj, Object anotherProxiedObj)
{
if (proxiedObj == null && anotherProxiedObj == null)
{
return true;
}
else if (proxiedObj == null || anotherProxiedObj == null)
{
return false;
}
else
{
Object unproxiedObj = unwrap(proxiedObj);
Object anotherUnproxiedObj = unwrap(anotherProxiedObj);

boolean sameClassName = unwrapProxyClassName(unproxiedObj.getClass()).equals(
unwrapProxyClassName(anotherUnproxiedObj.getClass()));
boolean sameHashcode = (unproxiedObj.hashCode() == anotherUnproxiedObj.hashCode());
return sameClassName && sameHashcode;
}
}

/**
* Checks if a proxied object is an instance of the specified {@link Class}
*/
public static boolean isInstance(Class<?> type, Object proxiedObject)
{
return type.isInstance(unwrap(proxiedObject));
}
}
43 changes: 43 additions & 0 deletions proxy/src/test/java/org/jboss/forge/proxy/Bean.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,50 @@

public class Bean
{

private String att;

public Bean()
{
}

public void setAtt(String att)
{
this.att = att;
}

public String getAtt()
{
return att;
}

@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((att == null) ? 0 : att.hashCode());
return result;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Bean other = (Bean) obj;
if (att == null)
{
if (other.att != null)
return false;
}
else if (!att.equals(other.att))
return false;
return true;
}

}
47 changes: 47 additions & 0 deletions proxy/src/test/java/org/jboss/forge/proxy/ProxiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,55 @@ public Object getDelegate()
return null;
}
});
Assert.assertNotEquals(Bean.class.getName(), enhancedObj.getClass().getName());
String result = Proxies.unwrapProxyClassName(enhancedObj.getClass());
Assert.assertEquals(Bean.class.getName(), result);
}

@Test
public void testAreEquivalent()
{
Bean enhancedObj = Proxies.enhance(Bean.class, new ForgeProxy()
{

@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
{
return proceed.invoke(self, args);
}

@Override
public Object getDelegate()
{
return null;
}
});
enhancedObj.setAtt("String");
Bean bean2 = new Bean();
bean2.setAtt("String");

Assert.assertTrue(Proxies.areEquivalent(enhancedObj, bean2));
}

@Test
public void testIsInstance()
{
Bean enhancedObj = Proxies.enhance(Bean.class, new ForgeProxy()
{

@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable
{
return proceed.invoke(self, args);
}

@Override
public Object getDelegate()
{
return null;
}
});
Assert.assertTrue(Proxies.isInstance(Bean.class, enhancedObj));
}

}

0 comments on commit 4447027

Please sign in to comment.