/** test batch loading. */ @SuppressWarnings("unchecked") @Test public void testBatchLoading2() { System.out.println("Batch-test 2"); PersistenceManager pm = null; Object oid = null; try { pm = TestTools.openPM(); pm.currentTransaction().begin(); DBArrayList<Object> dbv = new DBArrayList<Object>(); for (int i = 0; i < 120; i++) { dbv.add(new PersistentDummyImpl()); } pm.makePersistent(dbv); oid = pm.getObjectId(dbv); pm.currentTransaction().commit(); pm.close(); pm = null; pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); dbv.setBatchSize(110); for (Object o : dbv) { o.getClass(); } pm.currentTransaction().commit(); } finally { if (pm != null) { pm.close(); } } }
/** Test the remove method removes the correct values. */ @Test public void testRemove2() { _dbVector.remove(ELEMENT2); assertEquals("Check element 1", ELEMENT1, _dbVector.get(0)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(1)); assertTrue("Check the number of values", _dbVector.size() == 2); }
/** Run before each test. The setUp method tests the put method. */ @Before public void before() { // create a DBHashtable _dbVector = new DBArrayList<String>(); _dbVector.add(ELEMENT1); _dbVector.add(ELEMENT2); _dbVector.add(ELEMENT3); }
/** Test the set method sets the correct values. */ @Test public void testSet() { _dbVector.set(0, ELEMENT2); _dbVector.set(1, ELEMENT1); assertTrue("Check the number of values", _dbVector.size() == 3); assertEquals("Check element 1", ELEMENT1, _dbVector.get(1)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(0)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(2)); }
/** This used to fail because deserialized DbCollection were marked dirty. */ @SuppressWarnings("unchecked") @Test public void testSerialization() { PersistenceManager pm = TestTools.openPM(); pm.currentTransaction().begin(); DBHashMap<TestClass, TestClass> h1 = new DBHashMap<TestClass, TestClass>(); DBHashMap<TestClass, TestClass> h2 = new DBHashMap<TestClass, TestClass>(); TestClass k = new TestClass(); h2.put(k, new TestClass()); DBArrayList<TestClass> l1 = new DBArrayList<TestClass>(); DBArrayList<TestClass> l2 = new DBArrayList<TestClass>(); l2.add(new TestClass()); pm.makePersistent(h1); pm.makePersistent(h2); pm.makePersistent(k); pm.makePersistent(l1); pm.makePersistent(l2); Object oid1 = pm.getObjectId(h1); Object oid2 = pm.getObjectId(h2); Object oidk = pm.getObjectId(k); Object oid3 = pm.getObjectId(l1); Object oid4 = pm.getObjectId(l2); pm.currentTransaction().commit(); TestTools.closePM(); pm = TestTools.openPM(); pm.currentTransaction().begin(); h1 = (DBHashMap<TestClass, TestClass>) pm.getObjectById(oid1); assertEquals(false, JDOHelper.isDirty(h1)); h2 = (DBHashMap<TestClass, TestClass>) pm.getObjectById(oid2); assertEquals(false, JDOHelper.isDirty(h2)); k = (TestClass) pm.getObjectById(oidk); assertEquals(false, JDOHelper.isDirty(k)); assertEquals(false, JDOHelper.isDirty(h2.get(k))); l1 = (DBArrayList<TestClass>) pm.getObjectById(oid3); assertEquals(false, JDOHelper.isDirty(l1)); l2 = (DBArrayList<TestClass>) pm.getObjectById(oid4); assertEquals(false, JDOHelper.isDirty(l2)); assertEquals(false, JDOHelper.isDirty(l2.get(0))); pm.currentTransaction().commit(); pm.close(); }
/** Test the iterator method iterates over the correct collection. */ @Test public void testIterator() { Iterator<String> i = _dbVector.iterator(); try { assertEquals("Check element 1", ELEMENT1, i.next()); assertEquals("Check element 2", ELEMENT2, i.next()); assertEquals("Check element 3", ELEMENT3, i.next()); } catch (NoSuchElementException e) { fail("This shouldn't happen since _dbHastable should contain " + "three elements"); } assertFalse("Check the number of remaining elements", i.hasNext()); }
/** Test the values method returns the correct collection. */ @Test public void testToArray() { Object[] arrayO = _dbVector.toArray(); assertTrue("Check the number of values", arrayO.length == 3); assertEquals("Check value 1", ELEMENT1, arrayO[0]); assertEquals("Check value 2", ELEMENT2, arrayO[1]); assertEquals("Check value 3", ELEMENT3, arrayO[2]); String[] array = _dbVector.toArray(new String[0]); assertTrue("Check the number of values", array.length == 3); assertEquals("Check value 1", ELEMENT1, array[0]); assertEquals("Check value 2", ELEMENT2, array[1]); assertEquals("Check value 3", ELEMENT3, array[2]); array = _dbVector.toArray(new String[10]); assertTrue("Check the number of values", array.length == 10); assertEquals("Check value 1", ELEMENT1, array[0]); assertEquals("Check value 2", ELEMENT2, array[1]); assertEquals("Check value 3", ELEMENT3, array[2]); assertEquals("Check value 4", null, array[3]); }
/** Test the clear() and size() methods. */ @Test public void testClearAndSize() { assertEquals("Check size 1", 3, _dbVector.size()); _dbVector.clear(); assertEquals("Check size 2", 0, _dbVector.size()); }
/** Run after each test. */ @After public void after() { _dbVector.clear(); }
/** test batch loading. */ @SuppressWarnings("unchecked") @Test public void testBatchLoading() { System.out.println("Batch-test"); PersistenceManager pm = null; Object oid = null; pm = TestTools.openPM(); pm.currentTransaction().begin(); DBArrayList<Object> dbv = new DBArrayList<Object>(); dbv.add("TestString"); for (int i = 0; i < 100; i++) { dbv.add(new PersistentDummyImpl()); } dbv.add("TestString2"); pm.makePersistent(dbv); oid = pm.getObjectId(dbv); pm.currentTransaction().commit(); pm.close(); pm = null; pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); long t1 = System.currentTimeMillis(); for (Object o : dbv) { o.hashCode(); } long t2 = System.currentTimeMillis(); System.out.println("NORMAL: " + (t2 - t1)); pm.currentTransaction().commit(); pm.close(); pm = null; pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); t1 = System.currentTimeMillis(); dbv.setBatchSize(1000); for (Object o : dbv) { o.hashCode(); } t2 = System.currentTimeMillis(); System.out.println("BATCHED: " + (t2 - t1)); pm.currentTransaction().commit(); pm.close(); pm = null; // Close the store and load the stuff pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); t1 = System.currentTimeMillis(); dbv.setBatchSize(1); for (Object o : dbv) { o.hashCode(); } t2 = System.currentTimeMillis(); System.out.println("NORMAL: " + (t2 - t1)); pm.currentTransaction().commit(); pm.close(); pm = null; // Close the store and load the stuff pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); t1 = System.currentTimeMillis(); dbv.setBatchSize(0); for (Object o : dbv) { o.hashCode(); } t2 = System.currentTimeMillis(); System.out.println("BATCHED: " + (t2 - t1)); pm.currentTransaction().commit(); pm.close(); pm = null; // Close the store, load the stuff and test with transient object pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); PersistentDummyImpl dummyTrans = new PersistentDummyImpl(); dbv.add(13, dummyTrans); t1 = System.currentTimeMillis(); dbv.setBatchSize(0); for (Object o : dbv) { o.hashCode(); } t2 = System.currentTimeMillis(); assertEquals(dummyTrans, dbv.get(13)); System.out.println("BATCHED: " + (t2 - t1)); pm.currentTransaction().commit(); pm.close(); pm = null; // Close the store, load the stuff and test with modified object pm = TestTools.openPM(); pm.currentTransaction().begin(); dbv = (DBArrayList<Object>) pm.getObjectById(oid); ((PersistentDummyImpl) dbv.get(18)).setData(new byte[] {15}); t1 = System.currentTimeMillis(); dbv.setBatchSize(0); for (Object o : dbv) { o.hashCode(); } t2 = System.currentTimeMillis(); assertEquals(15, ((PersistentDummyImpl) dbv.get(18)).getData()[0]); System.out.println("BATCHED but dirty: " + (t2 - t1)); pm.currentTransaction().rollback(); TestTools.closePM(); // TODO use setBatch() also for all other tests to verify batch loading! // Or call these tests here again, outside the store.! }
/** Test the addAll methods add the correct values. */ @Test public void testAddAll() { LinkedList<String> l = new LinkedList<String>(); l.add(ELEMENT3); l.add(ELEMENT2); l.add(ELEMENT1); _dbVector.addAll(l); assertTrue("Check the number of values", _dbVector.size() == 6); assertEquals("Check element 1", ELEMENT1, _dbVector.get(0)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(1)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(2)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(3)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(4)); assertEquals("Check element 1", ELEMENT1, _dbVector.get(5)); _dbVector.addAll(1, l); assertTrue("Check the number of values", _dbVector.size() == 9); assertEquals("Check element 1", ELEMENT1, _dbVector.get(0)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(1)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(2)); assertEquals("Check element 1", ELEMENT1, _dbVector.get(3)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(4)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(5)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(6)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(7)); assertEquals("Check element 1", ELEMENT1, _dbVector.get(8)); }
/** Test the get method returns the correct values. */ @Test public void testGet() { assertEquals("Check element 1", ELEMENT1, _dbVector.get(0)); assertEquals("Check element 2", ELEMENT2, _dbVector.get(1)); assertEquals("Check element 3", ELEMENT3, _dbVector.get(2)); }