public void testProxyForClassWithEquals() {
try {
// creating original object
final String originalType = "stilton";
final int originalPrice = 15;
final CheeseEqual cheese = new CheeseEqual( originalType,
originalPrice );
// creating proxy
final Class proxy = ShadowProxyFactory.getProxy( CheeseEqual.class );
final CheeseEqual cheeseProxy = (CheeseEqual) proxy.getConstructor( new Class[]{CheeseEqual.class} ).newInstance( new Object[]{cheese} );
// proxy is proxying the values
Assert.assertEquals( originalType,
cheeseProxy.getType() );
Assert.assertEquals( originalPrice,
cheeseProxy.getPrice() );
Assert.assertSame( cheese,
((ShadowProxy) cheeseProxy).getShadowedObject() );
// proxy must recongnize the original object on equals()/hashcode() calls
//Assert.assertEquals( cheeseProxy.hashCode(), cheese.hashCode() );
Assert.assertEquals( cheeseProxy,
cheese );
// changing original values
final String actualType = "rotten stilton";
final int actualPrice = 1;
cheese.setType( actualType );
cheese.setPrice( actualPrice );
// proxy does not see changes
Assert.assertEquals( actualType,
cheese.getType() );
Assert.assertFalse( actualType.equals( cheeseProxy.getType() ) );
Assert.assertEquals( originalType,
cheeseProxy.getType() );
Assert.assertEquals( actualPrice,
cheese.getPrice() );
Assert.assertFalse( actualPrice == cheeseProxy.getPrice() );
Assert.assertEquals( originalPrice,
cheeseProxy.getPrice() );
// reseting proxy
((ShadowProxy) cheeseProxy).updateProxy();
// now proxy see changes
Assert.assertEquals( actualType,
cheese.getType() );
Assert.assertEquals( actualType,
cheeseProxy.getType() );
Assert.assertFalse( originalType.equals( cheeseProxy.getType() ) );
Assert.assertEquals( actualPrice,
cheese.getPrice() );
Assert.assertEquals( actualPrice,
cheeseProxy.getPrice() );
Assert.assertFalse( originalPrice == cheeseProxy.getPrice() );
// Another cheese
final CheeseEqual cheese2 = new CheeseEqual( "brie",
10 );
final CheeseEqual cheese2Proxy = (CheeseEqual) proxy.getConstructor( new Class[]{CheeseEqual.class} ).newInstance( new Object[]{cheese2} );
assertFalse( cheeseProxy.equals( cheese2Proxy ) );
assertFalse( cheese2Proxy.equals( cheeseProxy ) );
} catch ( final Exception e ) {
fail( "Error: " + e.getMessage() );
}
}