1 package org.apache.ojb.odmg; 2 3 import java.sql.Connection ; 4 import java.sql.ResultSet ; 5 import java.util.Collection ; 6 import java.util.Iterator ; 7 8 import org.apache.ojb.broker.PersistenceBroker; 9 import org.apache.ojb.broker.query.Query; 10 import org.apache.ojb.junit.ODMGTestCase; 11 import org.apache.ojb.odmg.oql.EnhancedOQLQuery; 12 import org.apache.ojb.odmg.shared.Person; 13 import org.apache.ojb.odmg.shared.PersonImpl; 14 import org.odmg.OQLQuery; 15 import org.odmg.QueryInvalidException; 16 import org.odmg.Transaction; 17 18 22 public class ScrollableQueryResultsTest extends ODMGTestCase 23 { 24 private static final int CONTROL_SIZE = 50; 25 26 public static void main(String [] args) 27 { 28 String [] arr = {ScrollableQueryResultsTest.class.getName()}; 29 junit.textui.TestRunner.main(arr); 30 } 31 32 public ScrollableQueryResultsTest(String name) 33 { 34 super(name); 35 } 36 37 private void createData() throws Exception 38 { 39 Transaction tx = odmg.newTransaction(); 40 tx.begin(); 41 for(int i = 1; i <= CONTROL_SIZE; i++) 42 { 43 Person aPerson = new PersonImpl(); 44 aPerson.setFirstname("firstname" + i); 45 aPerson.setLastname("lastname" + i); 46 database.makePersistent(aPerson); 47 } 48 tx.commit(); 49 } 50 51 private void removeAllData() throws Exception 52 { 53 Transaction tx = odmg.newTransaction(); 54 tx.begin(); 55 OQLQuery query = odmg.newOQLQuery(); 56 String sql = "select allPersons from " + Person.class.getName(); 57 query.create(sql); 58 Collection allPersons = (Collection ) query.execute(); 59 Iterator it = allPersons.iterator(); 60 while(it.hasNext()) 61 { 62 database.deletePersistent(it.next()); 63 } 64 tx.commit(); 65 } 66 67 70 public void testGetAllUnrestricted() throws Exception 71 { 72 removeAllData(); 74 76 createData(); 77 78 Transaction tx = odmg.newTransaction(); 80 tx.begin(); 81 82 OQLQuery query = odmg.newOQLQuery(); 83 String sql = "select allPersons from " + Person.class.getName(); 84 query.create(sql); 85 Collection allPersons = (Collection ) query.execute(); 86 Iterator it = allPersons.iterator(); 88 int count = 0; 89 while(it.hasNext()) 90 { 91 it.next(); 92 count++; 93 } 94 tx.commit(); 95 96 if(count != (CONTROL_SIZE)) 98 { 99 fail("count not right, found <" 100 + count 101 + "> should have got <" 102 + (CONTROL_SIZE) 103 + "> This failure is expected if your driver doesn't support advanced JDBC operations."); 104 } 105 } 106 107 110 111 public void testGetSomeA() throws Exception 112 { 113 int start = 10; 114 int end = 15; 115 removeAllData(); 117 createData(); 119 Transaction tx = odmg.newTransaction(); 121 tx.begin(); 122 EnhancedOQLQuery query = odmg.newOQLQuery(); 123 String sql = "select somePersons from " + Person.class.getName(); 124 query.create(sql, start, end); 125 Collection somePersons = (Collection ) query.execute(); 126 127 Iterator it = somePersons.iterator(); 129 int count = 0; 130 while(it.hasNext()) 131 { 132 it.next(); 133 count++; 134 } 135 tx.commit(); 136 if(count != (end - start + 1)) 138 { 139 fail("count not right, found <" 140 + count 141 + "> should have got <" 142 + (end - start) 143 + "> This failure is expected if your driver doesn't support advanced JDBC operations."); 144 } 145 } 146 147 150 public void testGetSomeB() throws Exception 151 { 152 int start = Query.NO_START_AT_INDEX; 153 int end = 15; 154 try 155 { 156 removeAllData(); 158 createData(); 160 161 Transaction tx = odmg.newTransaction(); 163 tx.begin(); 164 165 EnhancedOQLQuery query = odmg.newOQLQuery(); 166 String sql = "select somePersons from " + Person.class.getName(); 167 query.create(sql, start, end); 168 Collection somePersons = (Collection ) query.execute(); 169 Iterator it = somePersons.iterator(); 171 int count = 0; 172 while(it.hasNext()) 173 { 174 it.next(); 175 count++; 176 } 177 tx.commit(); 178 if(count != end) 180 { 181 fail("count not right, found <" 182 + count 183 + "> should have got <" 184 + (end) 185 + "> This failure is expected if your driver doesn't support advanced JDBC operations."); 186 } 187 } 188 catch(Throwable t) 189 { 190 t.printStackTrace(System.out); 191 fail("testGetSomeB: " + t.getMessage()); 192 } 193 } 194 195 198 public void testGetSomeC() throws Exception 199 { 200 int start = 10; 201 int end = Query.NO_END_AT_INDEX; 202 removeAllData(); 204 206 createData(); 207 208 TransactionExt tx = (TransactionExt) odmg.newTransaction(); 210 tx.begin(); 211 PersistenceBroker broker = tx.getBroker(); 212 213 Connection conn = broker.serviceConnectionManager().getConnection(); 214 217 if(!conn.getMetaData() 218 .supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) 219 { 220 tx.commit(); 221 return; 222 } 223 224 EnhancedOQLQuery query = odmg.newOQLQuery(); 225 String sql = "select somePersons from " + Person.class.getName(); 226 query.create(sql, start, end); 227 Collection somePersons = (Collection ) query.execute(); 228 Iterator it = somePersons.iterator(); 230 int count = 0; 231 while(it.hasNext()) 232 { 233 it.next(); 234 count++; 235 } 236 tx.commit(); 237 if(count != (CONTROL_SIZE - start + 1)) 239 { 240 fail("count not right, found <" 241 + count 242 + "> should have got <" 243 + (CONTROL_SIZE - start + 1) 244 + "> This failure is expected if your driver doesn't support advanced JDBC operations."); 245 } 246 } 247 248 251 public void testGetSomeD() throws Exception 252 { 253 int start = 10; 254 int end = 5; 255 Transaction tx = odmg.newTransaction(); 256 try 257 { 258 tx.begin(); 259 EnhancedOQLQuery query = odmg.newOQLQuery(); 260 String sql = "select somePersons from " + Person.class.getName(); 261 query.create(sql, start, end); 262 query.execute(); 263 fail("should have thrown QueryInvalidException"); 264 } 265 catch(QueryInvalidException iqe) 266 { 267 assertTrue(true); 269 tx.abort(); 270 } 271 } 272 273 276 public void testGetSomeE() throws Exception 277 { 278 int start = 10; 279 int end = 10; 280 Transaction tx = null; 281 try 282 { 283 tx = odmg.newTransaction(); 284 tx.begin(); 285 EnhancedOQLQuery query = odmg.newOQLQuery(); 286 String sql = "select somePersons from " + Person.class.getName(); 287 query.create(sql, start, end); 288 query.execute(); 289 fail("should have thrown QueryInvalidException"); 290 } 291 catch(QueryInvalidException iqe) 292 { 293 assertTrue(true); 295 } 296 catch(Throwable t) 297 { 298 t.printStackTrace(System.out); 299 fail("testGetSomeC: " + t.getMessage()); 300 } 301 finally 302 { 303 if(tx != null) 304 { 305 tx.abort(); 306 } 307 } 308 } 309 } 310 | Popular Tags |