KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejb.NoSuchEJBException JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
33 import org.jboss.ejb3.embedded.EJB3StandaloneDeployer;
34 import org.jboss.ejb3.embedded.EJB3StandaloneDeployment;
35 import org.jboss.ejb3.test.standalone.CalculatorRemote;
36 import org.jboss.ejb3.test.standalone.Customer;
37 import org.jboss.ejb3.test.standalone.CustomerDAO;
38 import org.jboss.ejb3.test.standalone.ShoppingCart;
39 import junit.framework.TestCase;
40 import junit.framework.Test;
41 import junit.framework.TestSuite;
42 import junit.extensions.TestSetup;
43
44 /**
45  * POJO Environment tests
46  *
47  * @author <a HREF="bill@jboss.org">Bill Burke</a>
48  * @version $Revision: 57789 $
49  */

50 public class DeployerTestCase extends TestCase
51 {
52    private static boolean booted = false;
53
54    public DeployerTestCase(String JavaDoc name)
55    {
56       super(name);
57    }
58
59    public static Test suite() throws Exception JavaDoc
60    {
61       TestSuite suite = new TestSuite();
62       suite.addTestSuite(DeployerTestCase.class);
63
64
65       // setup test so that embedded JBoss is started/stopped once for all tests here.
66
TestSetup wrapper = new TestSetup(suite)
67       {
68          protected void setUp()
69          {
70             startupEmbeddedJboss();
71          }
72
73          protected void tearDown()
74          {
75             shutdownEmbeddedJboss();
76          }
77       };
78
79       return wrapper;
80    }
81
82    public static void startupEmbeddedJboss()
83    {
84          EJB3StandaloneBootstrap.boot(null);
85    }
86
87    public static void shutdownEmbeddedJboss()
88    {
89       EJB3StandaloneBootstrap.shutdown();
90    }
91
92
93    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
94    {
95       return new InitialContext JavaDoc(getInitialContextProperties());
96    }
97
98    protected Hashtable JavaDoc getInitialContextProperties()
99    {
100       return EJB3StandaloneBootstrap.getInitialContextProperties();
101    }
102
103
104    private Properties JavaDoc getDefaultPersistenceProperties()
105            throws IOException JavaDoc
106    {
107       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties");
108       assertNotNull(is);
109       Properties JavaDoc defaults = new Properties JavaDoc();
110       defaults.load(is);
111       return defaults;
112    }
113
114    public void testArchives() throws Throwable JavaDoc
115    {
116       InitialContext JavaDoc ctx = getInitialContext();
117
118       URL JavaDoc res = Thread.currentThread().getContextClassLoader().getResource("marker.txt");
119       URL JavaDoc archive = EJB3StandaloneDeployer.getContainingUrlFromResource(res, "marker.txt");
120       EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
121       deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
122       deployer.setJndiProperties(getInitialContextProperties());
123       deployer.getArchives().add(archive);
124
125       completeTest(deployer, ctx);
126
127    }
128
129    public void testDeploy() throws Throwable JavaDoc
130    {
131       InitialContext JavaDoc ctx = getInitialContext();
132
133       URL JavaDoc res = Thread.currentThread().getContextClassLoader().getResource("marker.txt");
134       URL JavaDoc deployDir = EJB3StandaloneDeployer.getDeployDirFromResource(res, "marker.txt");
135       EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
136       deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
137       deployer.setJndiProperties(getInitialContextProperties());
138       deployer.setDefaultPersistenceProperties(getDefaultPersistenceProperties());
139       System.out.println("deployDir = " + deployDir);
140       deployer.getDeployDirs().add(deployDir);
141
142       completeTest(deployer, ctx);
143
144    }
145
146    public void testArchivesByResource() throws Throwable JavaDoc
147    {
148       InitialContext JavaDoc ctx = getInitialContext();
149
150       EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
151       deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
152       deployer.setJndiProperties(getInitialContextProperties());
153       deployer.setDefaultPersistenceProperties(getDefaultPersistenceProperties());
154       deployer.getArchivesByResource().add("marker.txt");
155
156       completeTest(deployer, ctx);
157
158    }
159
160    public void testDeployByResource() throws Throwable JavaDoc
161    {
162       InitialContext JavaDoc ctx = getInitialContext();
163
164       EJB3StandaloneDeployer deployer = new EJB3StandaloneDeployer();
165       deployer.setKernel(EJB3StandaloneBootstrap.getKernel());
166       deployer.setJndiProperties(getInitialContextProperties());
167       deployer.setDefaultPersistenceProperties(getDefaultPersistenceProperties());
168       deployer.getDeployDirsByResource().add("marker.txt");
169
170       completeTest(deployer, ctx);
171
172    }
173
174    public void testCleanup() throws Throwable JavaDoc
175    {
176       boolean exceptionThrown = false;
177       try
178       {
179          executeEJBs(getInitialContext());
180       }
181       catch (Exception JavaDoc e)
182       {
183          exceptionThrown = true;
184       }
185       assertTrue(exceptionThrown);
186
187    }
188
189    private void completeTest(EJB3StandaloneDeployer deployer, InitialContext JavaDoc ctx)
190            throws Exception JavaDoc
191    {
192       deployer.create();
193       deployer.start();
194
195       executeEJBs(ctx);
196
197       deployer.stop();
198       deployer.destroy();
199       
200       deployer.create();
201       deployer.start();
202
203       executeEJBs(ctx);
204
205       deployer.stop();
206       deployer.destroy();
207    }
208
209    private void executeEJBs(InitialContext JavaDoc ctx)
210            throws NamingException JavaDoc
211    {
212       CustomerDAO local = (CustomerDAO)ctx.lookup("CustomerDAOBean/local");
213       long id = local.createCustomer();
214       Customer cust = local.findCustomer(id);
215       assertEquals("Bill", cust.getName());
216
217       ShoppingCart cart = (ShoppingCart)ctx.lookup("ShoppingCartBean/local");
218       cart.getCart().add("beer");
219       cart.getCart().add("wine");
220       assertEquals(2, cart.getCart().size());
221
222       cart.checkout();
223
224       boolean exceptionThrown = false;
225       try
226       {
227          cart.getCart();
228       }
229       catch (NoSuchEJBException JavaDoc e)
230       {
231          exceptionThrown = true;
232       }
233
234       assertTrue(exceptionThrown);
235
236       CalculatorRemote calc = (CalculatorRemote)ctx.lookup("CalculatorBean/remote");
237       assertEquals(2, calc.add(1, 1));
238    }
239
240    protected void configureLoggingAfterBootstrap()
241    {
242       //enableTrace("org.jboss.tm");
243
}
244 }
Popular Tags