KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > junit > AOPTestDelegate


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.aop.junit;
23
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import org.jboss.aop.AspectXmlLoader;
28 import org.jboss.test.AbstractTestDelegate;
29
30 import EDU.oswego.cs.dl.util.concurrent.CopyOnWriteArrayList;
31
32 /**
33  * AOPTestDelegate.
34  *
35  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
36  * @version $Revision: 42515 $
37  */

38 public class AOPTestDelegate extends AbstractTestDelegate
39 {
40    /** The deployed urls */
41    private static final CopyOnWriteArrayList urls = new CopyOnWriteArrayList();
42    
43    /**
44     * Create a new AOPTestDelegate.
45     *
46     * @param clazz the test class
47     * @throws Exception for any error
48     */

49    public AOPTestDelegate(Class JavaDoc clazz) throws Exception JavaDoc
50    {
51       super(clazz);
52    }
53
54    public void setUp() throws Exception JavaDoc
55    {
56       super.setUp();
57       
58       try
59       {
60          deploy();
61       }
62       catch (RuntimeException JavaDoc e)
63       {
64          throw e;
65       }
66       catch (Exception JavaDoc e)
67       {
68          throw e;
69       }
70       catch (Error JavaDoc e)
71       {
72          throw e;
73       }
74       catch (Throwable JavaDoc e)
75       {
76          throw new RuntimeException JavaDoc(e);
77       }
78    }
79
80    public void tearDown() throws Exception JavaDoc
81    {
82       super.tearDown();
83       undeploy();
84    }
85    
86    /**
87     * Deploy the aop config
88     *
89     * @throws Exception for any error
90     */

91    protected void deploy() throws Exception JavaDoc
92    {
93       String JavaDoc testName = clazz.getName();
94       testName = testName.replace('.', '/') + "-aop.xml";
95       URL JavaDoc url = clazz.getClassLoader().getResource(testName);
96       if (url != null)
97          deploy(url);
98       else
99          log.debug("No test specific deployment " + testName);
100    }
101    
102    protected void undeploy()
103    {
104       for (Iterator JavaDoc i = urls.iterator(); i.hasNext();)
105       {
106          URL JavaDoc url = (URL JavaDoc) i.next();
107          undeploy(url);
108       }
109    }
110    
111    /**
112     * Deploy the aop config
113     *
114     * @param url the url
115     * @throws Exception for any error
116     */

117    protected void deploy(URL JavaDoc url) throws Exception JavaDoc
118    {
119       log.debug("Deploying " + url);
120       urls.add(url);
121       AspectXmlLoader.deployXML(url);
122    }
123
124    /**
125     * Undeploy the aop config
126     *
127     * @param url the url
128     */

129    protected void undeploy(URL JavaDoc url)
130    {
131       try
132       {
133          log.debug("Undeploying " + url);
134          urls.remove(url);
135          AspectXmlLoader.undeployXML(url);
136       }
137       catch (Exception JavaDoc e)
138       {
139          log.warn("Ignored error undeploying " + url, e);
140       }
141    }
142 }
143
Popular Tags