KickJava   Java API By Example, From Geeks To Geeks.

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


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: Ejb2Client.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 java.rmi.RemoteException JavaDoc;
33
34 import javax.ejb.CreateException JavaDoc;
35 import javax.ejb.EJBMetaData JavaDoc;
36 import javax.ejb.RemoveException JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40
41 import org.objectweb.easybeans.log.JLog;
42 import org.objectweb.easybeans.log.JLogFactory;
43
44 /**
45  * Client that has a 2.1 view of the ejb.
46  * @author Gisele Pinheiro Souza
47  * @author Eduardo Studzinski Estima de Castro
48  */

49 public abstract class Ejb2Client implements ItfEjb2Client {
50
51     /**
52      * Logger.
53      */

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

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

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

74     public void createWithoutParameters() throws CreateException JavaDoc, RemoteException JavaDoc {
75         SimpleEjb2Home beanHome = getBeanHome();
76         SimpleEjb2 bean = beanHome.create();
77         assertEquals(bean.getCode(), SimpleEjb2.DEFAULT_CODE_REMOTE, "The bean was not created with the correct value.");
78
79     }
80
81     /**
82      * Creates a bean using the home object that was injected. The bean is
83      * created with the method init() witho the annotation init.
84      * @throws CreateException if an error occurs during the creation.
85      * @throws RemoteException if system level error occurs.
86      */

87     public void createWithParameters() throws CreateException JavaDoc, RemoteException JavaDoc {
88         SimpleEjb2Home beanHome = getBeanHome();
89         SimpleEjb2 bean = beanHome.create(DEFAULT_CODE, DEFAULT_NAME);
90         assertEquals(bean.getCode(), DEFAULT_CODE, "The bean was not created with the correct value.");
91     }
92
93     /**
94      * Gets an instance of the home object by a lookup.After that, creates an
95      * instance of the bean.
96      * @throws NamingException if an error during the lookup occurs.
97      * @throws CreateException if an error occurs during the creation.
98      * @throws RemoteException if system level error occurs.
99      */

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

120     public void removeObject() throws RemoteException JavaDoc, RemoveException JavaDoc, CreateException JavaDoc {
121         SimpleEjb2Home beanHome = getBeanHome();
122         SimpleEjb2 bean = beanHome.create();
123         beanHome.remove(bean);
124
125         try {
126             bean.toString();
127             fail("The bean was not discarded");
128         } catch (Exception JavaDoc e) {
129             logger.debug("The bean threw an expected exception :{0}", e);
130         }
131     }
132
133     /**
134      * Tries to get the ejbMetaData and uses the method isStatelessSession in
135      * the metadata.
136      * @throws RemoteException if a system level error occurs.
137      */

138     public void getEJBMetaData() throws RemoteException JavaDoc {
139         SimpleEjb2Home beanHome = getBeanHome();
140         EJBMetaData JavaDoc data = beanHome.getEJBMetaData();
141
142         assertFalse(data.isStatelessSession(),
143                 "The metadata interface is doing an incorrect value for the isStatelessSession method.");
144     }
145
146     /**
147      * Verifies if the identity for stateful bean is correct. An instance of
148      * stateful bean can not be considered identical to other instance.
149      * @throws RemoteException if a system level error occurs.
150      * @throws CreateException if an error occurs during the creation.
151      */

152     public void verifyIdentity() throws CreateException JavaDoc, RemoteException JavaDoc {
153         SimpleEjb2Home beanHome = getBeanHome();
154         SimpleEjb2 bean1 = beanHome.create();
155         SimpleEjb2 bean2 = beanHome.create();
156
157         assertTrue(bean1.isIdentical(bean1), "The bean is not considered identical to itself.");
158         assertFalse(bean1.isIdentical(bean2), "The two different beans are considered identical.");
159     }
160
161     /**
162      * Verifies if the exception correct is thrown when the method
163      * getPrimaryKey() is called.
164      * @throws CreateException if a problem during the creation occurs.
165      * @throws RemoteException if a system level error occurs.
166      */

167     public void verifyGetPrimaryKey() throws RemoteException JavaDoc, CreateException JavaDoc{
168         SimpleEjb2Home beanHome = getBeanHome();
169         SimpleEjb2 bean1 = beanHome.create();
170         try{
171             bean1.getPrimaryKey();
172             fail("The client cannot call the getPrimaryKey method.");
173         }catch (RemoteException JavaDoc e){
174             logger.debug("The bean threw an expected exception :{0}", e);
175         }
176     }
177 }
178
Popular Tags