1 24 package org.objectweb.jalisto.test.core.suite; 25 26 import junit.framework.Test; 27 import org.objectweb.jalisto.se.api.internal.InternalMetaRepository; 28 import org.objectweb.jalisto.se.exception.IdentityException; 29 import org.objectweb.jalisto.se.exception.JalistoException; 30 import org.objectweb.jalisto.se.impl.server.IdentityProvider; 31 import org.objectweb.jalisto.se.impl.LogicalOid; 32 import org.objectweb.jalisto.se.test.data.Book; 33 import org.objectweb.jalisto.se.test.workbench.JalistoTestCase; 34 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite; 35 36 import java.util.*; 37 38 public class BasicFunctionnalityTestCase extends JalistoTestCase { 39 40 public BasicFunctionnalityTestCase() { 41 } 42 43 public BasicFunctionnalityTestCase(String name) { 44 super(name); 45 } 46 47 protected void tearDown() throws Exception { 48 super.tearDown(); 49 endTransaction(); 50 oids.clear(); 51 books.clear(); 52 } 53 54 public static Test suite() { 55 JalistoTestSuite suite = new JalistoTestSuite(); 56 BasicFunctionnalityTestCase tc = 57 (BasicFunctionnalityTestCase) newTestCase(suite, new BasicFunctionnalityTestCase()); 58 59 tc.define(); 60 61 tc.bookBaseTestWithDelete(10); 62 tc.bookBaseTestGetOid(10); 63 tc.testUpdate(); 64 tc.testWrongRead(); 65 tc.testReserveFloid(); 66 tc.testPointer(); 67 tc.testPointerCollection(); 68 tc.cleanUp(); 69 70 tc.testRemoveOverOidTableSize(); 71 tc.cleanUp(); 72 73 tc.testMakeOidExtentAndDelete(); 74 tc.cleanUp(); 75 76 tc.testBigExtent(20000); 77 tc.cleanUp(); 78 79 tc.deleteOptiTest(); 80 tc.cleanUp(); 81 tc.makeOidOverInstancePageSize(); 82 83 tc.testNotSerialisable(); 84 85 tc.finishTests(); 86 87 return suite; 88 } 89 90 public void testNotSerialisable() { 91 tx.begin(); 92 Object [] o = Book.newBook().toArray(); 93 o[0] = new NotSerialisableData("test"); 94 try { 95 session.createObject(o, Book.class); 96 assertTrue("should raise exception", false); 97 } catch (JalistoException jalistoExc) { 98 assertTrue("exception's messages must be equal", 99 jalistoExc.getMessage().startsWith("not serializabel class in data : ")); 100 } 101 tx.commit(); 102 } 103 104 public void bookBaseTestWithDelete(int num) { 105 ArrayList oids = new ArrayList(); 106 tx.begin(); 107 for (int i = 0; i < num; i++) { 108 oids.add(session.createObject(Book.newBook().toArray(), Book.class)); 109 } 110 tx.commit(); 111 112 HashMap books = new HashMap(); 113 tx.begin(); 114 for (int i = 0; i < oids.size(); i++) { 115 Object oid = oids.get(i); 116 books.put(oid, Book.toBook(session.readObjectByOid(oid))); 117 } 118 tx.commit(); 119 120 String r1 = "{0.2='Metes and Boundsfrom Andy Andrews, 3 pages, 20 E', 0.4='A Cold Case of Murderfrom John Sandford, 5 pages, 30 E', 0.9='Death Off Stagefrom Robert Dallek, 10 pages, 25 E', 0.8='The Jesterfrom John Sandford, 9 pages, 20 E', 0.6='Tipping the Velvetfrom Andy Andrews, 7 pages, 10 E', 0.1='Fingersmithfrom Robert Dallek, 2 pages, 15 E', 0.3='A Fountain Filled with Bloodfrom James McManus, 4 pages, 25 E', 0.7='Bittersweetfrom James McManus, 8 pages, 15 E', 0.5='Innocent Heartsfrom Robert Dallek, 6 pages, 5 E', 0.0='At the Edgefrom John Sandford, 1 pages, 10 E'}"; 121 assertEquals("should be equals", r1, String.valueOf(books)); 122 123 tx.begin(); 124 String r2 = "[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]"; 125 assertEquals("should be equals", r2, String.valueOf(session.getExtent(Book.class).readFully())); 126 tx.commit(); 127 128 tx.begin(); 129 for (int i = 0; i < oids.size(); i++) { 130 Object oid = oids.get(i); 131 session.deleteObjectByOid(oid); 132 } 133 tx.commit(); 134 135 tx.begin(); 136 assertTrue("should be empty", session.getExtent(Book.class).readFully().isEmpty()); 137 tx.commit(); 138 } 139 140 public void bookBaseTestGetOid(int num) { 141 tx.begin(); 142 for (int i = 0; i < num; i++) { 143 oids.add(session.makeNewFileOid(Book.class)); 144 } 145 tx.commit(); 146 147 tx.begin(); 148 for (int i = 0; i < num; i++) { 149 session.createObject(oids.get(i), Book.newBook().toArray()); 150 } 151 tx.commit(); 152 153 tx.begin(); 154 for (int i = 0; i < oids.size(); i++) { 155 Object oid = oids.get(i); 156 books.put(oid, Book.toBook(session.readObjectByOid(oid))); 157 } 158 tx.commit(); 159 160 String r1 = "{0.17='Bittersweetfrom Robert Dallek, 18 pages, 5 E', 0.15='Innocent Heartsfrom James McManus, 16 pages, 25 E', 0.13='A Fountain Filled with Bloodfrom Robert Dallek, 14 pages, 15 E', 0.19='Death Off Stagefrom James McManus, 20 pages, 15 E', 0.11='Fingersmithfrom James McManus, 12 pages, 5 E', 0.16='Tipping the Velvetfrom John Sandford, 17 pages, 30 E', 0.18='The Jesterfrom Andy Andrews, 19 pages, 10 E', 0.14='A Cold Case of Murderfrom Andy Andrews, 15 pages, 20 E', 0.10='At the Edgefrom Andy Andrews, 11 pages, 30 E', 0.12='Metes and Boundsfrom John Sandford, 13 pages, 10 E'}"; 161 assertEquals("should be equals", r1, String.valueOf(books)); 162 163 tx.begin(); 164 String r2 = "[0.10, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19]"; 165 assertEquals("should be equals", r2, String.valueOf(session.getExtent(Book.class).readFully())); 166 tx.commit(); 167 } 168 169 public void testUpdate() { 170 tx.begin(); 171 Iterator extent = session.getExtent(Book.class).readFully().iterator(); 172 while (extent.hasNext()) { 173 Object oid = extent.next(); 174 Book book = Book.toBook(session.readObjectByOid(oid)); 175 book.setPrice(book.getPrice() + 100); 176 session.updateObjectByOid(oid, book.toArray()); 177 } 178 tx.commit(); 179 180 tx.begin(); 181 extent = session.getExtent(Book.class).readFully().iterator(); 182 ArrayList books = new ArrayList(); 183 while (extent.hasNext()) { 184 Object oid = extent.next(); 185 books.add(Book.toBook(session.readObjectByOid(oid))); 186 } 187 tx.commit(); 188 189 String r1 = "['At the Edgefrom Andy Andrews, 11 pages, 130 E', 'Fingersmithfrom James McManus, 12 pages, 105 E', 'Metes and Boundsfrom John Sandford, 13 pages, 110 E', 'A Fountain Filled with Bloodfrom Robert Dallek, 14 pages, 115 E', 'A Cold Case of Murderfrom Andy Andrews, 15 pages, 120 E', 'Innocent Heartsfrom James McManus, 16 pages, 125 E', 'Tipping the Velvetfrom John Sandford, 17 pages, 130 E', 'Bittersweetfrom Robert Dallek, 18 pages, 105 E', 'The Jesterfrom Andy Andrews, 19 pages, 110 E', 'Death Off Stagefrom James McManus, 20 pages, 115 E']"; 190 assertEquals("should be equals", r1, String.valueOf(books)); 191 } 192 193 public void testBigExtent(int nbrBook) { 194 tx.begin(); 195 for (int i = 0; i < nbrBook; i++) { 196 session.createObject(Book.newBook().toArray(), Book.class); 197 } 198 tx.commit(); 199 200 tx.begin(); 201 Iterator extent = session.getExtent(Book.class).readFully().iterator(); 202 while (extent.hasNext()) { 203 Object oid = extent.next(); 204 session.deleteObjectByOid(oid); 205 } 206 tx.commit(); 207 } 208 209 public void testRemoveOverOidTableSize() { 210 int size = session.getInternalSession().getProperties().getOidTableSize() * 2; 211 ArrayList oids = new ArrayList(); 212 tx.begin(); 213 for (int i = 0; i < size; i++) { 214 oids.add(session.createObject(Book.newBook().toArray(), Book.class)); 215 } 216 tx.commit(); 217 218 tx.begin(); 219 for (int i = 0; i < oids.size(); i++) { 220 session.readObjectByOid(oids.get(i)); 221 } 222 tx.commit(); 223 } 224 225 public void testMakeOidExtentAndDelete() { 226 tx.begin(); 227 Object sessionId = session.getInternalSession().getSessionId(); 228 Object clid = ((InternalMetaRepository)session.getMetaRepository()).getClidFromClassName(Book.class.getName()); 229 Collection txOids = session.getInternalSession().getOidTable().getFloidsFromClid(sessionId, clid, true); 230 int initialSize = txOids.size(); 231 tx.commit(); 232 233 tx.begin(); 234 for (int i = 0; i < 10; i++) { 235 session.makeNewFileOid(Book.class); 236 } 237 tx.commit(); 238 239 tx.begin(); 240 assertEquals("should be equal", 0, session.getExtent(Book.class).readFully().size()); 241 tx.commit(); 242 243 tx.begin(); 244 txOids = session.getInternalSession().getOidTable().getFloidsFromClid(sessionId, clid, true); 245 assertEquals("should be equal", 10 + initialSize, txOids.size()); 246 tx.commit(); 247 248 tx.begin(); 249 Iterator toRemove = txOids.iterator(); 250 while(toRemove.hasNext()) { 251 session.deleteObjectByOid(toRemove.next()); 252 } 253 tx.commit(); 254 } 255 256 public void testReserveFloid() { 257 if (session.getInternalSession().isRemoteSession()) { 258 System.out.println("can't execute this test with remote client"); 259 return; 260 } 261 int number = 25; 262 String bookClassName = Book.getMetaDescription().getClassName(); 263 InternalMetaRepository metaRepository = ((InternalMetaRepository)session.getMetaRepository()); 264 short bookClid = ((Short )metaRepository.getClidFromClassName(bookClassName)).shortValue(); 265 tx.begin(); 266 IdentityProvider identityProvider = session.getInternalSession().getOidTable().getIdentityProvider(); 267 long currentFloidCounter = identityProvider.getCurrentFloidCounter(bookClid); 268 long firstReservedFloidValue = session.getInternalSession().reserveFloids(bookClid, number).longValue(); 269 assertEquals("floid's initial value must be equal", firstReservedFloidValue, currentFloidCounter); 270 long afterReserveLongValue = identityProvider.getCurrentFloidCounter(bookClid); 271 assertEquals("must be equal", currentFloidCounter + number, afterReserveLongValue); 272 tx.commit(); 273 274 tx.begin(); 275 LogicalOid floid01 = (LogicalOid) session.createObject(Book.newBook().toArray(), bookClassName); 276 assertEquals("floids must be equals", afterReserveLongValue, floid01.getFloidAsLong()); 277 278 LogicalOid floid02 = (LogicalOid) session.getInternalSession().makeNewFileOid(new LogicalOid(firstReservedFloidValue)); 279 session.createObject(floid02, Book.newBook().toArray()); 280 assertEquals("floids must be equals", firstReservedFloidValue, floid02.getFloidAsLong()); 281 tx.commit(); 282 } 283 284 public void testWrongRead() { 285 tx.begin(); 286 LogicalOid readOid = (LogicalOid) session.makeNewFileOid(Book.class); 287 LogicalOid wrongOid = new LogicalOid(readOid.getFloidAsLong()+1); 288 tx.commit(); 289 290 tx.begin(); 291 try { 292 session.readObjectByOid(wrongOid); 293 assertTrue("should raise exception", false); 294 } catch (IdentityException jalistoIdentityExc) { 295 assertEquals("exception's messages must be equal", 296 "the given oid 0.21 doesn't exist in this Jalisto datastore", 297 jalistoIdentityExc.getMessage()); 298 } 299 try { 300 Object [] result = session.readObjectByOid(readOid); 301 assertTrue("should raise exception", false); 302 } catch (IdentityException jalistoIdentityExc) { 303 assertEquals("exception's messages must be equal", 304 "the given oid 0.20 doesn't exist in this Jalisto datastore", 305 jalistoIdentityExc.getMessage()); 306 } 307 tx.commit(); 308 } 309 310 public void testPointer() { 311 Object [] book = Book.newBook().toArray(); 312 313 tx.begin(); 314 LogicalOid bookOid = (LogicalOid) session.createObject(book, Book.getMetaDescription().getClassName()); 315 Object newValue = new Integer (456); 316 book[0] = newValue; 317 Object [] bookInDatastore = session.readObjectByOid(bookOid); 318 tx.commit(); 319 320 assertTrue("must not be equal", book != bookInDatastore); 321 assertTrue("must not be equal", !newValue.equals(bookInDatastore[0])); 322 } 324 325 public void testPointerCollection() { 326 Object [] book = Book.newBook().toArray(); 327 ArrayList list = new ArrayList(); 328 list.add("tralala"); 329 list.add("trilili"); 330 book[0] = list; 331 332 tx.begin(); 333 LogicalOid bookOid = (LogicalOid) session.createObject(book, Book.getMetaDescription().getClassName()); 334 list.add("trololo"); 335 Object [] bookInDatastore = session.readObjectByOid(bookOid); 336 tx.commit(); 337 338 System.out.println(bookInDatastore[0]); 339 340 assertTrue(" : EXPECTED FAILURE ON THIS TEST : must not be equal", list != bookInDatastore[0]); 341 assertEquals("size must be equals to 2", 2, ((ArrayList)bookInDatastore[0]).size()); 343 assertTrue("must not contains", ((ArrayList)bookInDatastore[0]).contains("trololo")); 344 } 345 346 public void deleteOptiTest() { 347 tx.begin(); 348 Object oid = session.createObject(Book.newBook().toArray(), Book.class); 349 session.deleteObjectByOid(oid); 350 try { 351 tx.commit(); } catch (Exception e) { 353 if (tx.isActive()) { 354 tx.rollback(); 355 } 356 assertTrue(" : MINOR FAILURE TO FIX SOON : "+e.getMessage(), false); 357 } 358 } 359 360 public void makeOidOverInstancePageSize() { 361 int size = session.getInternalSession().getProperties().getInstancePageSize() * 2; 362 tx.begin(); 363 for (int i = 0; i < size; i++) { 364 oids.add(session.createObject(Book.newBook().toArray(), Book.class)); 365 } 366 tx.commit(); 367 368 tx.begin(); 369 for (int i = 0; i < oids.size(); i++) { 370 session.deleteObjectByOid(oids.get(i)); 371 } 372 tx.commit(); 373 } 374 375 private ArrayList oids = new ArrayList(); 376 private HashMap books = new HashMap(); 377 378 381 382 public void define() { 383 super.initSession(true); 384 super.define(Book.getMetaDescription()); 385 Book.counter = 0; 386 } 387 388 public void cleanUp() { 389 super.cleanUp(Book.class); 390 oids.clear(); 391 books.clear(); 392 } 393 394 public void finishTests() { 395 super.finishTests(); 396 } 397 } 398 | Popular Tags |