1 package org.apache.ojb.ejb.odmg; 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 testRollbackOdmgAbort() throws Exception 229 { 230 System.out.println("## testRollbackOdmgAbort"); 231 int personsBefore = rollbackBean.getPersonCount(); 232 233 List persons = VOHelper.createNewPersonList(4); 234 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 245 { 246 System.out.println("## testRollbackSetRollbackOnly"); 247 int personsBefore = rollbackBean.getPersonCount(); 248 249 List persons = VOHelper.createNewPersonList(7); 250 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 258 { 259 System.out.println("## testRollbackSetRollbackAndAbort"); 260 int personsBefore = rollbackBean.getPersonCount(); 261 262 List persons = VOHelper.createNewPersonList(7); 263 try 264 { 265 rollbackBean.rollbackSetRollbackAndThrowException(persons); 266 fail("RemoteException expected"); 267 } 268 catch (RemoteException e) 269 { 270 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 279 { 280 System.out.println("## testRollbackBreakIteration"); 281 int personsBefore = rollbackBean.getPersonCount(); 282 283 List persons = VOHelper.createNewPersonList(5); 284 try 285 { 286 rollbackBean.rollbackBreakIteration(persons); 287 fail("RemoteException expected"); 288 } 289 catch (RemoteException e) 290 { 291 assertTrue(true); 293 } 294 295 int personsAfterFailedStore = rollbackBean.getPersonCount(); 296 assertEquals("Rollback of stored objects failed", personsBefore, personsAfterFailedStore); 297 } 298 } 299 | Popular Tags |