001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.aop.bean;
023:
024: import org.jboss.aop.Advised;
025: import org.jboss.aop.InstanceAdvisor;
026: import org.jboss.aop.metadata.ThreadMetaData;
027: import org.jboss.logging.Logger;
028: import org.jboss.system.ServiceMBeanSupport;
029:
030: import javax.management.MBeanRegistration;
031: import javax.management.MBeanServer;
032: import javax.management.ObjectName;
033:
034: /**
035: *
036: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
037: * @version $Revision: 57211 $
038: */
039: public class AOPTester extends ServiceMBeanSupport implements
040: AOPTesterMBean, MBeanRegistration {
041: // Constants ----------------------------------------------------
042: // Attributes ---------------------------------------------------
043: static Logger log = Logger.getLogger(AOPTester.class);
044: MBeanServer m_mbeanServer;
045:
046: // Static -------------------------------------------------------
047:
048: // Constructors -------------------------------------------------
049: public AOPTester() {
050: }
051:
052: // Public -------------------------------------------------------
053:
054: // MBeanRegistration implementation -----------------------------------
055: public ObjectName preRegister(MBeanServer server, ObjectName name)
056: throws Exception {
057: m_mbeanServer = server;
058: return name;
059: }
060:
061: public void postRegister(Boolean registrationDone) {
062: }
063:
064: public void preDeregister() throws Exception {
065: }
066:
067: public void postDeregister() {
068: }
069:
070: protected void startService() throws Exception {
071: // this is to load up the management console so that we can view everything
072: try {
073: testBasic();
074: testInheritance();
075: testMetadata();
076: testDynamicInterceptors();
077: testFieldInterception();
078: testMethodInterception();
079: testConstructorInterception();
080: testExceptions();
081: testMixin();
082: testCallerPointcut();
083: } catch (Exception ignored) {
084: }
085:
086: }
087:
088: protected void stopService() {
089: }
090:
091: public void testBasic() {
092: log.info("RUNNING TEST BASIC");
093: try {
094: POJO pojo = new POJO();
095: if (!(pojo instanceof org.jboss.aop.Advised))
096: throw new RuntimeException(
097: "POJO is not instanceof Advised");
098: SimpleInterceptor.lastIntercepted = null;
099: SimpleInterceptor.lastTransAttributeAccessed = null;
100: pojo.someMethod();
101: if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
102: throw new RuntimeException(
103: "Failed on interception test");
104: if (!"RequiresNew"
105: .equals(SimpleInterceptor.lastTransAttributeAccessed))
106: throw new RuntimeException("Failed on metadata test");
107:
108: InstanceOfInterceptor.intercepted = false;
109: Implements1 impl1 = new Implements1();
110: if (InstanceOfInterceptor.intercepted == false)
111: throw new RuntimeException(
112: "failed all(instanceof) constructor interception");
113: InstanceOfInterceptor.intercepted = false;
114: impl1.foo = 1;
115: if (InstanceOfInterceptor.intercepted == false)
116: throw new RuntimeException(
117: "failed all(instanceof) field interception");
118: InstanceOfInterceptor.intercepted = false;
119: impl1.someMethod();
120: if (InstanceOfInterceptor.intercepted == false)
121: throw new RuntimeException(
122: "failed all(instanceof) method interception");
123:
124: InstanceOfInterceptor.intercepted = false;
125: Implements2 impl2 = new Implements2();
126: if (InstanceOfInterceptor.intercepted == true)
127: throw new RuntimeException(
128: "failed method only (instanceof) constructor interception");
129: InstanceOfInterceptor.intercepted = false;
130: impl2.someMethod();
131: if (InstanceOfInterceptor.intercepted == false)
132: throw new RuntimeException(
133: "failed method only(instanceof) method interception");
134: InstanceOfInterceptor.intercepted = false;
135:
136: CFlowedPOJO cflow = new CFlowedPOJO();
137: InterceptorCounter.count = 0;
138: cflow.method3();
139: if (InterceptorCounter.count > 0)
140: throw new RuntimeException(
141: "method3 count should be null");
142: InterceptorCounter.count = 0;
143: cflow.method1();
144: if (InterceptorCounter.count != 1)
145: throw new RuntimeException("method1 count should be 1");
146: InterceptorCounter.count = 0;
147: cflow.recursive(1);
148: if (InterceptorCounter.count == 0)
149: throw new RuntimeException(
150: "recursive never get intercepted");
151: if (InterceptorCounter.count > 1)
152: throw new RuntimeException(
153: "recursive too many interceptions");
154: } catch (Throwable ex) {
155: log.error("failed", ex);
156: ex.printStackTrace();
157: throw new RuntimeException(ex.getMessage());
158: }
159: }
160:
161: public void testInheritance() {
162: log.info("RUNNING TEST INHERITANCE");
163: try {
164: SimpleInterceptor.lastIntercepted = null;
165: SimpleInterceptor.lastTransAttributeAccessed = null;
166: POJOChild pojo = new POJOChild();
167: pojo.someMethod2();
168: if (!"someMethod2"
169: .equals(SimpleInterceptor.lastIntercepted))
170: throw new RuntimeException(
171: "Failed on interception test");
172: if (!"RequiresNew"
173: .equals(SimpleInterceptor.lastTransAttributeAccessed))
174: throw new RuntimeException("Failed on metadata test");
175:
176: SimpleInterceptor.lastIntercepted = null;
177: SimpleInterceptor.lastTransAttributeAccessed = null;
178: pojo.someMethod();
179: if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
180: throw new RuntimeException(
181: "Failed on interception test");
182: if (!"RequiresNew"
183: .equals(SimpleInterceptor.lastTransAttributeAccessed))
184: throw new RuntimeException("Failed on metadata test");
185:
186: } catch (Throwable ex) {
187: log.error("failed", ex);
188: throw new RuntimeException(ex);
189: }
190: }
191:
192: public void testMetadata() {
193: log.info("RUNNING TEST METADATA");
194:
195: try {
196: POJOChild pojo = new POJOChild();
197: SimpleInterceptor.lastIntercepted = null;
198: SimpleInterceptor.lastTransAttributeAccessed = null;
199: pojo.someMethod();
200: if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
201: throw new RuntimeException(
202: "Failed on interception test");
203: if (!"RequiresNew"
204: .equals(SimpleInterceptor.lastTransAttributeAccessed))
205: throw new RuntimeException("Failed on metadata test");
206:
207: SimpleInterceptor.lastIntercepted = null;
208: SimpleInterceptor.lastTransAttributeAccessed = null;
209: pojo.anotherMethod();
210: if (!"anotherMethod"
211: .equals(SimpleInterceptor.lastIntercepted))
212: throw new RuntimeException(
213: "Failed on interception test");
214: if (!"Required"
215: .equals(SimpleInterceptor.lastTransAttributeAccessed))
216: throw new RuntimeException("Failed on metadata test");
217:
218: SimpleInterceptor.lastIntercepted = null;
219: SimpleInterceptor.lastTransAttributeAccessed = null;
220: pojo.someMethod2();
221: if (!"someMethod2"
222: .equals(SimpleInterceptor.lastIntercepted))
223: throw new RuntimeException(
224: "Failed on interception test");
225: if (!"RequiresNew"
226: .equals(SimpleInterceptor.lastTransAttributeAccessed))
227: throw new RuntimeException("Failed on metadata test");
228:
229: SimpleInterceptor.lastIntercepted = null;
230: SimpleInterceptor.lastTransAttributeAccessed = null;
231: pojo.someMethod3();
232: if (!"someMethod3"
233: .equals(SimpleInterceptor.lastIntercepted))
234: throw new RuntimeException(
235: "Failed on interception test");
236: if (!"Supports"
237: .equals(SimpleInterceptor.lastTransAttributeAccessed))
238: throw new RuntimeException("Failed on metadata test");
239:
240: SimpleInterceptor.lastIntercepted = null;
241: SimpleInterceptor.lastTransAttributeAccessed = null;
242: org.jboss.aop.metadata.ThreadMetaData.instance()
243: .addMetaData("transaction", "trans-attribute",
244: "Never");
245: pojo.someMethod3();
246: if (!"someMethod3"
247: .equals(SimpleInterceptor.lastIntercepted))
248: throw new RuntimeException(
249: "Failed on interception test");
250: if (!"Never"
251: .equals(SimpleInterceptor.lastTransAttributeAccessed))
252: throw new RuntimeException("Failed on metadata test");
253: org.jboss.aop.metadata.ThreadMetaData.instance().clear();
254:
255: SimpleInterceptor.lastIntercepted = null;
256: SimpleInterceptor.lastTransAttributeAccessed = null;
257: InstanceAdvisor instanceAdvisor = ((Advised) pojo)
258: ._getInstanceAdvisor();
259: instanceAdvisor.getMetaData().addMetaData("transaction",
260: "trans-attribute", "NotSupported");
261: pojo.someMethod3();
262: if (!"someMethod3"
263: .equals(SimpleInterceptor.lastIntercepted))
264: throw new RuntimeException(
265: "Failed on interception test");
266: if (!"NotSupported"
267: .equals(SimpleInterceptor.lastTransAttributeAccessed))
268: throw new RuntimeException("Failed on metadata test");
269: org.jboss.aop.metadata.ThreadMetaData.instance().clear();
270:
271: } catch (Throwable ex) {
272: log.error("failed", ex);
273: throw new RuntimeException(ex);
274: }
275:
276: }
277:
278: public void testDynamicInterceptors() {
279: log.info("RUNNING TEST DYNAMIC INTERCEPTORS");
280: try {
281: POJOChild pojo = new POJOChild();
282: SimpleInterceptor.lastIntercepted = null;
283: SimpleInterceptor.lastTransAttributeAccessed = null;
284: BeforeInterceptor.lastIntercepted = null;
285: BeforeInterceptor.lastTransAttributeAccessed = null;
286: ((Advised) pojo)._getInstanceAdvisor().insertInterceptor(
287: new BeforeInterceptor());
288: pojo.someMethod();
289: if (!"someMethod".equals(SimpleInterceptor.lastIntercepted))
290: throw new RuntimeException(
291: "Failed on interception test");
292: if (!"RequiresNew"
293: .equals(SimpleInterceptor.lastTransAttributeAccessed))
294: throw new RuntimeException("Failed on metadata test");
295: if (!"someMethod".equals(BeforeInterceptor.lastIntercepted))
296: throw new RuntimeException(
297: "Failed on interception test");
298: if (!"RequiresNew"
299: .equals(BeforeInterceptor.lastTransAttributeAccessed))
300: throw new RuntimeException("Failed on metadata test");
301:
302: SimpleInterceptor.lastIntercepted = null;
303: SimpleInterceptor.lastTransAttributeAccessed = null;
304: BeforeInterceptor.lastIntercepted = null;
305: BeforeInterceptor.lastTransAttributeAccessed = null;
306: AfterInterceptor.lastIntercepted = null;
307: AfterInterceptor.lastTransAttributeAccessed = null;
308: ((Advised) pojo)._getInstanceAdvisor().appendInterceptor(
309: new AfterInterceptor());
310: pojo.someMethod();
311: if (!"someMethod".equals(BeforeInterceptor.lastIntercepted))
312: throw new RuntimeException(
313: "Failed on interception test");
314: if (!"RequiresNew"
315: .equals(BeforeInterceptor.lastTransAttributeAccessed))
316: throw new RuntimeException("Failed on metadata test");
317: if (!"someMethod".equals(AfterInterceptor.lastIntercepted))
318: throw new RuntimeException(
319: "Failed on interception test");
320: if (!"RequiresNew"
321: .equals(AfterInterceptor.lastTransAttributeAccessed))
322: throw new RuntimeException("Failed on metadata test");
323:
324: } catch (Throwable ex) {
325: log.error("failed", ex);
326: throw new RuntimeException(ex);
327: }
328:
329: }
330:
331: public void testFieldInterception() {
332: log.info("RUNNING TEST FIELD INTERCEPTION");
333: try {
334:
335: POJO pojo = new POJO();
336: SimpleInterceptor.lastFieldIntercepted = null;
337: SimpleInterceptor.lastFieldTransAttributeAccessed = null;
338: pojo.accessField();
339:
340: if (!"privateField"
341: .equals(SimpleInterceptor.lastFieldIntercepted))
342: throw new RuntimeException(
343: "Failed on interception test");
344: if (!"NotSupported"
345: .equals(SimpleInterceptor.lastFieldTransAttributeAccessed))
346: throw new RuntimeException("Failed on metadata test");
347:
348: POJOChild child = new POJOChild();
349: SimpleInterceptor.lastFieldIntercepted = null;
350: SimpleInterceptor.lastFieldTransAttributeAccessed = null;
351: child.accessField();
352: if (!"privateField"
353: .equals(SimpleInterceptor.lastFieldIntercepted))
354: throw new RuntimeException(
355: "Failed on interception test");
356: if (!"NotSupported"
357: .equals(SimpleInterceptor.lastFieldTransAttributeAccessed))
358: throw new RuntimeException("Failed on metadata test");
359:
360: SimpleInterceptor.lastFieldIntercepted = null;
361: SimpleInterceptor.lastFieldTransAttributeAccessed = null;
362: child.accessProtectedField();
363: if (!"protectedField"
364: .equals(SimpleInterceptor.lastFieldIntercepted))
365: throw new RuntimeException(
366: "Failed on interception test");
367: if (!"Supports"
368: .equals(SimpleInterceptor.lastFieldTransAttributeAccessed))
369: throw new RuntimeException("Failed on metadata test");
370:
371: POJORef ref = new POJORef();
372: SimpleInterceptor.lastFieldIntercepted = null;
373: SimpleInterceptor.lastFieldTransAttributeAccessed = null;
374: ref.refPOJO();
375:
376: if (!"protectedField"
377: .equals(SimpleInterceptor.lastFieldIntercepted))
378: throw new RuntimeException(
379: "Failed on interception test");
380: if (!"Supports"
381: .equals(SimpleInterceptor.lastFieldTransAttributeAccessed))
382: throw new RuntimeException("Failed on metadata test");
383:
384: pojo.accessStaticField();
385:
386: } catch (Throwable ex) {
387: log.error("failed", ex);
388: throw new RuntimeException(ex);
389: }
390: }
391:
392: public void testMethodInterception() {
393: System.out.println("RUNNING METHOD INTERCEPTION");
394: try {
395: POJO.staticMethod();
396: POJOConstructorTest vanilla;
397: vanilla = new POJOConstructorTest();
398:
399: vanilla.data = "error";
400: vanilla.someMethod();
401: if (!vanilla.data.equals("someMethod"))
402: throw new RuntimeException(
403: "someMethod() didn't get correct method metadata");
404:
405: vanilla.data = "error";
406: vanilla.another();
407: if (!vanilla.data.equals("another()"))
408: throw new RuntimeException(
409: "another() didn't get correct method metadata: "
410: + vanilla.data);
411:
412: vanilla.data = "nothing";
413: POJOMethodInterceptor.wasHit = false;
414: vanilla.another(1);
415: if (POJOMethodInterceptor.wasHit)
416: throw new RuntimeException(
417: "interceptor should not have been called");
418: if (!vanilla.data.equals("nothing"))
419: throw new RuntimeException(
420: "another(int) shouldn't get intercepted: "
421: + vanilla.data);
422:
423: vanilla.data = "nothing";
424: vanilla.another(1, 1);
425: if (!vanilla.data.equals("another(int, int)"))
426: throw new RuntimeException(
427: "another(int, int) didn't get intercepted: "
428: + vanilla.data);
429: } catch (Throwable ex) {
430: ex.printStackTrace();
431: throw new RuntimeException(ex.getMessage());
432: }
433: }
434:
435: public void testAspect() {
436: System.out.println("RUNNING ASPECT TEST");
437: try {
438: POJO.staticMethod();
439: POJOAspectTester vanilla;
440: vanilla = new POJOAspectTester();
441: if (!vanilla.marker.equals("interceptConstructor"))
442: throw new RuntimeException(
443: "vanilla constructor didn't get intercepted");
444:
445: vanilla.marker = "error";
446: vanilla.someMethod();
447: if (!vanilla.marker.equals("interceptMethod"))
448: throw new RuntimeException(
449: "vanilla.someMethod() didn't get intercepted");
450:
451: vanilla.marker = "error";
452: vanilla.field = 5;
453: if (!vanilla.marker.equals("interceptField"))
454: throw new RuntimeException(
455: "vanilla.field didn't get intercepted");
456:
457: } catch (Throwable ex) {
458: ex.printStackTrace();
459: throw new RuntimeException(ex);
460: }
461: }
462:
463: public void testConstructorInterception() {
464: System.out.println("RUNNING CONSTRUCTOR INTERCEPTION");
465: try {
466:
467: POJO pojo = new POJO();
468: POJOChild child = new POJOChild();
469:
470: POJORef ref = new POJORef();
471: ref.constructPOJO();
472:
473: POJOWildCardConstructorTest wild;
474: wild = new POJOWildCardConstructorTest();
475: if (wild == null)
476: throw new RuntimeException("wild was null!");
477: if (wild.data.equals("error"))
478: throw new RuntimeException("wild() didn't intercept");
479: wild = new POJOWildCardConstructorTest(1);
480: if (wild.data.equals("error"))
481: throw new RuntimeException("wild(int) didn't intercept");
482:
483: POJOConstructorTest vanilla;
484: vanilla = new POJOConstructorTest();
485: if (vanilla == null)
486: throw new RuntimeException("vanilla was null!");
487: if (vanilla.data.equals("error"))
488: throw new RuntimeException("vanilla() didn't intercept");
489: if (!vanilla.data.equals("empty"))
490: throw new RuntimeException(
491: "vanilla() didn't get correct constructor metadata");
492: vanilla = new POJOConstructorTest(1, 1);
493: if (vanilla.data.equals("error"))
494: throw new RuntimeException(
495: "vanilla(int, int) didn't intercept");
496: if (!vanilla.data.equals("int, int"))
497: throw new RuntimeException(
498: "vanilla(int, int) didn't get correct constructor metadata");
499: vanilla = new POJOConstructorTest(1);
500: if (!vanilla.data.equals("error"))
501: throw new RuntimeException(
502: "vanilla(int) did intercept when it shouldn't have");
503:
504: } catch (Throwable ex) {
505: ex.printStackTrace();
506: throw new RuntimeException(ex.getMessage());
507: }
508: }
509:
510: public void testExceptions() {
511: log.info("TEST AOP EXCEPTIONS");
512: try {
513: NoInterceptorsPOJO pojo = new NoInterceptorsPOJO();
514:
515: pojo.throwException();
516:
517: } catch (SomeException ignored) {
518: log.info("caught SomeException successfully");
519: }
520: try {
521: POJO pojo = new POJO();
522:
523: pojo.throwException();
524: } catch (SomeException ignored) {
525: log.info("caught SomeException successfully");
526: }
527: }
528:
529: public void testMixin() {
530: try {
531: log.info("TEST MIXIN");
532: POJO pojo = new POJO();
533: log.info("TEST Introduction");
534: Introduction intro = (Introduction) pojo;
535: log.info(intro.helloWorld("world"));
536: log.info("TEST Introduction2");
537: Introduction2 intro2 = (Introduction2) pojo;
538: log.info(intro2.goodbye("world"));
539: log.info("TEST InterfaceMixin");
540: InterfaceMixin mixin = (InterfaceMixin) pojo;
541: log.info(mixin.whazup());
542:
543: POJOChild child = new POJOChild();
544: log.info("TEST child Introduction");
545: intro = (Introduction) child;
546: log.info(intro.helloWorld("world"));
547: log.info("TEST child Introduction2");
548: intro2 = (Introduction2) child;
549: log.info(intro2.goodbye("world"));
550: log.info("TEST child AnotherIntroduction");
551: SubclassIntroduction sub = (SubclassIntroduction) child;
552: log.info(sub.subclassHelloWorld("world"));
553: log.info("TEST metadata introduction pointcut");
554: NoInterceptorsPOJO nopojo = new NoInterceptorsPOJO();
555: intro = (Introduction) nopojo;
556:
557: } catch (Exception ex) {
558: ex.printStackTrace();
559: throw new RuntimeException(ex);
560: }
561: }
562:
563: public void testCallerPointcut() {
564: log.info("TEST CALLER");
565: CallingPOJO callingPOJO = new CallingPOJO();
566: callingPOJO.callSomeMethod();
567: callingPOJO.nocallSomeMethod();
568: callingPOJO.callUnadvised();
569: }
570:
571: // Inner classes -------------------------------------------------
572: }
|