KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > ejb > odmg > RollbackClient


1 package org.apache.ojb.ejb.odmg;
2
3 /* Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import javax.ejb.EJBHome JavaDoc;
19 import javax.naming.Context JavaDoc;
20 import javax.rmi.PortableRemoteObject JavaDoc;
21 import javax.transaction.UserTransaction JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.util.List JavaDoc;
24
25 import junit.framework.TestCase;
26 import org.apache.ojb.ejb.ContextHelper;
27 import org.apache.ojb.ejb.VOHelper;
28
29 /**
30  * Test client using the {@link RollbackBean}.
31  *
32  * @author <a HREF="mailto:arminw@apache.de">Armin Waibel</a>
33  * @version $Id: RollbackClient.java,v 1.1.2.2 2005/12/21 22:21:39 tomdz Exp $
34  */

35 public class RollbackClient extends TestCase
36 {
37     RollbackRemote rollbackBean;
38
39     public RollbackClient(String JavaDoc s)
40     {
41         super(s);
42     }
43
44     public RollbackClient()
45     {
46         super(RollbackClient.class.getName());
47     }
48
49     public static void main(String JavaDoc[] args)
50     {
51         junit.textui.TestRunner.main(new String JavaDoc[] {RollbackClient.class.getName()});
52     }
53
54     protected void setUp() throws Exception JavaDoc
55     {
56         super.setUp();
57         init();
58     }
59
60     protected void tearDown() throws Exception JavaDoc
61     {
62         super.tearDown();
63     }
64
65     protected void init()
66     {
67         Context JavaDoc ctx = ContextHelper.getContext();
68         try
69         {
70             Object JavaDoc object = PortableRemoteObject.narrow(ctx.lookup(RollbackHome.JNDI_NAME), EJBHome JavaDoc.class);
71             rollbackBean = ((RollbackHome) object).create();
72         }
73         catch (Exception JavaDoc e)
74         {
75             e.printStackTrace();
76         }
77     }
78
79     /*
80     TODO: Make this work
81     */

82     public void YYYtestRollbackRemoteUserTransaction() throws Exception JavaDoc
83     {
84         System.out.println("## testRollbackRemoteUserTransaction");
85         int articlesBefore = rollbackBean.getArticleCount();
86         int personsBefore = rollbackBean.getPersonCount();
87         try
88         {
89             UserTransaction JavaDoc tx = (UserTransaction JavaDoc) ContextHelper.getContext().lookup("UserTransaction");
90             tx.begin();
91             List JavaDoc articles = VOHelper.createNewArticleList(10);
92             List JavaDoc persons = VOHelper.createNewPersonList(5);
93             articles = rollbackBean.storeObjects(articles);
94             persons = rollbackBean.storeObjects(persons);
95             tx.rollback();
96         }
97         catch (RemoteException JavaDoc e)
98         {
99             // should we expect that??
100
e.printStackTrace();
101         }
102
103         int articlesAfterStore = rollbackBean.getArticleCount();
104         int personsAfterStore = rollbackBean.getPersonCount();
105         assertEquals("Storing of articles failed", articlesBefore+10, articlesAfterStore);
106         assertEquals("Storing of persons faile", personsBefore+5, personsAfterStore);
107     }
108
109     public void testRollbackOtherBeanUsing() throws Exception JavaDoc
110     {
111         System.out.println("## testRollbackOtherBeanUsing");
112         int articlesBefore = rollbackBean.getArticleCount();
113         int personsBefore = rollbackBean.getPersonCount();
114
115         try
116         {
117             rollbackBean.rollbackOtherBeanUsing(VOHelper.createNewArticleList(4), VOHelper.createNewPersonList(6));
118             // we should get an exception
119
fail("Expect an RemoteException");
120         }
121         catch (RemoteException JavaDoc e)
122         {
123             assertTrue(true);
124         }
125
126         int personsAfter = rollbackBean.getPersonCount();
127         int articlesAfter = rollbackBean.getArticleCount();
128
129         assertEquals(articlesBefore, articlesAfter);
130         assertEquals(personsBefore, personsAfter);
131     }
132
133     public void testRollbackOtherBeanUsing_2() throws Exception JavaDoc
134     {
135         System.out.println("## testRollbackOtherBeanUsing_2");
136         int articlesBefore = rollbackBean.getArticleCount();
137         int personsBefore = rollbackBean.getPersonCount();
138
139         try
140         {
141             rollbackBean.rollbackOtherBeanUsing_2(VOHelper.createNewArticle(13), VOHelper.createNewPersonList(6));
142             // we should get an exception
143
fail("Expect an RemoteException");
144         }
145         catch (RemoteException JavaDoc e)
146         {
147             assertTrue(true);
148         }
149
150         int personsAfter = rollbackBean.getPersonCount();
151         int articlesAfter = rollbackBean.getArticleCount();
152
153         assertEquals(articlesBefore, articlesAfter);
154         assertEquals(personsBefore, personsAfter);
155     }
156
157     public void testRollbackClientWrongInput() throws Exception JavaDoc
158     {
159         System.out.println("## testRollbackClientWrongInput");
160         int articlesBefore = rollbackBean.getArticleCount();
161         int personsBefore = rollbackBean.getPersonCount();
162
163         try
164         {
165             List JavaDoc persons = VOHelper.createNewPersonList(6);
166             // add non-persistent object to cause failure
167
persons.add(new Object JavaDoc());
168             rollbackBean.rollbackClientWrongInput(VOHelper.createNewArticleList(4), persons);
169             // we should get an exception
170
fail("Expect an RemoteException");
171         }
172         catch (RemoteException JavaDoc e)
173         {
174             assertTrue(true);
175         }
176
177         int personsAfter = rollbackBean.getPersonCount();
178         int articlesAfter = rollbackBean.getArticleCount();
179
180         assertEquals(articlesBefore, articlesAfter);
181         assertEquals(personsBefore, personsAfter);
182     }
183
184     public void testRollbackThrowException() throws Exception JavaDoc
185     {
186         System.out.println("## testRollbackThrowException");
187         int personsBefore = rollbackBean.getPersonCount();
188
189         List JavaDoc persons = VOHelper.createNewPersonList(7);
190         try
191         {
192             rollbackBean.rollbackThrowException(persons);
193             fail("RemoteException expected");
194         }
195         catch (RemoteException JavaDoc e)
196         {
197             // we expect this exception
198
assertTrue(true);
199         }
200
201         int personsAfterFailedStore = rollbackBean.getPersonCount();
202         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
203     }
204
205     public void testRollbackPassInvalidObject() throws Exception JavaDoc
206     {
207         System.out.println("## testRollbackPassInvalidObject");
208         int personsBefore = rollbackBean.getPersonCount();
209
210         List JavaDoc persons = VOHelper.createNewPersonList(7);
211         // add invalid non-persistent object
212
persons.add(new Object JavaDoc());
213         try
214         {
215             rollbackBean.rollbackPassInvalidObject(persons);
216             fail("RemoteException expected");
217         }
218         catch (RemoteException JavaDoc e)
219         {
220             // we expect this exception
221
assertTrue(true);
222         }
223
224         int personsAfterFailedStore = rollbackBean.getPersonCount();
225         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
226     }
227
228     public void testRollbackOdmgAbort() throws Exception JavaDoc
229     {
230         System.out.println("## testRollbackOdmgAbort");
231         int personsBefore = rollbackBean.getPersonCount();
232
233         List JavaDoc persons = VOHelper.createNewPersonList(4);
234         /*
235         only odmg-abort call was done on server side, this does not thrown
236         an RemoteExeption, bean will silent be rollback
237         */

238         rollbackBean.rollbackOdmgAbort(persons);
239
240         int personsAfterFailedStore = rollbackBean.getPersonCount();
241         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
242     }
243
244     public void testRollbackSetRollbackOnly() throws Exception JavaDoc
245     {
246         System.out.println("## testRollbackSetRollbackOnly");
247         int personsBefore = rollbackBean.getPersonCount();
248
249         List JavaDoc persons = VOHelper.createNewPersonList(7);
250         // silient rollback without any insert expected
251
rollbackBean.rollbackSetRollbackOnly(persons);
252
253         int personsAfterFailedStore = rollbackBean.getPersonCount();
254         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
255     }
256
257     public void testRollbackSetRollbackAndAbort() throws Exception JavaDoc
258     {
259         System.out.println("## testRollbackSetRollbackAndAbort");
260         int personsBefore = rollbackBean.getPersonCount();
261
262         List JavaDoc persons = VOHelper.createNewPersonList(7);
263         try
264         {
265             rollbackBean.rollbackSetRollbackAndThrowException(persons);
266             fail("RemoteException expected");
267         }
268         catch (RemoteException JavaDoc e)
269         {
270             // we expect this exception
271
assertTrue(true);
272         }
273
274         int personsAfterFailedStore = rollbackBean.getPersonCount();
275         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
276     }
277
278     public void testRollbackBreakIteration() throws Exception JavaDoc
279     {
280         System.out.println("## testRollbackBreakIteration");
281         int personsBefore = rollbackBean.getPersonCount();
282
283         List JavaDoc persons = VOHelper.createNewPersonList(5);
284         try
285         {
286             rollbackBean.rollbackBreakIteration(persons);
287             fail("RemoteException expected");
288         }
289         catch (RemoteException JavaDoc e)
290         {
291             // we expect this exception
292
assertTrue(true);
293         }
294
295         int personsAfterFailedStore = rollbackBean.getPersonCount();
296         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
297     }
298 }
299
Popular Tags