1 package org.apache.ojb.broker; 2 3 import java.util.Iterator ; 4 5 import junit.framework.TestCase; 6 import org.apache.ojb.broker.query.*; 7 import org.apache.ojb.broker.accesslayer.RsIterator; 8 9 15 public class RsIteratorTest extends TestCase 16 { 17 private PersistenceBroker broker; 18 19 public static void main(String [] args) 20 { 21 String [] arr = {RsIteratorTest.class.getName()}; 22 junit.textui.TestRunner.main(arr); 23 } 24 25 public RsIteratorTest(String name) 26 { 27 super(name); 28 } 29 30 public void setUp() 31 { 32 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 33 } 34 35 public void tearDown() 36 { 37 if(broker != null) 38 { 39 broker.close(); 40 } 41 } 42 43 public void testRsIterator() throws Exception 44 { 45 String name = "testRsIterator_" + System.currentTimeMillis(); 46 prepareTest(name); 47 48 Criteria criteria = new Criteria(); 49 criteria.addLike("name", name+"*"); 50 Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria); 51 52 Iterator it = broker.getIteratorByQuery(query); 53 int k = 0; 54 while(it.hasNext()) 55 { 56 it.next(); 57 k++; 58 } 59 assertEquals("Wrong number of items found", 2, k); 60 } 61 62 65 public void testRsIteratorAutomaticCleanupCheck_1() throws Exception 66 { 67 String name = "testRsIteratorAutomaticCleanupCheck_1_" + System.currentTimeMillis(); 68 prepareTest(name); 69 70 Criteria criteria = new Criteria(); 71 criteria.addLike("name", name+"*"); 72 Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria); 73 74 Iterator it = broker.getIteratorByQuery(query); 75 it.hasNext(); 76 broker.beginTransaction(); 77 broker.commitTransaction(); 78 81 try 82 { 83 it.next(); 84 fail("We expect RsIterator has released resources on pb.commit.."); 85 } 86 catch (RsIterator.ResourceClosedException e) 87 { 88 assertTrue(true); 89 } 90 91 92 it = broker.getIteratorByQuery(query); 93 it.hasNext(); 94 it.next(); 95 broker.beginTransaction(); 96 broker.commitTransaction(); 97 100 try 101 { 102 it.hasNext(); 103 it.next(); 104 fail("We expect RsIterator has released resources on pb.commit.."); 105 } 106 catch (RsIterator.ResourceClosedException e) 107 { 108 assertTrue(true); 109 } 110 } 111 112 115 public void testRsIteratorAutomaticCleanupCheck_2() throws Exception 116 { 117 String name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis(); 118 prepareTest(name); 119 120 Criteria criteria = new Criteria(); 121 criteria.addLike("name", name+"*"); 122 Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria); 123 124 Iterator it = broker.getIteratorByQuery(query); 125 it.hasNext(); 126 broker.beginTransaction(); 127 broker.abortTransaction(); 128 131 try 132 { 133 it.next(); 134 fail("We expect RsIterator has released resources on pb.commit.."); 135 } 136 catch (RsIterator.ResourceClosedException e) 137 { 138 assertTrue(true); 139 } 140 141 142 it = broker.getIteratorByQuery(query); 143 it.hasNext(); 144 it.next(); 145 broker.beginTransaction(); 146 broker.abortTransaction(); 147 150 try 151 { 152 it.hasNext(); 153 it.next(); 154 fail("We expect RsIterator has released resources on pb.commit.."); 155 } 156 catch (RsIterator.ResourceClosedException e) 157 { 158 assertTrue(true); 159 } 160 } 161 162 165 public void testRsIteratorAutomaticCleanupCheck_3() throws Exception 166 { 167 String name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis(); 168 prepareTest(name); 169 170 Criteria criteria = new Criteria(); 171 criteria.addLike("name", name+"*"); 172 Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria); 173 174 Iterator it = broker.getIteratorByQuery(query); 175 broker.close(); 176 179 try 180 { 181 if(it.hasNext()) it.next(); 182 fail("We expect RsIterator has released resources on pb.commit.."); 183 } 184 catch (RsIterator.ResourceClosedException e) 185 { 186 assertTrue(true); 187 } 188 } 189 190 193 public void testRsIteratorUserCleanup_1() throws Exception 194 { 195 String name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis(); 196 prepareTest(name); 197 198 Criteria criteria = new Criteria(); 199 criteria.addLike("name", name+"*"); 200 Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria); 201 202 Iterator it = broker.getIteratorByQuery(query); 203 204 208 if(!(it instanceof RsIterator)) 209 { 210 return; 212 } 213 214 ((RsIterator) it).setAutoRelease(false); 216 217 it.hasNext(); 218 broker.beginTransaction(); 219 broker.abortTransaction(); 220 223 try 224 { 225 it.next(); 226 fail("We expect RsIterator has released resources on pb.commit.."); 227 } 228 catch (RsIterator.ResourceClosedException e) 229 { 230 assertTrue(true); 231 } 232 233 234 it = broker.getIteratorByQuery(query); 235 ((RsIterator) it).setAutoRelease(false); 237 238 it.hasNext(); 239 it.next(); 240 broker.beginTransaction(); 241 broker.abortTransaction(); 242 245 try 246 { 247 it.hasNext(); 248 it.next(); 249 fail("We expect RsIterator has released resources on pb.commit.."); 250 } 251 catch (RsIterator.ResourceClosedException e) 252 { 253 assertTrue(true); 254 } 255 } 256 257 260 public void testRsIteratorUserCleanup_2() throws Exception 261 { 262 String name = "testRsIteratorAutomaticCleanupCheck_" + System.currentTimeMillis(); 263 prepareTest(name); 264 265 Criteria criteria = new Criteria(); 266 criteria.addLike("name", name+"*"); 267 Query query = new QueryByCriteria(ObjectRepository.Component.class, criteria); 268 269 Iterator it = broker.getIteratorByQuery(query); 270 271 275 if(!(it instanceof RsIterator)) 276 { 277 return; 279 } 280 281 ((RsIterator) it).setAutoRelease(false); 283 284 while(it.hasNext()) 285 { 286 ObjectRepository.Component c = (ObjectRepository.Component) it.next(); 287 assertNotNull(c.getId()); 288 } 289 290 try 291 { 292 ((RsIterator) it).relative(1); 293 } 294 catch(RsIterator.ResourceClosedException e) 295 { 296 fail("RsIterator should not close resources by itself"); 297 } 298 catch (PersistenceBrokerException ignore) 299 { 300 } 301 302 ((RsIterator) it).releaseDbResources(); 304 } 305 306 307 private void prepareTest(String objectName) 308 { 309 ObjectRepository.Component c1 = new ObjectRepository.Component(); 310 c1.setName(objectName + "_1"); 311 ObjectRepository.Component c2 = new ObjectRepository.Component(); 312 c2.setName(objectName + "_2"); 313 314 broker.beginTransaction(); 315 broker.store(c1); 316 broker.store(c2); 317 broker.commitTransaction(); 318 } 319 320 324 public void testInternUsedRsIterator() throws Exception 325 { 326 Criteria criteria = new Criteria(); 328 criteria.addEqualTo("id", new Integer (1)); 329 Query query = new QueryByCriteria(Person.class, criteria); 330 Person person = (Person) broker.getObjectByQuery(query); 332 assertNotNull("Person with id 1 was not found", person); 333 } 334 335 } 336 | Popular Tags |