001: package org.apache.ojb.ejb.pb;
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 org.apache.ojb.ejb.odmg.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:38 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 testRollbackSetRollbackOnly() throws Exception {
207: System.out.println("## testRollbackSetRollbackOnly");
208: int personsBefore = rollbackBean.getPersonCount();
209:
210: List persons = VOHelper.createNewPersonList(7);
211: // silient rollback without any insert expected
212: rollbackBean.rollbackSetRollbackOnly(persons);
213:
214: int personsAfterFailedStore = rollbackBean.getPersonCount();
215: assertEquals("Rollback of stored objects failed",
216: personsBefore, personsAfterFailedStore);
217: }
218:
219: public void testRollbackSetRollbackAndThrowException()
220: throws Exception {
221: System.out
222: .println("## testRollbackSetRollbackAndThrowException");
223: int personsBefore = rollbackBean.getPersonCount();
224:
225: List persons = VOHelper.createNewPersonList(7);
226: try {
227: rollbackBean.rollbackSetRollbackAndThrowException(persons);
228: fail("RemoteException expected");
229: } catch (RemoteException e) {
230: // we expect this exception
231: assertTrue(true);
232: }
233:
234: int personsAfterFailedStore = rollbackBean.getPersonCount();
235: assertEquals("Rollback of stored objects failed",
236: personsBefore, personsAfterFailedStore);
237: }
238:
239: public void testRollbackBreakIteration() throws Exception {
240: System.out.println("## testRollbackBreakIteration");
241: int personsBefore = rollbackBean.getPersonCount();
242:
243: List persons = VOHelper.createNewPersonList(5);
244: try {
245: rollbackBean.rollbackBreakIteration(persons);
246: fail("RemoteException expected");
247: } catch (RemoteException e) {
248: // we expect this exception
249: assertTrue(true);
250: }
251:
252: int personsAfterFailedStore = rollbackBean.getPersonCount();
253: assertEquals("Rollback of stored objects failed",
254: personsBefore, personsAfterFailedStore);
255: }
256: }
|