001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest.spring.aop;
005:
006: import org.springframework.aop.framework.ProxyFactoryBean;
007:
008: import com.tc.test.TCTestCase;
009: import com.tcspring.FastAopProxy;
010:
011: // XXX use test decorator to activate AW pipeline
012: public class FastAopProxy_Test extends TCTestCase {
013: IMessageWriter target = null;
014: ProxyFactoryBean proxyFactoryBean = null;
015:
016: Object cglibProxied = null;
017: Object cglibProxied2 = null;
018:
019: Object jdkDynamicProxied = null;
020: Object jdkDynamicProxied2 = null;
021:
022: FastAopProxy fastProxy = null;
023:
024: public FastAopProxy_Test() {
025: disableAllUntil("2008-09-18"); // XXX timebombed
026: }
027:
028: public void setUp() {
029: target = new MessageWriter();
030:
031: proxyFactoryBean = new ProxyFactoryBean();
032: proxyFactoryBean.setFrozen(false);
033: proxyFactoryBean.setOptimize(true);
034: proxyFactoryBean.setTarget(target);
035: proxyFactoryBean.addAdvice(new SimpleBeforeAdvice());
036:
037: cglibProxied = proxyFactoryBean.getObject();
038:
039: proxyFactoryBean
040: .setInterfaces(new Class[] { IMessageWriter.class });
041: proxyFactoryBean.setOptimize(false);
042: jdkDynamicProxied = proxyFactoryBean.getObject();
043:
044: // get the 2nd copy
045:
046: proxyFactoryBean = new ProxyFactoryBean();
047: proxyFactoryBean.setFrozen(false);
048: proxyFactoryBean.setOptimize(true);
049: proxyFactoryBean.setTarget(target);
050: proxyFactoryBean.addAdvice(new SimpleBeforeAdvice());
051:
052: cglibProxied2 = proxyFactoryBean.getObject();
053:
054: proxyFactoryBean
055: .setInterfaces(new Class[] { IMessageWriter.class });
056: proxyFactoryBean.setOptimize(false);
057:
058: jdkDynamicProxied2 = proxyFactoryBean.getObject();
059:
060: // get the fast proxy
061:
062: fastProxy = new FastAopProxy(proxyFactoryBean);
063: }
064:
065: public void testClassLoader() throws Exception {
066: assertTrue(cglibProxied instanceof MessageWriter);
067:
068: assertTrue(jdkDynamicProxied instanceof IMessageWriter);
069: assertFalse(jdkDynamicProxied instanceof MessageWriter);
070:
071: Object fastProxied1 = fastProxy.getProxy();
072: Object fastProxied2 = fastProxy.getProxy(target.getClass()
073: .getClassLoader());
074: Object fastProxied3 = fastProxy.getProxy(new MyClassLoader(
075: target.getClass().getClassLoader()));
076:
077: assertTrue(fastProxied1 instanceof IMessageWriter);
078: assertFalse(fastProxied1 instanceof MessageWriter);
079:
080: assertTrue(fastProxied2 instanceof IMessageWriter);
081: assertFalse(fastProxied2 instanceof MessageWriter);
082:
083: assertTrue(fastProxied3 instanceof IMessageWriter);
084: assertFalse(fastProxied3 instanceof MessageWriter);
085: }
086:
087: // interesting to observation
088: public void testEquality() throws Exception {
089: assertFalse(jdkDynamicProxied.equals(jdkDynamicProxied2));
090:
091: assertFalse(cglibProxied.equals(cglibProxied2));
092:
093: Object fastProxied1 = fastProxy.getProxy();
094: assertFalse(fastProxied1.equals(fastProxy.getProxy()));
095: }
096:
097: public void testBeforeAdvice() throws Exception {
098: Logger.log = null;
099: String cglibProxiedRv = ((MessageWriter) cglibProxied)
100: .writeMessage();
101: String cglibProxiedLog = Logger.log;
102: assertEquals("World", cglibProxiedRv);
103: assertTrue(cglibProxiedLog.indexOf("*EMPTY*") > -1);
104:
105: Logger.log = null;
106: String jdkDynamicProxiedRv = ((IMessageWriter) cglibProxied)
107: .writeMessage();
108: String jdkDynamicProxiedLog = Logger.log;
109: assertEquals("World", jdkDynamicProxiedRv);
110: assertTrue(jdkDynamicProxiedLog.indexOf("*EMPTY*") > -1);
111:
112: Object fastProxied = fastProxy.getProxy();
113: Logger.log = null;
114: String fastProxiedRv = ((IMessageWriter) fastProxied)
115: .writeMessage();
116: String fastProxiedLog = Logger.log;
117: assertEquals("World", fastProxiedRv);
118: assertTrue("This is what we get: " + fastProxiedLog,
119: fastProxiedLog.indexOf("*EMPTY*") > -1);
120: }
121:
122: public void testNested1() {
123: ProxyFactoryBean factory = new ProxyFactoryBean();
124: factory.setFrozen(false);
125: factory.setOptimize(true);
126: factory.setInterfaces(new Class[] { IMessageWriter.class });
127: factory.setTarget(cglibProxied);
128:
129: FastAopProxy proxy = new FastAopProxy(factory);
130:
131: Object nested = proxy.getProxy();
132:
133: Logger.log = null;
134: String nestedRv = ((IMessageWriter) nested).writeMessage();
135: String nestedLog = Logger.log;
136: assertEquals("World", nestedRv);
137: }
138:
139: public void testNested2() {
140: ProxyFactoryBean factory = new ProxyFactoryBean();
141: factory.setFrozen(false);
142: factory.setOptimize(true);
143: factory.setInterfaces(new Class[] { IMessageWriter.class });
144: factory.setTarget(this .jdkDynamicProxied);
145:
146: FastAopProxy proxy = new FastAopProxy(factory);
147:
148: Object nested = proxy.getProxy();
149:
150: Logger.log = null;
151: String nestedRv = ((IMessageWriter) nested).writeMessage();
152: String nestedLog = Logger.log;
153: assertEquals("World", nestedRv);
154: }
155:
156: public void testNested3() {
157: ProxyFactoryBean factory = new ProxyFactoryBean();
158: factory.setFrozen(false);
159: factory.setOptimize(true);
160: factory.setInterfaces(new Class[] { IMessageWriter.class });
161: factory.setTarget(fastProxy.getProxy());
162:
163: FastAopProxy proxy = new FastAopProxy(factory);
164:
165: Object nested = proxy.getProxy();
166:
167: Logger.log = null;
168: String nestedRv = ((IMessageWriter) nested).writeMessage();
169: String nestedLog = Logger.log;
170: assertEquals("World", nestedRv);
171: }
172:
173: public void testNullConfig() {
174: try {
175: this .fastProxy = new FastAopProxy(null);
176: fastProxy.getProxy();
177: fail("Shouldn't allow null interceptors");
178: } catch (Exception ex) {
179: ex.printStackTrace();
180: }
181: }
182:
183: public void testNoTarget() {
184: proxyFactoryBean = new ProxyFactoryBean();
185: proxyFactoryBean.setFrozen(false);
186: proxyFactoryBean.setOptimize(true);
187: proxyFactoryBean
188: .setInterfaces(new Class[] { IMessageWriter.class });
189: proxyFactoryBean.addAdvice(new SimpleBeforeAdvice());
190:
191: try {
192: this .fastProxy = new FastAopProxy(proxyFactoryBean);
193: fastProxy.getProxy();
194: fail("Shouldn't allow null interceptors");
195: } catch (Exception ex) {
196: ex.printStackTrace();
197: }
198: }
199:
200: private static class MyClassLoader extends ClassLoader {
201: public MyClassLoader(ClassLoader parent) {
202: super(parent);
203: }
204: }
205:
206: }
|