KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > kernel > junit > MicrocontainerTestDelegate


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.kernel.junit;
23
24 import java.net.URL JavaDoc;
25
26 import org.jboss.dependency.spi.ControllerState;
27 import org.jboss.kernel.Kernel;
28 import org.jboss.kernel.plugins.bootstrap.AbstractBootstrap;
29 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
30 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
31 import org.jboss.kernel.spi.dependency.KernelController;
32 import org.jboss.kernel.spi.dependency.KernelControllerContext;
33 import org.jboss.kernel.spi.deployment.KernelDeployment;
34 import org.jboss.test.AbstractTestDelegate;
35
36 /**
37  * A MicrocontainerTestDelegate.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 57859 $
41  */

42 public class MicrocontainerTestDelegate extends AbstractTestDelegate
43 {
44    /** The kernel */
45    protected Kernel kernel;
46
47    /** The deployer */
48    protected BasicXMLDeployer deployer;
49    
50    /**
51     * Create a new MicrocontainerTestDelegate.
52     *
53     * @param clazz the test class
54     * @throws Exception for any error
55     */

56    public MicrocontainerTestDelegate(Class JavaDoc clazz) throws Exception JavaDoc
57    {
58       super(clazz);
59    }
60
61    public void setUp() throws Exception JavaDoc
62    {
63       super.setUp();
64       
65       try
66       {
67          // Bootstrap the kernel
68
AbstractBootstrap bootstrap = getBootstrap();
69          bootstrap.run();
70          kernel = bootstrap.getKernel();
71          
72          // Create the deployer
73
deployer = new BasicXMLDeployer(kernel);
74          
75          // Deploy
76
deploy();
77       }
78       catch (RuntimeException JavaDoc e)
79       {
80          throw e;
81       }
82       catch (Exception JavaDoc e)
83       {
84          throw e;
85       }
86       catch (Error JavaDoc e)
87       {
88          throw e;
89       }
90       catch (Throwable JavaDoc e)
91       {
92          throw new RuntimeException JavaDoc(e);
93       }
94    }
95
96    public void tearDown() throws Exception JavaDoc
97    {
98       super.tearDown();
99       undeploy();
100    }
101    
102    /**
103     * Get the kernel bootstrap
104     *
105     * @return the bootstrap
106     * @throws Exception for any error
107     */

108    protected AbstractBootstrap getBootstrap() throws Exception JavaDoc
109    {
110       return new BasicBootstrap();
111    }
112    
113    /**
114     * Get a bean
115     *
116     * @param name the name of the bean
117     * @param state the state of the bean
118     * @return the bean
119     * @throws IllegalStateException when the bean does not exist at that state
120     */

121    protected Object JavaDoc getBean(final Object JavaDoc name, final ControllerState state)
122    {
123       KernelControllerContext context = getControllerContext(name, state);
124       return context.getTarget();
125    }
126    
127    /**
128     * Get a context
129     *
130     * @param name the name of the bean
131     * @param state the state of the bean
132     * @return the context
133     * @throws IllegalStateException when the context does not exist at that state
134     */

135    protected KernelControllerContext getControllerContext(final Object JavaDoc name, final ControllerState state)
136    {
137       KernelController controller = kernel.getController();
138       KernelControllerContext context = (KernelControllerContext) controller.getContext(name, state);
139       if (context == null)
140          throw new IllegalStateException JavaDoc("Bean not found " + name + " at state " + state);
141       return context;
142    }
143    
144    /**
145     * Validate
146     *
147     * @throws Exception for any error
148     */

149    protected void validate() throws Exception JavaDoc
150    {
151       try
152       {
153          deployer.validate();
154       }
155       catch (RuntimeException JavaDoc e)
156       {
157          throw e;
158       }
159       catch (Exception JavaDoc e)
160       {
161          throw e;
162       }
163       catch (Error JavaDoc e)
164       {
165          throw e;
166       }
167       catch (Throwable JavaDoc t)
168       {
169          throw new RuntimeException JavaDoc(t);
170       }
171    }
172    
173    /**
174     * Deploy a url
175     *
176     * @param url the deployment url
177     * @return the deployment
178     * @throws Exception for any error
179     */

180    protected KernelDeployment deploy(URL JavaDoc url) throws Exception JavaDoc
181    {
182       try
183       {
184          log.debug("Deploying " + url);
185          KernelDeployment deployment = deployer.deploy(url);
186          log.trace("Deployed " + url);
187          return deployment;
188       }
189       catch (RuntimeException JavaDoc e)
190       {
191          throw e;
192       }
193       catch (Exception JavaDoc e)
194       {
195          throw e;
196       }
197       catch (Error JavaDoc e)
198       {
199          throw e;
200       }
201       catch (Throwable JavaDoc t)
202       {
203          throw new RuntimeException JavaDoc(t);
204       }
205    }
206    
207    /**
208     * Undeploy a deployment
209     *
210     * @param deployment the deployment
211     */

212    protected void undeploy(KernelDeployment deployment)
213    {
214       log.debug("Undeploying " + deployment.getName());
215       try
216       {
217          deployer.undeploy(deployment);
218          log.trace("Undeployed " + deployment.getName());
219       }
220       catch (Throwable JavaDoc t)
221       {
222          log.warn("Error during undeployment: " + deployment.getName(), t);
223       }
224    }
225    
226    /**
227     * Undeploy a deployment
228     *
229     * @param url the url
230     */

231    protected void undeploy(URL JavaDoc url)
232    {
233       log.debug("Undeploying " + url);
234       try
235       {
236          deployer.undeploy(url);
237          log.trace("Undeployed " + url);
238       }
239       catch (Throwable JavaDoc t)
240       {
241          log.warn("Error during undeployment: " + url, t);
242       }
243    }
244    
245    /**
246     * Deploy the beans
247     *
248     * @throws Exception for any error
249     */

250    protected void deploy() throws Exception JavaDoc
251    {
252       String JavaDoc testName = clazz.getName();
253       testName = testName.replace('.', '/') + ".xml";
254       URL JavaDoc url = clazz.getClassLoader().getResource(testName);
255       if (url != null)
256          deploy(url);
257       else
258          log.debug("No test specific deployment " + testName);
259    }
260
261    /**
262     * Undeploy all
263     */

264    protected void undeploy()
265    {
266       log.debug("Undeploying " + deployer.getDeploymentNames());
267       deployer.shutdown();
268    }
269 }
270
Popular Tags