KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > standalone > unit > StandardTestCase


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.ejb3.test.standalone.unit;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.lang.reflect.Constructor JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.net.URL JavaDoc;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingEnumeration JavaDoc;
39
40
41 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
42 import org.jboss.ejb3.embedded.EJB3StandaloneDeployer;
43 import junit.framework.TestCase;
44 import junit.framework.Test;
45 import junit.framework.TestSuite;
46 import junit.extensions.TestSetup;
47
48 import org.jboss.logging.Logger;
49
50 /**
51  * @version <tt>$Revision: 56944 $</tt>
52  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
53  */

54 public class StandardTestCase extends TestCase
55 {
56    private static final Logger log = Logger
57    .getLogger(StandardTestCase.class);
58    
59    private static boolean booted = false;
60    
61    private static List JavaDoc tests = new LinkedList JavaDoc();
62    
63    static
64    {
65       tests.add(new StandardTestCaseTest("org.jboss.ejb3.test.jca.inflowmdb.unit.InflowUnitTestCase", "jmsinflowmdb.jar", "testJMS", "standard/jca-inflowmdb-beans.xml"));
66       tests.add(new StandardTestCaseTest("org.jboss.ejb3.test.mdb.unit.MDBUnitTestCase", "mdb-test.jar", null, "security-beans.xml,standard/testjms.xml"));
67    }
68
69    public StandardTestCase(String JavaDoc name)
70    {
71       super(name);
72    }
73
74    public static Test suite() throws Exception JavaDoc
75    {
76       TestSuite suite = new TestSuite();
77       suite.addTestSuite(StandardTestCase.class);
78       
79       return suite;
80    }
81
82    public static void startupEmbeddedJboss(StandardTestCaseTest test)
83    {
84       EJB3StandaloneBootstrap.boot(null);
85       EJB3StandaloneBootstrap.deployXmlResource("jboss-jms-beans.xml");
86       
87       if (test.xmlResources != null)
88       {
89          StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(test.xmlResources, ",");
90          while (tokenizer.hasMoreTokens())
91          {
92             String JavaDoc testXml = tokenizer.nextToken();
93             EJB3StandaloneBootstrap.deployXmlResource(testXml);
94          }
95       }
96    }
97
98    public static void shutdownEmbeddedJboss()
99    {
100       EJB3StandaloneBootstrap.shutdown();
101    }
102
103
104    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
105    {
106       return new InitialContext JavaDoc(getInitialContextProperties());
107    }
108
109    protected Hashtable JavaDoc getInitialContextProperties()
110    {
111       return EJB3StandaloneBootstrap.getInitialContextProperties();
112    }
113
114    private Properties JavaDoc getDefaultPersistenceProperties()
115            throws IOException JavaDoc
116    {
117       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties");
118       assertNotNull(is);
119       Properties JavaDoc defaults = new Properties JavaDoc();
120       defaults.load(is);
121       return defaults;
122    }
123
124    public void testStandardTests() throws Throwable JavaDoc
125    {
126       Iterator JavaDoc standardTests = tests.iterator();
127       while (standardTests.hasNext())
128       {
129          StandardTestCaseTest test = (StandardTestCaseTest)standardTests.next();
130          
131          System.out.println("Testing standard test " + test.testClass);
132          
133          startupEmbeddedJboss(test);
134          EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
135          deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
136          deployer.setJndiProperties(getInitialContextProperties());
137          
138          if (test.deployments != null)
139          {
140             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(test.deployments, ",");
141             while (tokenizer.hasMoreTokens())
142             {
143                String JavaDoc testJar = tokenizer.nextToken();
144                URL JavaDoc archive = Thread.currentThread().getContextClassLoader().getResource(testJar);
145                deployer.getArchives().add(archive);
146             }
147          }
148          
149          if (test.testMethods != null)
150             runTest(deployer, test.testClass, test.testMethods);
151          else
152             runTest(deployer, test.testClass);
153          
154          shutdownEmbeddedJboss();
155       }
156    }
157    
158    private void runTest(EJB3StandaloneDeployer deployer, String JavaDoc testClassName, String JavaDoc methods)
159       throws Exception JavaDoc
160    {
161       startTest(deployer);
162            
163       Class JavaDoc testClass = Thread.currentThread().getContextClassLoader().loadClass(testClassName);
164       String JavaDoc[] constructorParams = {testClass.getName()};
165       Class JavaDoc[] constructorSignature = {String JavaDoc.class};
166       Constructor JavaDoc constructor = testClass.getConstructor(constructorSignature);
167       Object JavaDoc testCase = constructor.newInstance(constructorParams);
168       
169       Class JavaDoc[] signature = new Class JavaDoc[0];
170       StringTokenizer JavaDoc methodTokenizer = new StringTokenizer JavaDoc(methods, ",");
171       Object JavaDoc[] params = new Object JavaDoc[0];
172       while (methodTokenizer.hasMoreTokens())
173       {
174          String JavaDoc methodName = methodTokenizer.nextToken();
175          Method JavaDoc method = testClass.getMethod(methodName, signature);
176          System.out.println("-- executing method " + method.getName());
177          method.invoke(testCase, params);
178       }
179    
180       stopTest(deployer);
181    }
182    
183    private void runTest(EJB3StandaloneDeployer deployer, String JavaDoc testClassName)
184       throws Exception JavaDoc
185    {
186       startTest(deployer);
187        
188       Class JavaDoc testClass = Thread.currentThread().getContextClassLoader().loadClass(testClassName);
189       String JavaDoc[] constructorParams = {testClass.getName()};
190       Class JavaDoc[] signature = {String JavaDoc.class};
191       Constructor JavaDoc constructor = testClass.getConstructor(signature);
192       Object JavaDoc testCase = constructor.newInstance(constructorParams);
193       
194       Object JavaDoc[] params = new Object JavaDoc[0];
195       for (Method JavaDoc method: testClass.getMethods())
196       {
197          if (method.getName().startsWith("test") && method.getParameterTypes().length == 0)
198          {
199             System.out.println("-- executing method " + method.getName());
200             method.invoke(testCase, params);
201          }
202       }
203
204       stopTest(deployer);
205    }
206    
207    private void startTest(EJB3StandaloneDeployer deployer)
208       throws Exception JavaDoc
209    {
210       deployer.create();
211       deployer.start();
212    }
213    
214    private void stopTest(EJB3StandaloneDeployer deployer)
215       throws Exception JavaDoc
216    {
217       deployer.stop();
218       deployer.destroy();
219    }
220    
221    private void lookup(String JavaDoc name)
222    {
223       System.out.println("lookup " + name);
224       try {
225          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
226          NamingEnumeration JavaDoc names = jndiContext.list(name);
227          if (names != null){
228             while (names.hasMore()){
229                System.out.println(" " + names.next());
230             }
231          }
232       } catch (Exception JavaDoc e){
233       }
234    }
235    
236    static class StandardTestCaseTest
237    {
238       public String JavaDoc testClass;
239       public String JavaDoc deployments;
240       public String JavaDoc testMethods;
241       public String JavaDoc xmlResources;
242       
243       public StandardTestCaseTest(String JavaDoc testClass, String JavaDoc deployments, String JavaDoc testMethods, String JavaDoc xmlResources)
244       {
245          this.testClass = testClass;
246          this.deployments = deployments;
247          this.testMethods = testMethods;
248          this.xmlResources = xmlResources;
249       }
250    }
251 }
Popular Tags