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