KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Properties JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
32 import org.jboss.ejb3.embedded.EJB3StandaloneDeployer;
33 import org.jboss.ejb3.test.standalone.DependsRemote;
34 import junit.framework.TestCase;
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37 import junit.extensions.TestSetup;
38
39 /**
40  * @version <tt>$Revision: 44596 $</tt>
41  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
42  */

43 public class DependencyTestCase extends TestCase
44 {
45    private static boolean booted = false;
46
47    public DependencyTestCase(String JavaDoc name)
48    {
49       super(name);
50    }
51
52    public static Test suite() throws Exception JavaDoc
53    {
54       TestSuite suite = new TestSuite();
55       suite.addTestSuite(DeployerTestCase.class);
56
57
58       // setup test so that embedded JBoss is started/stopped once for all tests here.
59
TestSetup wrapper = new TestSetup(suite)
60       {
61          protected void setUp()
62          {
63             startupEmbeddedJboss();
64          }
65
66          protected void tearDown()
67          {
68             shutdownEmbeddedJboss();
69          }
70       };
71
72       return wrapper;
73    }
74
75    public static void startupEmbeddedJboss()
76    {
77       EJB3StandaloneBootstrap.boot(null);
78    }
79
80    public static void shutdownEmbeddedJboss()
81    {
82       EJB3StandaloneBootstrap.shutdown();
83    }
84
85
86    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
87    {
88       return new InitialContext JavaDoc(getInitialContextProperties());
89    }
90
91    protected Hashtable JavaDoc getInitialContextProperties()
92    {
93       return EJB3StandaloneBootstrap.getInitialContextProperties();
94    }
95
96    private Properties JavaDoc getDefaultPersistenceProperties()
97            throws IOException JavaDoc
98    {
99       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties");
100       assertNotNull(is);
101       Properties JavaDoc defaults = new Properties JavaDoc();
102       defaults.load(is);
103       return defaults;
104    }
105
106    public void testDepends() throws Throwable JavaDoc
107    {
108       InitialContext JavaDoc ctx = getInitialContext();
109
110       URL JavaDoc res = Thread.currentThread().getContextClassLoader().getResource("marker.txt");
111       URL JavaDoc archive = EJB3StandaloneDeployer.getContainingUrlFromResource(res, "marker.txt");
112       EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
113       deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
114       deployer.setJndiProperties(getInitialContextProperties());
115       deployer.getArchives().add(archive);
116
117       completeTest(deployer, ctx);
118
119    }
120    
121    public void testCleanup() throws Throwable JavaDoc
122    {
123       boolean exceptionThrown = false;
124       try
125       {
126          executeEJBs(getInitialContext());
127       }
128       catch (Exception JavaDoc e)
129       {
130          exceptionThrown = true;
131       }
132       assertTrue(exceptionThrown);
133
134    }
135
136    private void completeTest(EJB3StandaloneDeployer deployer, InitialContext JavaDoc ctx)
137            throws Exception JavaDoc
138    {
139       deployer.create();
140       deployer.start();
141
142       executeEJBs(ctx);
143
144       deployer.stop();
145       deployer.destroy();
146    }
147
148    private void executeEJBs(InitialContext JavaDoc ctx)
149            throws NamingException JavaDoc
150    {
151       DependsRemote stateless = (DependsRemote)ctx.lookup("DependsBean/remote");
152       assertNotNull(stateless);
153       stateless.test();
154      
155    }
156 }
Popular Tags