final Cheese cheese = new Cheese( originalType,
originalPrice );
// creating proxy
final Class proxy = ShadowProxyFactory.getProxy( CheeseInterface.class );
final CheeseInterface cheeseProxy = (CheeseInterface) proxy.getConstructor( new Class[]{CheeseInterface.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() );
// 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() );
} catch ( final Exception e ) {
fail( "Error: " + e.getMessage() );
}
}