001: package org.apache.ojb.ejb.odmg;
002:
003: /* Copyright 2004-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import javax.ejb.EJBHome;
019: import javax.naming.Context;
020: import javax.rmi.PortableRemoteObject;
021: import javax.transaction.UserTransaction;
022: import java.rmi.RemoteException;
023: import java.util.List;
024:
025: import junit.framework.TestCase;
026: import org.apache.ojb.ejb.ContextHelper;
027: import org.apache.ojb.ejb.VOHelper;
028:
029: /**
030: * Test client using the {@link RollbackBean}.
031: *
032: * @author <a href="mailto:arminw@apache.de">Armin Waibel</a>
033: * @version $Id: RollbackClient.java,v 1.1.2.2 2005/12/21 22:21:39 tomdz Exp $
034: */
035: public class RollbackClient extends TestCase {
036: RollbackRemote rollbackBean;
037:
038: public RollbackClient(String s) {
039: super (s);
040: }
041:
042: public RollbackClient() {
043: super (RollbackClient.class.getName());
044: }
045:
046: public static void main(String[] args) {
047: junit.textui.TestRunner
048: .main(new String[] { RollbackClient.class.getName() });
049: }
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053: init();
054: }
055:
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: protected void init() {
061: Context ctx = ContextHelper.getContext();
062: try {
063: Object object = PortableRemoteObject.narrow(ctx
064: .lookup(RollbackHome.JNDI_NAME), EJBHome.class);
065: rollbackBean = ((RollbackHome) object).create();
066: } catch (Exception e) {
067: e.printStackTrace();
068: }
069: }
070:
071: /*
072: TODO: Make this work
073: */
074: public void YYYtestRollbackRemoteUserTransaction() throws Exception {
075: System.out.println("## testRollbackRemoteUserTransaction");
076: int articlesBefore = rollbackBean.getArticleCount();
077: int personsBefore = rollbackBean.getPersonCount();
078: try {
079: UserTransaction tx = (UserTransaction) ContextHelper
080: .getContext().lookup("UserTransaction");
081: tx.begin();
082: List articles = VOHelper.createNewArticleList(10);
083: List persons = VOHelper.createNewPersonList(5);
084: articles = rollbackBean.storeObjects(articles);
085: persons = rollbackBean.storeObjects(persons);
086: tx.rollback();
087: } catch (RemoteException e) {
088: // should we expect that??
089: e.printStackTrace();
090: }
091:
092: int articlesAfterStore = rollbackBean.getArticleCount();
093: int personsAfterStore = rollbackBean.getPersonCount();
094: assertEquals("Storing of articles failed", articlesBefore + 10,
095: articlesAfterStore);
096: assertEquals("Storing of persons faile", personsBefore + 5,
097: personsAfterStore);
098: }
099:
100: public void testRollbackOtherBeanUsing() throws Exception {
101: System.out.println("## testRollbackOtherBeanUsing");
102: int articlesBefore = rollbackBean.getArticleCount();
103: int personsBefore = rollbackBean.getPersonCount();
104:
105: try {
106: rollbackBean.rollbackOtherBeanUsing(VOHelper
107: .createNewArticleList(4), VOHelper
108: .createNewPersonList(6));
109: // we should get an exception
110: fail("Expect an RemoteException");
111: } catch (RemoteException e) {
112: assertTrue(true);
113: }
114:
115: int personsAfter = rollbackBean.getPersonCount();
116: int articlesAfter = rollbackBean.getArticleCount();
117:
118: assertEquals(articlesBefore, articlesAfter);
119: assertEquals(personsBefore, personsAfter);
120: }
121:
122: public void testRollbackOtherBeanUsing_2() throws Exception {
123: System.out.println("## testRollbackOtherBeanUsing_2");
124: int articlesBefore = rollbackBean.getArticleCount();
125: int personsBefore = rollbackBean.getPersonCount();
126:
127: try {
128: rollbackBean.rollbackOtherBeanUsing_2(VOHelper
129: .createNewArticle(13), VOHelper
130: .createNewPersonList(6));
131: // we should get an exception
132: fail("Expect an RemoteException");
133: } catch (RemoteException e) {
134: assertTrue(true);
135: }
136:
137: int personsAfter = rollbackBean.getPersonCount();
138: int articlesAfter = rollbackBean.getArticleCount();
139:
140: assertEquals(articlesBefore, articlesAfter);
141: assertEquals(personsBefore, personsAfter);
142: }
143:
144: public void testRollbackClientWrongInput() throws Exception {
145: System.out.println("## testRollbackClientWrongInput");
146: int articlesBefore = rollbackBean.getArticleCount();
147: int personsBefore = rollbackBean.getPersonCount();
148:
149: try {
150: List persons = VOHelper.createNewPersonList(6);
151: // add non-persistent object to cause failure
152: persons.add(new Object());
153: rollbackBean.rollbackClientWrongInput(VOHelper
154: .createNewArticleList(4), persons);
155: // we should get an exception
156: fail("Expect an RemoteException");
157: } catch (RemoteException e) {
158: assertTrue(true);
159: }
160:
161: int personsAfter = rollbackBean.getPersonCount();
162: int articlesAfter = rollbackBean.getArticleCount();
163:
164: assertEquals(articlesBefore, articlesAfter);
165: assertEquals(personsBefore, personsAfter);
166: }
167:
168: public void testRollbackThrowException() throws Exception {
169: System.out.println("## testRollbackThrowException");
170: int personsBefore = rollbackBean.getPersonCount();
171:
172: List persons = VOHelper.createNewPersonList(7);
173: try {
174: rollbackBean.rollbackThrowException(persons);
175: fail("RemoteException expected");
176: } catch (RemoteException e) {
177: // we expect this exception
178: assertTrue(true);
179: }
180:
181: int personsAfterFailedStore = rollbackBean.getPersonCount();
182: assertEquals("Rollback of stored objects failed",
183: personsBefore, personsAfterFailedStore);
184: }
185:
186: public void testRollbackPassInvalidObject() throws Exception {
187: System.out.println("## testRollbackPassInvalidObject");
188: int personsBefore = rollbackBean.getPersonCount();
189:
190: List persons = VOHelper.createNewPersonList(7);
191: // add invalid non-persistent object
192: persons.add(new Object());
193: try {
194: rollbackBean.rollbackPassInvalidObject(persons);
195: fail("RemoteException expected");
196: } catch (RemoteException e) {
197: // we expect this exception
198: assertTrue(true);
199: }
200:
201: int personsAfterFailedStore = rollbackBean.getPersonCount();
202: assertEquals("Rollback of stored objects failed",
203: personsBefore, personsAfterFailedStore);
204: }
205:
206: public void testRollbackOdmgAbort() throws Exception {
207: System.out.println("## testRollbackOdmgAbort");
208: int personsBefore = rollbackBean.getPersonCount();
209:
210: List persons = VOHelper.createNewPersonList(4);
211: /*
212: only odmg-abort call was done on server side, this does not thrown
213: an RemoteExeption, bean will silent be rollback
214: */
215: rollbackBean.rollbackOdmgAbort(persons);
216:
217: int personsAfterFailedStore = rollbackBean.getPersonCount();
218: assertEquals("Rollback of stored objects failed",
219: personsBefore, personsAfterFailedStore);
220: }
221:
222: public void testRollbackSetRollbackOnly() throws Exception {
223: System.out.println("## testRollbackSetRollbackOnly");
224: int personsBefore = rollbackBean.getPersonCount();
225:
226: List persons = VOHelper.createNewPersonList(7);
227: // silient rollback without any insert expected
228: rollbackBean.rollbackSetRollbackOnly(persons);
229:
230: int personsAfterFailedStore = rollbackBean.getPersonCount();
231: assertEquals("Rollback of stored objects failed",
232: personsBefore, personsAfterFailedStore);
233: }
234:
235: public void testRollbackSetRollbackAndAbort() throws Exception {
236: System.out.println("## testRollbackSetRollbackAndAbort");
237: int personsBefore = rollbackBean.getPersonCount();
238:
239: List persons = VOHelper.createNewPersonList(7);
240: try {
241: rollbackBean.rollbackSetRollbackAndThrowException(persons);
242: fail("RemoteException expected");
243: } catch (RemoteException e) {
244: // we expect this exception
245: assertTrue(true);
246: }
247:
248: int personsAfterFailedStore = rollbackBean.getPersonCount();
249: assertEquals("Rollback of stored objects failed",
250: personsBefore, personsAfterFailedStore);
251: }
252:
253: public void testRollbackBreakIteration() throws Exception {
254: System.out.println("## testRollbackBreakIteration");
255: int personsBefore = rollbackBean.getPersonCount();
256:
257: List persons = VOHelper.createNewPersonList(5);
258: try {
259: rollbackBean.rollbackBreakIteration(persons);
260: fail("RemoteException expected");
261: } catch (RemoteException e) {
262: // we expect this exception
263: assertTrue(true);
264: }
265:
266: int personsAfterFailedStore = rollbackBean.getPersonCount();
267: assertEquals("Rollback of stored objects failed",
268: personsBefore, personsAfterFailedStore);
269: }
270: }
|