KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > bank > unit > BankDeploymentDescriptorTestCase


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.bank.unit;
23
24 import javax.ejb.EJBAccessException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.NoSuchEJBException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import org.jboss.ejb3.ClientKernelAbstraction;
31 import org.jboss.ejb3.KernelAbstractionFactory;
32 import org.jboss.ejb3.test.bank.Bank;
33 import org.jboss.ejb3.test.bank.ProxyFactoryInterface;
34 import org.jboss.ejb3.test.bank.Teller;
35 import org.jboss.ejb3.test.bank.TellerInterceptor;
36 import org.jboss.ejb3.test.bank.TestStatus;
37 import org.jboss.logging.Logger;
38 import org.jboss.security.SecurityAssociation;
39 import org.jboss.security.SimplePrincipal;
40 import org.jboss.test.JBossTestCase;
41 import junit.framework.Test;
42
43 /**
44  * Test for EJB3 deployment of EJB2.0 Bank EJBs
45  *
46  * @version <tt>$Revision: 58478 $</tt>
47  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
48  */

49 public class BankDeploymentDescriptorTestCase
50     extends JBossTestCase {
51  // extends TestCase {
52

53    private static final Logger log = Logger
54          .getLogger(BankDeploymentDescriptorTestCase.class);
55
56    public BankDeploymentDescriptorTestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60    
61    public void testEnvEntry() throws Exception JavaDoc
62    {
63       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
64       SecurityAssociation.setCredential("password".toCharArray());
65       
66       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
67       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
68       assertNotNull(bank);
69       String JavaDoc id = bank.getEnvEntryId();
70       assertEquals(id, "5678");
71    }
72
73    public void testStatelessTeller() throws Exception JavaDoc
74    {
75       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
76       
77       SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
78       SecurityAssociation.setCredential("password".toCharArray());
79  
80       String JavaDoc greeting;
81       Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
82       assertNotNull(teller);
83       
84       greeting = teller.greetWithRequiredTransaction("Hello");
85       assertNotNull(greeting);
86       assertEquals("Hello", greeting);
87       greeting = teller.greetWithNotSupportedTransaction("Hello");
88       assertNotNull(greeting);
89       assertEquals("Hello", greeting);
90       greeting = teller.greetUnchecked("Hello");
91       assertNotNull(greeting);
92       assertEquals("Hello", greeting);
93       
94       try {
95          greeting = teller.greetChecked("Hello");
96          assertTrue(false);
97       } catch (Exception JavaDoc e){
98          assertTrue(e instanceof EJBAccessException JavaDoc);
99       }
100       
101       SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
102       SecurityAssociation.setCredential("password".toCharArray());
103       
104       try{
105          greeting = teller.greetChecked("Hello");
106          assertNotNull(greeting);
107          assertEquals("Hello", greeting);
108       } catch (Exception JavaDoc e){
109          e.printStackTrace();
110       }
111    }
112
113    public void testInjectionAnnotations() throws Exception JavaDoc
114    {
115       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
116       Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
117       assertNotNull(teller);
118       
119       SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
120       SecurityAssociation.setCredential("password".toCharArray());
121       
122       String JavaDoc greeting = teller.greetChecked("Hello");
123       assertNotNull(greeting);
124       assertEquals("Hello", greeting);
125       assertTrue(teller.isConstructed());
126
127    }
128
129    public void testFieldInject() throws Exception JavaDoc
130    {
131       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
132  
133       Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
134       assertNotNull(teller);
135       
136       String JavaDoc greeting = teller.greetWithServiceTimer("Hello");
137       assertEquals("Hello", greeting);
138    }
139    
140    public void testRunAs() throws Exception JavaDoc
141    {
142       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
143       
144       SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
145       SecurityAssociation.setCredential("password".toCharArray());
146  
147       String JavaDoc customerId = "CustomerId";
148       String JavaDoc greeting;
149       Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
150       assertNotNull(teller);
151       
152       String JavaDoc tmpId = teller.retrieveCustomerId();
153       assertEquals("defaultId", tmpId);
154    }
155
156    public void testStatefulBank() throws Exception JavaDoc
157    {
158       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
159       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
160       assertNotNull(bank);
161       String JavaDoc customerId = "CustomerId";
162       
163       SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
164       SecurityAssociation.setCredential("password".toCharArray());
165       try {
166          bank.storeCustomerId(customerId);
167          assertTrue(false);
168       } catch (Exception JavaDoc e){
169          assertTrue(e instanceof EJBAccessException JavaDoc);
170       }
171       
172       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
173       SecurityAssociation.setCredential("password".toCharArray());
174       
175       bank.storeCustomerId(customerId);
176       String JavaDoc tmpId = bank.retrieveCustomerId();
177       assertEquals(customerId, tmpId);
178    }
179    
180    public void testStatefulState() throws Exception JavaDoc
181    {
182       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
183       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
184       assertNotNull(bank);
185       String JavaDoc customerId = "CustomerId";
186       
187       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
188       SecurityAssociation.setCredential("password".toCharArray());
189       
190       bank.storeCustomerId(customerId);
191       String JavaDoc tmpId = bank.retrieveCustomerId();
192       assertEquals(customerId, tmpId);
193       
194       bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
195       assertNotNull(bank);
196       tmpId = bank.retrieveCustomerId();
197       assertEquals("defaultId", tmpId);
198    }
199  
200    public void testStatefulBank21() throws Exception JavaDoc
201    {
202       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
203       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME + "21");
204       assertNotNull(bank);
205       
206       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
207       SecurityAssociation.setCredential("password".toCharArray());
208       
209       String JavaDoc activated = bank.isActivated();
210       assertEquals(activated, "_CREATED");
211    }
212  
213    public void testCallbackListenersAndInteceptors() throws Exception JavaDoc
214    {
215       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
216       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
217       assertNotNull(bank);
218       TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote");
219       status.clear();
220       
221       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
222       SecurityAssociation.setCredential("password".toCharArray());
223       
224       String JavaDoc id = bank.interceptCustomerId("CustomerId");
225       assertEquals("CustomerId_SecondInterceptor_FirstInterceptor", id);
226       assertTrue(status.postConstruct());
227    }
228
229    public void testResource() throws Exception JavaDoc
230    {
231       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
232       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
233       assertNotNull(bank);
234       
235       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
236       SecurityAssociation.setCredential("password".toCharArray());
237       
238       bank.testResource();
239    }
240    
241    public void testRemove() throws Exception JavaDoc
242    {
243       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
244       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
245       assertNotNull(bank);
246       
247       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
248       SecurityAssociation.setCredential("password".toCharArray());
249       
250       bank.remove();
251       
252       try {
253          bank.testResource();
254          assertTrue(false);
255       }
256       catch (NoSuchEJBException JavaDoc e)
257       {
258          // correct exception
259
}
260    }
261    
262    public void testInit() throws Exception JavaDoc
263    {
264       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
265       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
266       assertNotNull(bank);
267       
268       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
269       SecurityAssociation.setCredential("password".toCharArray());
270       
271       String JavaDoc initialized = bank.isInitialized();
272       assertEquals("YESYES", initialized);
273    }
274    
275    public void testTeller() throws Exception JavaDoc
276    {
277       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
278       Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
279       assertNotNull(teller);
280       
281       SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
282       SecurityAssociation.setCredential("password".toCharArray());
283       
284       try {
285          teller.excludedMethod();
286          assertTrue(false);
287       } catch (Exception JavaDoc e){
288          assertTrue(e instanceof EJBAccessException JavaDoc);
289       }
290    }
291    
292    public void testRemoteBindingProxyFactory() throws Exception JavaDoc
293    {
294       ProxyFactoryInterface teller = (ProxyFactoryInterface)getInitialContext().lookup(Teller.JNDI_NAME);
295       assertNotNull(teller);
296    }
297    
298    public void testRemoteBindingInterceptorStack() throws Exception JavaDoc
299    {
300       Teller teller = (Teller)getInitialContext().lookup(Teller.JNDI_NAME);
301       assertNotNull(teller);
302       assertTrue(TellerInterceptor.accessed);
303    }
304    
305    public void testTransactionTimeout() throws Exception JavaDoc
306    {
307       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
308       
309       SecurityAssociation.setPrincipal(new SimplePrincipal("customer"));
310       SecurityAssociation.setCredential("password".toCharArray());
311       
312       Teller teller = (Teller) jndiContext.lookup(Teller.JNDI_NAME);
313       assertNotNull(teller);
314
315       boolean exceptionThrown = false;
316       try
317       {
318          teller.testTransactionTimeout();
319       }
320       catch (Exception JavaDoc e)
321       {
322          exceptionThrown = true;
323       }
324       assertTrue(exceptionThrown);
325
326    }
327    
328    public void testStatefulTransactionTimeout() throws Exception JavaDoc
329    {
330       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
331       
332       SecurityAssociation.setPrincipal(new SimplePrincipal("teller"));
333       SecurityAssociation.setCredential("password".toCharArray());
334       
335       Bank bank = (Bank) jndiContext.lookup(Bank.JNDI_NAME);
336       assertNotNull(bank);
337       
338       try{
339          bank.testTransactionTimeout();
340          String JavaDoc state = bank.getTransactionState();
341          assertEquals("failed", state);
342       } catch (Exception JavaDoc e){
343       }
344    }
345
346    protected void setUp() throws Exception JavaDoc
347    {
348    }
349    
350    protected void tearDown() throws Exception JavaDoc
351    {
352    }
353
354    public static Test suite() throws Exception JavaDoc
355    {
356       ClientKernelAbstraction kernel = KernelAbstractionFactory.getClientInstance();
357       ObjectName JavaDoc propertiesServiceON = new ObjectName JavaDoc("jboss:type=Service,name=SystemProperties");
358       kernel.invoke(
359             propertiesServiceON,
360             "set",
361             new Object JavaDoc[]{"test.datasource.jndi","java:/DefaultDS"},
362             new String JavaDoc[]{"java.lang.String", "java.lang.String"}
363       );
364       
365       kernel.invoke(
366             propertiesServiceON,
367             "set",
368             new Object JavaDoc[]{"test.transactionmanager.jndi","java:/TransactionManager"},
369             new String JavaDoc[]{"java.lang.String", "java.lang.String"}
370       );
371       
372       return getDeploySetup(BankDeploymentDescriptorTestCase.class, "bank.jar");
373    }
374
375 }
376  
Popular Tags