1 package org.apache.ojb.ejb.pb; 2 3 17 18 import javax.ejb.EJBHome ; 19 import javax.naming.Context ; 20 import javax.rmi.PortableRemoteObject ; 21 import javax.transaction.UserTransaction ; 22 import java.rmi.RemoteException ; 23 import java.util.List ; 24 25 import junit.framework.TestCase; 26 import org.apache.ojb.ejb.ContextHelper; 27 import org.apache.ojb.ejb.VOHelper; 28 29 35 public class RollbackClient extends TestCase 36 { 37 RollbackRemote rollbackBean; 38 39 public RollbackClient(String s) 40 { 41 super(s); 42 } 43 44 public RollbackClient() 45 { 46 super(RollbackClient.class.getName()); 47 } 48 49 public static void main(String [] args) 50 { 51 junit.textui.TestRunner.main(new String [] {RollbackClient.class.getName()}); 52 } 53 54 protected void setUp() throws Exception 55 { 56 super.setUp(); 57 init(); 58 } 59 60 protected void tearDown() throws Exception 61 { 62 super.tearDown(); 63 } 64 65 protected void init() 66 { 67 Context ctx = ContextHelper.getContext(); 68 try 69 { 70 Object object = PortableRemoteObject.narrow(ctx.lookup(RollbackHome.JNDI_NAME), EJBHome .class); 71 rollbackBean = ((RollbackHome) object).create(); 72 } 73 catch (Exception e) 74 { 75 e.printStackTrace(); 76 } 77 } 78 79 82 public void YYYtestRollbackRemoteUserTransaction() throws Exception 83 { 84 System.out.println("## testRollbackRemoteUserTransaction"); 85 int articlesBefore = rollbackBean.getArticleCount(); 86 int personsBefore = rollbackBean.getPersonCount(); 87 try 88 { 89 UserTransaction tx = (UserTransaction ) ContextHelper.getContext().lookup("UserTransaction"); 90 tx.begin(); 91 List articles = VOHelper.createNewArticleList(10); 92 List persons = VOHelper.createNewPersonList(5); 93 articles = rollbackBean.storeObjects(articles); 94 persons = rollbackBean.storeObjects(persons); 95 tx.rollback(); 96 } 97 catch (RemoteException e) 98 { 99 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 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 fail("Expect an RemoteException"); 120 } 121 catch (RemoteException 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 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 fail("Expect an RemoteException"); 144 } 145 catch (RemoteException 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 158 { 159 System.out.println("## testRollbackClientWrongInput"); 160 int articlesBefore = rollbackBean.getArticleCount(); 161 int personsBefore = rollbackBean.getPersonCount(); 162 163 try 164 { 165 List persons = VOHelper.createNewPersonList(6); 166 persons.add(new Object ()); 168 rollbackBean.rollbackClientWrongInput(VOHelper.createNewArticleList(4), persons); 169 fail("Expect an RemoteException"); 171 } 172 catch (RemoteException 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 185 { 186 System.out.println("## testRollbackThrowException"); 187 int personsBefore = rollbackBean.getPersonCount(); 188 189 List persons = VOHelper.createNewPersonList(7); 190 try 191 { 192 rollbackBean.rollbackThrowException(persons); 193 fail("RemoteException expected"); 194 } 195 catch (RemoteException e) 196 { 197 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 206 { 207 System.out.println("## testRollbackPassInvalidObject"); 208 int personsBefore = rollbackBean.getPersonCount(); 209 210 List persons = VOHelper.createNewPersonList(7); 211 persons.add(new Object ()); 213 try 214 { 215 rollbackBean.rollbackPassInvalidObject(persons); 216 fail("RemoteException expected"); 217 } 218 catch (RemoteException e) 219 { 220 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 229 { 230 System.out.println("## testRollbackSetRollbackOnly"); 231 int personsBefore = rollbackBean.getPersonCount(); 232 233 List persons = VOHelper.createNewPersonList(7); 234 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 242 { 243 System.out.println("## testRollbackSetRollbackAndThrowException"); 244 int personsBefore = rollbackBean.getPersonCount(); 245 246 List persons = VOHelper.createNewPersonList(7); 247 try 248 { 249 rollbackBean.rollbackSetRollbackAndThrowException(persons); 250 fail("RemoteException expected"); 251 } 252 catch (RemoteException e) 253 { 254 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 263 { 264 System.out.println("## testRollbackBreakIteration"); 265 int personsBefore = rollbackBean.getPersonCount(); 266 267 List persons = VOHelper.createNewPersonList(5); 268 try 269 { 270 rollbackBean.rollbackBreakIteration(persons); 271 fail("RemoteException expected"); 272 } 273 catch (RemoteException e) 274 { 275 assertTrue(true); 277 } 278 279 int personsAfterFailedStore = rollbackBean.getPersonCount(); 280 assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore); 281 } 282 } 283 | Popular Tags |