KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateful > containermanaged > ejb2view > Ejb2LocalClient


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Ejb2LocalClient.java 981 2006-07-28 14:16:22Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.ejb2view;
26
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.Assert.assertFalse;
29 import static org.testng.Assert.assertTrue;
30 import static org.testng.Assert.fail;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.RemoveException JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38
39 import org.objectweb.easybeans.log.JLog;
40 import org.objectweb.easybeans.log.JLogFactory;
41
42 /**
43  * Is a bean with ejb 2.1 client local view.
44  * @author Gisele Pinheiro Souza
45  * @author Eduardo Studzinski Estima de Castro
46  *
47  */

48 public abstract class Ejb2LocalClient implements ItfEjb2LocalClient {
49
50     /**
51      * Logger.
52      */

53     private static JLog logger = JLogFactory.getLog(Ejb2Client.class);
54
55     /**
56      * Returns the bean home object.
57      * @return the home object.
58      */

59     public abstract SimpleEjb2LocalHome getBeanHome();
60
61     /**
62      * Returns the bean JNDI address.
63      * @return the JNDI address.
64      */

65     public abstract String JavaDoc getBeanName();
66
67     /**
68      * Creates a bean using the home object that was injected. The bean is
69      * created with the method ejbCreate() without the annotation init.
70      * @throws CreateException if an error occurs during the creation.
71      */

72     public void createWithIntParameter() throws CreateException JavaDoc{
73         SimpleEjb2LocalHome beanHome = getBeanHome();
74         SimpleEjb2Local bean = beanHome.create(DEFAULT_CODE);
75         assertEquals(bean.getNameLocal(), SimpleEjb2Local.DEFAULT_NAME_LOCAL, "The bean was not created with the correct value.");
76
77     }
78
79     /**
80      * Creates a bean using the home object that was injected. The bean is
81      * created with the method init() with the annotation init.
82      * @throws CreateException if an error occurs during the creation.
83       */

84     public void createWithStrParameter() throws CreateException JavaDoc{
85         SimpleEjb2LocalHome beanHome = getBeanHome();
86         SimpleEjb2Local bean = beanHome.create(DEFAULT_NAME);
87         assertEquals(bean.getCodeLocal(), DEFAULT_CODE, "The bean was not created with the correct value.");
88     }
89
90     /**
91      * Gets an instance of the home object by a lookup.After that, creates an
92      * instance of the bean.
93      * @throws NamingException if an error during the lookup occurs.
94      * @throws CreateException if an error occurs during the creation.
95      */

96     public void getBeanByLookup() throws NamingException JavaDoc, CreateException JavaDoc {
97         // Obtain the default initial JNDI context.
98
Context JavaDoc initCtx = new InitialContext JavaDoc();
99
100         Object JavaDoc result = initCtx.lookup(getBeanName());
101
102         SimpleEjb2LocalHome beanHomeLookup = (SimpleEjb2LocalHome) javax.rmi.PortableRemoteObject.narrow(result,
103                 SimpleEjb2LocalHome.class);
104
105         SimpleEjb2Local beanLookup = beanHomeLookup.create(DEFAULT_NAME);
106         assertEquals(beanLookup.getCodeLocal(), DEFAULT_CODE, "The bean was not created with the correct value.");
107
108     }
109
110     /**
111      * Creates a bean and after that, removes it.
112      * @throws RemoveException if the bean can not be deleted.
113      * @throws CreateException if an error occurs during the creation.
114      */

115     public void removeObject() throws RemoveException JavaDoc, CreateException JavaDoc {
116         SimpleEjb2LocalHome beanHome = getBeanHome();
117         SimpleEjb2Local bean = beanHome.create(DEFAULT_CODE);
118         beanHome.remove(bean);
119
120         try {
121             bean.toString();
122             fail("The bean was not discarded");
123         } catch (Exception JavaDoc e) {
124             logger.debug("The bean threw an expected exception :{0}", e);
125         }
126     }
127
128     /**
129      * Verifies if the identity for stateful bean is correct. An instance of
130      * stateful bean can not be considered identical to other instance.
131      * @throws CreateException if an error occurs during the creation.
132      */

133     public void verifyIdentity() throws CreateException JavaDoc{
134         SimpleEjb2LocalHome beanHome = getBeanHome();
135         SimpleEjb2Local bean1 = beanHome.create(DEFAULT_NAME);
136         SimpleEjb2Local bean2 = beanHome.create(DEFAULT_NAME);
137
138         assertTrue(bean1.isIdentical(bean1), "The bean is not considered identical to itself.");
139         assertFalse(bean1.isIdentical(bean2), "The two different beans are considered identical.");
140     }
141
142     /**
143      * Verifies if the exception correct is thrown when the method
144      * getPrimaryKey() is called.
145      * @throws CreateException if a problem during the creation occurs.
146      */

147     public void verifyGetPrimaryKey() throws CreateException JavaDoc{
148         SimpleEjb2LocalHome beanHome = getBeanHome();
149         SimpleEjb2Local bean1 = beanHome.create(DEFAULT_NAME);
150         try{
151             bean1.getPrimaryKey();
152             fail("The client cannot call the getPrimaryKey method.");
153         }catch(EJBException JavaDoc e){
154             logger.debug("The bean threw an expected exception :{0}", e);
155         }
156     }
157 }
158
Popular Tags