KickJava   Java API By Example, From Geeks To Geeks.

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


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

47 public class JavaClassPathTestCase extends TestCase
48 {
49    public JavaClassPathTestCase(String JavaDoc name)
50    {
51       super(name);
52    }
53
54    public static Test suite() throws Exception JavaDoc
55    {
56       TestSuite suite = new TestSuite();
57       suite.addTestSuite(JavaClassPathTestCase.class);
58
59       // setup test so that embedded JBoss is started/stopped once for all tests here.
60
TestSetup wrapper = new TestSetup(suite)
61       {
62          protected void setUp()
63          {
64             startupEmbeddedJboss();
65          }
66
67          protected void tearDown()
68          {
69             shutdownEmbeddedJboss();
70          }
71       };
72
73       return wrapper;
74    }
75
76    public static void startupEmbeddedJboss()
77    {
78       EJB3StandaloneBootstrap.boot(null);
79       EJB3StandaloneBootstrap.scanClasspath("standalone.jar");
80    }
81
82    public static void shutdownEmbeddedJboss()
83    {
84       EJB3StandaloneBootstrap.shutdown();
85    }
86
87    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
88    {
89       return new InitialContext JavaDoc(getInitialContextProperties());
90    }
91
92    protected Hashtable JavaDoc getInitialContextProperties()
93    {
94       return EJB3StandaloneBootstrap.getInitialContextProperties();
95    }
96
97
98    public void testJavaClassPath() throws Throwable JavaDoc
99    {
100       InitialContext JavaDoc ctx = getInitialContext();
101
102       executeEJBs(ctx);
103    }
104
105    private void executeEJBs(InitialContext JavaDoc ctx)
106            throws NamingException JavaDoc
107    {
108       CustomerDAO local = (CustomerDAO) ctx.lookup("CustomerDAOBean/local");
109       long id = local.createCustomer();
110       Customer cust = local.findCustomer(id);
111       assertEquals("Bill", cust.getName());
112
113       ShoppingCart cart = (ShoppingCart) ctx.lookup("ShoppingCartBean/local");
114       cart.getCart().add("beer");
115       cart.getCart().add("wine");
116       assertEquals(2, cart.getCart().size());
117
118       cart.checkout();
119
120       boolean exceptionThrown = false;
121       try
122       {
123          cart.getCart();
124       }
125       catch (NoSuchEJBException JavaDoc e)
126       {
127          exceptionThrown = true;
128       }
129
130       assertTrue(exceptionThrown);
131
132       CalculatorRemote calc = (CalculatorRemote) ctx.lookup("CalculatorBean/remote");
133       StupidInterceptor.count = 0;
134       CalculatorBean.test = true;
135       try
136       {
137          assertEquals(2, calc.add(1, 1));
138          assertEquals(1, StupidInterceptor.count);
139          StupidInterceptor.count = 0;
140          CalculatorBean.test = true;
141          assertEquals(0, calc.sub(1, 1));
142          assertEquals(1, StupidInterceptor.count);
143       }
144       finally
145       {
146          CalculatorBean.test = false;
147       }
148    }
149
150    protected void configureLoggingAfterBootstrap()
151    {
152       //enableTrace("org.jboss.tm");
153
}
154 }
Popular Tags