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.scoped;
023:
024: import org.jboss.aop.Advised;
025: import org.jboss.test.aop.scoped.excluded.ExcludedPOJO;
026: import org.jboss.test.aop.scoped.excluded.included.IncludedPOJO;
027:
028: /**
029: *
030: * @author <a href="kabir.khan@jboss.com">Kabir Khan</a>
031: * @version $Revision$
032: */
033: public class ScopedTester implements ScopedTesterMBean {
034: int expectedInterceptorValue;
035: int expectedAspectValue;
036: int metadataSuffix;
037:
038: String ctorPlainMetadata;
039: String methodPlainMetadata;
040: String customMetadata;
041:
042: public void setExpectedInterceptorValue(int i) {
043: expectedInterceptorValue = i;
044: }
045:
046: public void setExpectedAspectValue(int i) {
047: expectedAspectValue = i;
048: }
049:
050: public int getExpectedInterceptorValue() {
051: return expectedInterceptorValue;
052: }
053:
054: public int getExpectedAspectValue() {
055: return expectedAspectValue;
056: }
057:
058: public void setMetadataSuffix(int i) {
059: metadataSuffix = i;
060: ctorPlainMetadata = "ctor" + metadataSuffix;
061: methodPlainMetadata = "method" + metadataSuffix;
062: customMetadata = "custom" + metadataSuffix;
063: }
064:
065: public void testExpectedValues() {
066: if (ScopedInterceptor.value != expectedInterceptorValue) {
067: throw new RuntimeException("Expected Interceptor value "
068: + expectedInterceptorValue + ", was "
069: + ScopedInterceptor.value);
070: }
071: if (ScopedAspect.value != expectedAspectValue) {
072: throw new RuntimeException("Expected Aspect value "
073: + expectedAspectValue + ", was "
074: + ScopedAspect.value);
075: }
076:
077: }
078:
079: public void testScoped() throws Exception {
080: System.out
081: .println("--------------------------- TESTING SCOPED ------------------");
082: System.out.println("MY CLASSLOADER "
083: + getClass().getClassLoader());
084: System.out.println("SCOPED INTERCEPTOR CLASSLOADER "
085: + ScopedInterceptor.class.getClassLoader());
086:
087: System.out.println("------- CTOR");
088: ScopedAspect.intercepted = 0;
089: ScopedFactoryAspect.intercepted = 0;
090: ScopedFactoryAspect.metadata = null;
091: ScopedFactoryAspect.customMetadata = null;
092:
093: POJO pojo = new POJO();
094: if (ScopedAspect.intercepted != 1) {
095: throw new RuntimeException(
096: "Expected ScopedAspect 1 for POJO constructor, was "
097: + ScopedAspect.intercepted);
098: }
099: if (ScopedFactoryAspect.intercepted != 1) {
100: throw new RuntimeException(
101: "Expected ScopedFactoryAspect 1 for POJO constructor, was "
102: + ScopedFactoryAspect.intercepted);
103: }
104: if (!ctorPlainMetadata.equals(ScopedFactoryAspect.metadata)) {
105: throw new RuntimeException("Expected ctor metadata "
106: + ctorPlainMetadata + ", was "
107: + ScopedFactoryAspect.metadata);
108: }
109: if (!customMetadata.equals(ScopedFactoryAspect.customMetadata)) {
110: throw new RuntimeException(
111: "Expected ctor customm metadata " + customMetadata
112: + ", was "
113: + ScopedFactoryAspect.customMetadata);
114: }
115:
116: System.out.println("------- METHOD");
117: ScopedInterceptor.intercepted = 0;
118: ScopedAspect.intercepted = 0;
119: ScopedFactoryAspect.intercepted = 0;
120: ScopedFactoryAspect.metadata = null;
121: ScopedFactoryAspect.customMetadata = null;
122:
123: pojo.method();
124: if (ScopedInterceptor.intercepted != 1) {
125: throw new RuntimeException(
126: "Expected ScopedInterceptor 1 for POJO method, was "
127: + ScopedInterceptor.intercepted);
128: }
129: if (ScopedAspect.intercepted != 1) {
130: throw new RuntimeException(
131: "Expected ScopedAspect 1 for POJO method, was "
132: + ScopedAspect.intercepted);
133: }
134: if (ScopedFactoryAspect.intercepted != 1) {
135: throw new RuntimeException(
136: "Expected ScopedFactoryAspect 1 for POJO method, was "
137: + ScopedFactoryAspect.intercepted);
138: }
139: if (!methodPlainMetadata.equals(ScopedFactoryAspect.metadata)) {
140: throw new RuntimeException("Expected method metadata '"
141: + methodPlainMetadata + ", was "
142: + ScopedFactoryAspect.metadata);
143: }
144: if (!customMetadata.equals(ScopedFactoryAspect.customMetadata)) {
145: throw new RuntimeException(
146: "Expected method customm metadata "
147: + customMetadata + ", was "
148: + ScopedFactoryAspect.customMetadata);
149: }
150: }
151:
152: public void testAnnotatedScopedAnnotationsDeployed() {
153: AnnotatedInterceptor.invoked = false;
154: POJO pojo = new POJO();
155: if (!AnnotatedInterceptor.invoked)
156: throw new RuntimeException(
157: "AnnotatedInterceptor should have been invoked");
158: }
159:
160: public void testAnnotatedScopedAnnotationsNotDeployed() {
161: try {
162: AnnotatedInterceptor.invoked = false;
163: throw new RuntimeException(
164: "AnnotatedInterceptor should not be available in this deployment");
165: } catch (NoClassDefFoundError expected) {
166: }
167: }
168:
169: public void testIntroduction1() {
170: IntroducedPOJO pojo = new IntroducedPOJO();
171: Scope1Interface iface = (Scope1Interface) pojo;
172:
173: ScopedInterceptor.intercepted = 0;
174: iface.testMethod();
175: if (ScopedInterceptor.intercepted != 1) {
176: throw new RuntimeException(
177: "Expected ScopedInterceptor 1 for introduction "
178: + ScopedInterceptor.intercepted);
179: }
180:
181: try {
182: Scope2Interface iface2 = (Scope2Interface) pojo;
183: throw new RuntimeException(
184: "Should not be implementing Scope2Interface");
185: } catch (ClassCastException expected) {
186: }
187: }
188:
189: public void testIntroduction2() {
190: IntroducedPOJO pojo = new IntroducedPOJO();
191: Scope2Interface iface = (Scope2Interface) pojo;
192:
193: ScopedInterceptor.intercepted = 0;
194: iface.testMethod();
195: if (ScopedInterceptor.intercepted != 1) {
196: throw new RuntimeException(
197: "Expected ScopedInterceptor 1 for introduction "
198: + ScopedInterceptor.intercepted);
199: }
200:
201: try {
202: Scope1Interface iface2 = (Scope1Interface) pojo;
203: throw new RuntimeException(
204: "Should not be implementing Scope1Interface");
205: } catch (ClassCastException expected) {
206: }
207: }
208:
209: public void testInclude() {
210: if (!Advised.class.isAssignableFrom(IncludedPOJO.class)) {
211: throw new RuntimeException("IncludedPOJO should be Advised");
212: }
213: }
214:
215: public void testExclude() {
216: if (Advised.class.isAssignableFrom(ExcludedPOJO.class)) {
217: throw new RuntimeException(
218: "ExcludedPOJO should not be Advised");
219: }
220: }
221:
222: public void testIgnore() {
223: if (Advised.class.isAssignableFrom(POJO$$Ignored$$123.class)) {
224: throw new RuntimeException(
225: "POJO$$Ignored$$123 should not be Advised");
226: }
227: }
228: }
|