Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Rollback.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Rollback.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Rollback.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Rollback.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,196 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.jdo.Extent; +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.PointFactory; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.test.util.JDORITestRunner; +import org.apache.jdo.test.util.Util; + +/** +* Tests that we can rollback and restore values. Reads an extent of PCPoint +* objects, and set the coordinate values in them to all be x = 555, y = 1212. +* +* @author Marina Vatkina +*/ +public class Test_Rollback extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Rollback.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + updateObject(false, false); + checkExtent(factory.getPCClass(), numInsert); + } + + /** */ + public void testRestoreValues() throws Exception { + insertObjects(); + updateObject(false, true); + checkExtent(factory.getPCClass(), numInsert); + } + + /** */ + public void testOptimistic() throws Exception { + insertObjects(); + updateObject(true, false); + checkExtent(factory.getPCClass(), numInsert); + } + + /** */ + public void testOptimisticRestoreValues() throws Exception { + insertObjects(); + updateObject(true, true); + checkExtent(factory.getPCClass(), numInsert); + } + + // Gets an extent of points, and changes them. + protected void updateObject(boolean optimistic, boolean restoreValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("\nROLLBACK with RestoreValues: " + restoreValues + + " in " + (optimistic?"OPTIMISTIC":"DATASTORE") + + " Transaction"); + + tx.setOptimistic(optimistic); + tx.setRestoreValues(restoreValues); + tx.setNontransactionalRead(true); + + tx.begin(); + Extent e = pm.getExtent(PCPoint.class, false); + + // Change every *other* value by increasing the existing values by a + // well known (phone) number + int count = 0; + // Use ArrayList to store objects. This allows to verify fields + // without going to the database. + List arr = new ArrayList(); + List oids = new ArrayList(); + for (Iterator i = PCPoint.getSortedIterator(e.iterator()); i.hasNext();) { + PCPoint p = (PCPoint)i.next(); + arr.add(p); + oids.add(pm.getObjectId(p)); + + if (count++ % 2 == 1) { + if (debug) + logger.debug("delete : " + Util.getClassName(p) + + ": " + JDOHelper.getObjectId(p) + ", " + p); + pm.deletePersistent(p); + assertTrue("isDeleted before rollback", JDOHelper.isDeleted(p)); + continue; + } + + int x = p.getX() + 755; + Integer yi = p.getY(); + + int y = 0; + if (null != yi) { // should not happen, but ignore it and continue + y = yi.intValue(); + } + y += 7212; + + if (debug) + logger.debug("update to x=" + x + ", y=" + y + ": " + + Util.getClassName(p) + ": " + + JDOHelper.getObjectId(p) + ", " + p); + + p.setX(x); + p.setY(new Integer(y)); + assertTrue("isDirty before rollback", JDOHelper.isDirty(p)); + assertEquals("getX before rollback", x, p.getX()); + assertEquals("getY before rollback", new Integer(y), p.getY()); + + if (debug) + logger.debug("updated to x=" + x + ", y=" + y + ": " + + Util.getClassName(p) + ": " + + JDOHelper.getObjectId(p) + ", " + p); + } + tx.rollback(); tx = null; + + count = 0; + for (Iterator i = arr.iterator(); i.hasNext();) { + PCPoint p = (PCPoint)i.next(); + if (debug) + logger.debug("restored to x=" + p.x + ", y=" + p.y + ": " + + Util.getClassName(p) + ": " + + JDOHelper.getObjectId(p) + ", " + p); + verifyPCPoint(p, count); + count++; + } + pm.close(); pm = null; + + // read the instances in a new pm and verify that no updates + // are stored in the datastore + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + count = 0; + for (Iterator i = oids.iterator(); i.hasNext();) { + PCPoint p = (PCPoint)pm.getObjectById(i.next(), true); + verifyPCPoint(p, count); + count++; + } + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + private void verifyPCPoint(PCPoint p, int count) + { + assertFalse("isDirty after rollback", JDOHelper.isDirty(p)); + assertFalse("isDeleted after rollback", JDOHelper.isDeleted(p)); + assertEquals("getX after rollback", count, p.getX()); + assertEquals("getY after rollback", new Integer(count), p.getY()); + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc = new PointFactory(); + rc.setVerify(verify); + return rc; + } + + /** */ + protected int getDefaultInsert() + { + return 5; + } +}
Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_RollbackFlushedNew.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_RollbackFlushedNew.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_RollbackFlushedNew.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_RollbackFlushedNew.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,82 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.Collection; +import java.util.Iterator; + +import javax.jdo.PersistenceManager; +import javax.jdo.Query; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * This tests exists to demonstrate that a flushed persistent new instance is + * correctly rolled back. See 4522830. + */ +public class Test_RollbackFlushedNew extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_RollbackFlushedNew.class); + } + + /**/ + public void test() throws Exception + { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try { + tx.setOptimistic(false); + tx.begin(); + + // create new PCPoint + PCPoint newPoint = new PCPoint(5, 10); + pm.makePersistent(newPoint); + + // Create query + Query query = pm.newQuery(PCPoint.class); + query.setFilter("true"); + query.setCandidates(pm.getExtent(PCPoint.class, false)); + // Query.execute flushes the new PCPoint instance + Object result = query.execute(); + + // iterate result set + int nrOfObjects = 0; + for (Iterator i = ((Collection)result).iterator(); i.hasNext();) { + i.next(); + nrOfObjects++; + } + if (nrOfObjects != 1) + throw new Exception("Query result has wrong number of objects; expected 1, actual " + nrOfObjects); + // Rollback the transaction + tx.rollback(); + + checkExtent(PCPoint.class, 0); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,57 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import org.apache.jdo.pc.PCSCO; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* This test tries to insert a persistent capable object into a database; +* the object has a field of each of the kind of SCO types. +* +* @author Dave Bristor +*/ +public class Test_SCO extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCO.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + checkExtent(PCSCO.class, 1); + } + + /** */ + protected void insertObjects() { + insertDate(); + } + + /** + * redefine verify called by readObjects to check whether the read + * instance is correct. + */ + protected void verify(int i, Object pc) { + if (i > 0) + fail("Wrong number of inserted objects, expected only one"); + assertEqualsPCSCO(createDate(), pc); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayList.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayList.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayList.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayList.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,181 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOCollection; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO ArrayList correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOArrayList extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOArrayList.class); + } + + /** */ + public void test() { + insertObjects(); + runArrayListTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runArrayListTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runArrayListTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runArrayListTest(true, true); + } + + /** */ + protected void runArrayListTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + ArrayList c = new ArrayList(); + PCPoint p = new PCPoint(1, 1); + c.add(p); + p = new PCPoint(2, 2); + c.add(p); + + tx.begin(); + PCCollections pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + ArrayList arr = null; + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList: ", arr, 8); + arr.add(p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after add: ", arr, 9); + arr.remove(p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after remove: ", arr, 8); + arr.add(1, p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after add after 1: ", arr, 9); + Object o = arr.remove(1); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after remove #1: ", arr, 8); + if (debug) logger.debug("removed #1: " + o); + assertEquals("Wrong removed instance", p, o); + arr.addAll(c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after addAll 2: ", arr, 10); + arr.removeAll(c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after removeAll 2: ", arr, 8); + arr.addAll(1, c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after addAll 2 from 1: ", arr, 10); + arr.retainAll(c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after retainAll 2: ", arr, 2); + c = (ArrayList) arr.clone(); + c.add(p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after clone: ", arr, 2); + assertNullOwner("Owner of clone: ", (SCOCollection)c); + arr.clear(); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList after clear: ", arr, 0); + pcCollections.setSCOArrayList(new ArrayList()); + PCPoint pcPoint = new PCPoint(42, 99); + pcCollections.getSCOArrayList().add(pcPoint); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + assertCollectionSize("ArrayList restored: ", arr, 8); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void insertObjects() { + insertAllTypes(); + } + + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayListOptimistic.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayListOptimistic.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayListOptimistic.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOArrayListOptimistic.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,215 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Vector; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOCollection; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO ArrayList correctly performs update operations +* in optimistic transactions. +* +* @author Marina Vatkina +*/ +public class Test_SCOArrayListOptimistic extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOArrayListOptimistic.class); + } + + /** */ + public void test() { + insertObjects(); + runArrayListOptimisticTest(); + } + + protected void runArrayListOptimisticTest() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.setOptimistic(true); + tx.setRetainValues(false); + + ArrayList c = new ArrayList(); + PCPoint p = new PCPoint(1, 1); + c.add(p); + p = new PCPoint(2, 2); + c.add(p); + + tx.begin(); + PCCollections pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + assertCollectionSize("ArrayList: ", pcCollections.getSCOArrayList(), 7); + + tx.begin(); + if (debug) + logger.debug ("pcCollections: " + pcCollections.toString()); + tx.commit(); + + tx.begin(); + ArrayList al = new ArrayList(); + pcCollections.setSCOArrayList(al); + + tx.commit(); + + tx.begin(); + ArrayList arr = pcCollections.getSCOArrayList(); + arr.add(p); + tx.commit(); + assertCollectionSize("ArrayList after add: ", + pcCollections.getSCOArrayList(), 8); + + /* Next operation fails after: + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.remove(p); + tx.commit(); + assertCollectionSize("ArrayList after remove: ", pcCollections.getSCOArrayList(), ?); + */ + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.add(1, p); + tx.commit(); + assertCollectionSize("ArrayList after add after 1: ", + pcCollections.getSCOArrayList(), 9); + + /* Next operation fails after: + tx.begin(); + arr = pcCollections.getSCOArrayList(); + Object o = arr.remove(1); + tx.commit(); + assertCollectionSize("ArrayList after remove #1: ", pcCollections.getSCOArrayList(), ?); + if (verbose) logger.debug("removed #1: " + o); + */ + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.addAll(c); + tx.commit(); + assertCollectionSize("ArrayList after addAll 2: ", + pcCollections.getSCOArrayList(), 11); + + /* Next operation fails after: + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.removeAll(c); + tx.commit(); + assertCollectionSize("ArrayList after removeAll 2: ", pcCollections.getSCOArrayList(), ?); + */ + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.addAll(1, c); + tx.commit(); + assertCollectionSize("ArrayList after addAll 2 from 1: ", + pcCollections.getSCOArrayList(), 13); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.retainAll(c); + tx.commit(); + assertCollectionSize("ArrayList after retainAll 2: ", + pcCollections.getSCOArrayList(), 6); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + c = (ArrayList) arr.clone(); + c.add(p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + assertCollectionSize("ArrayList after clone: ", + pcCollections.getSCOArrayList(), 6); + assertNullOwner("Owner of clone: ", (SCOCollection)c); + + tx.begin(); + arr = pcCollections.getSCOArrayList(); + arr.clear(); + tx.commit(); + assertCollectionSize("ArrayList after clear: ", + pcCollections.getSCOArrayList(), 0); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void insertObjects() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + PCCollections pcCollections = new PCCollections(); + + ArrayList al = new ArrayList(); + pcCollections.setSCOArrayList(al); + + Vector v = new Vector(3); + pcCollections.setSCOVector(v); + + LinkedList ll = new LinkedList(); + pcCollections.setSCOLinkedList(ll); + + PCPoint pcPoint = new PCPoint(42, 99); + if (debug) logger.debug("Before makePersistent: " + pcCollections); + + // Next statement allows this to work whether or not reachability or + // navigation work. + pm.makePersistent(pcPoint); + + // The order of this and the previous statements is important! + pm.makePersistent(pcCollections); + if (debug) logger.debug("After makePersistent: " + pcCollections); + tx.commit(); + + + // Next 2 statements allow this to work whether or not reachability or + // navigation work. + oid_point = pm.getObjectId(pcPoint); + oids.add(oid_point); + + oid_collections = pm.getObjectId(pcCollections); + + if (debug) + logger.debug("inserted pcCollections: " + oid_collections); + + oids.add(oid_collections); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCODate.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCODate.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCODate.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCODate.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,200 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCSCO; +import org.apache.jdo.sco.SCO; +import org.apache.jdo.test.util.JDORITestRunner; +import org.apache.jdo.test.util.Util; + +/** +* Tests that we correctly process updates of SCO Date. +* +* @author Marina Vatkina +*/ +public class Test_SCODate extends Test_SCO_Base { + + static GregorianCalendar date; + static { + date = new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + date.set(1969, 7, 20, 20, 0 , 0); + date.set(GregorianCalendar.MILLISECOND, 0); + } + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCODate.class); + } + + /** */ + public void test() { + insertObjects(); + runDateTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runDateTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runDateTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runDateTest(true, true); + } + + protected void runDateTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + tx.begin(); + PCSCO pcSCO = (PCSCO) pm.getObjectById(oid_date, true); + Date sco = pcSCO.getSCODate(); + long orig = sco.getTime(); + tx.commit(); + if (debug) + logger.debug("Date: " + + Util.longFormatter.format(pcSCO.getSCODate())); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setTime(date.getTime().getTime()); + tx.commit(); + if (debug) + logger.debug("Date after setTime to 1969/Aug/20: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setTime to 1969/Aug/20: ", + date.getTime().getTime(), pcSCO.getSCODate().getTime()); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setYear(99); + tx.commit(); + if (debug) + logger.debug("Date after setYear to 1999: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setYear to 1999: ", 99, + pcSCO.getSCODate().getYear()); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setMonth(10); + tx.commit(); + if (debug) + logger.debug("Date after setMonth to Nov: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setMonth to Nov: ", 10, + pcSCO.getSCODate().getMonth()); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setDate(15); + tx.commit(); + if (debug) + logger.debug("Date after setDate to 15: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setDate to 15: ", 15, + pcSCO.getSCODate().getDate()); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setHours(5); + tx.commit(); + if (debug) + logger.debug("Date after setHours to 5: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setHours to 5: ", 5, + pcSCO.getSCODate().getHours()); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setMinutes(25); + tx.commit(); + if (debug) + logger.debug("Date after setMinutes to 25: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setMinutes to 25: ", 25, + pcSCO.getSCODate().getMinutes()); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setSeconds(55); + tx.commit(); + if (debug) + logger.debug("Date after setSeconds to 55: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setSeconds to 55: ", 55, + pcSCO.getSCODate().getSeconds()); + long time = pcSCO.getSCODate().getTime(); + + tx.begin(); + sco = pcSCO.getSCODate(); + Date d = (Date) sco.clone(); + d.setTime(orig); + assertNotIsDirty("Is dirty: ", pcSCO); + tx.commit(); + if (debug) + logger.debug("Date after clone: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after clone: ", time, + pcSCO.getSCODate().getTime()); + assertNullOwner("Owner of clone: ", (SCO)d); + + tx.begin(); + sco = pcSCO.getSCODate(); + sco.setTime(orig); + tx.commit(); + if (debug) + logger.debug("Date after restore: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after restore: ", orig, + pcSCO.getSCODate().getTime()); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void insertObjects() { + insertDate(); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashMap.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashMap.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashMap.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashMap.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,153 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.HashMap; +import java.util.Map; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOMap; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO HashMap correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOHashMap extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOHashMap.class); + } + + /** */ + public void test() { + insertObjects(); + runHashMapTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runHashMapTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runHashMapTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runHashMapTest(true, true); + } + + /** */ + protected void runHashMapTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + Map c = new HashMap(); + PCPoint p = new PCPoint(1, 1); + String s = "abc"; + c.put(s, p); + p = new PCPoint(2, 2); + s = "xyz"; + c.put(s, p); + + tx.begin(); + PCCollections pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + HashMap map = null; + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap: ", map, 9); + map.put(s, p); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap after put: ", map, 10); + map.remove(s); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap after remove: ", map, 9); + map.putAll(c); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap after putAll 2: ", map, 11); + HashMap h = (HashMap) map.clone(); + h.put(s, p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap after clone: ", map, 11); + assertNullOwner("Owner of clone: ",(SCOMap)h); + map.clear(); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap after clear: ", map, 0); + pcCollections.setSCOHashMap(new HashMap()); + PCPoint pcPoint = new PCPoint(42, 99); + pcCollections.getSCOHashMap().put("hello", pcPoint); + + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashMap(); + assertMapSize("HashMap restored: ", map, 9); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void insertObjects() { + insertAllTypes(); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashSet.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashSet.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashSet.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashSet.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,170 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.HashSet; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOCollection; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO HashSet correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOHashSet extends Test_SCO_Base { + + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOHashSet.class); + } + + + /** */ + public void test() { + insertObjects(); + runHashSetTest(false, true); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runHashSetTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runHashSetTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runHashSetTest(true, true); + } + + /** */ + protected void runHashSetTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + tx.setNontransactionalRead(false); + + ArrayList c = new ArrayList(); + PCPoint p = new PCPoint(1, 1); + c.add(p); + p = new PCPoint(2, 2); + c.add(p); + + tx.begin(); + PCCollections pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + HashSet set = null; + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet: ", set, 8); + set.add(p); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after add: ", set, 9); + set.remove(p); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after remove: ", set, 8); + set.addAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after addAll 2: ", set, 10); + set.retainAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after retainAll 2: ", set, 2); + HashSet h = (HashSet) set.clone(); + h.add(p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after clone: ", set, 2); + assertNullOwner("Owner of clone: ", (SCOCollection)h); + set.removeAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after removeAll 2: ", set, 0); + set.addAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after addAll 2: ",set, 2); + set.clear(); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet after clear: ", set, 0); + pcCollections.setSCOHashSet(new HashSet()); + PCPoint pcPoint = new PCPoint(42, 99); + pcCollections.getSCOHashSet().add(pcPoint); + + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOHashSet(); + assertCollectionSize("HashSet restored: ", set, 8); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void insertObjects() { + insertAllTypes(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashtable.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashtable.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashtable.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOHashtable.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,171 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOMap; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO Hashtable correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOHashtable extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOHashtable.class); + } + + /** */ + public void test() { + insertObjects(); + runHashtableTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runHashtableTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runHashtableTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runHashtableTest(true, true); + } + + /** */ + protected void runHashtableTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + Map c = new HashMap(); + PCPoint p = new PCPoint(1, 1); + String s = "abc"; + c.put(s, p); + p = new PCPoint(2, 2); + s = "xyz"; + c.put(s, p); + + tx.begin(); + PCCollections pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + Hashtable map = null; + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable: ", map, 9); + map.put(s, p); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable after put: ", map, 10); + map.remove(s); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable after remove: ", map, 9); + map.putAll(c); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable after putAll 2: ", map, 11); + Hashtable h = (Hashtable) map.clone(); + h.put(s, p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable after clone: ", map, 11); + assertNullOwner("Owner of clone: ", (SCOMap)h); + map.clear(); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable after clear: ", map, 0); + pcCollections.setSCOHashtable(new Hashtable()); + PCPoint pcPoint = new PCPoint(42, 99); + pcCollections.getSCOHashtable().put("hello", pcPoint); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOHashtable(); + assertMapSize("Hashtable restored: ", map, 9); + tx.commit(); + + pm.close(); + // The following block tests the fix for bug 5048297: + // the common.sco.TreeMap missed to redefined some methods that + // need to thaw the map before performing the operation. + // The tests fetch the object using a new pm to make sure the sco + // is not reused from a previous tx. + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + map = pcCollections.getSCOHashtable(); + boolean more = map.elements().hasMoreElements(); + if (debug) + logger.debug("Hashtable.elements().hasMoreElements(): " + more); + assertTrue("Hashtable.elements().hasMoreElements(): ", more); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + // We override this from Test_ActivateClass and insert our own objects + protected void insertObjects() { + insertAllTypes(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOLinkedList.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOLinkedList.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOLinkedList.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOLinkedList.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,221 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.LinkedList; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOCollection; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * Tests that SCO LinkedList correctly performs all update operations + * in both datastore and optimistic transactions. + * + * @author Marina Vatkina + * @version 1.0.1 + */ +public class Test_SCOLinkedList extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOLinkedList.class); + } + + /** */ + public void test() { + insertObjects(); + runLinkedListTest(false, false); + } + + /** */ + public void testRetainValues() { + insertAllTypes(); + runLinkedListTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runLinkedListTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertAllTypes(); + runLinkedListTest(true, true); + } + + /** */ + protected void runLinkedListTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + ArrayList c = new ArrayList(); + PCPoint p = new PCPoint(1, 1); + c.add(p); + p = new PCPoint(2, 2); + c.add(p); + + tx.begin(); + PCCollections pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + LinkedList arr = null; + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList: ", arr, 9); + arr.add(p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after add: ", arr, 10); + arr.remove(p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after remove: ", arr, 9); + arr.addFirst(p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after addFirst: ", arr, 10); + if (debug) logger.debug("Element added as #: " + arr.indexOf(p)); + assertEquals("Element added as #: ", 0, arr.indexOf(p)); + Object o = arr.removeFirst(); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after removeFirst: ", arr, 9); + if (debug) logger.debug("Element was removed: " + o); + assertEquals("Element was removed: ", p, o); + arr.addLast(p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after addLast: ", arr, 10); + if (debug) logger.debug("Element added as #: " + arr.indexOf(p)); + assertEquals("Element added as #: ", 9, arr.indexOf(p)); + o = arr.removeLast(); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after removeLast: ", arr, 9); + if (debug) logger.debug("Element was removed: " + o); + assertEquals("Element was removed: ", p, o); + arr.add(1, p); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after add #1: ", arr, 10); + o = arr.remove(1); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after remove #1: ", arr, 9); + if (debug) logger.debug("removed #1: " + o); + assertEquals("removed #1: ", p, o); + arr.addAll(c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after addAll 2: ", arr, 11); + arr.removeAll(c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after removeAll 2: ", arr, 9); + arr.addAll(1, c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after addAll 2 from 1: ", arr, 11); + arr.retainAll(c); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after retainAll 2: ", arr, 2); + LinkedList v = (LinkedList) arr.clone(); + v.add(p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after clone: ", arr, 2); + assertNullOwner("Owner of clone: ", (SCOCollection)v); + o = arr.set(1, o); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after set #1: ", arr, 2); + if (debug) logger.debug("Replaced #1: " + o); + assertEquals("Replaced #1: ", p, o); + arr.clear(); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList after clear: ", arr, 0); + pcCollections.setSCOLinkedList(new LinkedList()); + PCPoint pcPoint = new PCPoint(42, 99); + pcCollections.getSCOLinkedList().add(pcPoint); + tx.commit(); + + tx.begin(); + arr = pcCollections.getSCOLinkedList(); + assertCollectionSize("LinkedList restored: ", pcCollections.getSCOLinkedList(), 9); + tx.commit(); + } + + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void insertObjects() { + insertAllTypes(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCORollback.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCORollback.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCORollback.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCORollback.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,225 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.HashSet; +import java.util.TimeZone; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.PCSCO; +import org.apache.jdo.test.util.JDORITestRunner; +import org.apache.jdo.test.util.Util; + +/** +* This test verifies the expected behavior of rollback operation +* on an instance with references to SCO objects. +* +* @author Marina Vatkina +*/ +public class Test_SCORollback extends Test_SCO_Base { + + static GregorianCalendar date; + static { + date = new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + date.set(1979, 7, 21, 20, 0 , 0); + date.set(GregorianCalendar.MILLISECOND, 0); + } + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCORollback.class); + } + + /** */ + public void test() + { + insertDate(); + insertAllTypes(); + updateObjects(false, false); + } + + /** */ + public void testRestoreValues() + { + insertDate(); + insertAllTypes(); + updateObjects(false, true); + } + + /** */ + public void testOptimistic() + { + insertDate(); + insertAllTypes(); + updateObjects(true, false); + } + + /** */ + public void testOptimisticRestoreValues() + { + insertDate(); + insertAllTypes(); + updateObjects(true, true); + } + + /** */ + public void testInsertAllRollback() + { + insertAllRollback(false, false); + } + + /** */ + public void testInsertAllRollbackRestoreValues() + { + insertAllRollback(false, true); + } + + /** */ + public void testInsertAllRollbackOptimistic() + { + insertAllRollback(true, false); + } + + /** */ + public void testInsertAllRollbackOptimisticRestoreValues() + { + insertAllRollback(true, true); + } + + protected void updateObjects(boolean optimistic, boolean restoreValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("\nUPDATE-ROLLBACK " + (optimistic?"optimistic":"datastore") + + " transactions with RestoreValues is set to " + + (restoreValues ? "TRUE" : "FALSE")); + tx.setOptimistic(optimistic); + tx.setRestoreValues(restoreValues); + + PCPoint p = new PCPoint(2, 2); + + tx.begin(); + pcSCO = (PCSCO) pm.getObjectById(oid_date, true); + Date sco = pcSCO.getSCODate(); + pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + HashSet hs = pcCollections.getSCOHashSet(); + + if (debug) + logger.debug("Date before update: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date before update: ", + Util.moonWalkDate.getTime().getTime(), + pcSCO.getSCODate().getTime()); + assertCollectionSize("HashSet before update: ", + pcCollections.getSCOHashSet(), 8); + + sco.setTime(date.getTime().getTime()); + hs.add(p); + if (debug) + logger.debug("Date after setTime to 1979/Aug/20: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setTime to 1979/Aug/20: ", + date.getTime().getTime(), pcSCO.getSCODate().getTime()); + assertCollectionSize("HashSet after add 1 element: ", + pcCollections.getSCOHashSet(), 9); + + tx.rollback(); + if (debug) + logger.debug("Date after rollback: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after rollback: ", + Util.moonWalkDate.getTime().getTime(), + pcSCO.getSCODate().getTime()); + assertCollectionSize("HashSet after rollback: ", + pcCollections.getSCOHashSet(), 8); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + protected void insertAllRollback(boolean optimistic, boolean restoreValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("\nINSERT-ROLLBACK " + (optimistic?"optimistic":"datastore") + + " transactions with RestoreValues is set to " + + ((restoreValues)? "TRUE" : "FALSE")); + tx.setOptimistic(optimistic); + tx.setRestoreValues(restoreValues); + tx.begin(); + + insertDateWithoutCommit(pm); + insertAllTypesWithoutCommit(pm); + + if (debug) + logger.debug("Date after makePersistent: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after makePersistent: ", + Util.moonWalkDate.getTime().getTime(), + pcSCO.getSCODate().getTime()); + assertCollectionSize("HashSet after makePersistent: ", + pcCollections.getSCOHashSet(), 8); + + PCPoint p = new PCPoint(2, 2); + + Date sco = pcSCO.getSCODate(); + HashSet hs = pcCollections.getSCOHashSet(); + sco.setTime(date.getTime().getTime()); + hs.add(p); + + if (debug) + logger.debug("Date after setTime to 1979/Aug/21: " + + Util.longFormatter.format(pcSCO.getSCODate())); + assertEquals("Date after setTime to 1979/Aug/21: ", + date.getTime().getTime(), pcSCO.getSCODate().getTime()); + assertCollectionSize("HashSet after replace: ", + pcCollections.getSCOHashSet(), 9); + + tx.rollback(); + sco = pcSCO.getSCODate(); + hs = pcCollections.getSCOHashSet(); + if (debug) + logger.debug("Date after rollback: " + + ((sco == null)? "NULL" : Util.longFormatter.format(sco))); + assertEquals("Date after rollback: ", + (restoreValues ? Util.moonWalkDate.getTime().getTime() : + date.getTime().getTime()), + sco.getTime()); + assertCollectionSize("HashSet after rollback: ", hs, + (restoreValues ? 8 : 9)); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOSqlDate.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOSqlDate.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOSqlDate.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOSqlDate.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,169 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.sql.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCSCO; +import org.apache.jdo.sco.SCO; +import org.apache.jdo.test.util.JDORITestRunner; +import org.apache.jdo.test.util.Util; + +/** +* Tests that we correctly process updates of SCO java.sql.Date. +* +* @author Marina Vatkina +*/ +public class Test_SCOSqlDate extends Test_SCO_Base { + + static GregorianCalendar date; + static { + date = new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + date.set(1969, 7, 20, 20, 0 , 0); + date.set(GregorianCalendar.MILLISECOND, 0); + } + + Object oid = null; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOSqlDate.class); + } + + /** */ + public void test() { + insertObjects(); + runSqlDateTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runSqlDateTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runSqlDateTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runSqlDateTest(true, true); + } + + /** */ + protected void runSqlDateTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + tx.begin(); + PCSCO pcSCO = (PCSCO) pm.getObjectById(oid_date, true); + Date sco = pcSCO.getSCOSqlDate(); + long orig = sco.getTime(); + tx.commit(); + if (debug) + logger.debug("SqlDate: " + + Util.longFormatter.format(pcSCO.getSCOSqlDate())); + + tx.begin(); + sco = pcSCO.getSCOSqlDate(); + sco.setTime(date.getTime().getTime()); + tx.commit(); + if (debug) + logger.debug("SqlDate after setTime to 1969/Aug/20: " + + Util.longFormatter.format(pcSCO.getSCOSqlDate())); + assertEquals("SqlDate after setTime to 1969/Aug/20: ", + date.getTime().getTime(), + pcSCO.getSCOSqlDate().getTime()); + + tx.begin(); + sco = pcSCO.getSCOSqlDate(); + sco.setYear(99); + tx.commit(); + if (debug) + logger.debug("SqlDate after setYear to 1999: " + + Util.longFormatter.format(pcSCO.getSCOSqlDate())); + assertEquals("SqlDate after setYear to 1999: ", 99, + pcSCO.getSCOSqlDate().getYear()); + + tx.begin(); + sco = pcSCO.getSCOSqlDate(); + sco.setMonth(10); + tx.commit(); + if (debug) + logger.debug("SqlDate after setMonth to Nov: " + + Util.longFormatter.format(pcSCO.getSCOSqlDate())); + assertEquals("SqlDate after setMonth to Nov: ", 10, + pcSCO.getSCOSqlDate().getMonth()); + + tx.begin(); + sco = pcSCO.getSCOSqlDate(); + sco.setDate(15); + tx.commit(); + if (debug) + logger.debug("SqlDate after setDate to 15: " + + Util.longFormatter.format(pcSCO.getSCOSqlDate())); + assertEquals("SqlDate after setDate to 15: ", 15, + pcSCO.getSCOSqlDate().getDate()); + long time = pcSCO.getSCOSqlDate().getTime(); + + tx.begin(); + sco = pcSCO.getSCOSqlDate(); + Date d = (Date) sco.clone(); + d.setTime(orig); + assertNotIsDirty("Is dirty: ", pcSCO); + tx.commit(); + assertEquals("SqlDate after clone: ", time, + pcSCO.getSCOSqlDate().getTime()); + assertNullOwner("Owner of clone: ", (SCO)d); + + tx.begin(); + sco = pcSCO.getSCOSqlDate(); + sco.setTime(orig); + tx.commit(); + if (debug) + logger.debug("SqlDate after restore: " + + Util.longFormatter.format(pcSCO.getSCOSqlDate())); + assertEquals("Date after restore: ", orig, + pcSCO.getSCOSqlDate().getTime()); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + protected void insertObjects() { + insertDate(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeMap.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeMap.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeMap.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeMap.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,182 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.HashMap; +import java.util.Map; +import java.util.TreeMap; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.sco.SCOMap; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO TreeMap correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOTreeMap extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOTreeMap.class); + } + + /** */ + public void test() { + insertObjects(); + runTreeMapTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runTreeMapTest(false, true); + } + + /** */ + public void testOptimitic() { + insertObjects(); + runTreeMapTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runTreeMapTest(true, true); + } + + /** */ + protected void runTreeMapTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + Map c = new HashMap(); + PCPoint p = new PCPoint(1, 1); + Double s = new Double(123); + c.put(s, p); + p = new PCPoint(2, 2); + s = new Double(1230); + c.put(s, p); + + tx.begin(); + PCCollections pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + TreeMap map = null; + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap: ", map, 9); + map.put(s, p); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap after put: ", map, 10); + map.remove(s); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap after remove: ", map, 9); + map.putAll(c); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap after putAll 2: ", map, 11); + TreeMap h = (TreeMap) map.clone(); + h.put(s, p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap after clone: ", map, 11); + assertNullOwner("Owner of clone: ", (SCOMap)h); + map.clear(); + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap after clear: ", map, 0); + pcCollections.setSCOTreeMap(new TreeMap()); + map = pcCollections.getSCOTreeMap(); + PCPoint pcPoint = new PCPoint(42, 99); + map.put(new Double(300000), pcPoint); + + tx.commit(); + + tx.begin(); + map = pcCollections.getSCOTreeMap(); + assertMapSize("TreeMap restored: ", map, 9); + tx.commit(); tx = null; + pm.close(); pm = null; + + // The following block tests the fix for bug 5048297: + // the common.sco.TreeMap missed to redefined some methods that + // need to thaw the map before performing the operation. + // The tests fetch the object using a new pm to make sure the sco + // is not reused from a previous tx. + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + map = pcCollections.getSCOTreeMap(); + Map tailMap = map.tailMap(new Double(200000)); + // the size should be 1 + assertMapSize("TreeMap.tailMap().size(): ", tailMap, 1); + tx.commit(); tx = null; + pm.close(); pm = null; + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + pcCollections = (PCCollections) pm.getObjectById(oid_collections, true); + map = pcCollections.getSCOTreeMap(); + Object firstKey = map.firstKey(); + if (debug) logger.debug("TreeMap.firstKey: " + firstKey); + assertEquals("TreeMap.firstKey: ", new Double(-123.0), firstKey); + tx.commit(); tx = null; + pm.close(); pm = null; + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + // We override this from Test_ActivateClass and insert our own objects + protected void insertObjects() { + insertAllTypes(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeSet.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeSet.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeSet.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOTreeSet.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,194 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.Set; +import java.util.TreeSet; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.sco.SCOCollection; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that SCO TreeSet correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOTreeSet extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOTreeSet.class); + } + + /** */ + public void test() { + insertObjects(); + runTreeSetTest(false, false); + } + + /** */ + public void testRetainValues() { + insertObjects(); + runTreeSetTest(false, true); + } + + /** */ + public void testOptimistic() { + insertObjects(); + runTreeSetTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runTreeSetTest(true, true); + } + + /** */ + protected void runTreeSetTest(boolean optimistic, boolean retainValues) { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datastore") + + " transactions with retainValues = " + retainValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + ArrayList c = new ArrayList(); + Double p = new Double(123); + c.add(p); + p = new Double(1230); + c.add(p); + + tx.begin(); + PCCollections pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + TreeSet set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet: ", set, 7); + set.add(p); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after add: ", set, 8); + set.remove(p); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after remove: ", set, 7); + set.addAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after addAll 2: ", set, 9); + TreeSet h = (TreeSet) set.clone(); + h.add(p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after clone: ", set, 9); + assertNullOwner("Owner of clone: ", (SCOCollection)h); + set.removeAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after removeAll 2: ", set, 7); + set.addAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after addAll 2: ", set, 9); + set.retainAll(c); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after retainAll 2: ", set, 2); + set.clear(); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet after clear: ", set, 0); + pcCollections.setSCOTreeSet(new TreeSet()); + tx.commit(); + + tx.begin(); + set = pcCollections.getSCOTreeSet(); + assertCollectionSize("TreeSet restored: ", set, 7); + tx.commit(); tx = null; + pm.close(); pm = null; + + // The following block tests the fix for bug 5048297: + // the common.sco.TreeSet missed to redefined some methods that + // need to thaw the map before performing the operation. + // The tests fetch the object using a new pm to make sure the sco + // is not reused from a previous tx. + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + set = pcCollections.getSCOTreeSet(); + Set headSet = set.headSet(new Double(1230)); + assertCollectionSize("TreeSet.headSet().size(): ", headSet, 6); + tx.commit(); tx = null; + pm.close(); pm = null; + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + pcCollections = + (PCCollections) pm.getObjectById(oid_collections, true); + set = pcCollections.getSCOTreeSet(); + Object first = set.first(); + if (debug) logger.debug("TreeSet.first(): " + first); + assertEquals("TreeSet.first(): ", new Double(-123.0), first); + tx.commit(); tx = null; + pm.close(); pm = null; + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + // We override this from Test_ActivateClass and insert our own objects + protected void insertObjects() { + insertAllTypes(); + } +}