KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > system > controller > integration > test > IntegrationTestDelegate


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, 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.system.controller.integration.test;
23
24 import java.net.URL JavaDoc;
25
26 import org.jboss.dependency.spi.ControllerContext;
27 import org.jboss.dependency.spi.ControllerState;
28 import org.jboss.kernel.Kernel;
29 import org.jboss.kernel.plugins.bootstrap.AbstractBootstrap;
30 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
31 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer;
32 import org.jboss.kernel.spi.dependency.KernelController;
33 import org.jboss.kernel.spi.deployment.KernelDeployment;
34 import org.jboss.system.ServiceController;
35 import org.jboss.system.ServiceControllerMBean;
36 import org.jboss.test.system.controller.NewControllerTestDelegate;
37
38 /**
39  * IntegrationTestDelegate.
40  *
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 1.1 $
43  */

44 public class IntegrationTestDelegate extends NewControllerTestDelegate
45 {
46    /** The kernel */
47    protected Kernel kernel;
48
49    /** The deployer */
50    protected BasicXMLDeployer beanDeployer;
51
52    public IntegrationTestDelegate(Class JavaDoc clazz)
53    {
54       super(clazz);
55    }
56    
57    public void setUp() throws Exception JavaDoc
58    {
59       super.setUp();
60       
61       try
62       {
63          // Create the deployer
64
beanDeployer = new BasicXMLDeployer(kernel);
65          
66          // Deploy
67
deployMC();
68          
69          validateMC();
70       }
71       catch (RuntimeException JavaDoc e)
72       {
73          throw e;
74       }
75       catch (Exception JavaDoc e)
76       {
77          throw e;
78       }
79       catch (Error JavaDoc e)
80       {
81          throw e;
82       }
83       catch (Throwable JavaDoc e)
84       {
85          throw new RuntimeException JavaDoc(e);
86       }
87    }
88
89    // TODO Temporary
90
protected boolean isValidateAtSetUp()
91    {
92       return false;
93    }
94    
95    public ServiceControllerMBean createServiceController() throws Exception JavaDoc
96    {
97       try
98       {
99          // Bootstrap the kernel
100
AbstractBootstrap bootstrap = getBootstrap();
101          bootstrap.run();
102          kernel = bootstrap.getKernel();
103          
104          // Create the service controller
105
ServiceController result = new ServiceController();
106          result.setKernel(kernel);
107          return result;
108       }
109       catch (RuntimeException JavaDoc e)
110       {
111          throw e;
112       }
113       catch (Exception JavaDoc e)
114       {
115          throw e;
116       }
117       catch (Error JavaDoc e)
118       {
119          throw e;
120       }
121       catch (Throwable JavaDoc e)
122       {
123          throw new RuntimeException JavaDoc(e);
124       }
125    }
126
127    public void tearDown() throws Exception JavaDoc
128    {
129       undeployMC();
130       super.tearDown();
131    }
132    
133    /**
134     * Get the kernel bootstrap
135     *
136     * @return the bootstrap
137     */

138    protected AbstractBootstrap getBootstrap() throws Exception JavaDoc
139    {
140       return new BasicBootstrap();
141    }
142    
143    /**
144     * Get a bean
145     *
146     * @param name the name of the bean
147     * @param state the state of the bean
148     * @return the bean
149     * @throws IllegalStateException when the bean does not exist at that state
150     */

151    protected Object JavaDoc getBean(final Object JavaDoc name, final ControllerState state)
152    {
153       ControllerContext context = getControllerContext(name, state);
154       return context.getTarget();
155    }
156    
157    /**
158     * Get a context
159     *
160     * @param name the name of the bean
161     * @param state the state of the bean
162     * @return the context
163     * @throws IllegalStateException when the context does not exist at that state
164     */

165    protected ControllerContext getControllerContext(final Object JavaDoc name, final ControllerState state)
166    {
167       KernelController controller = kernel.getController();
168       ControllerContext context = controller.getContext(name, state);
169       if (context == null)
170          throw new IllegalStateException JavaDoc("Context not found: " + name);
171       return context;
172    }
173
174    /**
175     * Validate
176     */

177    protected void validateMC() throws Exception JavaDoc
178    {
179       try
180       {
181          beanDeployer.validate();
182       }
183       catch (RuntimeException JavaDoc e)
184       {
185          throw e;
186       }
187       catch (Exception JavaDoc e)
188       {
189          throw e;
190       }
191       catch (Error JavaDoc e)
192       {
193          throw e;
194       }
195       catch (Throwable JavaDoc t)
196       {
197          throw new RuntimeException JavaDoc(t);
198       }
199    }
200
201    /**
202     * Deploy a url
203     *
204     * @param url the deployment url
205     * @return the deployment
206     * @throws Throwable for any error
207     */

208    protected KernelDeployment deployMC(URL JavaDoc url) throws Exception JavaDoc
209    {
210       try
211       {
212          log.debug("Deploying " + url);
213          KernelDeployment deployment = beanDeployer.deploy(url);
214          log.trace("Deployed " + url);
215          return deployment;
216       }
217       catch (RuntimeException JavaDoc e)
218       {
219          throw e;
220       }
221       catch (Exception JavaDoc e)
222       {
223          throw e;
224       }
225       catch (Error JavaDoc e)
226       {
227          throw e;
228       }
229       catch (Throwable JavaDoc t)
230       {
231          throw new RuntimeException JavaDoc(t);
232       }
233    }
234    
235    /**
236     * Undeploy a deployment
237     *
238     * @param url the url
239     */

240    protected void undeployMC(URL JavaDoc url)
241    {
242       log.debug("Undeploying " + url);
243       try
244       {
245          beanDeployer.undeploy(url);
246          log.trace("Undeployed " + url);
247       }
248       catch (Throwable JavaDoc t)
249       {
250          log.warn("Error during undeployment: " + url, t);
251       }
252    }
253    
254    /**
255     * Undeploy a deployment
256     *
257     * @param deployment the deployment
258     */

259    protected void undeployMC(KernelDeployment deployment)
260    {
261       log.debug("Undeploying " + deployment.getName());
262       try
263       {
264          beanDeployer.undeploy(deployment);
265          log.trace("Undeployed " + deployment.getName());
266       }
267       catch (Throwable JavaDoc t)
268       {
269          log.warn("Error during undeployment: " + deployment.getName(), t);
270       }
271    }
272
273    /**
274     * Deploy the beans
275     *
276     * @throws Exception for any error
277     */

278    protected void deployMC() throws Exception JavaDoc
279    {
280       String JavaDoc testName = clazz.getName();
281       testName = testName.replace('.', '/') + "-mc.xml";
282       URL JavaDoc url = clazz.getClassLoader().getResource(testName);
283       if (url != null)
284          deployMC(url);
285       else
286          log.debug("No test specific deployment " + testName);
287    }
288
289    /**
290     * Undeploy all
291     */

292    protected void undeployMC()
293    {
294       log.debug("Undeploying " + beanDeployer.getDeploymentNames());
295       beanDeployer.shutdown();
296    }
297
298 }
299
Popular Tags