KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.ejb.pb;
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 org.apache.ojb.ejb.odmg.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:38 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 testRollbackSetRollbackOnly() throws Exception JavaDoc
229     {
230         System.out.println("## testRollbackSetRollbackOnly");
231         int personsBefore = rollbackBean.getPersonCount();
232
233         List JavaDoc persons = VOHelper.createNewPersonList(7);
234         // silient rollback without any insert expected
235
rollbackBean.rollbackSetRollbackOnly(persons);
236
237         int personsAfterFailedStore = rollbackBean.getPersonCount();
238         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
239     }
240
241     public void testRollbackSetRollbackAndThrowException() throws Exception JavaDoc
242     {
243         System.out.println("## testRollbackSetRollbackAndThrowException");
244         int personsBefore = rollbackBean.getPersonCount();
245
246         List JavaDoc persons = VOHelper.createNewPersonList(7);
247         try
248         {
249             rollbackBean.rollbackSetRollbackAndThrowException(persons);
250             fail("RemoteException expected");
251         }
252         catch (RemoteException JavaDoc e)
253         {
254             // we expect this exception
255
assertTrue(true);
256         }
257
258         int personsAfterFailedStore = rollbackBean.getPersonCount();
259         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
260     }
261
262     public void testRollbackBreakIteration() throws Exception JavaDoc
263     {
264         System.out.println("## testRollbackBreakIteration");
265         int personsBefore = rollbackBean.getPersonCount();
266
267         List JavaDoc persons = VOHelper.createNewPersonList(5);
268         try
269         {
270             rollbackBean.rollbackBreakIteration(persons);
271             fail("RemoteException expected");
272         }
273         catch (RemoteException JavaDoc e)
274         {
275             // we expect this exception
276
assertTrue(true);
277         }
278
279         int personsAfterFailedStore = rollbackBean.getPersonCount();
280         assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore);
281     }
282 }
283
Popular Tags