From jdo-commits-return-246-apmail-db-jdo-commits-archive=www.apache.org@db.apache.org Sat Mar 19 05:17:15 2005 Return-Path: Delivered-To: apmail-db-jdo-commits-archive@www.apache.org Received: (qmail 88514 invoked from network); 19 Mar 2005 05:17:15 -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:15 -0000 Received: (qmail 94179 invoked by uid 500); 19 Mar 2005 05:17:15 -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 90595 invoked by uid 99); 19 Mar 2005 01:12:59 -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: <20050319010615.28338.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:15 -0000 Subject: svn commit: r158176 [65/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_EmpDeptS= erialization.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_EmpDeptSerialization.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_EmpDeptSeri= alization.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptSeri= alization.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,257 @@ +/* + * 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_EmpDeptSerialization.java + * + * Created on June 29, 2001, 3:37 PM + */ + +package org.apache.jdo.test; + +import java.util.*; +import java.io.*; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.impl.fostore.FOStorePMF; +import org.apache.jdo.pc.empdept.PCDepartment; +import org.apache.jdo.pc.empdept.PCEmployee; +import org.apache.jdo.pc.empdept.PCFullTimeEmployee; +import org.apache.jdo.pc.empdept.PCInsurance; +import org.apache.jdo.pc.empdept.PCPartTimeEmployee; +import org.apache.jdo.pc.empdept.PCProject; + +/** This class measures the difference between Serialization and JDO as a + * persistence strategy. =20 + * Some number of instances are created, and then the test starts timing. + * First the objects are serialized; then a transaction is started, the=20 + * objects are made persistent in JDO, and the transaction is committed. + * + * @author Craig Russell + * @version 1.0 + */ +public class Test_EmpDeptSerialization extends AbstractTest { + =20 + ArrayList allObjects =3D new ArrayList(); + PCEmployee scott; + PCEmployee ed; + PCInsurance scottIns; + PCInsurance edIns; + PCDepartment board; + PCDepartment emg; + PCProject solaris; + PCProject sparc; + =20 + /** + * @param args the command line arguments + */ + public static void main (String args[]) { + switch (args.length) { + case 0: + JDORITestRunner.run(Test_EmpDeptSerialization.class); + break; + case 2: + runLocal(args[0], args[1]); + break; + default:=20 + System.err.println ("usage: \nTest_EmpDeptSerialization = \n\t: the url for fostore\n\t: the file name for serializa= tion"); + break; + } + } + =20 + /** */ + public void test() throws Exception { + doTest("serialize.tmp"); + } + + /** Run the test. */ + void doTest(String fileName) throws Exception { + createObjects(); + long serializeTime =3D serializeObjects(fileName); + long fostoreTime =3D persistObjects(); + } + =20 + /** */ + void createObjects() { + HashSet h; + =20 + // Create and set up employees. Scott is Ed's manager. Ed is + // Scott's sole employee. + // + GregorianCalendar born =3D + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")= ); + GregorianCalendar hired =3D + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")= ); + + born.set(1969, 7, 20); + hired.set(1982, 5, 5); + scott =3D + new PCFullTimeEmployee( + "Scott", "McNealy", born.getTime(),=20 + 1L, hired.getTime(),=20 + //null, null, null, null, null, + 200000.0); + born.set(1960, 4, 8); + hired.set(1985, 2, 3); + ed =3D=20 + new PCPartTimeEmployee( + "Ed", "Zander", born.getTime(),=20 + 100L, hired.getTime(),=20 + //null, null, null, null, null, + 400.0);=20 + allObjects.add(ed); + allObjects.add(scott); + ed.setManager(scott); + + h =3D new HashSet(); + h.add(ed); + scott.setEmployees(h); + + // Set up their departments. + board =3D + new PCDepartment(100L, "board"); + h =3D new HashSet(); + h.add(scott); + board.setEmployees(h); + scott.setDepartment(board); + + emg =3D + new PCDepartment(200L, "emg"); + h =3D new HashSet(); + h.add(ed); + emg.setEmployees(h); + ed.setDepartment(emg); + + // Insure these guys + scottIns =3D new PCInsurance(1000, "Aetna", scott); + edIns =3D new PCInsurance(1001, "BlueCross", ed); + scott.setInsurance(scottIns); + ed.setInsurance(edIns); + + // Give them some projects to work on. Scott works on both; Ed on= ly + // on one. + solaris =3D new PCProject(1L, "Solaris"); + sparc =3D new PCProject(2L, "Sparc"); + h =3D new HashSet(); + h.add(scott); + h.add(ed); + solaris.setEmployees(h); // Solaris is worked on by Scott and Ed + + h =3D new HashSet(); + h.add(scott); + sparc.setEmployees(h); // Sparc is worked on by Scott + =20 + h =3D new HashSet(); + h.add(solaris); + h.add(sparc); + scott.setProjects(h); // Scott works on Solaris and Sparc + + h =3D new HashSet(); + h.add(solaris); + ed.setProjects(h); // Ed works on Solaris + + // Show what we've got + if (debug) { + logger.debug("Before insert: "); + logger.debug(scott.toString()); + logger.debug(ed.toString()); + logger.debug(board.toString()); + logger.debug(emg.toString()); + logger.debug(scottIns.toString()); + logger.debug(edIns.toString()); + logger.debug(solaris.toString()); + logger.debug(sparc.toString()); + } + } + + /** Insert objects into the database. If reachability were implemente= d, we'd + * only have to make scott and ed persistent, as everything else is + * reachable from them. + */ + long persistObjects() { + Timer timer =3D new Timer(); + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + tx.begin(); + pm.makePersistentAll(allObjects); + tx.commit(); + pm.close(); + long elapsed =3D timer.stop(); + if (debug) logger.debug ("FOStore results: \t" + elapsed); + return elapsed; + } + =20 + /** Write the objects to a file using serialization. + */ + long serializeObjects(String fileName) throws IOException { + Timer timer =3D new Timer(); + ObjectOutputStream oos =3D getObjectOutputStream(fileName); + oos.writeObject(allObjects); + oos.flush(); + oos.close(); + long elapsed =3D timer.stop(); + if (debug) logger.debug ("Serialization results:\t" + elapsed); + return elapsed; + } + + /** Timer utility class. Measures wall clock time. + */ + static class Timer { + long startTime; + long stopTime; + Timer() { + startTime =3D new Date().getTime(); + } + =20 + long stop() { + stopTime =3D new Date().getTime(); + return (stopTime - startTime); + } + =20 + void start() { + startTime =3D new Date().getTime(); + } + } + =20 + /** */ + static void runLocal(String url, String file) { + try { + // create PMF + FOStorePMF pmf =3D new FOStorePMF(); + pmf.setConnectionURL(url); + pmf.setConnectionUserName("craig"); + pmf.setConnectionPassword("secret"); + pmf.setConnectionCreate(true); + + // create and setup test + Test_EmpDeptSerialization sm =3D new Test_EmpDeptSerialization= (); + sm.logger.debug("Test_EmpDeptSerialization using URL: " + url + + " FileName: " + file); + sm.pmf =3D pmf; + sm.debug =3D true; + sm.doTest(file); + sm.closePMF(); + } + catch(Exception ex) { + ex.printStackTrace(); + } + } + =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Extent.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Extent.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_Extent.java= (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Extent.java= Fri Mar 18 17:02:29 2005 @@ -0,0 +1,224 @@ +/* + * 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.*; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.test.util.Factory; + +/** +* This test retrieves an extent-full of instances. +* +* @author Dave Bristor +*/ +public class Test_Extent extends AbstractTest { + /** If true, get subclass instances too. */ + private final boolean subclasses; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Extent.class); + } + + /** */ + public Test_Extent() { + super(); + this.subclasses =3D Boolean.getBoolean("subclasses"); + } + + /** */ + public void testOutsideTx() throws Exception { + insertObjects(); + runOutsideTx(); + } + =20 + /** */ + public void testDatastoreTx() throws Exception { + insertObjects(); + runInsideTx(false); + } + + /** */ + public void testOptimisticTx() throws Exception { + insertObjects(); + runInsideTx(true); + } + + /** */ + public void testNewInstance() throws Exception { + insertObjects(); + runNewInstance(false); + } + + /** */ + public void testNewInstanceIgnoreCache() throws Exception { + insertObjects(); + runNewInstance(true); + } + + /** */ + public void testDeleteInstance() throws Exception { + insertObjects(); + runDeleteInstance(false); + } + =20 + /** */ + public void testDeleteInstanceIgnoreCache() throws Exception { + insertObjects(); + runDeleteInstance(true); + } + + /** */ + protected void runOutsideTx() { + PersistenceManager pm =3D null; + try { + if (debug) logger.debug("getExtent outside of a transaction"); + pm =3D pmf.getPersistenceManager(); + getExtent(pm, factory.getPCClass(), numInsert); + } + finally { + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void runInsideTx(boolean optimistic) { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) + logger.debug("getExtent in " + (optimistic?"OPTIMISTIC":"D= ATASTORE") + + " Transaction"); + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.setOptimistic(optimistic); + tx.begin(); + getExtent(pm, factory.getPCClass(), numInsert); + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void runNewInstance(boolean ignoreCache) { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) + logger.debug("getExtent in with persistence-new instance a= nd ignoreCache " + ignoreCache); + pm =3D pmf.getPersistenceManager(); + pm.setIgnoreCache(ignoreCache); + tx =3D pm.currentTransaction(); + tx.begin(); + pm.makePersistent(factory.create(numInsert)); + getExtent(pm, factory.getPCClass(), numInsert + 1); + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void runDeleteInstance(boolean ignoreCache) { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) + logger.debug("getExtent in with persistence-deleted instan= ce and ignoreCache " + ignoreCache); + pm =3D pmf.getPersistenceManager(); + pm.setIgnoreCache(ignoreCache); + tx =3D pm.currentTransaction(); + tx.begin(); + Object toBeDeleted =3D pm.getObjectById(oids.get(oids.size()-1= ), false); + if (debug) logger.debug("Now delete " + toBeDeleted); + pm.deletePersistent(toBeDeleted); + getExtent(pm, factory.getPCClass(), numInsert - 1); + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected void getExtent(PersistenceManager pm, + Class clazz, int expectedCount) { + String className =3D clazz.getName(); + if (debug) logger.debug("\nEXTENT of " + className); + Extent e =3D pm.getExtent(clazz, subclasses); + =20 + Map elements =3D new TreeMap(); + for (Iterator i =3D e.iterator(); i.hasNext();) { + Object pc =3D i.next(); + elements.put(JDOHelper.getObjectId(pc), pc); + } + int objCount =3D 0; + for (Iterator k =3D elements.values().iterator(); k.hasNext();) { + Object pc =3D k.next(); + verify(objCount, pc); + if (debug) logger.debug(pc.toString()); + objCount++; + } + if (debug) + logger.debug("extent of " + className + " has " + objCount + + " objects\n"); + assertEquals("extent of " + className + " has wrong number of inst= ances",=20 + expectedCount, objCount); + } + + /** */ + protected Factory getFactory(int verify) { + return getFactoryByClassProperty(verify, "org.apache.jdo.pc.PCPoin= t"); + } + + /** */ + protected int getDefaultInsert() + { + return 5; + } + + /** */ + protected int getDefaultVerify() + { + return 1; + } + + /** */ + protected void verify(int i, Object pc) { + assertEquals("Wrong instance type", factory.getPCClass(), pc.getCl= ass()); + super.verify(i, pc); + } + =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FSUID2.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_FSUID2.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_FSUID2.java= (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FSUID2.java= Fri Mar 18 17:02:29 2005 @@ -0,0 +1,335 @@ +/* + * 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.EOFException; +import java.io.IOException; +import java.io.InputStream; +import java.io.ObjectInputStream; +import java.lang.reflect.Constructor; +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; +import javax.jdo.spi.PersistenceCapable; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can insert, fetch, and get extent of instances of 2 distin= ct +* classes which have the same name. For this test, they are both +* org.apache.jdo.pc.PCPoint, but one of them has 2 coordinates and the +* other has 3. We use our own ClassLoader to be able to load them both in +* the same JVM.=20 +* +* @author Dave Brigstor +*/ +public class Test_FSUID2 extends AbstractTest { + /** Maps class names to classes. */ + // This is only for use byt FSUIDLoader, but inner classes cannot have + // static members, so here it is. + private static final HashMap cache =3D new HashMap(); + + /** Name of the jarfile property. */ + private static final String JARFILE_PROPERTY =3D "fsuidjar"; + + /** Default jarfile name, used if property is undefined. */ + private static final String DEFAULT_JARFILE =3D "fsuid2.jar"; + + /** Name of the jar file for class loading. */ + private String jarfile; + + /** Reflection constructor instance for 3D point class. */ + private Constructor p3Constructor; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_FSUID2.class); + } + + /** */ + public Test_FSUID2() { + super(); + jarfile =3D System.getProperty(JARFILE_PROPERTY, DEFAULT_JARFILE); + } + =20 + /** */ + public void test() throws Exception { + insertObjects(); + writeOIDs(); + readObjects(); + } + + /** + * Create and store instances of PCPoint whose classes are loaded from + * different class loaders and have different structure (and hence + * different FOStoreSchemaUID's). + */ + protected void insertObjects() throws Exception { + PersistenceManager pm =3D null; + Transaction tx =3D null; + =20 + try { + if (debug) logger.debug("\nINSERT"); + + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.begin(); + + // Get loader for and load class of 3-D point. + p3Constructor =3D get3DPointConstructor(); + + Object inserted1[] =3D new Object[numInsert]; + Object inserted2[] =3D new Object[numInsert]; + for (int i =3D 0; i < numInsert; i++) { + // create 2D point + Object pc =3D new PCPoint(i, new Integer(i)); + if (debug) + logger.debug("cl " + i + " " + pc.getClass().getClassL= oader()); + pm.makePersistent(pc); + inserted1[i] =3D pc; + + // create 3D point + pc =3D (PersistenceCapable)p3Constructor.newInstance( + new Object[] { + new Integer(i), new Integer(i), new Float(1.0 * i)= }); + if (debug) + logger.debug("cl " + i + " " + pc.getClass().getClassL= oader()); + pm.makePersistent(pc); + inserted2[i] =3D pc; + } + + tx.commit(); + + for (int i =3D 0; i < numInsert; i++) { + announce("inserted ", inserted1[i]); + announce("inserted ", inserted2[i]); + } + + if (debug) logger.debug("inserted " + insertedCount + " object= s"); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /**=20 + * Loads and verifies the pc instances specified by the stored oids. + */ + protected void readObjects() throws Exception { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + ObjectInputStream in =3D getOIDFileInputStream(); + tx.begin(); + try { + int count =3D 0; + while (true) { + Object oid =3D in.readObject(); + Object pc =3D pm.getObjectById(oid, true); + if (debug) logger.debug("fetching: " + oid + " -> " + = pc); + // The first numInsert instances are supposed to be + // 2D points and the next are supposed to be 3D points. + if (count < numInsert) + verify2DPoint(count, pc); + else + verify3DPoint(count - numInsert, pc); + count++; + } + }=20 + catch (EOFException ex) { + // OK + } + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + private void verify2DPoint(int i, Object pc) { + Object expected =3D new PCPoint(i, new Integer(i)); + assertEquals("Wrong 2D point instance", expected, pc); + } + + /** */ + private void verify3DPoint(int i, Object pc) throws Exception { + Object expected =3D p3Constructor.newInstance(new Object[] { + new Integer(i), new Integer(i), new Float(1.0 * i)}); + assertEquals("Wrong 2D point instance", expected, pc); + } + =20 + /** */ + protected int getDefaultInsert() + { + return 4; + } =20 + + private Constructor get3DPointConstructor() throws Exception { + try { + return (Constructor)AccessController.doPrivileged(=20 + new PrivilegedExceptionAction () { + public Object run () throws Exception { + FSUIDLoader l =3D new FSUIDLoader(jarfile); + Class p3Class =3D l.loadClass("org.apache.jdo.pc.P= CPoint", true); + return p3Class.getDeclaredConstructor( + new Class[] { int.class, Integer.class, float.= class }); + }}); + } + catch (PrivilegedActionException ex) { + // unwrap FileNotFoundException + throw ex.getException(); + } + } + =20 + /** + * Expects to load classes from within a specified jar file. + */ + class FSUIDLoader extends ClassLoader { + /** Name of the jar file for class loading. */ + private final String filename; + =20 + FSUIDLoader(String filename) { + this.filename =3D filename; + } + =20 + public InputStream getResourceAsStream(String name) { + if (!name.startsWith("org/apache/jdo/pc")) { + return getSystemResourceAsStream(name); + } + + try { + JarFile jarFile =3D getJarFile(); + JarEntry entry =3D jarFile.getJarEntry(name); + if (entry !=3D null) { + return jarFile.getInputStream(entry); + } + }=20 + catch (IOException ex) { + ex.printStackTrace(); + } + return null; + } + + public synchronized Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + =20 + Class c =3D (Class)cache.get(name); + =20 + try { + if (null =3D=3D c && ! name.startsWith("org.apache.jdo.pc"= )) { + try { + c =3D findSystemClass(name); + } catch (ClassNotFoundException ex) { + ; + } catch (NoClassDefFoundError ex) { + ; + } + } + + if (null =3D=3D c) { =20 + byte b[] =3D loadClassData(name); + if (null =3D=3D b) { + throw new NoClassDefFoundError(name); + } + c =3D defineClass(name, b, 0, b.length); + cache.put(name, c); + } + if (resolve) { + resolveClass(c); + } + } catch (NoClassDefFoundError ex) { + throw new ClassNotFoundException("loadClass " + name, ex); + } + return c; + } + + private byte[] loadClassData(String name) + throws ClassNotFoundException { + byte rc[] =3D null; + + // Class names in jar files have file separators...I hope they= 're + // the same on Un*x and Windows. + name =3D name.replace('.', '/') + ".class"; + try { + JarFile jf =3D getJarFile(); + for (Enumeration e =3D jf.entries(); e.hasMoreElements();)= { + JarEntry je =3D (JarEntry)e.nextElement(); + if (name.equals(je.getName())) { + InputStream is =3D jf.getInputStream(je); + int avail =3D is.available(); + if (-1 !=3D avail) { + int size =3D avail; + rc =3D new byte[size]; + int off =3D 0; + int count =3D 0; + while (size > 0) { + count =3D is.read(rc, off, size); + if (count <=3D 0) { + is.close(); + break; + } + off +=3D count; + size -=3D count; + } + if (off !=3D avail) { + throw new IOException( + "failed to read complete class " + nam= e); + } + } + break; + } + } + } catch (IOException ex) { + ex.printStackTrace(); + throw new ClassNotFoundException("loadClassData " + name, = ex); + } + return rc; + } + + private JarFile getJarFile() throws IOException { + try { + return (JarFile)AccessController.doPrivileged( + new PrivilegedExceptionAction () { + public Object run () throws IOException { + return new JarFile(filename); + }}); + } + catch (PrivilegedActionException ex) { + // unwrap FileNotFoundException + throw (IOException)ex.getException(); + } + } + + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch.ja= va URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Fetch.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_Fetch.java = (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch.java = Fri Mar 18 17:02:29 2005 @@ -0,0 +1,56 @@ +/* + * 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 org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.pc.PointFactory; + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. +* +* @author Dave Bristor +*/ +public class Test_Fetch extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Fetch.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc =3D new PointFactory(); + // verify in any case + rc.setVerify(verify); + return rc; + } + + /** */ + protected int getDefaultVerify() { + return 1; + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch2.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Fetch2.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_Fetch2.java= (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Fetch2.java= Fri Mar 18 17:02:29 2005 @@ -0,0 +1,44 @@ +/* + * 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 org.apache.jdo.test.util.JDORITestRunner; + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. Unlike +* TestFetch, this test inserts more objects. +* +* @author Dave Bristor +*/ +public class Test_Fetch2 extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Fetch2.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. + public void test() throws Exception { + super.test(); + + insertObjects(); // Do another insertion. + readObjects(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FetchIns= erted.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_FetchInserted.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_FetchInsert= ed.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_FetchInsert= ed.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,113 @@ +/* + * 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.EOFException; +import java.io.ObjectInputStream; + +import javax.jdo.JDOObjectNotFoundException; +import javax.jdo.PersistenceManager; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.Factory; +import org.apache.jdo.test.util.JDORITestRunner; + + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. Unlike +* Test_Fetch and Test_Fetch2, this should be run in a separate JVM +* after a first JVM has inserted some objects. +* +* Note that by default, this fetches inserted PCPoints...to test other kin= ds of +* objects, use -Dclass=3DXXX on the java command line. +* +* @author Dave Bristor +*/ +public class Test_FetchInserted extends AbstractTest { + /** By default, we expect that the instances for which we have oids sh= ould + * exist in the database. But if shouldExist is false, then we expect + * that they should not exist. + */ + private final boolean shouldExist; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_FetchInserted.class); + } + + /** */ + public Test_FetchInserted() { + super(); + existing =3D true; // Must use existing database! + shouldExist =3D Boolean.getBoolean("shouldExist"); + } + + // 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. + public void test() throws Exception { + readObjects(); + } + + protected void readObjects() throws Exception { + PersistenceManager pm =3D pmf.getPersistenceManager(); + try { + ObjectInputStream in =3D getOIDFileInputStream(); + if (debug) logger.debug("\nFETCH"); + + int count =3D 0; + while (true) { + Object oid =3D null; + try { + oid =3D in.readObject(); + if (debug) logger.debug("fetching: " + oid); + Object pc =3D pm.getObjectById(oid, true); + if (!shouldExist) { + fail("fetched " + oid + + " but it should not have been in database"); + } else { + if (debug) logger.debug("After fetch: " + pc); + verify(count++, pc); + } + } catch (JDOObjectNotFoundException ex) { + if (shouldExist) { + fail("should find object with ObjectId " + oid + "= : " + ex); + } else { + if (debug) + logger.debug(oid.toString() + + " does not exist in database, as expected"= ); + } + } + } + } catch (EOFException ex) { + // OK + } + finally { + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** + * Determines the kind of objects that are inserted. Override this if + * you want to insert some other kind of object. + */ + protected Factory getFactory(int verify) { + return getFactoryByClassProperty(verify, "org.apache.jdo.pc.PCPoin= t"); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Freezer.= java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Freezer.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_Freezer.jav= a (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Freezer.jav= a Fri Mar 18 17:02:29 2005 @@ -0,0 +1,221 @@ +/* + * 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_Freezer.java + * + * Created on April 12, 2003, 4:15 PM + */ + +package org.apache.jdo.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.jdo.impl.sco.Freezer; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * + * @author Craig Russell + * @version 1.0.1 + */ +public class Test_Freezer extends AbstractTest { + =20 + /** */ + static private final short s33 =3D 33; + + /** */ + private boolean verbose; + =20 + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Freezer.class); + } + + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + =20 + /** */ + public void testSet() { + Set ts =3D new HashSet(); + ts.add(new Integer(12)); + ts.add(new Integer(12)); + ts.add(new Integer(12)); + ts.add(new Integer(12)); + ts.add(new java.util.Date(2000)); + ts.add(new Float(2000)); + ts.add(new Float(2020)); + ts.add(new Float(2000.1)); + ts.add(new Integer(112)); + ts.add(new Integer(212)); + ts.add(new Float(2000.001)); + ts.add(new Float(2000.00001)); + ts.add(new Short(s33)); + ts.add(new Integer(2)); + ts.add(new Long(3456789L)); + ts.add(new java.util.Date(1000)); + List expected =3D new ArrayList(); + expected.add(new Float(2000)); + expected.add(new Float(2000.001)); + expected.add(new Float(2000.1)); + expected.add(new Float(2020)); + expected.add(new Integer(2)); + expected.add(new Integer(12)); + expected.add(new Integer(112)); + expected.add(new Integer(212)); + expected.add(new Long(3456789L)); + expected.add(new Short(s33)); + expected.add(new java.util.Date(1000)); + expected.add(new java.util.Date(2000)); + + // now freeze the set + Object[] ordered =3D Freezer.freeze(ts, ts.size()); + List al =3D Arrays.asList(ordered); + if (verbose)=20 + for (Iterator it=3Dal.iterator(); it.hasNext();) { + Object o =3D it.next(); + System.out.println(o.getClass().getName() + ":" + o.toStri= ng()); + } + assertEquals("freezing a Set returned unexpected result", expected= , al); + } + =20 + /** */ + public void testMap() { + Map hm =3D new HashMap(); + hm.put(new Integer(22), new Integer(1000)); + hm.put(new java.sql.Time(2004), new Integer(6)); + hm.put(new Integer(22), new Integer(1000)); + hm.put(new Integer(22), new Integer(1000)); + hm.put(new Integer(22), new Integer(16)); + hm.put(new java.sql.Date(2001), new Integer(2)); + hm.put(new java.util.Date(2000), new Integer(1)); + hm.put(new Float(2000.), new Integer(10)); + hm.put(new Float(2020), new Integer(14)); + hm.put(new org.apache.jdo.impl.sco.SqlTime(3000), new Integer(7)); + hm.put(new java.sql.Date(2002), new Integer(3)); + hm.put(new Float(2000.1), new Integer(13)); + hm.put(new Integer(112), new Integer(17)); + hm.put(new org.apache.jdo.impl.sco.SqlDate(3004), new Integer(5)); + hm.put(new Integer(212), new Integer(18)); + hm.put(new java.sql.Date(2003), new Integer(4)); + hm.put(new Float(2000.001), new Integer(11)); + hm.put(new Float(2000.01), new Integer(12)); + hm.put(new java.sql.Timestamp(2004), new Integer(8)); + hm.put(new org.apache.jdo.impl.sco.SqlTimestamp(3002), new Integer= (9)); + hm.put(new Short(s33), new Integer(20)); + hm.put(new Integer(2), new Integer(15)); + hm.put(new Long(3456789L), new Integer(19)); + hm.put(new java.util.Date(1000), new Integer(0)); + + List expected =3D new ArrayList(); + expected.add(new SimpleEntry(new Float(2000.), new Integer(10))); + expected.add(new SimpleEntry(new Float(2000.001), new Integer(11))= ); + expected.add(new SimpleEntry(new Float(2000.01), new Integer(12))); + expected.add(new SimpleEntry(new Float(2000.1), new Integer(13))); + expected.add(new SimpleEntry(new Float(2020), new Integer(14))); + expected.add(new SimpleEntry(new Integer(2), new Integer(15))); + expected.add(new SimpleEntry(new Integer(22), new Integer(16))); + expected.add(new SimpleEntry(new Integer(112), new Integer(17))); + expected.add(new SimpleEntry(new Integer(212), new Integer(18))); + expected.add(new SimpleEntry(new Long(3456789L), new Integer(19))); + expected.add(new SimpleEntry(new Short(s33), new Integer(20))); + expected.add(new SimpleEntry(new java.util.Date(1000), new Integer= (0))); + expected.add(new SimpleEntry(new java.util.Date(2000), new Integer= (1))); + expected.add(new SimpleEntry(new java.sql.Date(2001), new Integer(= 2))); + expected.add(new SimpleEntry(new java.sql.Date(2002), new Integer(= 3))); + expected.add(new SimpleEntry(new java.sql.Date(2003), new Integer(= 4))); + expected.add(new SimpleEntry(new org.apache.jdo.impl.sco.SqlDate(3= 004), new Integer(5))); + expected.add(new SimpleEntry(new java.sql.Time(2004), new Integer(= 6))); + expected.add(new SimpleEntry(new org.apache.jdo.impl.sco.SqlTime(3= 000), new Integer(7))); + expected.add(new SimpleEntry(new java.sql.Timestamp(2004), new Int= eger(8))); + expected.add(new SimpleEntry(new org.apache.jdo.impl.sco.SqlTimest= amp(3002), new Integer(9))); + =20 + Object[] ordered =3D Freezer.freeze(hm, hm.size()); + List al =3D Arrays.asList(ordered); + if (verbose) + for (Iterator it=3Dal.iterator(); it.hasNext();) { + Map.Entry o =3D (Map.Entry)it.next(); + Object key =3D o.getKey(); + Object value =3D o.getValue(); + System.out.println(" " + value + " " + key.getClass().getN= ame() + ":" + key.toString()); + } + assertEquals("freezing a Map returned unexpected result", expected= , al); + } + + /** */ + static class SimpleEntry implements Map.Entry { + Object key; + Object value; + + public SimpleEntry(Object key, Object value) { + this.key =3D key; + this.value =3D value; + } + =20 + public SimpleEntry(Map.Entry e) { + this.key =3D e.getKey(); + this.value =3D e.getValue(); + } + =20 + public Object getKey() { + return key; + } + =20 + public Object getValue() { + return value; + } + =20 + public Object setValue(Object value) { + Object oldValue =3D this.value; + this.value =3D value; + return oldValue; + } + =20 + public boolean equals(Object o) { + if (!(o instanceof Map.Entry)) + return false; + Map.Entry e =3D (Map.Entry)o; + return eq(key, e.getKey()) && eq(value, e.getValue()); + } + =20 + public int hashCode() { + Object v; + return ((key =3D=3D null) ? 0 : key.hashCode()) ^ + ((value =3D=3D null) ? 0 : value.hashCode()); + } + =20 + public String toString() { + return key + "=3D" + value; + } + =20 + private static boolean eq(Object o1, Object o2) { + return (o1 =3D=3D null ? o2 =3D=3D null : o1.equals(o2)); + } + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjec= tById.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_GetObjectById.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_GetObjectBy= Id.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectBy= Id.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,88 @@ +/* + * 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.JDOUserException; +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; + +/** +* This test is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and later retrieving those objects. +* +* @author Dave Bristor +*/ +public class Test_GetObjectById extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_GetObjectById.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. + public void test() throws Exception { + PersistenceManager pm =3D pmf.getPersistenceManager(); + try { + Object o =3D new Object(); + Object o2 =3D pm.getObjectById(o, false); + throw new RuntimeException("Expected exception not caught from= getObjectById()"); + } catch (JDOUserException ex) { + // this is expected + } + finally { + pm.close(); + } + =20 + pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + tx.begin(); + + Object inserted[] =3D new Object[numInsert]; + for (int i =3D 0; i < numInsert; i++) { + Object pc =3D factory.create(i); + pm.makePersistent(pc); + inserted[i] =3D pc; + + Object oid =3D pm.getObjectId(pc); + Object obj =3D pm.getObjectById(oid, false); + announce("GetObjectId for " + oid + ", obj is " + obj + ",= pc is ", pc); + assertSame(("object mismatch at " + i), pc, obj); + } + =20 + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + 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_GetObjec= tById2.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_GetObjectById2.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_GetObjectBy= Id2.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_GetObjectBy= Id2.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,88 @@ +/* + * 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.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.ObjectInputStream; + +import javax.jdo.PersistenceManager; + +import org.apache.jdo.impl.fostore.FOStorePMF; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * This is similar to Test_GetObjectById except that it is much simpler. I= t=20 + * explicitly does not use any features inherited from AbstractTest other = than=20 + * creating an url and accessing system properties. It ensures that we can= run=20 + * a user application which does not explicitly load a PersistenceCapable = class + * but can nonetheless fetch instances of that class by OID. + * + * @author Dave Bristor + */ +public class Test_GetObjectById2 extends AbstractTest { + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_GetObjectById2.class); + } + =20 + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + + /** */ + public void test () throws Exception { + File f =3D new File(dbName + ".oid"); + assertTrue("Test_GetObjectById2: " + dbName + ".oid does not exis= t", f.exists()); + + FOStorePMF pmf =3D new FOStorePMF(); + pmf.setConnectionCreate(false); + pmf.setConnectionUserName(System.getProperty ("user", "fred")); + pmf.setConnectionPassword(System.getProperty ("password", "wombat"= )); + + String url =3D createURL(); + pmf.setConnectionURL(url); + + PersistenceManager pm =3D pmf.getPersistenceManager(); + + try { + ObjectInputStream in =3D new ObjectInputStream( + new FileInputStream(f)); + if (debug) System.out.println("\nFETCH"); + + int count =3D 0; + while (true) { + Object oid =3D in.readObject(); + if (debug)=20 + System.out.println("Test_GetObjectById2: getting objec= t by id: " + oid); + Object pc =3D pm.getObjectById(oid, true); + if (debug)=20 + System.out.println("After getObjectById: " + pc); + } + } catch (EOFException ex) { + // OK + } + finally { + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_HollowUp= date.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_HollowUpdate.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_HollowUpdat= e=2Ejava (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_HollowUpdat= e=2Ejava Fri Mar 18 17:02:29 2005 @@ -0,0 +1,151 @@ +/* + * 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.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.pc.empdept.PCPerson; +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that we can do update of a hollow instance in Optimistic transacti= on. +* +* @author Marina Vatkina +*/ +public class Test_HollowUpdate extends AbstractTest { + =20 + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_HollowUpdate.class); + } + + /** */ + private static Date date; + + /** */ + static { + GregorianCalendar calendarDate =3D + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")= ); + calendarDate.set(1969, 7, 20); + date =3D calendarDate.getTime(); + } + + /** */ + public void testPCPoint() { + insertPCPoint(); + checkExtent(PCPoint.class, 1); + } + =20 + /** */ + public void testPCPerson() { + insertPCPerson(); + checkExtent(PCPerson.class, 1); + } + =20 + /** */ + private void insertPCPoint() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + if (debug) logger.debug("\nUPDATE HOLLOW"); + =20 + tx.setRetainValues(false); + tx.setOptimistic(true); + PCPoint p =3D new PCPoint(0xaa, 0xcc); + =20 + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK, p is " + p); + tx.commit(); + if (debug) logger.debug("Transaction commit OK"); + =20 + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + p.setY(new Integer(0xee)); + Object oid =3D pm.getObjectId(p); + if (debug) logger.debug("Updated to " + p); + tx.commit(); tx =3D null; + if (debug) logger.debug("Transaction commit OK, p is " + p); + pm.close(); pm =3Dnull; + + // check state of object in with pm + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.begin(); + PCPoint p2 =3D (PCPoint)pm.getObjectById(oid, true); + assertEquals("Field y has wrong value", new Integer(0xee), p2.= getY()); + tx.commit(); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + public void insertPCPerson() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + if (debug) logger.debug("\nUPDATE HOLLOW"); + + tx.setRetainValues(false); + tx.setOptimistic(true); + PCPerson p =3D new PCPerson("FFFFFF", "LLLL", date); + =20 + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK, p is " + p); + tx.commit(); + if (debug) logger.debug("Transaction commit OK"); + =20 + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + p.setLastname("xxxxxx"); + Object oid =3D pm.getObjectId(p); + if (debug) logger.debug("Updated to " + p); + tx.commit(); tx =3D null; + if (debug) logger.debug("Transaction commit OK, p is " + p); + pm.close(); pm =3D null; + + // check state of object in with pm + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + tx.begin(); + PCPerson p2 =3D (PCPerson)pm.getObjectById(oid, true); + assertEquals("Field lastname has wrong value", "xxxxxx", p2.ge= tLastname()); + tx.commit(); + } + 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_Inherita= nce.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Inheritance.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_Inheritance= .java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Inheritance= .java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,166 @@ +/* + * 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.Iterator; +import java.util.Map; +import java.util.TreeSet; + +import javax.jdo.Extent; +import javax.jdo.PersistenceManager; + +import org.apache.jdo.pc.empdept.PCDepartment; +import org.apache.jdo.pc.empdept.PCEmployee; +import org.apache.jdo.pc.empdept.PCFullTimeEmployee; +import org.apache.jdo.pc.empdept.PCInsurance; +import org.apache.jdo.pc.empdept.PCPartTimeEmployee; +import org.apache.jdo.pc.empdept.PCProject; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test a PC that has some fields which are PC's. +*/ +public class Test_Inheritance extends EmpDeptSupport { + =20 + Map emps =3D new HashMap(); + Map depts =3D new HashMap(); + Map inss =3D new HashMap(); + Map projs =3D new HashMap(); + =20 + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Inheritance.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. + public void test() throws Exception=20 + { + insertObjects(); + getEmpExtent(); + } + =20 + // The above test inserted a bunch of instances and committed them. We + // now try to load the extent of Employee and navigate to all associat= ed + // instances. + protected void getEmpExtent() { + PersistenceManager pm =3D null; + try { + if (debug) logger.debug("\ngetEmpExtent"); + pm =3D pmf.getPersistenceManager(); + Extent extent; + Iterator it; + extent =3D pm.getExtent (PCEmployee.class, true); + it =3D extent.iterator(); + + // Put the printed version of each emp in the extent into a So= rtedSet + TreeSet sortedEmps =3D new TreeSet(); + + while (it.hasNext()) { + PCEmployee e =3D (PCEmployee)it.next(); + sortedEmps.add("from extent of PCEmployee: " + e); + checkEmployee(e); + } + + // Print out the extent in sorted order + if (debug) + for (Iterator i =3D sortedEmps.iterator(); i.hasNext();) { + logger.debug((String)i.next()); + } + + PCFullTimeEmployee scott =3D (PCFullTimeEmployee) emps.get (ne= w Long (1L)); + PCPartTimeEmployee ed =3D (PCPartTimeEmployee) emps.get (new L= ong (100L)); + PCDepartment board =3D (PCDepartment) depts.get (new Long (100= L)); + PCDepartment emg =3D (PCDepartment) depts.get (new Long (200L)= ); + PCInsurance scottIns =3D (PCInsurance) inss.get (new Long (100= 0L)); + PCInsurance edIns =3D (PCInsurance) inss.get (new Long (1001L)= ); + PCProject solaris =3D (PCProject) projs.get (new Long (1L)); + PCProject sparc =3D (PCProject) projs.get (new Long (2L)); + + String repr =3D null; + repr =3D scott.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of scott",=20 + "PCFullTimeEmployee: PCEmployee: PCPerson: McNeal= y, Scott, born 20/Aug/1969, id=3D1, hired 5/Jun/1982 manager: none dept: bo= ard emps: 1 insurance: Aetna $200000.0", + repr);=20 + repr =3D ed.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of ed", + "PCPartTimeEmployee: PCEmployee: PCPerson: Zander= , Ed, born 8/May/1960, id=3D100, hired 3/Mar/1985 manager: McNealy dept: em= g emps: 0 insurance: BlueCross $400000.0", + repr); + repr =3D board.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of board",=20 + "Dept: board, id=3D100, emps: 1",=20 + repr); + repr =3D emg.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of emg",=20 + "Dept: emg, id=3D200, emps: 1",=20 + repr); + repr =3D scottIns.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of scotts insurance"= ,=20 + "Ins: Aetna, id=3D1000, emp McNealy",=20 + repr); + repr =3D edIns.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of eds insurance",=20 + "Ins: BlueCross, id=3D1001, emp Zander",=20 + repr); + repr =3D solaris.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of project solaris",=20 + "PCProject: Solaris, id=3D1, emps: 2",=20 + repr); + repr =3D sparc.toString(); + if (debug) logger.debug(repr); + assertEquals("Wrong string representation of project sparc",=20 + "PCProject: Sparc, id=3D2, emps: 1",=20 + repr); + } + finally { + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + =20 + protected void checkEmployee (PCEmployee e) { + emps.put (new Long(e.getEmpid()), e); + for (Iterator it =3D e.getProjects().iterator(); it.hasNext();) { + PCProject proj =3D (PCProject)it.next(); + checkProject (proj); + } + checkDepartment ((PCDepartment) e.getDepartment()); + checkInsurance ((PCInsurance) e.getInsurance()); + } + =20 + protected void checkDepartment (PCDepartment d) { + depts.put (new Long(d.getDeptid()), d); + } + =20 + protected void checkProject (PCProject p) { + projs.put (new Long(p.getProjid()), p); + } + =20 + protected void checkInsurance (PCInsurance i) { + inss.put (new Long(i.getInsid()), i); + } + =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert.j= ava URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Insert.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_Insert.java= (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert.java= Fri Mar 18 17:02:29 2005 @@ -0,0 +1,85 @@ +/* + * 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 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. +* +* @author Dave Bristor +*/ +public class Test_Insert extends AbstractTest { + =20 + /** */ + private static boolean silent =3D false; + + /** */ + public static void main(String args[]) { + handleArgs(args); + if (silent) + runSilentInsert(); + else + JDORITestRunner.run(Test_Insert.class); + } + + /** Write the OIDs of the objects that were inserted to a file dbName.= oid. + */ + public void test() throws Exception + { + insertObjects(); + checkExtent(factory.getPCClass(), numInsert); + writeOIDs(); + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc =3D new PointFactory(); + rc.setVerify(verify); + return rc; + } + + /** */ + private static void runSilentInsert() { + try { + Test_Insert insert =3D new Test_Insert(); + insert.setupPMF(); + insert.test(); + insert.closePMF(); + } + catch (Exception ex) { + System.out.println("Excetion during insert"); + ex.printStackTrace(); + } + } + + /** */ + private static void handleArgs(String[] args) { + for (int i =3D 0; i < args.length; i++) { + if ("-silent".equals(args[i])) + silent =3D true; + else + System.err.println("Test_Insert: ignoring unknon option" += args[i]); + } + } + =20 +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert2.= java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Insert2.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_Insert2.jav= a (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Insert2.jav= a Fri Mar 18 17:02:29 2005 @@ -0,0 +1,102 @@ +/* + * 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.JDOException; +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 rollback an insert and commit the same insert in the +* next transaction. +* +* @author Marina Vatkina +*/ +public class Test_Insert2 extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Insert2.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + checkExtent(factory.getPCClass(), 1); + readObjects(); + } + + /** Tries to insert an object rolled back in a previous tx. */ + protected void insertObjects() throws Exception { + PersistenceManager pm =3D null; + Transaction tx =3D null; + try { + if (debug) logger.debug("\nINSERT-ROLLBACK-INSERT-COMMIT"); + pm =3D pmf.getPersistenceManager(); + tx =3D pm.currentTransaction(); + PCPoint p =3D new PCPoint(11, 111); + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK"); + tx.rollback(); + if (debug) logger.debug("Transaction rollback OK"); + + tx.begin(); + if (debug) logger.debug("Transaction begin OK"); + pm.makePersistent(p); + if (debug) logger.debug("Make Persistent OK"); + + // This try-catch block is usefull for internal testing only. + // It should not affect this test in a regular test runs. + try { + tx.commit(); + if (debug) logger.debug("Transaction commit OK"); + } catch (JDOException e) { + if (debug) logger.debug("Transaction active: " + tx.isActi= ve()); + throw e; + } + announce("inserted ", p); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected Factory getFactory(int verify) { + PointFactory rc =3D new PointFactory(); + rc.setVerify(verify); + return rc; + } + + /** */ + protected void verify(int i, Object pc) { + assertEquals("wrong PCPoint instance", new PCPoint(11, 111), pc); + } + =20 +} + Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Interfac= es.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/a= pache/jdo/test/Test_Interfaces.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_Interfaces.= java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Interfaces.= java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,80 @@ +/* + * 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.JDOHelper; +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; + +import org.apache.jdo.pc.PCInterfaces; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that arrays can be stored in the datastore. +* +* @author Dave Bristor +*/ +public class Test_Interfaces extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Interfaces.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + checkExtent(PCInterfaces.class, 1); + } + + // We override this from Test_Fetch and insert our own objects + protected void insertObjects() { + PersistenceManager pm =3D pmf.getPersistenceManager(); + Transaction tx =3D pm.currentTransaction(); + try { + tx.begin(); + PCInterfaces pcInterfaces =3D new PCInterfaces(); + pcInterfaces.init(); + if (debug) logger.debug("Before insert: " + pcInterfaces); + pm.makePersistent(pcInterfaces); + tx.commit(); + =20 + Object oid1 =3D JDOHelper.getObjectId(pcInterfaces); + if (debug) logger.debug("inserted pcInterfaces: " + oid1); + oids.add(oid1); + } + finally { + if (tx !=3D null && tx.isActive()) + tx.rollback(); + if (pm !=3D null && !pm.isClosed()) + pm.close(); + } + } + + /** + * 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"); + PCInterfaces expected =3D new PCInterfaces(); + expected.init(); + assertEquals("Wrong instance returned from datastore", expected, p= c); + } +}