KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > jdk15 > dynamic > common > ScenarioLoader


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.jdk15.dynamic.common;
23
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLClassLoader JavaDoc;
29 import java.security.AccessController JavaDoc;
30 import java.security.PrivilegedActionException JavaDoc;
31 import java.security.PrivilegedExceptionAction JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import org.jboss.aop.AspectManager;
36
37 /**
38  * Loads the scenario classes using a class loader. This assures that the scenario to
39  * be tested runs isolated.
40  * The isolation of scenarios is essencial for testing, because each scenario
41  * requires that the POJO and ScenarioRunner classes were not loaded before its execution.
42  * With scenario loader, we can run more than one scenario in a same test instance, avoiding
43  * to write a TestCase class for each scenario.
44  * @author Flavia Rainone
45  *
46  */

47 public class ScenarioLoader
48 {
49    private URL JavaDoc[] urls;
50    private ClassLoader JavaDoc classLoader;
51    private Object JavaDoc scenarioRunner;
52    private Class JavaDoc scenarioRunnerClass;
53    private Field JavaDoc totalInterceptions;
54    private Field JavaDoc fieldReadInterceptions;
55    private Field JavaDoc fieldWriteInterceptions;
56    private Field JavaDoc constructorInterceptions;
57    private Field JavaDoc methodInterceptions;
58    private boolean scenarioRunned = false;
59    
60    /**
61     * Constructor.
62     */

63    public ScenarioLoader(Properties JavaDoc properties) throws Exception JavaDoc
64    {
65       urls = new URL JavaDoc[]{new URL JavaDoc("file:" + properties.getProperty("scenario.jar"))};
66
67       System.out.println("URLS " + Arrays.asList(urls));
68       // create a new class loader
69
this.classLoader = new URLClassLoader JavaDoc(urls, this.getClass().getClassLoader());
70       // loads the scenario reflection data.
71
this.scenarioRunnerClass = classLoader.loadClass(getClass().getPackage().getName() + ".scenario.ScenarioRunner");
72       Class JavaDoc interceptionsCountClass = this.classLoader.loadClass(InterceptionsCount.class.getName());
73       this.totalInterceptions = interceptionsCountClass.getField("total");
74       this.fieldReadInterceptions = interceptionsCountClass.getField("fieldRead");
75       this.fieldWriteInterceptions = interceptionsCountClass.getField("fieldWrite");
76       this.constructorInterceptions = interceptionsCountClass.getField("constructor");
77       this.methodInterceptions = interceptionsCountClass.getField("method");
78       
79       this.scenarioRunner = scenarioRunnerClass.newInstance();
80    }
81
82    /**
83     * Runs the "interceptPerClassLoadBefore" scenario.
84     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#interceptPerClassLoadBefore()
85     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
86     */

87    public POJOWrappingInfo[] interceptPerClassLoadBefore() throws Throwable JavaDoc
88    {
89       return this.executeScenario("interceptPerClassLoadBefore");
90    }
91    
92    /**
93     * Runs the "interceptPerClassLoadAfter" scenario.
94     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#interceptPerClassLoadAfter()
95     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
96     */

97    public POJOWrappingInfo[] interceptPerClassLoadAfter() throws Throwable JavaDoc
98    {
99       return this.executeScenario("interceptPerClassLoadAfter");
100    }
101    
102    /**
103     * Runs the "addAndRemoveBindingTwice" scenario.
104     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#addAndRemoveBindingTwice()
105     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
106     */

107    public POJOWrappingInfo[] addAndRemoveBindingTwice() throws Throwable JavaDoc
108    {
109       return this.executeScenario("addAndRemoveBindingTwice");
110    }
111    
112    /**
113     * Runs the "executeAfterBindingRemoval" scenario.
114     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#executeAfterBindingRemoval()
115     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
116     */

117    public POJOWrappingInfo[] executeAfterBindingRemoval() throws Throwable JavaDoc
118    {
119       return this.executeScenario("executeAfterBindingRemoval");
120    }
121    
122    /**
123     * Runs the "executeAfterBindingRemoval" scenario.
124     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#executeAfterBindingRemoval()
125     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
126     */

127    public POJOWrappingInfo[] interceptPerInstance() throws Throwable JavaDoc {
128       return this.executeScenario("interceptPerInstance");
129    }
130    
131    /**
132     * Runs the "interceptPerInstanceGC" scenario.
133     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#interceptPerInstanceGC()
134     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
135     */

136    public POJOWrappingInfo[] interceptPerInstanceGC() throws Throwable JavaDoc
137    {
138       return this.executeScenario("interceptPerInstanceGC");
139    }
140    
141    /**
142     * Runs the "interceptPerClassPerInstance" scenario.
143     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#interceptPerClassPerInstance()
144     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
145     */

146    public POJOWrappingInfo[] interceptPerClassPerInstance() throws Throwable JavaDoc
147    {
148       return this.executeScenario("interceptPerClassPerInstance");
149    }
150    
151    /**
152     * Runs the "interceptPerInstancePerClass" scenario.
153     * @see org.jboss.test.aop.jdk15.dynamic.common.scenario.ScenarioRunner#interceptPerInstancePerClass()
154     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
155     */

156    public POJOWrappingInfo[] interceptPerInstancePerClass() throws Throwable JavaDoc
157    {
158       return this.executeScenario("interceptPerInstancePerClass");
159    }
160       
161    /**
162     * Executes a test scenario.
163     * @param scenarioName the name of the scenario to be executed.
164     * @return the pojo wrapping infos obtained in the key points of the scenario execution.
165     */

166    private POJOWrappingInfo[] executeScenario(String JavaDoc scenarioName) throws Throwable JavaDoc
167    {
168       if (scenarioRunned)
169          throw new RuntimeException JavaDoc("Should not run more than one scenario.");
170       Method JavaDoc method = this.scenarioRunnerClass.getMethod(scenarioName);
171       POJOWrappingInfo[] result = null;
172       try
173       {
174          result = (POJOWrappingInfo[]) method.invoke(scenarioRunner, new Object JavaDoc[0]);
175       } catch (InvocationTargetException JavaDoc e)
176       {
177          throw e.getCause();
178       }
179       scenarioRunned = true;
180       this.removeClassLoaderFromAspectManager();
181       return result;
182    }
183    
184    /**
185     * Avoids conflicts. JBoss AOP uses a "unified class loader" structure in the class pools.
186     * This means that it sees the javassist ctclasses used in the previous scenarios executions
187     * and uses this classes instead of creating a new one. The intent of this
188     * class is to allow the running more than one scenario in a same java virtual
189     * machine instance as if each scenario runner was running in a different java virtual
190     * machine.
191     */

192    public void removeClassLoaderFromAspectManager()
193    {
194       AspectManager.instance().unregisterClassLoader(classLoader);
195    }
196 }
Popular Tags