Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_AppId.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_AppId.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_AppId.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_AppId.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,177 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.*; + +import javax.jdo.*; + +import org.apache.jdo.pc.PCPoint1; +import org.apache.jdo.pc.PCPoint1Key; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Tests that FOStore can perform insert/update/fetch/delete for +* an instance with application identity. +* +* @author Marina Vatkina +*/ +public class Test_AppId extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_AppId.class); + } + + /** */ + public void test() { + + int start = -numInsert; + int end = numInsert + 1; // exclusive! + int nrOfObjects = end - start; + PCPoint1Key pk = new PCPoint1Key(); + + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try { + if (debug){ + logger.debug("===================="); + logger.debug("INSERT"); + } + + tx.begin(); + for (int i = start; i < end; i++) { + PCPoint1 p = new PCPoint1(i,10*i); + pm.makePersistent(p); + if (debug) logger.debug("Inserting: " + p); + } + tx.commit(); tx = null; + pm.close(); pm = null; + + if (debug){ + logger.debug("===================="); + logger.debug("UPDATE"); + } + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + for (int i = start; i < end; i++) { + pk.x = i; + PCPoint1 p = (PCPoint1)pm.getObjectById(pk, true); + p.setY(new Integer(i+100)); + if (debug) logger.debug("Updating: " + p); + if (debug) printObject(p, pm); + } + tx.commit(); tx = null; + pm.close(); pm = null; + + if (debug) { + logger.debug("===================="); + logger.debug("EXTENT"); + } + + pm = pmf.getPersistenceManager(); + List list = new ArrayList(); + Extent ext = pm.getExtent(org.apache.jdo.pc.PCPoint1.class, false); + for (java.util.Iterator i = ext.iterator(); i.hasNext();) { + PCPoint1 p = (PCPoint1)i.next(); + if (debug) logger.debug("Fetched: " + p); + if (debug) printObject(p, pm); + list.add(p); + } + Collections.sort(list, new PCPoint1Comparator()); + assertEquals("Extent has wrong size", nrOfObjects, list.size()); + for (int i = start; i < end; i++) { + PCPoint1 p = (PCPoint1)list.get(i - start); + assertEquals("Wrong value of PCPoint1.x", i, p.getX()); + assertEquals("Wrong value of PCPoint1.y", (i+100), p.getY().intValue()); + } + pm.close(); pm = null; + + if (debug) { + logger.debug("===================="); + logger.debug("DELETE"); + } + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + for (int i = start; i < end; i++) { + pk.x = i; + PCPoint1 p = (PCPoint1)pm.getObjectById(pk, true); + if (debug) logger.debug("Deleting: " + p); + if (debug) printObject(p, pm); + pm.deletePersistent(p); + } + tx.commit(); tx = null; + pm.close(); pm = null; + + if (debug) { + logger.debug("===================="); + logger.debug("CHECK DELETED"); + } + + pm = pmf.getPersistenceManager(); + for (int i = start; i < end; i++) { + pk.x = i; + try { + PCPoint1 p = (PCPoint1)pm.getObjectById(pk, true); + fail("Object with primary key " + pk + " was found: " + p); + } catch (JDODataStoreException ex) { + // expected exception => OK; + if (debug) logger.debug("Deleted " + pk); + } + } + pm.close(); pm = null; + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + void printObject(PCPoint1 p, PersistenceManager pm) { + Object oid = pm.getObjectId(p); + logger.debug("Instance OID: " + oid); + + PersistenceManager pm1 = pmf.getPersistenceManager(); + Object o = pm1.getObjectById(oid, true); + logger.debug("Instance from OID: " + o); + logger.debug("OID from instance: " + pm1.getObjectId(o)); + pm1.close(); + } + + static class PCPoint1Comparator implements Comparator + { + public int compare(Object o1, Object o2) { + if (!(o1 instanceof PCPoint1)) + throw new ClassCastException("instance " + o1 + + " + is not a PCPoint1 instance"); + if (!(o2 instanceof PCPoint1)) + throw new ClassCastException("instance " + o2 + + " + is not a PCPoint1 instance"); + return new Integer(((PCPoint1)o1).getX()).compareTo( + new Integer(((PCPoint1)o2).getX())); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Arrays.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Arrays.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Arrays.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Arrays.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,80 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import javax.jdo.*; + +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.pc.PCArrays; + +/** +* Tests that arrays can be stored in the datastore. +* +* @author Dave Bristor +*/ +public class Test_Arrays extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Arrays.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + checkExtent(PCArrays.class, 1); + } + + /** Redefine to insert our own objects. */ + protected void insertObjects() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + PCArrays pcArrays = new PCArrays(); + pcArrays.init(); + if (debug) logger.debug("Before insert: " + pcArrays); + pm.makePersistent(pcArrays); + tx.commit(); + + Object oid1 = JDOHelper.getObjectId(pcArrays); + if (debug) logger.debug("inserted pcArrays: " + oid1); + oids.add(oid1); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != 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"); + PCArrays expected = new PCArrays(); + expected.init(); + // PCArrays does not redefine equals, so use the string representation + assertEquals("Wrong instance returned from datastore", expected.toString(), pc.toString()); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClassRegistration.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClassRegistration.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClassRegistration.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClassRegistration.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,229 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.*; +import java.lang.reflect.*; +import java.net.URL; +import java.net.URLClassLoader; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * Test registration and unregistration of pc classes. + * + * @author Michael Bouschen + * @since 1.0.2 + */ +public class Test_ClassRegistration extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_ClassRegistration.class); + } + + /** */ + protected void setUp() { } + + /** */ + protected void tearDown() { } + + /** */ + public void test() throws Throwable { + String jdoapi = System.getProperty("jdoapi"); + String pcclasses = System.getProperty("pcclasses"); + final URL jdoapiURL = new URL("file:" + jdoapi); + final URL pcclassesURL = new URL("file:" + pcclasses); + ClassLoader cl = (ClassLoader)AccessController.doPrivileged( + new PrivilegedAction () { + public Object run () { + return new URLClassLoader(new URL[] {pcclassesURL, jdoapiURL}, null); + }}); + Class implHelperClass = getImplHelperClass(cl); + Object implHelper = callGetInstance(implHelperClass); + + // ------------------------------------------- + // Test registration + // ------------------------------------------- + + // This loads the pc class + Class employeeClass = loadPCClass("org.apache.jdo.pc.empdept.PCEmployee", cl); + + Collection registeredClasses = + callGetRegisteredClasses(implHelperClass, implHelper); + int nrOfRegisteredClasses = registeredClasses.size(); + + // test whether PCEmployee is registered + assertTrue("Missing registered class " + employeeClass, + registeredClasses.contains(employeeClass)); + + // ------------------------------------------- + // Test unregistration by class + // ------------------------------------------- + + // test unregisterClass with null parameter + try { + callUnregisterClass(implHelperClass, implHelper, null); + fail("Missing exception when calling unregisterClass(null)"); + } + catch (NullPointerException ex) { + // expected exception => OK + } + + // test unregister by class + callUnregisterClass(implHelperClass, implHelper, employeeClass); + registeredClasses = callGetRegisteredClasses(implHelperClass, implHelper); + int newNrOfRegisteredClasses = registeredClasses.size(); + // test nr of registered classes is decremented + assertEquals("Wrong number of registered classes", + (nrOfRegisteredClasses - 1), newNrOfRegisteredClasses); + + // test PCEmployee not registered anymore + assertFalse("Class " + employeeClass + " still registered", + registeredClasses.contains(employeeClass)); + + // ------------------------------------------- + // Test unregistration by class loader + // ------------------------------------------- + + // test unregister by classLoader + callUnregisterClasses(implHelperClass, implHelper, cl); + registeredClasses = callGetRegisteredClasses(implHelperClass, implHelper); + assertTrue("Unregistration failed, set of registered classes is not empty: " + registeredClasses, + registeredClasses.isEmpty()); + + // Test succeeded + } + + /** */ + private Class loadPCClass(String className, ClassLoader cl) + throws Exception + { + Class pcClass = Class.forName(className, true, cl); + assertEquals("PC class " + className + " loaded by wrong class loader", + cl, getClassLoaderForClass(pcClass)); + return pcClass; + } + + /** */ + private Class getImplHelperClass(ClassLoader cl) + throws Exception + { + Class implHelperClass = + Class.forName("javax.jdo.spi.JDOImplHelper", true, cl); + assertEquals("JDOImplHelper loaded by wrong class loader", + cl, getClassLoaderForClass(implHelperClass)); + return implHelperClass; + } + + /** */ + private Object callGetInstance(Class implHelperClass) + throws Throwable + { + final Method method = implHelperClass.getMethod("getInstance", null); + try { + return AccessController.doPrivileged( + new PrivilegedExceptionAction () { + public Object run () throws Exception { + return method.invoke(null, null); + }}); + } + catch (PrivilegedActionException pae) { + Throwable e = pae.getException(); + if (e instanceof InvocationTargetException) + e = ((InvocationTargetException)e).getTargetException(); + throw e; + } + } + + /** */ + private Collection callGetRegisteredClasses(Class implHelperClass, Object implHelper) + throws Throwable + { + Method method = implHelperClass.getMethod("getRegisteredClasses", null); + try { + return (Collection)method.invoke(implHelper, null); + } + catch (InvocationTargetException ex) { + throw ((InvocationTargetException)ex).getTargetException(); + } + } + + /** */ + private void callUnregisterClass(final Class implHelperClass, + final Object implHelper, + final Class parameter) + throws Throwable + { + final Method method = implHelperClass.getMethod( + "unregisterClass", new Class[] {Class.class}); + try { + AccessController.doPrivileged( + new PrivilegedExceptionAction () { + public Object run () throws Exception { + return method.invoke(implHelper, new Object[] {parameter}); + }}); + } + catch (PrivilegedActionException pae) { + Throwable e = pae.getException(); + if (e instanceof InvocationTargetException) + e = ((InvocationTargetException)e).getTargetException(); + throw e; + } + } + + /** */ + private void callUnregisterClasses(final Class implHelperClass, + final Object implHelper, + final ClassLoader parameter) + throws Throwable + { + final Method method = implHelperClass.getMethod( + "unregisterClasses", new Class[] {ClassLoader.class}); + try { + AccessController.doPrivileged( + new PrivilegedExceptionAction () { + public Object run () throws Exception { + return method.invoke(implHelper, new Object[] {parameter}); + }}); + } + catch (PrivilegedActionException pae) { + Throwable e = pae.getException(); + if (e instanceof InvocationTargetException) + e = ((InvocationTargetException)e).getTargetException(); + throw e; + } + } + + /** */ + private ClassLoader getClassLoaderForClass(final Class clazz) + { + if (clazz == null) + return null; + return(ClassLoader)AccessController.doPrivileged( + new PrivilegedAction () { + public Object run () { + return clazz.getClassLoader(); + }}); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClosePMF.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClosePMF.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClosePMF.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_ClosePMF.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,159 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Test_ClosePMF.java + * + * Created on April 8, 2003, 2:59 PM + */ + +package org.apache.jdo.test; + +import java.security.AccessController; +import java.security.PrivilegedAction; + +import javax.jdo.PersistenceManager; +import javax.jdo.Transaction; +import javax.jdo.JDOUserException; +import javax.jdo.JDOException; + +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; + +/** + * Test that PMF.close() behaves properly. This test creates multiple PMs + * and verifies that close() throws an exception with a nested exception + * for each PM still open; once all PMs are closed, then close() should + * complete normally. + * + * @author Craig Russell + */ +public class Test_ClosePMF extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_ClosePMF.class); + } + + /** Creates a new instance of Test_ClosePMF */ + public Test_ClosePMF() { + super(); + } + + /** + * Inserts some number of objects in the database. Here, we just get + * some PMs and use them to add objects. Next, we get a PM that is not + * closed explicitly (it should be closed automatically by PMF.close()). + * Next, get a PM, begin a tx and verify that this one causes PMF.close() + * to fail. + */ + public void test() throws Exception { + PersistenceManager pm0 = null; + PersistenceManager pm2 = null; + PersistenceManager pm4 = null; + try { + pm0 = pmf.getPersistenceManager(); + pm0.currentTransaction().begin(); + pm0.currentTransaction().rollback(); + insertObjects(); // this pm is closed by insertObjects + pm2 = pmf.getPersistenceManager(); + pm2.currentTransaction().begin(); + pm2.currentTransaction().commit(); + insertObjects(); // this pm is closed by insertObjects + pm4 = pmf.getPersistenceManager(); + pm4.currentTransaction().begin(); + + assertNotClosed(pm0, "pm0"); + assertNotClosed(pm2, "pm2"); + assertNotClosed(pm4, "pm4"); + JDOUserException jdouex = null; + try { + AccessController.doPrivileged(new PrivilegedAction () { + public Object run () throws JDOUserException { + pmf.close(); // this should throw an exception with pm4 + return null; + }}); + } catch (JDOUserException ex) { + jdouex = ex; + } catch (Exception ex) { + fail("Wrong exception, expected JDOUserException, got " + ex); + } + + assertNotNull("Failed to catch exception from close.", jdouex); + + Throwable[] nested = jdouex.getNestedExceptions(); + assertEquals("Got wrong number of nested exceptions", 1, nested.length); + + JDOException jdoex = (JDOException)nested[0]; + Object failed = jdoex.getFailedObject(); + assertTrue("Got wrong failed object of type: " + failed.getClass().getName() + + ", expected PersistenceManager.", (failed instanceof PersistenceManager)); + + assertEquals("Got wrong PersistenceManager", pm4, failed); + + // since we didn't explicitly close these, they should still be open + assertNotClosed(pm0, "pm0"); + assertNotClosed(pm2, "pm2"); + assertNotClosed(pm4, "pm4"); + pm4.currentTransaction().commit(); + // now any exception is fatal + AccessController.doPrivileged(new PrivilegedAction () { + public Object run () throws JDOUserException { + pmf.close(); + return null; + }}); + // the pmf.close() should have closed all the open pms + assertClosed(pm0, "pm0"); + assertClosed(pm2, "pm2"); + assertClosed(pm4, "pm4"); + // should be able to call this now without effect + pmf.close(); + } + finally { + finalizePM(pm0); + finalizePM(pm2); + finalizePM(pm4); + } + } + + void assertNotClosed(PersistenceManager pm, String msg) { + assertFalse("PersistenceManager " + msg + "(" + pm + ") is closed.", pm.isClosed()); + } + + void assertClosed(PersistenceManager pm, String msg) { + assertTrue("PersistenceManager " + msg + "(" + pm + ") is not closed.", pm.isClosed()); + } + + void finalizePM(PersistenceManager pm) + { + if (pm != null && !pm.isClosed()) { + Transaction tx = pm.currentTransaction(); + if ((tx != null) && tx.isActive()) { + tx.rollback(); + } + pm.close(); + } + } + + /** Redefine getFactory called be insertObjects. */ + protected Factory getFactory(int verify) + { + return new PointFactory(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Collections.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Collections.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Collections.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Collections.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,66 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.pc.PCCollections; + +/** +* This test tries to insert a persistent capable object into a database; +* the object has a field of each of the kind of primitive and immutable +* Java types. +* +* @author Dave Bristor +*/ +public class Test_Collections extends Test_SCO_Base { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Collections.class); + } + + /** */ + public void test() throws Exception { + insertObjects(); + readObjects(); + checkExtent(PCCollections.class, 1); + } + + /** */ + protected void insertObjects() { + insertAllTypes(); + } + + /** + * redefine verify called by readObjects to check whether the read + * instance is correct. + */ + protected void verify(int i, Object pc) { + switch (i) { + case 0 : + assertEquals("Wrong PCPoint instance", createPoint(), pc); + break; + case 1: + assertEqualsPCCollections(createAllTypes(createPoint()), pc); + break; + default: + fail("Wrong number of inserted objects, expected two"); + break; + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Container.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Container.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Container.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Container.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,358 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Test_Container.java + * + * Created on August 30, 2001 + */ + +package org.apache.jdo.test; + +import java.util.*; + +import javax.jdo.*; + +import org.apache.jdo.pc.PCPoint; +import org.apache.jdo.test.util.Container; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +/** + * Test driver to test transactions in a managed environment + * + * @author Marina Vatkina + */ +public class Test_Container extends AbstractTest { + + Container tc = null; + String url = null; + Object oid = null; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Container.class); + } + + /** */ + protected void setUp() { + tc = new Container(debug); + url = createURL(); + } + + /** */ + protected void tearDown() { + pmf = tc.close(); + closePMF(); + } + + /** */ + public void test() { + String repr = null; + + // Insert - rollback + if (debug) logger.debug("===TESTCMT_NEG: "); + runTestCMT(false); + + // Insert - commit + if (debug) logger.debug("===TESTCMT_POS: "); + runTestCMT(true); + + // Get Extent with 1 instance + if (debug) logger.debug("===QUERY: "); + repr = queryInstance(); + assertEquals("Wrong PCPoint instance", + "org.apache.jdo.pc.PCPoint x: 9, y: 999", repr); + + // Update instance and test the same PMs in both "beans" + if (debug) logger.debug("===TESTCMT_CMT: "); + runTestCMT_CMT(); + + // Update instance and test the same PMs in both "beans" + if (debug) logger.debug("===TESTBMT_JDO_CMT: "); + runTestBMT_JDO_CMT(); + + // Update instance + if (debug) logger.debug("===TESTBMT_JDO_POS: "); + runTestBMT_JDO_POS(); + + // Get Extent with 1 updated instance + if (debug) logger.debug("===QUERY: "); + repr = queryInstance(); + assertEquals("Wrong PCPoint instance", + "org.apache.jdo.pc.PCPoint x: 72, y: 720", repr); + + // Delete the instance + if (debug) logger.debug("===TESTBMT_UT_POS: "); + runTestBMT_UT_POS(); + + // Get Extent with 0 instances + if (debug) logger.debug("===QUERY: "); + repr = queryInstance(); + assertEquals("Empty query result expected", "NONE", repr); + + // Fail to delete instance got by non-existent Oid + if (debug) logger.debug("===TESTBMT_UT_NEG: "); + runTestBMT_UT_NEG(); + + // Fail to update instance got by non-existent Oid + if (debug) logger.debug("===TESTBMT_JDO_NEG: "); + runTestBMT_JDO_NEG(); + } + + private void runTestCMT(boolean commit) { + PersistenceManager pm = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startCMT(); + pm = pmf.getPersistenceManager(); + + Object o = createInstance(); + pm.makePersistent(o); + + oid = pm.getObjectId(o); + } + finally { + pm.close(); + + // This is a hack to make transaction fail: + tc.finishCMT(commit); + } + } + + private void runTestCMT_CMT() { + PersistenceManager pm = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startCMT(); + pm = pmf.getPersistenceManager(); + + PCPoint p = (PCPoint)pm.getObjectById(oid, false); + updateInstance(p); + + validate(p, pm); + + } finally { + pm.close(); + tc.finishCMT(true); + } + } + + private void runTestBMT_JDO_CMT() { + PersistenceManager pm = null; + Transaction tx = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startBMT(); + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + + PCPoint p = (PCPoint)pm.getObjectById(oid, false); + updateInstance(p); + + validate(p, pm); + + tx.commit(); + } finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + pm.close(); + tc.finishBMT(); + } + } + + private void runTestBMT_JDO_POS() { + PersistenceManager pm = null; + Transaction tx = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startBMT(); + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + + updateInstance((PCPoint)pm.getObjectById(oid, false)); + + tx.commit(); + } + finally { + if ((tx != null) && tx.isActive()) + tx.rollback(); + pm.close(); + tc.finishBMT(); + } + } + + private void runTestBMT_JDO_NEG() { + PersistenceManager pm = null; + Transaction tx = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startBMT(); + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + + updateInstance((PCPoint)pm.getObjectById(oid, false)); + + tx.commit(); + fail("ERROR TO FINISH: "); + } catch (Exception e) { + if (debug) + logger.debug ("EXPECTED FOR testBMT_JDO_NEG: " + e.getMessage()); + tx.rollback(); + } + finally { + pm.close(); + tc.finishBMT(); + } + } + + + private void runTestBMT_UT_POS() { + PersistenceManager pm = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startBMT(); + tc.getUserTransaction().begin(); + + pm = pmf.getPersistenceManager(); + pm.deletePersistent(pm.getObjectById(oid, false)); + + pm.close(); + tc.getUserTransaction().commit(); + } catch (Exception e) { + if (debug) logger.debug ("testBMT_UT_POS: " + e); + try { + tc.getUserTransaction().rollback(); + } catch (Exception e1) { + if (debug) logger.debug("testBMT_UT_POS: " + e1); + fail("testBMT_UT_POS: " + e1); + } + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + tc.finishBMT(); + } + } + + private void runTestBMT_UT_NEG() { + PersistenceManager pm = null; + try { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startBMT(); + tc.getUserTransaction().begin(); + + pm = pmf.getPersistenceManager(); + pm.deletePersistent(pm.getObjectById(oid, false)); + + pm.close(); + + tc.getUserTransaction().commit(); + tc.finishBMT(); + fail("ERROR TO FINISH: "); + } catch (Exception e) { + if (debug) + logger.debug ("EXPECTED FOR testBMT_UT_NEG: " + e.getMessage()); + try { + tc.getUserTransaction().rollback(); + } catch (Exception e1) { + if (debug) logger.debug ("ERROR IN testBMT_UT_NEG: " + e1); + fail("ERROR IN testBMT_UT_NEG: " + e1); + } + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + } + + } + + private PCPoint createInstance() { + PCPoint p = new PCPoint(9, 999); + return p; + } + + private void updateInstance(PCPoint p) { + int i = p.getX(); + p.setX(i*2); + p.setY(new Integer(i*20)); + } + + + private String queryInstance() { + PersistenceManagerFactory pmf = tc.getPMF(url); + tc.startBMT(); + PersistenceManager pm = pmf.getPersistenceManager(); +/* Not yet implemented + Query query = pm.newQuery(); + query.setClass (test.PCPoint.class); + query.setFilter("x == " + ???); + query.setCandidates(pm.getExtent(test.PCPoint.class, false)); + + Object result = query.execute(); +*/ + Extent result = pm.getExtent(org.apache.jdo.pc.PCPoint.class, false); + + String s = ""; + if (result == null) { + s = "NULL"; + } else { + int count = 0; + Object pc = null; + + for (Iterator i = result.iterator(); i.hasNext();) { + pc = i.next(); + count++; + } + if ( count == 0 ) { + s = "NONE"; + } else if ( count > 1 ) { + s = "MANY"; + } else { + s = "" + pc; + } + } + pm.close(); + tc.finishBMT(); + + return s; + } + + private void validate(PCPoint e, PersistenceManager pm0) { + PersistenceManagerFactory pmf = tc.getPMF(url); + PersistenceManager pm = pmf.getPersistenceManager(); + + PCPoint p = (PCPoint)pm.getObjectById(oid, false); + + if (p != e) { + fail("OBJECTS NOT EQUAL: " + e + " <> " + p); + } else { + if (debug) logger.debug ("OBJECTS ARE EQUAL!!! " ); + } + + if (!pm0.equals(pm)) { + fail("PMS NOT EQUAL!!!"); + } else { + if (debug) logger.debug ("PMS ARE EQUAL!!!"); + } + + pm.close(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Cycle.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Cycle.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Cycle.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Cycle.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,130 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import javax.jdo.*; + +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.pc.PCCycle; +import org.apache.jdo.pc.PCCycle2; + +// +// This _test_ does not yet work. +// +// When printing the objects after reading them from the database, NPE's are +// generated because we don't yet have an enhancer...so the field references +// to the other class, made in each class's toString() method, fail after +// reading. When we have an enhancer, these will be enhanced to be function +// calls to get the field of the other class, and toString() will be invoked +// in the result of that. +// + +/** +* Test 2 PC's that refer to each other in a cycle/loop. +* +* @author Dave Bristor +*/ +public class Test_Cycle extends Test_Fetch { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_Cycle.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 { + insertObjects(); + initExpected(); + readObjects(); + checkExtent(PCCycle.class, 1); + checkExtent(PCCycle2.class, 1); + } + + // We override this from AbstractTest and insert our own objects + protected void insertObjects() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + try { + tx.begin(); + + PCCycle c = new PCCycle(); + PCCycle2 c2 = new PCCycle2(); + + c.init("cyrcular", c2); + c2.init(12358, c); + + if (debug) { + logger.debug("Before insert: " + c); + logger.debug("Before insert: " + c2); + } + + pm.makePersistent(c); + pm.makePersistent(c2); + + tx.commit(); + + Object oid_c = JDOHelper.getObjectId(c); + Object oid_c2 = JDOHelper.getObjectId(c2); + + if (debug) { + logger.debug("inserted c: " + oid_c); + logger.debug("inserted c2: " + oid_c2); + } + + oids.add(oid_c); + oids.add(oid_c2); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + private PCCycle expectedC = null; + private PCCycle2 expectedC2 =null; + + private void initExpected() { + expectedC = new PCCycle(); + expectedC2 = new PCCycle2(); + expectedC.init("cyrcular", expectedC2); + expectedC2.init(12358, expectedC); + } + + /** + * redefine verify called by readObjects to check whether the read + * instance is correct. + */ + protected void verify(int i, Object pc) { + // PCCycle and PCCycle2 do not redefine equals, so use the string representation. + switch(i) { + case 0 : + assertEquals("Wrong instance returned from datastore", expectedC.toString(), pc.toString()); + break; + case 1: + assertEquals("Wrong instance returned from datastore", expectedC2.toString(), pc.toString()); + break; + default: + fail("Wrong number of inserted objects, expected two"); + break; + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Delete.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Delete.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Delete.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_Delete.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,155 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.io.EOFException; +import java.io.ObjectInputStream; + +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 is similar to Test_ActivateClass, but it adds extra steps of +* getting OIDs for objects and deleting those objects. +* +* @author Dave Bristor +*/ +public class Test_Delete extends AbstractTest { + + /** */ + private static boolean silent = false; + + /** */ + public static void main(String args[]) { + handleArgs(args); + if (silent) + runSilentDelete(); + else + JDORITestRunner.run(Test_Delete.class); + } + + /** */ + public void test() throws Exception { + if (!existing) { + insertObjects(); + writeOIDs(); + } + deleteObjects(false, false); + checkExtent(factory.getPCClass(), 0); + } + + /** */ + public void testOptimistic() throws Exception { + if (!existing) { + insertObjects(); + writeOIDs(); + } + deleteObjects(true, false); + checkExtent(factory.getPCClass(), 0); + } + + /** */ + public void testValidate() throws Exception { + if (!existing) { + insertObjects(); + writeOIDs(); + } + deleteObjects(false, true); + checkExtent(factory.getPCClass(), 0); + } + + /** */ + public void testOptimisticValidate() throws Exception { + if (!existing) { + insertObjects(); + writeOIDs(); + } + deleteObjects(true, true); + checkExtent(factory.getPCClass(), 0); + } + + /** */ + void deleteObjects(boolean optimistic, boolean validate) throws Exception { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try { + ObjectInputStream in = getOIDFileInputStream(); + tx.setOptimistic(optimistic); + tx.begin(); + + try { + while (true) { + Object pc = null; + Object oid = in.readObject(); + if (debug) logger.debug("fetching: " + oid); + pc = pm.getObjectById(oid, validate); + if (debug) logger.debug("Before delete: " + pc); + pm.deletePersistent(pc); + assertTrue("IsDeleted", JDOHelper.isDeleted(pc)); + } + } catch (EOFException ex) { + // OK + } + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** */ + protected Factory getFactory(int verify) { + return getFactoryByClassProperty(verify, "org.apache.jdo.pc.PCPoint"); + } + + /** */ + protected int getDefaultInsert() + { + return 5; + } + + /** */ + private static void runSilentDelete() { + try { + Test_Delete delete = new Test_Delete(); + delete.setupPMF(); + delete.test(); + delete.closePMF(); + } + catch (Exception ex) { + System.out.println("Excetion during delete"); + ex.printStackTrace(); + } + } + + /** */ + private static void handleArgs(String[] args) { + for (int i = 0; i < args.length; i++) { + if ("-silent".equals(args[i])) + silent = true; + else + System.err.println("Test_Delete: ignoring unknon option" + args[i]); + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_DupAppId.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_DupAppId.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_DupAppId.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_DupAppId.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,103 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.pc.PCPoint1; + +/** +* Tests that FOStore can correctly determine dups for application identity. +* +* @author Marina Vatkina +*/ +public class Test_DupAppId extends AbstractTest { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_DupAppId.class); + } + + /** */ + public void test() { + insertObjects(); + checkExtent(PCPoint1.class, 1); + } + + /** */ + public void insertObjects() { + PersistenceManager pm = pmf.getPersistenceManager(); + Transaction tx = pm.currentTransaction(); + + try { + PCPoint1 p = new PCPoint1(1,10); + PCPoint1 p1 = new PCPoint1(1,100); + if (debug) logger.debug("INSERT"); + tx.begin(); + if (debug) logger.debug("Inserting: " + p); + pm.makePersistent(p); + try { + if (debug) logger.debug("Inserting: " + p1); + pm.makePersistent(p1); + fail("Missing JDOException during makePersistent with the same primary key in the PersistenceManager cache."); + } catch (JDOException ex) { + // expected exception => OK + if (debug) logger.debug("caught expected " + ex); + } + tx.commit(); tx = null; + + if (debug) { + logger.debug("Inserted: " + p); + logger.debug("P isPersistent: " + JDOHelper.isPersistent(p)); + } + assertTrue("instance " + p + " + expected to be persistent", + JDOHelper.isPersistent(p)); + if (debug) + logger.debug("P1 isPersistent: " + JDOHelper.isPersistent(p1)); + assertFalse("instance " + p1 + " + expected to be not persistent", + JDOHelper.isPersistent(p1)); + + pm.close(); pm = null; + + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + if (debug) logger.debug("INSERT DUPS"); + tx.begin(); + if (debug) logger.debug("Inserting: " + p1); + pm.makePersistent(p1); + try { + tx.commit(); + fail("Missing JDOException during commit when inserting a duplicate in the datastore."); + } catch (Exception ex) { + // expected exception => OK + if (debug) logger.debug("caught expected " + ex); + } + if (debug) + logger.debug("P1 isPersistent: " + JDOHelper.isPersistent(p1)); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDept.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDept.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDept.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDept.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,300 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import java.util.*; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.pc.PCDepartment; +import org.apache.jdo.pc.PCEmployee; +import org.apache.jdo.pc.PCInsurance; +import org.apache.jdo.pc.PCProject; + +/** +* Test a PC that has some fields which are PC's. +* +* @author Dave Bristor +*/ +public class Test_EmpDept extends AbstractTest { + PCEmployee scott; + PCEmployee ed; + + PCDepartment board; + PCDepartment emg; + + PCInsurance scottIns; + PCInsurance edIns; + + PCProject solaris; + PCProject sparc; + + Object scottOid; + Object edOid; + Object boardOid; + Object emgOid; + Object scottInsOid; + Object edInsOid; + Object solarisOid; + Object sparcOid; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_EmpDept.class); + } + + /** */ + public void test() throws Exception { + createObjects(); + insertObjects(); + readObjects(); + } + + // We override this from Test_ActivateClass and insert our own objects. + // Use Test_Extent to read them back. + protected void createObjects() { + HashSet h; + + // Create and set up employees. Scott is Ed's manager. Ed is + // Scott's sole employee. + // + GregorianCalendar born = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + GregorianCalendar hired = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + + born.set(1969, 7, 20); + hired.set(1982, 5, 5); + scott = new PCEmployee(1L, "McNealy", "Scott", 200000.0, + born.getTime(), hired.getTime()); + born.set(1960, 4, 8); + hired.set(1985, 2, 3); + ed = new PCEmployee(100L, "Zander", "Ed", 400000.0, + born.getTime(), hired.getTime()); + + ed.setManager(scott); + + h = new HashSet(); + h.add(ed); + scott.setEmployees(h); + + // Set up their departments. + board = new PCDepartment(100L, "board"); + h = new HashSet(); + h.add(scott); + board.setEmployees(h); + scott.setDepartment(board); + + emg = new PCDepartment(200L, "emg"); + h = new HashSet(); + h.add(ed); + emg.setEmployees(h); + ed.setDepartment(emg); + + // Insure these guys + scottIns = new PCInsurance(1000, "Aetna", scott); + edIns = new PCInsurance(1001, "BlueCross", ed); + scott.setInsurance(scottIns); + ed.setInsurance(edIns); + + // Give them some projects to work on. Scott works on both; Ed only + // on one. + solaris = new PCProject(1L, "Solaris"); + sparc = new PCProject(2L, "Sparc"); + h = new HashSet(); + h.add(scott); + h.add(ed); + solaris.setEmployees(h); // Solaris is worked on by Scott and Ed + + h = new HashSet(); + h.add(scott); + sparc.setEmployees(h); // Sparc is worked on by Scott + + h = new HashSet(); + h.add(solaris); + h.add(sparc); + scott.setProjects(h); // Scott works on Solaris and Sparc + + h = 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()); + } + } + + protected void insertObjects() throws Exception { + PersistenceManager pm = null; + Transaction tx = null; + try { + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + + // Make it all persistent. If reachability were implemented, we'd + // only have to make scott and ed persistent, as everything else is + // reachable from them. + pm.makePersistent(scott); + pm.makePersistent(ed); + pm.makePersistent(board); + pm.makePersistent(emg); + pm.makePersistent(scottIns); + pm.makePersistent(edIns); + pm.makePersistent(solaris); + pm.makePersistent(sparc); + + tx.commit(); + + scottOid = JDOHelper.getObjectId(scott); + edOid = JDOHelper.getObjectId(ed); + boardOid = JDOHelper.getObjectId(board); + emgOid = JDOHelper.getObjectId(emg); + scottInsOid = JDOHelper.getObjectId(scottIns); + edInsOid = JDOHelper.getObjectId(edIns); + solarisOid = JDOHelper.getObjectId(solaris); + sparcOid = JDOHelper.getObjectId(sparc); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + protected void readObjects() throws Exception { + PersistenceManager pm = null; + Transaction tx = null; + try { + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + if (debug) logger.debug("readObjects: "); + assertScott(pm.getObjectById(scottOid, true)); + assertEd(pm.getObjectById(edOid, true)); + assertBoard(pm.getObjectById(boardOid, true)); + assertEmg(pm.getObjectById(emgOid, true)); + assertScottIns(pm.getObjectById(scottInsOid, true)); + assertEdIns(pm.getObjectById(edInsOid, true)); + assertSolaris(pm.getObjectById(solarisOid, true)); + assertSparc(pm.getObjectById(sparcOid, true)); + tx.rollback(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + + } + + protected void assertScott(Object scott) { + assertNotNull("scott should not be null", scott); + if (debug) logger.debug(scott.toString()); + assertTrue("scott is expected to be a PCEmployee", + (scott instanceof PCEmployee)); + assertEquals("Wrong string representation of scott", + "Emp: McNealy, Scott, id=1, born 20/Aug/1969, hired 5/Jun/1982 $200000.0 manager: none dept: board emps: 1 insurance: Aetna", + scott.toString()); + } + + protected void assertEd(Object ed) { + assertNotNull("ed should not be null", ed); + if (debug) logger.debug(ed.toString()); + assertTrue("ed is expected to be a PCEmployee", + (ed instanceof PCEmployee)); + assertEquals("Wrong string representation of ed", + "Emp: Zander, Ed, id=100, born 8/May/1960, hired 3/Mar/1985 $400000.0 manager: McNealy dept: emg emps: 0 insurance: BlueCross", + ed.toString()); + } + + protected void assertBoard(Object board) { + assertNotNull("board should not be null", board); + if (debug) logger.debug(board.toString()); + assertTrue("board is expected to be a PCDepartment", + (board instanceof PCDepartment)); + assertEquals("Wrong string representation of board", + "Dept: board, id=100, emps: 1", + board.toString()); + } + + protected void assertEmg(Object emg) { + assertNotNull("emg should not be null", emg); + if (debug) logger.debug(emg.toString()); + assertTrue("emg is expected to be a PCDepartment", + (emg instanceof PCDepartment)); + assertEquals("Wrong string representation of emg", + "Dept: emg, id=200, emps: 1", + emg.toString()); + } + + protected void assertScottIns(Object scottIns) { + assertNotNull("scottIns should not be null", scottIns); + if (debug) logger.debug(scottIns.toString()); + assertTrue("scottIns is expected to be a PCInsurance", + (scottIns instanceof PCInsurance)); + assertEquals("Wrong string representation of scotts insurance", + "Ins: Aetna, id=1000, emp McNealy", + scottIns.toString()); + } + + protected void assertEdIns(Object edIns) { + assertNotNull("edIns should not be null", edIns); + if (debug) logger.debug(edIns.toString()); + assertTrue("edIns is expected to be a PCInsurance", + (edIns instanceof PCInsurance)); + assertEquals("Wrong string representation of eds insurance", + "Ins: BlueCross, id=1001, emp Zander", + edIns.toString()); + } + + protected void assertSolaris(Object solaris) { + assertNotNull("solaris should not be null", solaris); + if (debug) logger.debug(solaris.toString()); + assertTrue("solaris is expected to be a PCProject", + (solaris instanceof PCProject)); + assertEquals("Wrong string representation of project solaris", + "PCProject: Solaris, id=1, emps: 2", + solaris.toString()); + } + + protected void assertSparc(Object sparc) { + assertNotNull("sparc should not be null", sparc); + if (debug) logger.debug(sparc.toString()); + assertTrue("sparc is expected to be a PCProject", + (sparc instanceof PCProject)); + assertEquals("Wrong string representation of project sparc", + "PCProject: Sparc, id=2, emps: 1", + sparc.toString()); + } + +} + Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppId.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppId.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppId.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppId.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,357 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + + +import java.util.*; + +import javax.jdo.*; + +import org.apache.jdo.test.util.AbstractTest; +import org.apache.jdo.test.util.JDORITestRunner; + +import org.apache.jdo.pc.appid.PCDepartment; +import org.apache.jdo.pc.appid.PCEmployee; +import org.apache.jdo.pc.appid.PCFullTimeEmployee; +import org.apache.jdo.pc.appid.PCInsurance; +import org.apache.jdo.pc.appid.PCPartTimeEmployee; +import org.apache.jdo.pc.appid.PCProject; + +/** +* Test a PC that inherits from another PC with an application identity. +* +* @author Marina Vatkina +*/ +public class Test_EmpDeptAppId extends AbstractTest { + + protected PersistenceManager pm; + protected Transaction tx; + protected String scottRepr = null; + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_EmpDeptAppId.class); + } + + /** */ + public void test() throws Exception + { + insertObjects(); + checkObjects(); + updateObject(); + checkObjects(); + deleteObjects(); + } + + /** */ + protected void insertObjects() { + try + { + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + createObjects(); + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** Check created objects by Extent and getObjectById. + */ + protected void checkObjects() { + try { + pm = pmf.getPersistenceManager(); + if (debug) logger.debug("EXTENT PCEmployee"); + Extent ext = pm.getExtent(PCEmployee.class, true); + TreeMap elements = new TreeMap(); // Sort the results. + for (Iterator i = ext.iterator(); i.hasNext();) { + PCEmployee p = (PCEmployee)i.next(); + elements.put(p.toString(), p); + } + + int count = 0; + for (Iterator i = elements.values().iterator(); i.hasNext();) { + Object p = i.next(); + verify(count++, p); + loadAndVerifyObject(p, pm); + } + } + finally { + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + /** Delete objects and check by getObjectById. + */ + protected void deleteObjects() { + try { + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + + if (debug) logger.debug("EXTENT PCEmployee"); + Extent ext = pm.getExtent(PCEmployee.class, true); + TreeMap elements = new TreeMap(); // Sort the results. + for (Iterator i = ext.iterator(); i.hasNext();) { + PCEmployee p = (PCEmployee)i.next(); + elements.put(p.toString(), p); + } + + for (Iterator i = elements.values().iterator(); i.hasNext();) { + Object p = i.next(); + Object oid = pm.getObjectId(p); + if (debug) logger.debug("Delete with OID: " + oid); + pm.deletePersistent(p); + + Object o = pm.getObjectById(oid, true); + assertSame("Expected same instance from getObjectById", p, o); + assertTrue("Instance should be deleted", JDOHelper.isDeleted(o)); + } + tx.commit(); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + checkExtent(PCEmployee.class, 0); + } + + protected void createObjects() { + // Create and set up employees. Scott is Ed's manager. Ed is + // Scott's sole employee. + // + ArrayList arr; + + GregorianCalendar born = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + GregorianCalendar hired = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + + born.set(1945, 5, 9); + hired.set(2001, 3, 5); + PCEmployee john = + new PCEmployee( + "John", "One", born.getTime(), + 900L, hired.getTime(), + null, null, null, null, null); + + born.set(1969, 7, 20); + hired.set(1982, 5, 5); + PCEmployee scott = + new PCFullTimeEmployee( + "Scott", "McNealy", born.getTime(), + 1L, hired.getTime(), + 200000.0); + born.set(1960, 4, 8); + hired.set(1985, 2, 3); + PCEmployee ed = + new PCPartTimeEmployee( + "Ed", "Zander", born.getTime(), + 100L, hired.getTime(), + 400000.0); + + ed.setManager(scott); + + arr = new ArrayList(); + arr.add(ed); + scott.setEmployees(arr); + + // Set up their departments. + PCDepartment board = + new PCDepartment(100L, "board"); + HashSet h = new HashSet(); + h.add(scott); + board.setEmployees(h); + scott.setDepartment(board); + + PCDepartment emg = + new PCDepartment(200L, "emg"); + h = new HashSet(); + h.add(ed); + emg.setEmployees(h); + ed.setDepartment(emg); + john.setDepartment(emg); + + // Insure these guys + PCInsurance scottIns = new PCInsurance(1000, "Aetna", scott); + PCInsurance edIns = new PCInsurance(1001, "BlueCross", ed); + scott.setInsurance(scottIns); + ed.setInsurance(edIns); + PCInsurance johnIns = new PCInsurance(9001, "BlueCross", john); + john.setInsurance(johnIns); + + // Give them some projects to work on. Scott works on both; Ed only + // on one. + PCProject solaris = new PCProject(1L, "Solaris"); + PCProject sparc = new PCProject(2L, "Sparc"); + h = new HashSet(); + h.add(scott); + h.add(ed); + solaris.setEmployees(h); // Solaris is worked on by Scott and Ed + + h = new HashSet(); + h.add(scott); + sparc.setEmployees(h); // Sparc is worked on by Scott + + arr = new ArrayList(); + arr.add(solaris); + arr.add(sparc); + scott.setProjects(arr); // Scott works on Solaris and Sparc + + arr = new ArrayList(); + arr.add(solaris); + ed.setProjects(arr); // 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(john.toString()); + logger.debug(board.toString()); + logger.debug(emg.toString()); + logger.debug(scottIns.toString()); + logger.debug(edIns.toString()); + logger.debug(johnIns.toString()); + logger.debug(solaris.toString()); + logger.debug(sparc.toString()); + } + + // Make it all persistent. If reachability were implemented, we'd + // only have to make scott and ed persistent, as everything else is + // reachable from them. + pm.makePersistent(scott); + pm.makePersistent(ed); + pm.makePersistent(john); + + Object oid = pm.getObjectId(scott); + if (debug) logger.debug("Instance OID for scott: " + oid); + Object o = pm.getObjectById(oid, true); + assertSame("getObjectById should return the same instance", scott, o); + + oid = pm.getObjectId(ed); + if (debug) logger.debug("Instance OID for ed: " + oid); + o = pm.getObjectById(oid, true); + assertSame("getObjectById should return the same instance", ed, o); + + oid = pm.getObjectId(john); + if (debug) logger.debug("Instance OID for john: " + oid); + o = pm.getObjectById(oid, true); + assertSame("getObjectById should return the same instance", john, o); + + scottRepr = "PCFullTimeEmployee: PCEmployee: PCPerson: McNealy, Scott, born 20/Aug/1969, id=1, hired 5/Jun/1982 manager: none dept: board emps: 1 insurance: Aetna $200000.0"; + } + + /** Check created objects by Extent and getObjectById. + */ + protected void updateObject() { + try { + pm = pmf.getPersistenceManager(); + tx = pm.currentTransaction(); + tx.begin(); + String s = "1"; + Object oid = pm.newObjectIdInstance(PCEmployee.class, s); + if (debug) + logger.debug("GOT OID: " + oid.getClass().getName() + + " value: " + oid); + PCEmployee p = (PCEmployee)pm.getObjectById(oid, true); + if (debug) logger.debug("GOT INSTANCE TO UPDATE: " + p); + + GregorianCalendar date = + new GregorianCalendar(TimeZone.getTimeZone("America/New_York")); + date.set(1999, 7, 9); + + p.setHiredate(date.getTime()); + tx.commit(); + + oid = pm.getObjectId(p); + Object o = pm.getObjectById(oid, true); + if (debug) logger.debug("GOT INSTANCE AFTER COMMIT: " + o); + assertSame("Expected to get same instance", p, o); + scottRepr = "PCFullTimeEmployee: PCEmployee: PCPerson: McNealy, Scott, born 20/Aug/1969, id=1, hired 9/Aug/1999 manager: none dept: board emps: 1 insurance: Aetna $200000.0"; + assertEquals("Wrong string represenatation of updated instance scott", + scottRepr, o.toString()); + } + finally { + if (tx != null && tx.isActive()) + tx.rollback(); + if (pm != null && !pm.isClosed()) + pm.close(); + } + } + + protected void loadAndVerifyObject(Object obj, PersistenceManager pm) { + Object oid = pm.getObjectId(obj); + if (debug) logger.debug("Instance OID: " + oid); + + PersistenceManager pm1 = null; + try { + pm1 = pmf.getPersistenceManager(); + Object obj1 = pm1.getObjectById(oid, true); + if (debug) logger.debug("Instance from OID: " + obj1); + assertEquals("Wrong string represenatation of loaded instance", + obj.toString(), obj1.toString()); + Object oid1 = pm1.getObjectId(obj1); + if (debug) logger.debug("OID from instance: " + oid1); + assertEquals("OID from instance not equal", oid, oid1); + Object obj2 = pm1.getObjectById(oid, true); + assertSame("Same instance from getObjectById", obj1, obj2); + } + finally { + if (pm1 != null && !pm1.isClosed()) + pm1.close(); + } + } + + /** */ + protected void verify(int i, Object pc) { + if (debug) logger.debug("verify " + pc); + Object oid = JDOHelper.getObjectId(pc); + switch(i) { + case 0 : + // should be John + assertEquals("Unexpected string represenatation of John", + "PCEmployee: PCPerson: One, John, born 9/Jun/1945, id=900, hired 5/Apr/2001 manager: none dept: emg emps: 0 insurance: BlueCross", + pc.toString()); + assertEquals("Unexpected oid", new PCEmployee.Oid("900"), oid); + break; + case 1: + // should be Scott + assertEquals("Unexpected string represenatation of Scott", scottRepr, pc.toString()); + assertEquals("Unexpected oid", new PCEmployee.Oid("1"), oid); + break; + case 2: + // should be Ed + assertEquals("Unexpected string represenatation of Ed", + "PCPartTimeEmployee: PCEmployee: PCPerson: Zander, Ed, born 8/May/1960, id=100, hired 3/Mar/1985 manager: McNealy dept: emg emps: 0 insurance: BlueCross $400000.0", + pc.toString()); + assertEquals("Unexpected oid", new PCEmployee.Oid("100"), oid); + break; + default: + fail("Wrong number of inserted objects, expected three"); + break; + } + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdDelete.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdDelete.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdDelete.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdDelete.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,39 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test a PC that inherits from another PC with an application identity. +* +* @author Marina Vatkina +*/ +public class Test_EmpDeptAppIdDelete extends Test_EmpDeptAppId { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_EmpDeptAppIdDelete.class); + } + + /** */ + public void test() throws Exception + { + deleteObjects(); + } + +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdInsert.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdInsert.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdInsert.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdInsert.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,39 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test a PC that inherits from another PC with an application identity. +* +* @author Marina Vatkina +*/ +public class Test_EmpDeptAppIdInsert extends Test_EmpDeptAppId { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_EmpDeptAppIdInsert.class); + } + + /** */ + public void test() throws Exception + { + insertObjects(); + checkObjects(); + } +} Added: incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdUpdate.java URL: http://svn.apache.org/viewcvs/incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdUpdate.java?view=auto&rev=158176 ============================================================================== --- incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdUpdate.java (added) +++ incubator/jdo/trunk/ri11/test/java/org/apache/jdo/test/Test_EmpDeptAppIdUpdate.java Fri Mar 18 17:02:29 2005 @@ -0,0 +1,40 @@ +/* + * Copyright 2005 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.jdo.test; + +import org.apache.jdo.test.util.JDORITestRunner; + +/** +* Test a PC that inherits from another PC with an application identity. +* +* @author Marina Vatkina +*/ +public class Test_EmpDeptAppIdUpdate extends Test_EmpDeptAppId { + + /** */ + public static void main(String args[]) { + JDORITestRunner.run(Test_EmpDeptAppIdUpdate.class); + } + + /** */ + public void test() throws Exception + { + updateObject(); + checkObjects(); + } + +}