001: /*
002: * $Id: LazyInitProxyFactoryTest.java 460933 2006-06-05 13:58:16Z janne $
003: * $Revision: 460933 $
004: * $Date: 2006-06-05 15:58:16 +0200 (Mon, 05 Jun 2006) $
005: *
006: * ==============================================================================
007: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
008: * use this file except in compliance with the License. You may obtain a copy of
009: * the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
015: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
016: * License for the specific language governing permissions and limitations under
017: * the License.
018: */
019: package wicket.proxy;
020:
021: import java.lang.reflect.Proxy;
022:
023: import junit.framework.TestCase;
024:
025: import wicket.proxy.LazyInitProxyFactory.ProxyReplacement;
026: import wicket.proxy.util.ConcreteObject;
027: import wicket.proxy.util.IInterface;
028: import wicket.proxy.util.IObjectMethodTester;
029: import wicket.proxy.util.InterfaceObject;
030: import wicket.proxy.util.ObjectMethodTester;
031: import wicket.util.lang.Objects;
032: import wicket.util.tester.WicketTester;
033:
034: /**
035: * Tests lazy init proxy factory
036: *
037: * @author Igor Vaynberg (ivaynberg)
038: *
039: */
040: public class LazyInitProxyFactoryTest extends TestCase {
041:
042: protected void setUp() throws Exception {
043: new WicketTester(null);
044: }
045:
046: private static InterfaceObject interfaceObject = new InterfaceObject(
047: "interface");
048:
049: private static ConcreteObject concreteObject = new ConcreteObject(
050: "concrete");
051:
052: private static IProxyTargetLocator interfaceObjectLocator = new IProxyTargetLocator() {
053: public Object locateProxyTarget() {
054: return LazyInitProxyFactoryTest.interfaceObject;
055: }
056: };
057:
058: private static IProxyTargetLocator concreteObjectLocator = new IProxyTargetLocator() {
059: public Object locateProxyTarget() {
060: return LazyInitProxyFactoryTest.concreteObject;
061: }
062: };
063:
064: /**
065: * Tests lazy init proxy to represent interfaces
066: */
067: public void testInterfaceProxy() {
068: // test proxy creation for an interface class
069: IInterface proxy = (IInterface) LazyInitProxyFactory
070: .createProxy(IInterface.class, interfaceObjectLocator);
071:
072: // test we have a jdk dynamic proxy
073: assertTrue(Proxy.isProxyClass(proxy.getClass()));
074:
075: // test proxy implements ILazyInitProxy
076: assertTrue(proxy instanceof ILazyInitProxy);
077: assertTrue(((ILazyInitProxy) proxy).getObjectLocator() == interfaceObjectLocator);
078:
079: // test method invocation
080: assertEquals(proxy.getMessage(), "interface");
081:
082: // test serialization
083: IInterface proxy2 = (IInterface) Objects.cloneObject(proxy);
084: assertTrue(proxy != proxy2);
085: assertEquals(proxy2.getMessage(), "interface");
086:
087: // test equals/hashcode method interception
088: final IObjectMethodTester tester = new ObjectMethodTester();
089: assertTrue(tester.isValid());
090:
091: IProxyTargetLocator testerLocator = new IProxyTargetLocator() {
092: public Object locateProxyTarget() {
093: return tester;
094: }
095: };
096:
097: IObjectMethodTester testerProxy = (IObjectMethodTester) LazyInitProxyFactory
098: .createProxy(IObjectMethodTester.class, testerLocator);
099: testerProxy.equals(this );
100: testerProxy.hashCode();
101: testerProxy.toString();
102: assertTrue(tester.isValid());
103: }
104:
105: /**
106: * Tests lazy init proxy to represent concrete objects
107: */
108: public void testConcreteProxy() {
109: ConcreteObject proxy = (ConcreteObject) LazyInitProxyFactory
110: .createProxy(ConcreteObject.class,
111: concreteObjectLocator);
112:
113: // test proxy implements ILazyInitProxy
114: assertTrue(proxy instanceof ILazyInitProxy);
115: assertTrue(((ILazyInitProxy) proxy).getObjectLocator() == concreteObjectLocator);
116:
117: // test we do not have a jdk dynamic proxy
118: assertFalse(Proxy.isProxyClass(proxy.getClass()));
119:
120: // test method invocation
121: assertEquals(proxy.getMessage(), "concrete");
122:
123: // test serialization
124: ConcreteObject proxy2 = (ConcreteObject) Objects
125: .cloneObject(proxy);
126: assertTrue(proxy != proxy2);
127: assertEquals(proxy2.getMessage(), "concrete");
128:
129: // test equals/hashcode method interception
130: final IObjectMethodTester tester = new ObjectMethodTester();
131: assertTrue(tester.isValid());
132:
133: IProxyTargetLocator testerLocator = new IProxyTargetLocator() {
134: public Object locateProxyTarget() {
135: return tester;
136: }
137: };
138:
139: ObjectMethodTester testerProxy = (ObjectMethodTester) LazyInitProxyFactory
140: .createProxy(ObjectMethodTester.class, testerLocator);
141: testerProxy.equals(this );
142: testerProxy.hashCode();
143: testerProxy.toString();
144: assertTrue(tester.isValid());
145: }
146:
147: /**
148: * Tests lazy init concrete replacement replacement
149: */
150: public void testCGLibInterceptorReplacement() {
151: ProxyReplacement ser = new ProxyReplacement(
152: ConcreteObject.class.getName(), concreteObjectLocator);
153:
154: ConcreteObject proxy2 = (ConcreteObject) Objects
155: .cloneObject(ser);
156: assertEquals(proxy2.getMessage(), "concrete");
157: }
158:
159: }
|