From jdo-commits-return-245-apmail-db-jdo-commits-archive=www.apache.org@db.apache.org Sat Mar 19 05:17:08 2005 Return-Path: Delivered-To: apmail-db-jdo-commits-archive@www.apache.org Received: (qmail 88454 invoked from network); 19 Mar 2005 05:17:08 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 19 Mar 2005 05:17:08 -0000 Received: (qmail 93856 invoked by uid 500); 19 Mar 2005 05:17:07 -0000 Mailing-List: contact jdo-commits-help@db.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jdo-dev@db.apache.org Delivered-To: mailing list jdo-commits@db.apache.org Delivered-To: moderator for jdo-commits@db.apache.org Received: (qmail 90639 invoked by uid 99); 19 Mar 2005 01:13:01 -0000 X-ASF-Spam-Status: No, hits=-9.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Message-ID: <20050319010617.28365.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Mailer: svnmailer-1.0.0-dev Date: Sat, 19 Mar 2005 01:06:16 -0000 Subject: svn commit: r158176 [70/79] - in incubator/jdo/trunk/ri11: ./ src/ src/conf/ src/java/ src/java/org/ src/java/org/apache/ src/java/org/apache/jdo/ src/java/org/apache/jdo/ejb/ src/java/org/apache/jdo/enhancer/ src/java/org/apache/jdo/impl/ src/java/org/apache/jdo/impl/enhancer/ src/java/org/apache/jdo/impl/enhancer/classfile/ src/java/org/apache/jdo/impl/enhancer/core/ src/java/org/apache/jdo/impl/enhancer/generator/ src/java/org/apache/jdo/impl/enhancer/meta/ src/java/org/apache/jdo/impl/enhancer/meta/model/ src/java/org/apache/jdo/impl/enhancer/meta/prop/ src/java/org/apache/jdo/impl/enhancer/meta/util/ src/java/org/apache/jdo/impl/enhancer/util/ src/java/org/apache/jdo/impl/fostore/ src/java/org/apache/jdo/impl/jdoql/ src/java/org/apache/jdo/impl/jdoql/jdoqlc/ src/java/org/apache/jdo/impl/jdoql/scope/ src/java/org/apache/jdo/impl/jdoql/tree/ src/java/org/apache/jdo/impl/model/ src/java/org/apache/jdo/impl/model/java/ src/java/org/apache/jdo/impl/model/java/runtime/ src/java/org/apache/jdo/impl/model/jdo/ src/java/org/apache/jdo/impl/model/jdo/caching/ src/java/org/apache/jdo/impl/model/jdo/util/ src/java/org/apache/jdo/impl/model/jdo/xml/ src/java/org/apache/jdo/impl/pm/ src/java/org/apache/jdo/impl/sco/ src/java/org/apache/jdo/impl/state/ src/java/org/apache/jdo/jdoql/ src/java/org/apache/jdo/jdoql/tree/ src/java/org/apache/jdo/model/ src/java/org/apache/jdo/model/java/ src/java/org/apache/jdo/model/jdo/ src/java/org/apache/jdo/pm/ src/java/org/apache/jdo/sco/ src/java/org/apache/jdo/state/ src/java/org/apache/jdo/store/ src/java/org/apache/jdo/util/ test/ test/conf/ test/enhancer/ test/enhancer/sempdept/ test/enhancer/sempdept/src/ test/enhancer/sempdept/src/empdept/ test/fsuid2/ test/fsuid2/org/ test/fsuid2/org/apache/ test/fsuid2/org/apache/jdo/ test/fsuid2/org/apache/jdo/pc/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/jdo/ test/java/org/apache/jdo/impl/ test/java/org/apache/jdo/impl/fostore/ test/java/org/apache/jdo/pc/ test/java/org/apache/jdo/pc/appid/ test/java/org/apache/jdo/pc/empdept/ test/java/org/apache/jdo/pc/serializable/ test/java/org/apache/jdo/pc/xempdept/ test/java/org/apache/jdo/test/ test/java/org/apache/jdo/test/query/ test/java/org/apache/jdo/test/util/ test/jdo/ test/jdo/org/ test/jdo/org/apache/ test/jdo/org/apache/jdo/ test/jdo/org/apache/jdo/pc/ test/jdo/org/apache/jdo/pc/appid/ test/jdo/org/apache/jdo/pc/empdept/ test/jdo/org/apache/jdo/pc/serializable/ test/jdo/org/apache/jdo/pc/xempdept/ xdocs/ To: jdo-commits@db.apache.org From: mbo@apache.org X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOVecto= r=2Ejava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_SCOVector.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOVector.j= ava (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCOVector.j= ava Fri Mar 18 17:02:29 2005 @@ -0,0 +1,233 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +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 Vector correctly performs all update operations +* in both datastore and optimistic transactions. +* +* @author Marina Vatkina +* @version 1.0.1 +*/ +public class Test_SCOVector extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SCOVector.class); + } + + /** */ + public void test() { + insertObjects(); + runVectorTest(false, false); + } + =20 + /** */ + public void testRetainValues() { + insertObjects(); + runVectorTest(false, true); + } + =20 + /** */ + public void testOptimistic() { + insertObjects(); + runVectorTest(true, false); + } + + /** */ + public void testOptimisticRetainValues() { + insertObjects(); + runVectorTest(true, true); + } + + /** */ + protected void runVectorTest(boolean optimistic, boolean retainValues)= { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datast= ore") +=20 + " transactions with retainValues =3D " + reta= inValues); + tx.setOptimistic(optimistic); + tx.setRetainValues(retainValues); + + ArrayList c =3D new ArrayList(); + PCPoint p =3D new PCPoint(1, 1); + c.add(p); + p =3D new PCPoint(2, 2); + c.add(p); + + tx.begin(); + PCCollections pcCollections =3D + (PCCollections) pm.getObjectById(oid_collections, true); + tx.commit(); + + tx.begin(); + Vector arr =3D null; + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector: ", arr, 9); + arr.add(p); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after add: ", arr, 10); + arr.remove(p); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after remove: ", arr, 9); + arr.addElement(p); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after addElement: ", arr, 10); + if (debug) logger.debug("Element added as #: " + arr.indexOf(p= )); + assertEquals("Element added as #: ", 9, arr.indexOf(p)); + boolean b =3D arr.removeElement(p); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after removeElement: ", arr, 9); + if (debug) logger.debug("Element was removed: " + b); + assertTrue("Element not removed", b); + arr.setElementAt(p, 1); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after setElementAt: ", arr, 9); + if (debug) logger.debug("Element set as #: " + arr.indexOf(p)); + assertEquals("Element set as #: ", 1, arr.indexOf(p)); + arr.removeElementAt(1); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after removeElementAt #1: ", arr,= 8); + arr.add(1, p); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after add #1: ", arr, 9); + Object o =3D arr.remove(1); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after remove #1: ", arr, 8); + if (debug) logger.debug("removed #1: " + o); + assertEquals("removed #1: ", p, o); + arr.addAll(c); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after addAll 2: ", arr, 10); + arr.removeAll(c); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after removeAll 2: ", arr, 8); + arr.addAll(1, c); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after addAll 2 from 1: ", arr, 10= ); + arr.retainAll(c); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after retainAll 2: ", arr, 2); + arr.insertElementAt(p, 1); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after insertElementAt #1: ", arr,= 3); + Vector v =3D (Vector) arr.clone(); + v.add(p); + assertNotIsDirty("Is dirty: ", pcCollections); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after clone: ", arr, 3); + assertNullOwner("Owner of clone: ", (SCOCollection)v); + arr.removeAllElements(); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after removeAllElements: ", arr, = 0); + pcCollections.setSCOVector(new Vector()); + arr =3D pcCollections.getSCOVector(); + o =3D arr.set(1, p); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after restore and set #1: ", arr,= 8); + if (debug) logger.debug("Replaced #1: " + o); + assertEquals("Replaced #1: ", "Call me Ishmael.", o); + arr.clear(); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector after clear: ", arr, 0); + pcCollections.setSCOVector(new Vector()); + PCPoint pcPoint =3D new PCPoint(42, 99); + pcCollections.getSCOVector().add(pcPoint); + tx.commit(); + + tx.begin(); + arr =3D pcCollections.getSCOVector(); + assertCollectionSize("Vector restored: ", arr, 9); + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + =20 + /** */ + protected void insertObjects() { + insertAllTypes(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO_Base= .java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_SCO_Base.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO_Base.ja= va (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SCO_Base.ja= va Fri Mar 18 17:02:29 2005 @@ -0,0 +1,419 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.LinkedList; +import java.util.Map; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Vector; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.impl.sco.Freezer; +import org.apache.jdo.pc.PCCollections; +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.PCSCO; +import org.apache.jdo.sco.SCO; + +/** +* This test is a base class for all SCO tests. It cannot be run itself. +* +* @author Craig Russell +*/ +public class Test_SCO_Base extends Test_Fetch { + + protected Object oid_point =3D null; + protected Object oid_date =3D null; + protected Object oid_collections =3D null; + + protected PCPoint pcPoint =3D null; + protected PCCollections pcCollections =3D null; + protected PCSCO pcSCO =3D null; + + /** this method can be called from insertObjects in subclasses */ + protected void insertDate() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + tx.begin(); + insertDateWithoutCommit(pm); + tx.commit(); + =20 + oid_date =3D pm.getObjectId(pcSCO); + if (debug) logger.debug("inserted pcSCO: " + oid_date); + oids.add(oid_date); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + =20 + /** Allows to commit or rollback the transaction. + * This method can be called from insertObjects in subclasses + */ + protected void insertDateWithoutCommit(PersistenceManager pm) { + pcSCO =3D createDate(); + if (debug) logger.debug("Before makePersistent: " + pcSCO); + pm.makePersistent(pcSCO); + if (debug) logger.debug("After makePersistent: " + pcSCO); + } + + /** this method can be called from insertObjects in subclasses */ + protected void insertAllTypes() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + tx.begin(); + insertAllTypesWithoutCommit(pm); + tx.commit(); + =20 + // Next 2 statements allow this to work whether or not=20 + // reachability or navigation work. + oid_point =3D pm.getObjectId(pcPoint); + oids.add(oid_point); + =20 + oid_collections =3D pm.getObjectId(pcCollections); + if (debug)=20 + logger.debug("inserted pcCollections: " + oid_collections); + oids.add(oid_collections); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** Allows to commit or rollback the transaction. + * this method can be called from insertObjects in subclasses + */ + protected void insertAllTypesWithoutCommit(PersistenceManager pm) { + + pcPoint =3D createPoint(); + pcCollections =3D createAllTypes(pcPoint); + 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); + + } + + /** */ + protected PCSCO createDate() { + PCSCO pcSCO =3D new PCSCO(); + pcSCO.init(); + // Note, the value of the specified Date is changed by setSCODate + pcSCO.setSCODate(new Date()); + return pcSCO; + } + =20 + /** */ + protected PCPoint createPoint() { + return new PCPoint(42, 99); + } + + /** */ + protected PCCollections createAllTypes(PCPoint pcPoint) { + PCCollections pcCollections =3D new PCCollections(); + pcCollections.init(); + =20 + ArrayList al =3D new ArrayList(3); + pcCollections.setSCOArrayList(al); + + Vector v =3D new Vector(3); + pcCollections.setSCOVector(v); + =20 + HashSet hs =3D new HashSet(3); + pcCollections.setSCOHashSet(hs); + + LinkedList ll =3D new LinkedList(); + pcCollections.setSCOLinkedList(ll); + =20 + HashMap hm =3D new HashMap(3); + pcCollections.setSCOHashMap(hm); + =20 + Hashtable ht =3D new Hashtable(3); + pcCollections.setSCOHashtable(ht); + =20 + TreeMap tm =3D new TreeMap(); + pcCollections.setSCOTreeMap(tm); + =20 + TreeSet ts =3D new TreeSet(); + pcCollections.setSCOTreeSet(ts); + + pcCollections.addPoint(pcPoint); + =20 + return pcCollections; + } + + /** */ + protected void assertEqualsPCSCO(PCSCO expected, Object actual) + { + assertNotNull("Actual instance is null", actual); + assertTrue("Expected PCSCO instance; instance has type " +=20 + actual.getClass().getName(), (actual instanceof PCSCO)); + + // Note, this implementation=20 + // - assumes all _nullXXX fields are null + // - assumes all _XXX fields are not null in this instance + // - compares the time for all date fields + =20 + PCSCO other =3D (PCSCO)actual; + assertEquals("Wrong _date value", expected._date, other._date); + assertEquals("Wrong _nullDate value",=20 + expected._nullDate, other._nullDate); + assertEquals("Wrong _scoDate value", expected._scoDate, other._sco= Date); + assertEquals("Wrong _nullSCODate value", + expected._nullSCODate, other._nullSCODate); + assertEquals("Wrong _sqlDate value", expected._sqlDate, other._sql= Date); + assertEquals("Wrong _nullSqlDate value", + expected._nullSqlDate, other._nullSqlDate); + assertEquals("Wrong _bigDecimal value",=20 + expected._bigDecimal, other._bigDecimal); + assertEquals("Wrong _nullBigInteger value",=20 + expected._nullBigInteger, other._nullBigInteger); + assertEquals("Wrong _bigInteger value",=20 + expected._bigInteger, other._bigInteger); + assertEquals("Wrong _nullBigInteger value", + expected._nullBigInteger, other._nullBigInteger); + assertEquals("Wrong _bitSet value", expected._bitSet, other._bitSe= t); + assertEquals("Wrong _nullBitSet value", + expected._nullBitSet, other._nullBitSet); + assertEquals("Wrong _locale value", expected._locale, other._local= e); + assertEquals("Wrong _nullLocale value", + expected._nullLocale, other._nullLocale); + } + =20 + /** */ + protected void assertEqualsPCCollections(PCCollections expected, + Object actual) + { + assertNotNull("Actual instance is null", actual); + assertTrue("Expected PCCollections instance; instance has type " + + actual.getClass().getName(),=20 + (actual instanceof PCCollections)); + =20 + PCCollections other =3D (PCCollections)actual; + + assertEqualsCollection("Wrong _arrayList value", + expected._arrayList, other._arrayList); + assertEquals("Wrong _emptyArrayList value", + expected._emptyArrayList, other._emptyArrayList); + assertEquals("Wrong _nullArrayList value", + expected._nullArrayList, other._nullArrayList); + assertEqualsCollection("Wrong _sco_arrayList value", + expected._sco_arrayList, other._sco_arrayLi= st); + assertEquals("Wrong _sco_emptyArrayList value", + expected._sco_emptyArrayList, other._sco_emptyArrayLi= st); + assertEquals("Wrong _sco_nullArrayList value", + expected._sco_nullArrayList, other._sco_nullArrayList= ); + assertEqualsCollection("Wrong _vector value", + expected._vector, other._vector); + assertEquals("Wrong _emptyVector value", + expected._emptyVector, other._emptyVector); + assertEquals("Wrong _nullVector value", + expected._nullVector, other._nullVector); + assertEqualsCollection("Wrong _sco_vector value", + expected._sco_vector, other._sco_vector); + assertEquals("Wrong _sco_emptyVector value", + expected._sco_emptyVector, other._sco_emptyVector); + assertEquals("Wrong _sco_nullVector value", + expected._sco_nullVector, other._sco_nullVector); + assertEqualsMap("Wrong _hashMap value", + expected._hashMap, other._hashMap); + assertEquals("Wrong _emptyHashMap value", + expected._emptyHashMap, other._emptyHashMap); + assertEquals("Wrong _nullHashMap value", + expected._nullHashMap, other._nullHashMap); + assertEqualsMap("Wrong _sco_hashMap value", + expected._sco_hashMap, other._sco_hashMap); + assertEquals("Wrong _sco_emptyHashMap value", + expected._sco_emptyHashMap, other._sco_emptyHashMap); + assertEquals("Wrong _sco_nullHashMap value", + expected._sco_nullHashMap, other._sco_nullHashMap); + assertEqualsMap("Wrong _hashtable value", + expected._hashtable, other._hashtable); + assertEquals("Wrong _sco_emptyHashtable value", + expected._sco_emptyHashtable, other._sco_emptyHashtab= le); + assertEquals("Wrong _sco_nullHashtable value", + expected._sco_nullHashtable, other._sco_nullHashtable= ); + assertEqualsCollection("Wrong _hashSet value", + expected._hashSet, other._hashSet); + assertEquals("Wrong _emptyHashSet value", + expected._emptyHashSet, other._emptyHashSet); + assertEquals("Wrong _nullHashSet value", + expected._nullHashSet, other._nullHashSet); + assertEqualsCollection("Wrong _sco_hashSet value", + expected._sco_hashSet, other._sco_hashSet); + assertEquals("Wrong _sco_emptyHashSet value", + expected._sco_emptyHashSet, other._sco_emptyHashSet); + assertEquals("Wrong _sco_nullHashSet value", + expected._sco_nullHashSet, other._sco_nullHashSet); + assertEqualsCollection("Wrong _linkedList value", + expected._linkedList, other._linkedList); + assertEquals("Wrong _emptyLinkedList value", + expected._emptyLinkedList, other._emptyLinkedList); + assertEquals("Wrong _nullLinkedList value", + expected._nullLinkedList, other._nullLinkedList); + assertEqualsCollection("Wrong _sco_linkedList value", + expected._sco_linkedList, other._sco_linked= List); + assertEquals("Wrong _sco_emptyLinkedList value", + expected._sco_emptyLinkedList, other._sco_emptyLinked= List); + assertEquals("Wrong _sco_nullLinkedList value", + expected._sco_nullLinkedList, other._sco_nullLinkedLi= st); + assertEqualsMap("Wrong _treeMap value", + expected._treeMap, other._treeMap); + assertEquals("Wrong _emptyTreeMap value", + expected._emptyTreeMap, other._emptyTreeMap); + assertEquals("Wrong _nullTreeMap value", + expected._nullTreeMap, other._nullTreeMap); + assertEqualsMap("Wrong _sco_treeMap value", + expected._sco_treeMap, other._sco_treeMap); + assertEquals("Wrong _sco_emptyTreeMap value", + expected._sco_emptyTreeMap, other._sco_emptyTreeMap); + assertEquals("Wrong _sco_nullTreeMap value", + expected._sco_nullTreeMap, other._sco_nullTreeMap); + assertEqualsCollection("Wrong _treeSet value", + expected._treeSet, other._treeSet); + assertEquals("Wrong _emptyTreeSet value", + expected._emptyTreeSet, other._emptyTreeSet); + assertEquals("Wrong _nullTreeSet value", + expected._nullTreeSet, other._nullTreeSet); + assertEqualsCollection("Wrong _sco_treeSet value", + expected._sco_treeSet, other._sco_treeSet); + assertEquals("Wrong _sco_emptyTreeSet value", + expected._sco_emptyTreeSet, other._sco_emptyTreeSet); + assertEquals("Wrong _sco_nullTreeSet value", + expected._sco_nullTreeSet, other._sco_nullTreeSet); + } + =20 + /** */ + protected void assertEqualsCollection(String msg, Collection expected,=20 + Collection actual) + { + if (expected =3D=3D null && actual =3D=3D null) + return; + int expectedSize =3D expected.size(); + int actualSize =3D actual.size(); + assertEquals(msg + " collection size differs ", expectedSize, actu= alSize); + Object[] expectedFrozen =3D Freezer.freeze(expected, expectedSize); + Object[] actualFrozen =3D Freezer.freeze(actual, actualSize); + for (int i =3D 0; i < expectedSize; i++) { + Object expectedElement =3D expectedFrozen[i]; + Object actualElement =3D actualFrozen[i]; + assertEqualsExtended(msg + " element", expectedFrozen[i],=20 + actualFrozen[i]); + } + } + + /** */ + protected void assertEqualsMap(String msg, Map expected, Map actual) + { + if (expected =3D=3D null && actual =3D=3D null) + return; + int expectedSize =3D expected.size(); + int actualSize =3D actual.size(); + assertEquals(msg + " map size differs ", expectedSize, actualSize); + Map.Entry[] expectedFrozen =3D Freezer.freeze(expected, expectedSi= ze); + Map.Entry[] actualFrozen =3D Freezer.freeze(actual, actualSize); + for (int i =3D 0; i < expectedSize; i++) { + assertEqualsExtended(msg + " key", expectedFrozen[i].getKey(),= actualFrozen[i].getKey()); + assertEqualsExtended(msg + " value", expectedFrozen[i].getValu= e(), actualFrozen[i].getValue()); + } + } + + /** */ + protected void assertEqualsExtended(String msg, Object expected, Objec= t actual) { + if (expected =3D=3D null && actual =3D=3D null) + return; + if ((expected instanceof Collection) && (actual instanceof Collect= ion)) + assertEqualsCollection(msg, (Collection)expected, (Collection)= actual); + else if ((expected instanceof Map) && (actual instanceof Map)) + assertEqualsMap(msg, (Map)expected, (Map)actual); + else if ((expected instanceof Date) && (actual instanceof Date)) + assertEquals(msg, ((Date)expected).getTime(), ((Date)actual).g= etTime()); + else if ((expected instanceof Object[]) && (actual instanceof Obje= ct[])) + assertTrue(msg, Arrays.equals((Object[])expected, (Object[])ac= tual)); + else if ((expected instanceof boolean[]) && (actual instanceof boo= lean[])) + assertTrue(msg, Arrays.equals((boolean[])expected, (boolean[])= actual)); + else if ((expected instanceof byte[]) && (actual instanceof byte[]= )) + assertTrue(msg, Arrays.equals((byte[])expected, (byte[])actual= )); + else if ((expected instanceof char[]) && (actual instanceof char[]= )) + assertTrue(msg, Arrays.equals((char[])expected, (char[])actual= )); + else if ((expected instanceof int[]) && (actual instanceof int[])) + assertTrue(msg, Arrays.equals((int[])expected, (int[])actual)); + else if ((expected instanceof long[]) && (actual instanceof long[]= )) + assertTrue(msg, Arrays.equals((long[])expected, (long[])actual= )); + else if ((expected instanceof float[]) && (actual instanceof float= [])) + assertTrue(msg, Arrays.equals((float[])expected, (float[])actu= al)); + else if ((expected instanceof double[]) && (actual instanceof doub= le[])) + assertTrue(msg, Arrays.equals((double[])expected, (double[])ac= tual)); + else if ((expected instanceof Object[]) && (actual instanceof Obje= ct[])) + assertTrue(msg, Arrays.equals((Object[])expected, (Object[])ac= tual)); + else + assertEquals(msg, expected, actual); + } + + /** */ + protected void assertCollectionSize(String msg,=20 + Collection collection, int expecte= d) { + int size =3D collection =3D=3D null ? 0 : collection.size(); + if (debug) logger.debug(msg + size); + assertEquals("Wrong size of " + msg, expected, size); + } + + /** */ + protected void assertMapSize(String msg, Map map, int expected) { + int size =3D map =3D=3D null ? 0 : map.size(); + if (debug) logger.debug(msg + size); + assertEquals("Wrong size of " + msg, expected, size); + } + + /** */ + protected void assertNotIsDirty(String msg, Object pc) { + boolean isDirty =3D JDOHelper.isDirty(pc); + if (debug) logger.debug(msg + isDirty); + assertFalse("Unexpected dirty instance " + pc, isDirty); + } + + /** */ + protected void assertNullOwner(String msg, SCO sco) { + Object owner =3D sco.getOwner(); + if (debug) logger.debug(msg + owner); + assertNull(msg, owner); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerialPM= s=2Ejava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_SerialPMs.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerialPMs.j= ava (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerialPMs.j= ava Fri Mar 18 17:02:29 2005 @@ -0,0 +1,96 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +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; + +/** +* Tests that we can insert objects into a database. Creates objects as per +* Test_ActivateClass, and writes their OIDs into a file. +* Test_FetchInserted reads that file, and fetches the objects by OID. +* +* Both this and Test_Insert can be instructed (via the "insert" JVM proper= ty) +* to insert some number of objects. If that is greater than one, Test_Ins= ert +* inserts all its instances using one PersistenceManager. Test_SerialPMs,= on +* the other hand, uses a different PM for each insert. +* +* @author Dave Bristor +*/ +public class Test_SerialPMs extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SerialPMs.class); + } + + /** */ + protected void insertObjects() { + PersistenceManager[] pms =3D new PersistenceManager[numInsert]; + Transaction[] txs =3D new Transaction[numInsert]; + + try { + if (debug) logger.debug("INSERT"); + for (int i =3D 0; i < numInsert; i++) { + pms[i] =3D pmf.getPersistenceManager(); + txs[i] =3D pms[i].currentTransaction(); + txs[i].begin(); + } + =20 + for (int i =3D 0; i < numInsert; i++) { + Object pc =3D factory.create(i); + pms[i].makePersistent(pc); + } + for (int i =3D 0; i < numInsert; i++) { + txs[i].commit(); + } + } + finally { + for (int i =3D 0; i < numInsert; i++) { + Transaction tx =3D txs[i]; + if (tx !=3D null && tx.isActive()) + tx.rollback(); + PersistenceManager pm =3D pms[i]; + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + if (debug) logger.debug("inserted " + numInsert + " objects"); + } + =20 + protected int getDefaultInsert() { + return 50; + } + =20 + /** + * Inserts some number of objects in the database + */ + public void test() { + insertObjects(); + checkExtent(factory.getPCClass(), numInsert); + } + =20 + protected Factory getFactory(int verify) { + PointFactory rc =3D new PointFactory(); + rc.setVerify(verify); + return rc; + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Serializ= e=2Ejava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Serialize.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Serialize.j= ava (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Serialize.j= ava Fri Mar 18 17:02:29 2005 @@ -0,0 +1,93 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can serialize PCPoint instances and read them back correct= ly. +* +* @author Marina Vatkina +*/ +public class Test_Serialize extends AbstractTest { + Object o =3D null; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Serialize.class); + } + + /** */ + public void test() throws Exception { + runTestSerialization(false); + } + + /** */ + public void testRetainValues() throws Exception { + runTestSerialization(true); + } + =20 + + /** Inserts a point instance, and serializes it. */ + protected void runTestSerialization(boolean retainValues) throws Excep= tion { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) logger.debug("\nINSERT"); + =20 + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.setRetainValues(retainValues); + tx.begin(); + + PCPoint p =3D new PCPoint(1, 10); + pm.makePersistent(p); + if (debug) logger.debug("Created: " + p); + + tx.commit(); + + if (debug) + logger.debug("\nSERIALIZE " + ((retainValues)?"P-NONTX":"H= OLLOW")); + + String f =3D "myfile.tmp"; + ObjectOutputStream out =3D getObjectOutputStream(f); + out.writeObject(p); + out.flush(); + if (debug) logger.debug("Wrote: " + p); + =20 + ObjectInputStream in =3D getObjectInputStream(f); + PCPoint p1 =3D (PCPoint)in.readObject(); + if (debug) logger.debug("Read: " + p1); + assertEquals("Unexpected deserialzed object", p, p1); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Serializ= eComplex.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_SerializeComplex.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerializeCo= mplex.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerializeCo= mplex.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,225 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.serializable.PCClass1; +import org.apache.jdo.pc.serializable.PCClass2A; +import org.apache.jdo.pc.serializable.PCClass2B; +import org.apache.jdo.pc.serializable.PCSub3; +import org.apache.jdo.pc.serializable.PCSub4A; +import org.apache.jdo.pc.serializable.PCSub4B; +import org.apache.jdo.pc.serializable.PCSuper; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; +/** +* Tests that the enhancer adds method writeObject or adds a call of=20 +* jdoPreSerialize to an existing method writeObject or writeReplace. +* +* @author Michael Bouschen +*/ +public class Test_SerializeComplex extends AbstractTest { + + /** name of the writeObject method */ + public static String WRITE_OBJECT_NAME =3D "writeObject"; + /** parameter types of the writeObject method */ + public static Class[] WRITE_OBJECT_PARAMS =3D { ObjectOutputStream.cla= ss }; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SerializeComplex.class); + } + + /** + * This method checks whether the enhanced pc classes have or have not + * the method writeObject. + */ + public void testSerializationSupportMethods() + { + // PCClass1 must have writeObject + try { + PCClass1.class.getDeclaredMethod(WRITE_OBJECT_NAME, WRITE_OBJE= CT_PARAMS); + if (debug) + logger.debug("OK: Class PCClass1 has method " + WRITE_OBJE= CT_NAME); + } + catch (NoSuchMethodException ex) { + fail("ERROR: missing method " + WRITE_OBJECT_NAME + " in class= PCClass1"); + } + + // PCClass2A must have writeObject + try { + PCClass2A.class.getDeclaredMethod(WRITE_OBJECT_NAME, WRITE_OBJ= ECT_PARAMS); + if (debug) + logger.debug("OK: Class PCClass2A has method " + WRITE_OBJ= ECT_NAME); + } + catch (NoSuchMethodException ex) { + fail("ERROR: missing method " + WRITE_OBJECT_NAME + " in class= PCClass2A"); + } + + // PCClass2B must NOT have writeObject + try { + PCClass2B.class.getDeclaredMethod(WRITE_OBJECT_NAME, WRITE_OBJ= ECT_PARAMS); + fail("ERROR: Class PCClass1 must not have method " + WRITE_OBJ= ECT_NAME); + } + catch (NoSuchMethodException ex) { + if (debug) + logger.debug("OK: Class PCClass2B does not have method " += WRITE_OBJECT_NAME); + } + + // PCSuper must NOT have writeObject + try { + PCSuper.class.getDeclaredMethod(WRITE_OBJECT_NAME, WRITE_OBJEC= T_PARAMS); + fail("ERROR: Class PCSuper must not have method " + WRITE_OBJE= CT_NAME); + } + catch (NoSuchMethodException ex) { + if (debug) + logger.debug("OK: Class PCSuper does not have method " + W= RITE_OBJECT_NAME); + } + // PCSub4A must have writeObject + try { + PCSub4A.class.getDeclaredMethod(WRITE_OBJECT_NAME, WRITE_OBJEC= T_PARAMS); + if (debug) + logger.debug("OK: Class PCSub4A has method " + WRITE_OBJEC= T_NAME); + } + catch (NoSuchMethodException ex) { + fail("ERROR: missing method " + WRITE_OBJECT_NAME + " in class= PCSub4A"); + } + + // PCSub4B must NOT have writeObject + try { + PCSub4B.class.getDeclaredMethod(WRITE_OBJECT_NAME, WRITE_OBJEC= T_PARAMS); + fail("ERROR: Class PCSub4B must not have method " + WRITE_OBJE= CT_NAME); + } + catch (NoSuchMethodException ex) { + if (debug) + logger.debug("OK: Class PCSub4B does not have method " + W= RITE_OBJECT_NAME); + } + } + + /** + * This method creates pc instances, commits the transaction and then + * seralizes the pc instances. + */ + public void testSerialization() throws Exception { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) logger.debug("\nINSERT"); + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.setRetainValues(false); + + tx.begin(); + PCClass1 o1 =3D new PCClass1(1, 1);=20 + pm.makePersistent(o1); + Object o1Oid =3D pm.getObjectId(o1); + PCClass2A o2 =3D new PCClass2A(2, 2);=20 + pm.makePersistent(o2); + Object o2Oid =3D pm.getObjectId(o2); + PCClass2B o3 =3D new PCClass2B(3, 3);=20 + pm.makePersistent(o3); + Object o3Oid =3D pm.getObjectId(o3); + PCSub3 o4 =3D new PCSub3(4, 4, "4", "4");=20 + pm.makePersistent(o4); + Object o4Oid =3D pm.getObjectId(o4); + PCSub4A o5 =3D new PCSub4A(5, 5, "5", "5");=20 + pm.makePersistent(o5); + Object o5Oid =3D pm.getObjectId(o5); + PCSub4B o6 =3D new PCSub4B(6, 6, "6", "6");=20 + pm.makePersistent(o6); + Object o6Oid =3D pm.getObjectId(o6); + tx.commit(); tx =3D null; + pm.close(); pm =3D null; + + if (debug) logger.debug("\nSERIALIZE HOLLOW instances"); + doSerialize(o1Oid, false, new PCClass1(1, 0)); + doSerialize(o1Oid, true, new PCClass1(Integer.MIN_VALUE, 0)); + doSerialize(o2Oid, false, new PCClass2A(2, 0)); + doSerialize(o2Oid, true, new PCClass2A(Integer.MIN_VALUE, 0)); + doSerialize(o3Oid, false, new PCClass2B(3, 0)); + doSerialize(o3Oid, true, new PCClass2B(Integer.MIN_VALUE, 0)); + doSerialize(o4Oid, false, new PCSub3(Integer.MIN_VALUE, + Integer.MIN_VALUE, "4", null)); + doSerialize(o4Oid, true, new PCSub3(Integer.MIN_VALUE, + Integer.MIN_VALUE, "empty", null)); + doSerialize(o5Oid, false, new PCSub4A(Integer.MIN_VALUE, + Integer.MIN_VALUE, "5", null)); + doSerialize(o5Oid, true, new PCSub4A(Integer.MIN_VALUE, + Integer.MIN_VALUE, "empty", null)); + doSerialize(o6Oid, false, new PCSub4B(Integer.MIN_VALUE, + Integer.MIN_VALUE, "6", null)); + doSerialize(o6Oid, true, new PCSub4B(Integer.MIN_VALUE, + Integer.MIN_VALUE, "empty", null)); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** + * + */ + protected void doSerialize(Object oid,=20 + boolean makeTransient,=20 + Object expected) throws Exception=20 + { + PersistenceManager pm =3D null; + try { + // get the object + pm =3D pmf.getPersistenceManager(); + Object o =3D pm.getObjectById(oid, false); + =20 + // makeTransient? + if (makeTransient) + pm.makeTransient(o); + + // serialize + ByteArrayOutputStream bout =3D new ByteArrayOutputStream (); + ObjectOutputStream oout =3D new ObjectOutputStream (bout); + oout.writeObject(o); + oout.flush (); + if (debug) logger.debug("Wrote: " + o); + byte[] bytes =3D bout.toByteArray(); + oout.close (); + =20 + // deserialize=20 + ByteArrayInputStream bin =3D new ByteArrayInputStream (bytes); + ObjectInputStream oin =3D new ObjectInputStream (bin); + Object read =3D oin.readObject(); + if (debug) logger.debug("Read: " + read); + oin.close (); + + assertEquals("Unexpected deserialized object", expected, read); + }=20 + finally { + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + =20 + =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Serializ= eInher.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_SerializeInher.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerializeIn= her.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_SerializeIn= her.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,109 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCFullTimeEmployee1; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can serialize PC instances with classes that extend another +* PC class and read them back correctly. +* +* @author Marina Vatkina +*/ +public class Test_SerializeInher extends Test_ActivateClass { + Object o =3D null; + + private static GregorianCalendar born; + private static GregorianCalendar hired; + =20 + static { + born =3D new GregorianCalendar(TimeZone.getTimeZone("America/New_Y= ork")); + born.set(1969, 7, 20); + hired =3D new GregorianCalendar(TimeZone.getTimeZone("America/New_= York")); + hired.set(1982, 5, 5); + } + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_SerializeInher.class); + } + + /** */ + public void test() throws Exception { + runTestSerialization(false); + } + + /** */ + public void testRetainValues() throws Exception { + runTestSerialization(true); + } + =20 + /** Inserts a point instance, and serializes it. */ + protected void runTestSerialization(boolean retainValues) throws Excep= tion { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) logger.debug("\nINSERT"); + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.setRetainValues(retainValues); + tx.begin(); + + PCFullTimeEmployee1 p =3D new PCFullTimeEmployee1( + "Scott", "McNealy", born.getTime(), 1L, hired.getTime(),=20 + 200000.00, 15); + + pm.makePersistent(p); + if (debug) logger.debug("Created: " + p); + tx.commit(); + + if (debug) + logger.debug("\nSERIALIZE " + + ((retainValues)? "P-NONTX" : "HOLLOW")); + + String f =3D "myfile.tmp"; + + ObjectOutputStream out =3D getObjectOutputStream(f); + out.writeObject(p); + out.flush(); + if (debug) logger.debug("Wrote: " + p); + =20 + ObjectInputStream in =3D getObjectInputStream(f); + PCFullTimeEmployee1 p1 =3D (PCFullTimeEmployee1)in.readObject(= ); + =20 + if (debug) logger.debug("Read: " + p1); + assertEquals("Unexpected string repr of deserialized object",=20 + "PCFullTimeEmployee1: Emp: McNealy, Scott, id=3D1= , born 20/Aug/1969, hired 5/Jun/1982 $200000.0 manager: none dept: none emp= s: 0 insurance: null range: 15",=20 + p1.toString()); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_StringOI= D=2Ejava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_StringOID.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_StringOID.j= ava (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_StringOID.j= ava Fri Mar 18 17:02:29 2005 @@ -0,0 +1,74 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +/* + * Test_StringOID.java + * + * Created on August 22, 2001, 10:49 AM + */ + +package org.apache.jdo.test; + +/** + * + * @author Craig Russell + */ +import java.util.Iterator; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; + +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test getting OIDs and creating new OIDs from them. +*/ +public class Test_StringOID extends EmpDeptSupport { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_StringOID.class); + } + + /** The idea is that we're going to write a bunch of stuff to a data + * output stream, then read it back in; we should get the same data + * back.=20 + */ + public void test() { + insertObjects(); + } + + /** This method can be overridden by a subclass to perform special che= cks. + */ + protected void checkObjects(PersistenceManager pm) { + if (debug) logger.debug ("Verifying OIDs of all instances"); + for (Iterator it =3D persistentInstances.iterator(); it.hasNext();= ) { + Object oldObject =3D it.next(); + Object oldOid =3D JDOHelper.getObjectId(oldObject); + Object newOid =3D pm.newObjectIdInstance(oldObject.getClass(),= oldOid.toString()); + Object newObject =3D pm.getObjectById(newOid, false); + if (debug) { + logger.debug (" ---------------"); + logger.debug (" old object id: " + oldOid.toString()); + logger.debug (" new object id: " + newOid.toString()); + logger.debug (" old object: " + oldObject.toString()); + logger.debug (" new object: " + newObject.toString()); + } + assertNotNull("Null returned by getObjectById", newObject); + assertSame("Mismatch between old and new objects", oldObject, = newObject); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Stroke.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Stroke.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Stroke.java= (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Stroke.java= Fri Mar 18 17:02:29 2005 @@ -0,0 +1,127 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; + +import javax.jdo.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.PCStroke; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test a PC that contains a Collection of PC's. +* +* @author Dave Bristor +*/ +public class Test_Stroke extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Stroke.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + checkExtent(PCPoint.class, 2); + checkExtent(PCStroke.class, 1); + } + + /** */ + protected void insertObjects() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + tx.begin(); + + PCPoint p1 =3D new PCPoint(1, 3); + PCPoint p2 =3D new PCPoint(13, 42); + ArrayList points =3D new ArrayList(); + points.add(p1); + points.add(p2); + PCStroke pcStroke =3D new PCStroke(points); + + if (debug) logger.debug("Before insert: " + pcStroke); + // Next 2 statements allow this test to work whether or not + // reachability or navigation work. + pm.makePersistent(p1); + pm.makePersistent(p2); + + pm.makePersistent(pcStroke); + tx.commit(); + =20 + // Next 4 statements allow this test to work whether or not + // reachability or navigation work. + Object oid_p1 =3D JDOHelper.getObjectId(p1); + Object oid_p2 =3D JDOHelper.getObjectId(p2); + oids.add(oid_p1); + oids.add(oid_p2); + =20 + Object oid1 =3D JDOHelper.getObjectId(pcStroke); + if (debug) logger.debug("inserted pcStroke: " + oid1); + oids.add(oid1); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } =20 + =20 + /** + * redefine verify called by readObjects to check whether the read + * instance is correct. + */ + protected void verify(int i, Object pc) { + if (debug) logger.debug("verify (i =3D " + i + ") " + pc);=20 + PCPoint p1 =3D null; + PCPoint p2 =3D null; + // PCPoint and PCRefArrays do not redefine equals, + // so use the string representation. + switch(i) { + case 0 :=20 + p1 =3D new PCPoint(1, 3); + assertEquals("Wrong instance returned from datastore", + p1.toString(), pc.toString()); + break; + case 1: + p2 =3D new PCPoint(13, 42); + assertEquals("Wrong instance returned from datastore", + p2.toString(), pc.toString()); + break;=20 + case 2: + p1 =3D new PCPoint(1, 3); + p2 =3D new PCPoint(13, 42); + ArrayList points =3D new ArrayList(); + points.add(p1); + points.add(p2); + PCStroke pcStroke =3D new PCStroke(points); + assertEquals("Wrong instance returned from datastore", + pcStroke.toString(), pc.toString()); + break; + default: + fail("Wrong number of inserted objects, expected three"); + break; + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Update.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Update.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Update.java= (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Update.java= Fri Mar 18 17:02:29 2005 @@ -0,0 +1,168 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * 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; + +/** +* Tests that we can update objects in a database. Reads an extent of PCPo= int +* objects, and set the coordinate values in them to all be x =3D 555, y = =3D 1212. +* +* @author Dave Bristor +*/ +public class Test_Update extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Update.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + update(false); + checkExtent(factory.getPCClass(), numInsert); + } + + /** */ + public void testOptimistic() throws Exception { + insertObjects(); + update(true); + checkExtent(factory.getPCClass(), numInsert); + } + + // Gets an extent of points, and changes them. + protected void update(boolean optimistic) { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + if (debug) + logger.debug("UPDATE in " + (optimistic?"OPTIMISTIC":"DATA= STORE") + + " Transaction"); + tx.setOptimistic(optimistic); + tx.setNontransactionalRead(true); + =20 + tx.begin(); + Extent e =3D pm.getExtent(PCPoint.class, false); + + // Increase the existing values by a well known (phone) number + // Use ArrayList to store objects. This allows to verify fields + // without going to the database. + List arr =3D new ArrayList(); + List oids =3D new ArrayList(); + int count =3D 0; + for (Iterator i =3D PCPoint.getSortedIterator(e.iterator()); i= .hasNext();) { + PCPoint p =3D (PCPoint)i.next();=20 + arr.add(p); + oids.add(pm.getObjectId(p)); + =20 + // Change every *other* value + if (count++ % 2 =3D=3D 1) { + continue; + } + =20 + int x =3D p.getX() + 555; + Integer yi =3D p.getY(); + int y =3D 0; + if (null !=3D yi) { // should not happen, but ignore it an= d continue + y =3D yi.intValue(); + } + y +=3D 1212; + =20 + if (debug) logger.debug("updated to x=3D" + x + ", y=3D" += y + ": " + p); + =20 + p.setX(x); + p.setY(new Integer(y)); + assertTrue("IsDirty: ", JDOHelper.isDirty(p)); + assertEquals("getX before commit", x, p.getX()); + assertEquals("getY before commit", new Integer(y), p.getY(= )); + } + tx.commit(); tx =3D null; + + // verify updated values in same pm + + count =3D 0; + for (Iterator i =3D arr.iterator(); i.hasNext();) { + PCPoint p =3D (PCPoint)i.next(); + verifyPCPoint(p, count); + count++; + } + pm.close(); pm =3D null; + + // re-read objects in a new pm and verify updated values + + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.begin(); + count =3D 0; + for (Iterator i =3D oids.iterator(); i.hasNext();) { + PCPoint p =3D (PCPoint)pm.getObjectById(i.next(), true); + verifyPCPoint(p, count); + count++; + } + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + private void verifyPCPoint(PCPoint p, int count) + { + if (debug) logger.debug("verify " + p); + assertFalse("isDirty after commit", JDOHelper.isDirty(p)); + assertFalse("isDeleted after commit", JDOHelper.isDeleted(p)); + int x =3D count; + int y =3D count; + if (count % 2 !=3D 1) { + x +=3D 555; + y +=3D 1212; + } + assertEquals("getX after rollback", x, p.getX()); + assertEquals("getY after rollback", new Integer(y), p.getY()); + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc =3D new PointFactory(); + rc.setVerify(verify); + return rc; + } + + /** */ + protected int getDefaultInsert() + { + return 5; + } =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_UserHash= Code.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_UserHashCode.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_UserHashCod= e=2Ejava (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_UserHashCod= e=2Ejava Fri Mar 18 17:02:29 2005 @@ -0,0 +1,184 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; + +import javax.jdo.Extent; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCCollection; +import org.apache.jdo.pc.PCId; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +*=20 +* See bugid 4833532. Tests that a Set/Map containing PC instances which h= ave +* a user-supplied hashCode() referencing persistent fields works. +* +* @author Dave Bristor +* @version 1.0.1 +*/ +public class Test_UserHashCode extends AbstractTest { + =20 + private static final int NUMBER_TO_INITIALIZE =3D 2; + =20 + private static final int NUMBER_TO_ADD =3D 2; + =20 + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_UserHashCode.class); + } + + /** */ + public void test() { + populate(); + update(false); + update(true); + traverse(); + } + + /** Traverse an extent of objects, printing each one along the way. */ + protected void traverse() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + tx.setOptimistic(false); + =20 + if (debug) logger.debug("Begin traverse"); + =20 + tx.begin(); + =20 + if (debug) logger.debug("Traverse: getExtent"); + Extent e =3D pm.getExtent(PCCollection.class, true); + =20 + if (debug) + logger.debug("Traverse: begin iteration of PCCollection Ex= tent"); + int nrOfPCs =3D 0; + int nrOfSetElements =3D 0; + int nrOfMapEntries =3D 0; + for (Iterator i =3D e.iterator(); i.hasNext();) { + PCCollection c =3D (PCCollection)i.next(); + if (debug) logger.debug(c.toString()); + nrOfPCs++; + Iterator j =3D c.getSet().iterator(); + while (j.hasNext()) { + Object o =3D j.next(); + if (debug) logger.debug("Set element: " + o.toString()= ); + nrOfSetElements++; + } + assertEquals("Wrong number of set elements",=20 + (NUMBER_TO_INITIALIZE + NUMBER_TO_ADD * 2), + nrOfSetElements); + =20 + Iterator k =3D c.getMap().entrySet().iterator(); + while (k.hasNext()) { + Map.Entry p =3D (Map.Entry)k.next(); + if (debug) + logger.debug("Map entry: " + p.getKey()+ ";" + + p.getValue()); + nrOfMapEntries++; + } + assertEquals("Wrong number of set elements",=20 + (NUMBER_TO_INITIALIZE + NUMBER_TO_ADD * 2), + nrOfSetElements); + } + assertEquals("Wrong number of PCCollection instances", 1, nrOf= PCs); + =20 + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + =20 + /** Traverse an extent of objects, updating each one along the way. */ + protected void update(boolean optimistic) { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + if (debug) + logger.debug("Running " + (optimistic?"optimistic":"datast= ore") + + " transactions."); + tx.setOptimistic(optimistic); + =20 + if (debug) logger.debug("Begin update"); + =20 + tx.begin(); + =20 + if (debug) logger.debug("Update: getExtent"); + Extent e =3D pm.getExtent(PCCollection.class, true); + =20 + if (debug) logger.debug("Update: begin iteration for update"); + for (Iterator i =3D e.iterator(); i.hasNext();) { + if (debug) logger.debug("Update: before PCCollection.next(= )"); + PCCollection c =3D (PCCollection)i.next(); + if (debug) logger.debug(c.toString()); + for (int a =3D 0; a < NUMBER_TO_ADD; ++a) { + if (debug) logger.debug("Update: before Set.add()"); + c.getSet().add(new PCId()); + if (debug) logger.debug("Update: before Map.put()"); + c.getMap().put(new PCId(), new PCId()); + } + } + if (debug) logger.debug("Update: before commit()"); + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** Populate an extent of objects. */ + protected void populate() { + PersistenceManager pm =3D pmf.getPersistenceManager();=20 + Transaction tx =3D pm.currentTransaction(); + try { + if (debug) logger.debug("Begin populate"); + PCCollection pcCollection =3D new PCCollection(); + pcCollection.setSet(new HashSet()); + pcCollection.setMap(new HashMap()); + pcCollection.setId(0); + for (int i =3D 0; i < NUMBER_TO_INITIALIZE; i++) { + pcCollection.getSet().add(new PCId()); + pcCollection.getMap().put(new PCId(), new PCId()); + } + tx.setOptimistic(false); + tx.begin(); + pm.makePersistent(pcCollection); + tx.commit(); + if (debug) logger.debug("End populate"); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakHash= Set.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_WeakHashSet.java?view=3Dauto&rev=3D158176 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakHashSet= .java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_WeakHashSet= .java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,326 @@ +/* + * Copyright 2005 The Apache Software Foundation. + *=20 + * 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=20 + *=20 + * http://www.apache.org/licenses/LICENSE-2.0 + *=20 + * Unless required by applicable law or agreed to in writing, software=20 + * distributed under the License is distributed on an "AS IS" BASIS,=20 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied= .=20 + * See the License for the specific language governing permissions and=20 + * limitations under the License. + */ + +/* + * TestWeakHashSet.java + * + */ + +package org.apache.jdo.test; + +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.TreeSet; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; +import org.apache.jdo.util.WeakHashSet; + + +/** + * + * @author Michael Bouschen + * @author Markus Fuchs + */ +public class Test_WeakHashSet extends AbstractTest { + + static boolean verbose =3D Boolean.getBoolean("verbose"); + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_WeakHashSet.class); + } + =20 + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + + /** */ + public void testContains() { + // WeakHashSet + Set weakSet =3D new WeakHashSet(); + + X x11 =3D new X("x11"); + weakSet.add(x11); + weakSet.add(new X("x12")); + weakSet.add(null); + + // Regular HashSet + Set set =3D new HashSet(); + X x21 =3D new X("x21"); + set.add(x21); + set.add(new X("x22")); + set.add(null); + + // check whether instance is included in set + assertTrue("FAILURE: weakSet does not contain x11", weakSet.contai= ns(x11)); + assertTrue("FAILURE: weakSet does not contain x12", weakSet.contai= ns(new X("x12"))); + assertTrue("FAILURE: weakSet does not contain null", weakSet.conta= ins(null)); + assertTrue("FAILURE: set does not contain x21", set.contains(x21)); + assertTrue("FAILURE: set does not contain x22", set.contains(new X= ("x22"))); + assertTrue("FAILURE: set does not contain null", set.contains(null= )); + + if (verbose) System.out.println("\nTest GC:"); + if (verbose) System.out.println("Before GC: " + new XTreeSet(weakS= et));=20 + + // now gc + gc(); + + if (verbose) System.out.println("After GC: " + new XTreeSet(weakSe= t));=20 + + // x11 should still be in weakSet + assertTrue("FAILURE: weakSet does not contain x11 after gc", weakS= et.contains(x11)); + // x12 should not be in weakSet + assertTrue("FAILURE: weakSet still contains x12 after gc", !weakSe= t=2Econtains(new X("x12"))); + assertTrue("FAILURE: weakSet does not contain null", weakSet.conta= ins(null)); + + // set should not be affected by gc + assertEquals("FAILURE: set has wrong size ", 3, set.size()); + assertTrue("FAILURE: set does not contain x21 after gc", set.conta= ins(x21)); + assertTrue("FAILURE: set does not contain x22 after gc", set.conta= ins(new X("x22"))); + assertTrue("FAILURE: set does not contain null", set.contains(null= )); + + weakSet.clear(); + if (verbose) System.out.println("After Clear: " + new XTreeSet(wea= kSet));=20 + } + =20 + /** */ + public void testRemove() { + // WeakHashSet + gc(); + Set weakSet =3D new WeakHashSet(); + + X x11 =3D new X("x11"); + weakSet.add(x11); + weakSet.add(new X("x12")); + weakSet.add(null); + + // check whether instance is included in set + assertTrue("FAILURE: weakSet does not contain x11", weakSet.contai= ns(x11)); + assertTrue("FAILURE: weakSet does not contain x12", weakSet.contai= ns(new X("x12"))); + assertTrue("FAILURE: weakSet does not contain null", weakSet.conta= ins(null)); + + if (verbose) System.out.println("\nTest Remove: " + new XTreeSet(w= eakSet));=20 + if (verbose) System.out.println("Size =3D " + weakSet.size()); + assertEquals("Unexpected size of weakSet", 3, weakSet.size()); + + weakSet.remove(x11); + if (verbose) System.out.println("After Remove(x11): " + new XTreeS= et(weakSet));=20 + if (verbose) System.out.println("Size =3D " + weakSet.size()); + assertEquals("Unexpected size of weakSet", 2, weakSet.size()); + + weakSet.remove(new X("x12")); + if (verbose) System.out.println("After Remove(x12): " + new XTreeS= et(weakSet));=20 + if (verbose) System.out.println("Size =3D " + weakSet.size()); + assertEquals("Unexpected size of weakSet", 1, weakSet.size()); + + weakSet.remove(null); + if (verbose) System.out.println("After Remove(null): " + new XTree= Set(weakSet));=20 + if (verbose) System.out.println("Size =3D " + weakSet.size()); + assertEquals("Unexpected size of weakSet", 0, weakSet.size()); + + weakSet.add(x11); + weakSet.add(new X("x12")); + weakSet.add(null); + + XTreeSet xts =3D new XTreeSet(weakSet); + if (verbose) System.out.println("\nTest Iterator: " + xts); + if (verbose) System.out.println("Size =3D " + weakSet.size()); + assertEquals("Unexpected size of weakSet", 3, weakSet.size()); + + int size =3D 3; + for (Iterator i =3D weakSet.iterator(); i.hasNext();) { + Object o =3D i.next(); + if (verbose) System.out.println("iterator.remove: " + o); + i.remove(); + size--; + if (verbose) System.out.println("After remove: " + new XTreeSe= t(weakSet)); + if (verbose) System.out.println("Size =3D " + weakSet.size()); + assertEquals("Unexpected size of weakSet", size, weakSet.size(= )); + } + + weakSet.clear(); + } + + /** */ + public void testAll() { + // WeakHashSet + gc(); + Set weakSet =3D new WeakHashSet(); + + X x11 =3D new X("x11"); + weakSet.add(x11); + weakSet.add(new X("x12")); + weakSet.add(null); + + // Regular HashSet + Set set =3D new HashSet(); + set.add(x11); + set.add(new X("x12")); + set.add(null); + + if (verbose) System.out.println("\nTest containsAll: "); + if (verbose) System.out.println("Weak set Set1: " + new XTreeSet(w= eakSet)); + if (verbose) System.out.println("Regular set Set2: " + new XTreeSe= t(set)); + if (verbose) System.out.println("Set1.containsAll(Set2): " + weakS= et.containsAll(set)); + assertTrue("Set1.containsAll(Set2) should return true", weakSet.co= ntainsAll(set)); + if (verbose) System.out.println("Set2.containsAll(Set1): " + set.c= ontainsAll(weakSet)); + assertTrue("Set2.containsAll(Set1) should return true", set.contai= nsAll(weakSet)); + if (verbose) System.out.println("Set2.equals(Set1): " + set.equals= (weakSet)); + assertTrue("Set2.equals(Set1) should return true", set.equals(weak= Set)); + if (verbose) System.out.println("Set1.equals(Set2): " + weakSet.eq= uals(set)); + assertTrue("Set1.equals(Set2) should return true", weakSet.equals(= set)); + + weakSet.add(new X("x13")); + set.add(new X("x14")); + + if (verbose) System.out.println("\nTest containsAll: "); + if (verbose) System.out.println("Weak set Set1: " + new XTreeSet(w= eakSet)); + if (verbose) System.out.println("Regular set Set2: " + new XTreeSe= t(set)); + if (verbose) System.out.println("Set1.containsAll(Set2): " + weakS= et.containsAll(set)); + assertFalse("Set1.containsAll(Set2) should return false", weakSet.= containsAll(set)); + if (verbose) System.out.println("Set2.containsAll(Set1): " + set.c= ontainsAll(weakSet)); + assertFalse("Set2.containsAll(Set1) should return false", set.cont= ainsAll(weakSet)); + if (verbose) System.out.println("Set2.equals(Set1): " + set.equals= (weakSet)); + assertFalse("Set2.equals(Set1) should return false", set.equals(we= akSet)); + if (verbose) System.out.println("Set1.equals(Set2): " + weakSet.eq= uals(set)); + assertFalse("Set1.equals(Set2) should return false", weakSet.equal= s(set)); + + weakSet.remove(new X("x13")); + set.remove(new X("x14")); + + boolean result; + if (verbose) System.out.println("\nTest removeAll: "); + if (verbose) System.out.println("Weak set Set1: " + new XTreeSet(w= eakSet)); + if (verbose) System.out.println("Regular set Set2: " + new XTreeSe= t(set)); + result =3D weakSet.removeAll(set); + if (verbose) System.out.println("Set1.removeAll(Set2): " + result); + assertTrue("weakSet.removeAll(set) should change the weakSet", res= ult); + if (verbose) System.out.println("Weak set Set1: " + new XTreeSet(w= eakSet)); + assertTrue("weakSet should be empty", weakSet.isEmpty()); + gc(); + + weakSet.add(x11); + weakSet.add(new X("x12")); + weakSet.add(null); + + result =3D set.removeAll(weakSet); + if (verbose) System.out.println("Set2.removeAll(Set1): " + result); + assertTrue("set.removeAll(weakSet) should change the set", result); + if (verbose) System.out.println("Regular set Set2: " +new XTreeSet= ( set)); + assertTrue("set should be empty", set.isEmpty()); + + set.add(x11); + set.add(new X("x12")); + set.add(null); + set.add(new X("x14")); + weakSet.add(new X("x13")); + + if (verbose) System.out.println("\nTest removeAll: "); + if (verbose) System.out.println("Weak set Set1: " + new XTreeSet(w= eakSet)); + if (verbose) System.out.println("Regular set Set2: " + new XTreeSe= t(set)); + result =3D weakSet.removeAll(set); + if (verbose) System.out.println("Set1.removeAll(Set2): " + result); + assertTrue("weakSet.removeAll(set) should change the weakSet", res= ult); + if (verbose) System.out.println("Weak set Set1: " + new XTreeSet(w= eakSet)); + assertEquals("Unexpected size of weakSet", 1, weakSet.size()); + + weakSet.add(x11); + weakSet.add(new X("x12")); + weakSet.add(null); + weakSet.add(new X("x13")); + + result =3D set.removeAll(weakSet); + if (verbose) System.out.println("Set2.removeAll(Set1): " + result); + assertTrue("set.removeAll(weakSet) should change the set", result); + if (verbose) System.out.println("Regular set Set2: " + new XTreeSe= t(set)); + assertEquals("Unexpected size of set", 1, set.size()); + + weakSet.clear(); + } + + private static class X { + private String name; + public X(String name) { this.name =3D name; } + public String toString() { return this.name; } + public int hashCode() { return (name =3D=3D null) ? 0 : name.hashC= ode(); } + public boolean equals(Object o) + { + // compare by name only + if ((o =3D=3D null) || !(o instanceof X)) + return false; + else if (name =3D=3D null) + return ((X)o).name =3D=3D null; + else + return name.equals(((X)o).name); + } + } + + /** + * A Sorted set of X instances. + */ + private static class XTreeSet extends TreeSet { + private static final Comparator c =3D new Comparator() { + public int compare(Object o1, Object o2) { + if (o1 =3D=3D null) { + return -1; + } else if (o2 =3D=3D null) { + return 1; + } else { + String s1 =3D ((X)o1).toString(); + String s2 =3D ((X)o2).toString(); + if (s1 =3D=3D null) { + return -1; + } else if (s2 =3D=3D null) { + return 1; + } else { + return s1.compareTo(s2); + } + } + } + =20 + public boolean equals(Object obj) { + return true; + } + }; + =20 + XTreeSet(Set s) { + super(c); + this.addAll(s); + } + } + + private static void gc() { + //System.out.print("gc: "); + Runtime rt =3D Runtime.getRuntime(); + long oldfree; + long newfree =3D rt.freeMemory(); + do { + oldfree =3D newfree; + rt.runFinalization(); rt.gc(); + newfree =3D rt.freeMemory(); + //System.out.print('.'); + } while (newfree > oldfree); + //System.out.println(); + } + +} + +