openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r627979 [25/39] - in /openjpa/trunk: openjpa-lib/src/test/java/org/apache/openjpa/lib/test/ openjpa-persistence-jdbc/ openjpa-persistence-jdbc/src/test/java/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/ openjpa-per...
Date Fri, 15 Feb 2008 09:20:40 GMT
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestCheckConsistency.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,404 @@
+/*
+ * TestCheckConsistency.java
+ *
+ * Created on October 9, 2006, 6:23 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
+
+import org.apache.openjpa.persistence.JPAFacadeHelper;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+public class TestCheckConsistency extends BaseKernelTest {
+
+    private Object _oid = null;
+
+    /**
+     * Creates a new instance of TestCheckConsistency
+     */
+    public TestCheckConsistency(String name) {
+        super(name);
+    }
+
+    public void setUp() {
+        deleteAll(RuntimeTest1.class);
+
+        RuntimeTest1 pc = new RuntimeTest1();
+        pc.setIntField(1);
+        pc.setIntField1(1);
+        _oid = persist(pc);
+    }
+
+    public void testConsistentDatastoreTransaction() {
+
+        OpenJPAEntityManager pm = getPM();
+        pm.setOptimistic(false);
+        pm.validateChanges();        // no-op outside trans
+        startTx(pm);
+
+        RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid);
+        pc.setIntField1(100);
+
+        RuntimeTest1 npc = new RuntimeTest1();
+        npc.setIntField(2);
+        npc.setIntField1(2);
+        pm.persist(npc);
+        pm.validateChanges();
+
+        assertEquals(100, pc.getIntField1());
+        assertTrue(pm.isPersistent(npc));
+
+        pc.setIntField1(200);
+        npc.setIntField1(300);
+        endTx(pm);
+
+        assertEquals(200, pc.getIntField1());
+        assertTrue(pm.isPersistent(npc));
+        assertEquals(300, npc.getIntField1());
+        endEm(pm);
+    }
+
+    public void testConsistentDatastoreTransactionWithRollback() {
+        OpenJPAEntityManager pm = getPM();
+        pm.setOptimistic(false);
+        pm.validateChanges();        // no-op outside trans
+        startTx(pm);
+
+        RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid);
+        pc.setIntField1(100);
+
+        RuntimeTest1 npc = new RuntimeTest1();
+        pm.persist(npc);
+        Object noid = pm.getObjectId(npc);
+
+        pm.validateChanges();
+        assertEquals(100, pc.getIntField1());
+        assertTrue(pm.isPersistent(npc));
+
+        pc.setIntField1(200);
+        npc.setIntField1(300);
+        rollbackTx(pm);
+
+        assertEquals(1, pc.getIntField1());
+        assertFalse(pm.isPersistent(npc));
+        assertEquals(0, npc.getIntField1());
+        endEm(pm);
+
+        pm = getPM();
+        try {
+            RuntimeTest1 temp =
+                (RuntimeTest1) pm.find(RuntimeTest1.class, noid);
+            fail("Object should not exist." + temp.getIntField() + "::" +
+                temp.getIntField1());
+        } catch (Exception jonfe) {
+        }
+        endEm(pm);
+    }
+
+    //FIXME jthomas
+/*    
+    public void testInconsistentDatastoreTransaction() {
+        OpenJPAEntityManager pm = getPM();
+        FetchPlan fetch = (FetchPlan) pm.getFetchPlan();
+        pm.setOptimistic(false);
+        pm.setRetainState(false);
+        pm.validateChanges();        // no-op outside trans
+        pm.begin();
+  */
+    //FIXME jthomas
+    /*
+   fetch.setReadLockLevel(pm.LOCK_NONE);
+   fetch.setWriteLockLevel(pm.LOCK_NONE);
+    */
+    /*
+RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,_oid);
+pc.setIntField(100);
+
+OpenJPAEntityManager pm2 = getPM();
+pm2.begin();
+RuntimeTest1 copy = (RuntimeTest1) pm2.find(RuntimeTest1.class,_oid);
+copy.setIntField(-1);
+pm2.commit();
+pm2.close();
+
+RuntimeTest1 npc = new RuntimeTest1();
+pm.persist(npc);
+
+try {
+   pm.validateChanges();
+   fail("Didn't find inconsistency.");
+} catch (Exception jove) {
+   //FIXME
+   /*
+   Throwable[] t = jove.getNestedExceptions();
+   assertEquals(1, t.length);
+   assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
+    */
+//        }
+
+    /*        assertTrue(pm.getRollbackOnly());
+           pm.rollback();
+
+           assertEquals(-1, pc.getIntField());
+           assertFalse(pm.isPersistent(npc));
+           endEm(pm,());
+       }
+    */
+    public void testConsistentOptimisticTransaction() {
+        OpenJPAEntityManager pm = getPM();
+        pm.setOptimistic(true);
+        pm.validateChanges();        // no-op outside trans
+        startTx(pm);
+        boolean hasConn = hasConnection(pm);
+
+        RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid);
+        pc.setIntField1(100);
+
+        RuntimeTest1 npc = new RuntimeTest1();
+        npc.setIntField(2);
+        npc.setIntField1(2);
+        pm.persist(npc);
+
+        pm.validateChanges();
+        if (!hasConn)
+            assertFalse(hasConnection(pm));
+
+        assertEquals(100, pc.getIntField1());
+        assertTrue(pm.isPersistent(npc));
+
+        pc.setIntField1(200);
+        npc.setIntField1(300);
+        endTx(pm);
+
+        assertEquals(200, pc.getIntField1());
+        assertTrue(pm.isPersistent(npc));
+        assertEquals(300, npc.getIntField1());
+        endEm(pm);
+    }
+
+    private boolean hasConnection(OpenJPAEntityManager pm) {
+        return JPAFacadeHelper.toBroker(pm).hasConnection();
+    }
+
+    public void testConsistentOptimisticTransactionWithRollback() {
+        OpenJPAEntityManager pm = getPM();
+        pm.setOptimistic(true);
+        pm.validateChanges();        // no-op outside trans
+        startTx(pm);
+        boolean hasConn = hasConnection(pm);
+
+        RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class, _oid);
+        pc.setIntField1(100);
+
+        RuntimeTest1 npc = new RuntimeTest1();
+        pm.persist(npc);
+        Object noid = pm.getObjectId(npc);
+
+        pm.validateChanges();
+        if (!hasConn)
+            assertFalse(hasConnection(pm));
+
+        assertEquals(100, pc.getIntField1());
+        assertTrue(pm.isPersistent(npc));
+
+        pc.setIntField1(200);
+        npc.setIntField1(300);
+        rollbackTx(pm);
+
+        assertEquals(1, pc.getIntField1());
+        assertFalse(pm.isPersistent(npc));
+        assertEquals(0, npc.getIntField1());
+        endEm(pm);
+
+        pm = getPM();
+        try {
+            RuntimeTest1 temp =
+                (RuntimeTest1) pm.find(RuntimeTest1.class, noid);
+
+            fail("Object should not exist." + temp.getIntField() + "::" +
+                temp.getIntField1());
+        } catch (Exception jonfe) {
+        }
+    }
+//FIXME 
+    /*
+public void testInconsistentOptimisticTransactionWithoutRefresh() {
+OpenJPAEntityManager pm = getPM();
+pm.setRetainState(false);
+pm.setOptimistic(true);
+pm.validateChanges();        // no-op outside trans
+pm.begin();
+
+RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,_oid);
+pc.setIntField(100);
+
+OpenJPAEntityManager pm2 = getPM();
+pm2.begin();
+RuntimeTest1 copy = (RuntimeTest1) pm2.find(RuntimeTest1.class,_oid);
+copy.setIntField(-1);
+pm2.commit();
+pm2.close();
+
+RuntimeTest1 npc = new RuntimeTest1();
+pm.persist(npc);
+Object noid = pm.getObjectId(npc);
+
+try {
+   pm.validateChanges();
+   fail("Didn't find inconsistency.");
+} catch (Exception jove) {
+   //FIXME jthomas
+   /*
+   Throwable[] t = jove.getNestedExceptions();
+   assertEquals(1, t.length);
+   assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
+    */
+    //     }
+/*     assertFalse(pm.getRollbackOnly());
+        
+        try {
+            pm.commit();
+            fail("Committed inconsistent transaction.");
+        } catch (Exception je) {
+        }
+        
+        assertEquals(-1, pc.getIntField());
+        assertFalse(pm.isPersistent(npc));
+        endEm(pm,());
+        
+        pm = getPM();
+        try {
+            pm.find(RuntimeTest1.class,noid);
+            fail("Object should not exist.");
+        } catch (Exception jonfe) {
+        }
+        endEm(pm,());
+    }
+ */
+
+//FIXME 
+/*    
+    public void testInconsistentOptimisticTransactionWithRefresh() {
+        OpenJPAEntityManager pm = getPM();
+        pm.setOptimistic(true);
+        pm.validateChanges();        // no-op outside trans
+        pm.begin();
+        
+        RuntimeTest1 pc = pm.find(RuntimeTest1.class,_oid);
+        pc.setIntField(100);
+        
+        OpenJPAEntityManager pm2 = getPM();
+        pm2.begin();
+        RuntimeTest1 copy = pm2.find(RuntimeTest1.class,_oid);
+        copy.setIntField(-1);
+        pm2.commit();
+        pm2.close();
+        
+        RuntimeTest1 npc = new RuntimeTest1();
+        pm.persist(npc);
+        try {
+            pm.validateChanges();
+            fail("Didn't find inconsistency.");
+        } catch (Exception jove) {
+            //FIXME jthomas
+            /*
+            Throwable[] t = jove.getNestedExceptions();
+            assertEquals(1, t.length);
+            assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
+             */
+    //      }
+    /*       assertFalse(pm.getRollbackOnly());
+         pm.refresh(pc);
+
+         assertEquals(-1, pc.getIntField());
+         assertTrue(pm.isPersistent(npc));
+
+         pc.setIntField(200);
+         npc.setIntField(300);
+         pm.commit();
+
+         assertEquals(200, pc.getIntField());
+         assertTrue(pm.isPersistent(npc));
+         assertEquals(300, npc.getIntField());
+         endEm(pm,());
+     }
+    */
+
+//FIXME 
+
+/*    
+public void testInconsistentOptimisticTransactionWithRollback() {
+OpenJPAEntityManager pm = getPM();
+pm.setRetainState(false);
+pm.setOptimistic(true);
+pm.validateChanges();        // no-op outside trans
+pm.begin();
+
+RuntimeTest1 pc = (RuntimeTest1) pm.find(RuntimeTest1.class,_oid);
+pc.setIntField(100);
+
+OpenJPAEntityManager pm2 = getPM();
+pm2.begin();
+RuntimeTest1 copy = (RuntimeTest1) pm2.find(RuntimeTest1.class,_oid);
+copy.setIntField(-1);
+pm2.commit();
+pm2.close();
+
+RuntimeTest1 npc = new RuntimeTest1();
+pm.persist(npc);
+Object noid = pm.getObjectId(npc);
+
+try {
+pm.validateChanges();
+fail("Didn't find inconsistency.");
+} catch (Exception jove) {
+//FIXME jthomas
+/*
+Throwable[] t = jove.getNestedExceptions();
+assertEquals(1, t.length);
+assertEquals(pc, (((JDOException) t[0]).getFailedObject()));
+*/
+    //      }
+    /*       assertFalse(pm.getRollbackOnly());
+          pm.rollback();
+
+          assertEquals(-1, pc.getIntField());
+          assertFalse(pm.isPersistent(npc));
+          endEm(pm,());
+
+          pm = getPM();
+          try {
+              pm.find(RuntimeTest1.class,_oid);
+              fail("Object should not exist.");
+          } catch (Exception jonfe) {
+          }
+          endEm(pm,());
+      }
+
+    */
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestClassStringConstructor.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,61 @@
+/*
+ * TestClassStringConstructor.java
+ *
+ * Created on October 16, 2006, 2:48 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.AppIdClassString;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+
+public class TestClassStringConstructor extends BaseKernelTest {
+
+    /**
+     * Creates a new instance of TestClassStringConstructor
+     */
+    public TestClassStringConstructor(String name) {
+        super(name);
+    }
+
+    public void setUp() {
+        deleteAll(AppIdClassString.class);
+    }
+
+    public void testConstructor() {
+        OpenJPAEntityManager pm = getPM();
+        startTx(pm);
+        AppIdClassString pc = new AppIdClassString(5);
+        pm.persist(pc);
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        pc = pm.find(AppIdClassString.class, new AppIdClassString.Idkey("5"));
+        assertEquals(5, pc.getPk());
+        endEm(pm);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestConnectionRetainMode.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestConnectionRetainMode.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestConnectionRetainMode.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestConnectionRetainMode.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,118 @@
+/*
+ * TestConnectionRetainMode.java
+ *
+ * Created on October 10, 2006, 1:09 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+
+public class TestConnectionRetainMode extends BaseKernelTest {
+
+    /**
+     * Creates a new instance of TestConnectionRetainMode
+     */
+    public TestConnectionRetainMode() {
+    }
+
+    public TestConnectionRetainMode(String str) {
+        super(str);
+    }
+
+    public void testOnDemand()
+        throws Exception {
+        doTest("on-demand");
+    }
+
+    public void testTransaction()
+        throws Exception {
+        doTest("transaction");
+    }
+
+    public void testPersistenceManager()
+        throws Exception {
+        doTest("persistence-manager");
+    }
+
+    public void doTest(String mode)
+        throws Exception {
+        Map props = new HashMap();
+        props.put("openjpa.ConnectionRetainMode", mode);
+
+        OpenJPAEntityManagerFactory factory = getEmf(props);
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) factory.createEntityManager();
+        Object initialConnection = pm.getConnection();
+
+        try {
+            checkPM(pm, initialConnection, mode);
+
+            pm.setOptimistic(true);
+            startTx(pm);
+
+            Object optimisticConnection = pm.getConnection();
+            checkPM(pm, initialConnection, mode);
+            checkTransaction(pm, optimisticConnection, mode);
+
+            rollbackTx(pm);
+            checkPM(pm, initialConnection, mode);
+
+            pm.setOptimistic(false);
+            startTx(pm);
+
+            Object pessimisticConnection = pm.getConnection();
+            checkPM(pm, initialConnection, mode);
+            checkTransaction(pm, pessimisticConnection, mode);
+
+            rollbackTx(pm);
+            checkPM(pm, initialConnection, mode);
+        } finally {
+
+            rollbackTx(pm);
+            endEm(pm);
+        }
+    }
+
+    private void checkPM(OpenJPAEntityManager pm, Object c, String mode)
+        throws Exception {
+        if ("persistence-manager".equals(mode))
+            assertEquals(c, pm.getConnection());
+    }
+
+    private void checkTransaction(OpenJPAEntityManager pm, Object c,
+        String mode)
+        throws Exception {
+        if (!"on-demand".equals(mode)
+            || !pm.getOptimistic())
+            assertEquals(c, pm.getConnection());
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,134 @@
+/*
+ * TestDateQueries.java
+ *
+ * Created on October 10, 2006, 1:28 PM
+ *
+ * To change this template, choose Tools | Template Manager
+ * and open the template in the editor.
+ */
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.AllFieldTypesTest;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAQuery;
+
+public class TestDateQueries extends BaseKernelTest {
+
+    private OpenJPAEntityManager _pm = null;
+    private Date _date = null;
+    private Date _before = null;
+    private Date _after = null;
+
+    /**
+     * Creates a new instance of TestDateQueries
+     */
+    public TestDateQueries() {
+    }
+
+    public TestDateQueries(String name) {
+        super(name);
+    }
+
+    public void setUp()
+        throws Exception {
+        super.setUp();
+
+        SimpleDateFormat sdf = new SimpleDateFormat("MMMMM dd, yyyy");
+        _date = sdf.parse("April 26, 1978");
+        _before = sdf.parse("April 25, 1978");
+        _after = sdf.parse("April 27, 1978");
+        _pm = getPM();
+
+        // delete all existing instances
+        deleteAll(AllFieldTypesTest.class);
+
+        // create some instances to query on
+        startTx(_pm);
+        AllFieldTypesTest test = new AllFieldTypesTest();
+        test.setTestDate(_date);
+        _pm.persist(test);
+
+        test = new AllFieldTypesTest();
+        test.setTestDate(_before);
+        _pm.persist(test);
+
+        test = new AllFieldTypesTest();
+        test.setTestDate(_after);
+        _pm.persist(test);
+        endTx(_pm);
+    }
+
+    public void testEquals() {
+        Collection vals = executeQuery("testDate = :date");
+        assertEquals(1, vals.size());
+        assertEquals(_date, ((AllFieldTypesTest) vals.iterator().next()).
+            getTestDate());
+    }
+
+    public void testNotEquals() {
+        Collection vals = executeQuery("testDate <> :date");
+        assertEquals(2, vals.size());
+    }
+
+    public void testBefore() {
+        Collection vals = executeQuery("testDate < :date");
+        assertEquals(1, vals.size());
+        assertEquals(_before, ((AllFieldTypesTest) vals.iterator().next()).
+            getTestDate());
+    }
+
+    public void testAfter() {
+        Collection vals = executeQuery("testDate > :date");
+        assertEquals(1, vals.size());
+        assertEquals(_after, ((AllFieldTypesTest) vals.iterator().next()).
+            getTestDate());
+    }
+
+    public void testOrderBy() {
+        String query =
+            "SELECT o FROM AllFieldTypesTest o ORDER BY o.testDate ASC";
+        OpenJPAQuery q = _pm.createQuery(query);
+        List vals = q.getResultList();
+        assertEquals(3, vals.size());
+
+        Iterator i = vals.iterator();
+        assertEquals(_before, ((AllFieldTypesTest) i.next()).getTestDate());
+        assertEquals(_date, ((AllFieldTypesTest) i.next()).getTestDate());
+        assertEquals(_after, ((AllFieldTypesTest) i.next()).getTestDate());
+    }
+
+    private List executeQuery(String filter) {
+        String query = "SELECT o FROM AllFieldTypesTest o WHERE o." + filter;
+        OpenJPAQuery q = _pm.createQuery(query);
+        q.setParameter("date", _date);
+        return q.getResultList();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDependentFields2.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDependentFields2.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDependentFields2.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDependentFields2.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,566 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.DependentFieldsPC;
+import org.apache.openjpa.persistence.Extent;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAPersistence;
+
+public class TestDependentFields2 extends BaseKernelTest {
+
+    private static final int COMMIT = 0;
+    private static final int ROLLBACK = 1;
+    private static final int PRESTORE = 2;
+
+    private Object _root = null;
+    private Object _rel = null;
+    private Object _depRel = null;
+    private Object _deep = null;
+    private Object _coll = null;
+    private Object _depColl = null;
+    private Object _map = null;
+    private Object _depMap = null;
+    private Object _repeat = null;
+
+    public TestDependentFields2(String casename) {
+        super(casename);
+    }
+
+    public void setUp() throws Exception {
+        deleteAll(DependentFieldsPC.class);
+
+        DependentFieldsPC root = new DependentFieldsPC();
+        root.setRelation(new DependentFieldsPC());
+        root.getList().add(new DependentFieldsPC());
+        root.getMap().put("key", new DependentFieldsPC());
+        root.setDependentRelation(new DependentFieldsPC());
+        root.getDependentRelation().setDependentRelation
+            (new DependentFieldsPC());
+        root.getDependentList().add(new DependentFieldsPC());
+        root.getDependentMap().put("key", new DependentFieldsPC());
+
+        DependentFieldsPC repeat = new DependentFieldsPC();
+        root.getDependentList().add(repeat);
+        root.getDependentMap().put("repeat", repeat);
+
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        startTx(pm);
+        pm.persist(root);
+        endTx(pm);
+
+        _root = pm.getObjectId(root);
+        assertNotNull(_root);
+        _rel = pm.getObjectId(root.getRelation());
+        _depRel = pm.getObjectId(root.getDependentRelation());
+        _deep = pm.getObjectId(root.getDependentRelation().
+            getDependentRelation());
+        _coll = pm.getObjectId(root.getList().iterator().next());
+        Iterator itr = root.getDependentList().iterator();
+        _depColl = pm.getObjectId(itr.next());
+        _repeat = pm.getObjectId(itr.next());
+        _map = pm.getObjectId(root.getMap().get("key"));
+        _depMap = pm.getObjectId(root.getDependentMap().get("key"));
+
+        endEm(pm);
+    }
+
+    public void testDependentFieldsLoaded() {
+        delete(true, COMMIT);
+        checkFields();
+    }
+
+    public void testDependentFieldsLoadedWithRollback() {
+        delete(true, ROLLBACK);
+        checkFields();
+    }
+
+    public void testDependentFieldsLoadedWithPreStore() {
+        delete(true, PRESTORE);
+        checkFields();
+    }
+
+    public void testDependentFieldsUnloaded() {
+        delete(false, COMMIT);
+        checkFields();
+    }
+
+    public void testDependentFieldsUnloadedWithRollback() {
+        delete(false, ROLLBACK);
+        checkFields();
+    }
+
+    public void testDependentFieldsUnloadedWithPreStore() {
+        delete(false, PRESTORE);
+        checkFields();
+    }
+
+    private void delete(boolean load, int action) {
+        OpenJPAEntityManager pm = getPM(true, true);
+        DependentFieldsPC root;
+        Object rel = null;
+        Object depRel = null;
+        Object coll = null;
+        Object depColl = null;
+        Object map = null;
+        Object depMap = null;
+        Object repeat = null;
+        Object deep = null;
+        while (true) {
+            startTx(pm);
+            root = (DependentFieldsPC) pm.find(DependentFieldsPC.class, _root);
+            if (load) {
+                rel = root.getRelation();
+                assertNotNull(rel);
+                depRel = root.getDependentRelation();
+                assertNotNull(depRel);
+                deep = ((DependentFieldsPC) depRel).getDependentRelation();
+                assertNotNull(deep);
+                coll = root.getList().iterator().next();
+                assertNotNull(coll);
+                Iterator itr = root.getDependentList().iterator();
+                depColl = itr.next();
+                repeat = itr.next();
+                assertNotNull(depColl);
+                assertNotNull(repeat);
+                map = root.getMap().get("key");
+                assertNotNull(map);
+                depMap = root.getDependentMap().get("key");
+                assertNotNull(depMap);
+
+                // pcl: test both depColl and repeat, since they might
+                // have been out of order above.
+                Object o = root.getDependentMap().get("repeat");
+                if (o != repeat)
+                    fail("dependent map does not contain 'repeat'");
+            }
+            pm.remove(root);
+
+            if (action == ROLLBACK) {
+                rollbackTx(pm);
+                action = COMMIT;
+            } else if (action == COMMIT) {
+                endTx(pm);
+                break;
+            } else {
+                pm.preFlush();
+                break;
+            }
+        }
+
+        if (load) {
+            if (action == PRESTORE) {
+                assertFalse(pm.isRemoved(rel));
+                assertFalse(pm.isRemoved(coll));
+                assertFalse(pm.isRemoved(map));
+                assertTrue(pm.isRemoved(depRel));
+                assertTrue(pm.isRemoved(deep));
+                assertTrue(pm.isRemoved(depColl));
+                assertTrue(pm.isRemoved(depMap));
+                assertTrue(pm.isRemoved(repeat));
+            } else {
+                assertNotNull(OpenJPAPersistence.getEntityManager(rel));
+                assertNotNull(OpenJPAPersistence.getEntityManager(coll));
+                assertNotNull(OpenJPAPersistence.getEntityManager(map));
+                assertNull(OpenJPAPersistence.getEntityManager(depRel));
+                assertNull(OpenJPAPersistence.getEntityManager(deep));
+                assertNull(OpenJPAPersistence.getEntityManager(depColl));
+                assertNull(OpenJPAPersistence.getEntityManager(depMap));
+                assertNull(OpenJPAPersistence.getEntityManager(repeat));
+            }
+        }
+
+        if (action == PRESTORE)
+            endTx(pm);
+        endEm(pm);
+    }
+
+    private void checkFields() {
+        OpenJPAEntityManager pm = getPM(true, true);
+        assertNotNull(pm.find(DependentFieldsPC.class, _rel));
+        assertNotNull(pm.find(DependentFieldsPC.class, _coll));
+        assertNotNull(pm.find(DependentFieldsPC.class, _map));
+        assertNull(pm.find(DependentFieldsPC.class, _depRel));
+        assertNull(pm.find(DependentFieldsPC.class, _deep));
+        assertNull(pm.find(DependentFieldsPC.class, _depColl));
+        assertNull(pm.find(DependentFieldsPC.class, _depMap));
+
+        endEm(pm);
+    }
+
+    public void testNullDeletesDependent() {
+        nullDeletesDependent(COMMIT);
+    }
+
+    public void testNullDeletesDependentWithRollback() {
+        nullDeletesDependent(ROLLBACK);
+    }
+
+    public void testNullDeletesDependentWithPreStore() {
+        nullDeletesDependent(PRESTORE);
+    }
+
+    private void nullDeletesDependent(int action) {
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        DependentFieldsPC pc;
+        DependentFieldsPC depRel;
+        while (true) {
+            startTx(pm);
+            pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class, _root);
+            depRel = pc.getDependentRelation();
+            assertEquals(_depRel, pm.getObjectId(depRel));
+            pc.setDependentRelation(null);
+            if (action == ROLLBACK) {
+                rollbackTx(pm);
+                action = COMMIT;
+            } else if (action == COMMIT) {
+                endTx(pm);
+                break;
+            } else {
+                pm.preFlush();
+                break;
+            }
+        }
+
+        if (action == PRESTORE) {
+            assertTrue(pm.isRemoved(depRel));
+            endTx(pm);
+        }
+
+        assertTrue(!pm.isPersistent(depRel));
+        assertNull(pm.find(DependentFieldsPC.class, _depRel));
+        endEm(pm);
+    }
+
+    public void testRemoveDeletesDependent() {
+        removeDeletesDependent(COMMIT);
+    }
+
+    public void testRemoveDeletesDependentWithRollback() {
+        removeDeletesDependent(ROLLBACK);
+    }
+
+    public void testRemoveDeletesDependentWithPreStore() {
+        removeDeletesDependent(PRESTORE);
+    }
+
+    private void removeDeletesDependent(int action) {
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        DependentFieldsPC pc;
+        DependentFieldsPC depColl;
+        DependentFieldsPC depMap;
+        List list;
+        Map map;
+        while (true) {
+            startTx(pm);
+            pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class, _root);
+            list = pc.getDependentList();
+            assertEquals("list size =! 2", 2, list.size());
+            depColl = (DependentFieldsPC) list.remove(0);
+            assertEquals("_depColl is not pm.getObjectId(depColl)", _depColl,
+                pm.getObjectId(depColl));
+
+            map = pc.getDependentMap();
+            assertEquals("map size =! 2", 2, map.size());
+            depMap = (DependentFieldsPC) map.remove("key");
+            assertEquals("_depMap is not pm.getObjectId(depMap)", _depMap,
+                pm.getObjectId(depMap));
+
+            if (action == ROLLBACK) {
+                rollbackTx(pm);
+                action = COMMIT;
+            } else if (action == COMMIT) {
+                endTx(pm);
+                break;
+            } else {
+                pm.preFlush();
+                break;
+            }
+        }
+
+        if (action == PRESTORE) {
+            assertTrue(pm.isRemoved(depColl));
+            assertTrue(pm.isRemoved(depMap));
+            endTx(pm);
+        }
+
+//        assertTrue("depcoll is persistence", !pm.isPersistent(depColl));
+        assertNull(pm.find(DependentFieldsPC.class, _depColl));
+
+//        assertTrue("depMap is persistence", !pm.isPersistent(depMap));
+        assertNull(pm.find(DependentFieldsPC.class, _depMap));
+
+        assertNotNull("repeat is null",
+            pm.find(DependentFieldsPC.class, _repeat));
+        endEm(pm);
+    }
+
+    public void testMoveDependentInContainer() {
+        moveDependentInContainer(COMMIT);
+    }
+
+    public void testMoveDependentInContainerWithRollback() {
+        moveDependentInContainer(ROLLBACK);
+    }
+
+    public void testMoveDependentInContainerWithPreStore() {
+        moveDependentInContainer(PRESTORE);
+    }
+
+    private void moveDependentInContainer(int action) {
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        DependentFieldsPC pc;
+        DependentFieldsPC depColl;
+        DependentFieldsPC depMap;
+        List list;
+        Map map;
+        while (true) {
+            startTx(pm);
+            pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class, _root);
+            list = pc.getDependentList();
+            assertEquals(2, list.size());
+            depColl = (DependentFieldsPC) list.get(0);
+            assertEquals(_depColl, pm.getObjectId(depColl));
+            list.remove(0);
+            list.add(depColl);
+
+            map = pc.getDependentMap();
+            assertEquals(2, map.size());
+            depMap = (DependentFieldsPC) map.get("key");
+            assertEquals(_depMap, pm.getObjectId(depMap));
+            map.remove("key");
+            map.put("newkey", depMap);
+
+            if (action == ROLLBACK) {
+                rollbackTx(pm);
+                action = COMMIT;
+            } else if (action == COMMIT) {
+                endTx(pm);
+                break;
+            } else {
+                pm.preFlush();
+                break;
+            }
+        }
+
+        if (action == PRESTORE) {
+            assertFalse(pm.isRemoved(depColl));
+            assertFalse(pm.isRemoved(depMap));
+            endTx(pm);
+        }
+
+        assertTrue(pm.isPersistent(depColl));
+        assertNotNull(pm.find(DependentFieldsPC.class, _depColl));
+        assertTrue(pm.isPersistent(depMap));
+        assertNotNull(pm.find(DependentFieldsPC.class, _depMap));
+        assertNotNull(pm.find(DependentFieldsPC.class, _repeat));
+        endEm(pm);
+    }
+
+    public void testRefedDependentNotDeleted() {
+        refedDependentNotDeleted(COMMIT);
+    }
+
+    public void testRefedDependentNotDeletedWithRollback() {
+        refedDependentNotDeleted(ROLLBACK);
+    }
+
+    public void testRefedDependentNotDeletedWithPreStore() {
+        refedDependentNotDeleted(PRESTORE);
+    }
+
+    private void refedDependentNotDeleted(int action) {
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        DependentFieldsPC pc;
+        DependentFieldsPC newPC = null;
+        DependentFieldsPC depRel;
+        while (true) {
+            startTx(pm);
+            pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class, _root);
+            if (newPC == null)
+                newPC = new DependentFieldsPC();
+            depRel = pc.getDependentRelation();
+            newPC.setDependentRelation(depRel);
+            pc.setDependentRelation(null);
+            pm.persist(newPC);
+
+            if (action == ROLLBACK) {
+                rollbackTx(pm);
+                action = COMMIT;
+            } else if (action == COMMIT) {
+                endTx(pm);
+                break;
+            } else {
+                pm.preFlush();
+                break;
+            }
+        }
+
+        if (action == PRESTORE) {
+            assertFalse(pm.isRemoved(depRel));
+            endTx(pm);
+        }
+
+        assertTrue(pm.isPersistent(depRel));
+        assertNotNull(pm.find(DependentFieldsPC.class, _depRel));
+        endEm(pm);
+    }
+
+    public void testNullSharedDependent() {
+        nullSharedDependent(COMMIT);
+    }
+
+    public void testNullSharedDependentWithRollback() {
+        nullSharedDependent(ROLLBACK);
+    }
+
+    public void testNullSharedDependentWithPreStore() {
+        nullSharedDependent(PRESTORE);
+    }
+
+    private void nullSharedDependent(int action) {
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        DependentFieldsPC pc;
+        DependentFieldsPC repeat;
+        List list;
+        Map map;
+        while (true) {
+            startTx(pm);
+            pc = (DependentFieldsPC) pm.find(DependentFieldsPC.class, _root);
+            list = pc.getDependentList();
+            assertEquals(2, list.size());
+            repeat = (DependentFieldsPC) list.get(1);
+            assertEquals(_repeat, pm.getObjectId(repeat));
+            list.remove(1);
+
+            map = pc.getDependentMap();
+            assertEquals(2, map.size());
+            assertEquals(repeat, (DependentFieldsPC) map.remove("repeat"));
+
+            if (action == PRESTORE)
+                pm.preFlush();
+            else
+                pm.flush();
+            assertTrue(pm.isRemoved(repeat));
+
+            // now after deleting on flush, assigning to another field and
+            // attempting to commit should throw an error -- can't undelete an
+            // object
+            pc.getList().add(repeat);
+
+            if (action == ROLLBACK) {
+                rollbackTx(pm);
+                action = COMMIT;
+            } else {
+                try {
+                    endTx(pm);
+                    fail("Committed with ref to deleted dependent object");
+                } catch (Exception je) {
+                } finally {
+                    rollbackTx(pm);
+                }
+                break;
+            }
+        }
+
+        endEm(pm);
+    }
+
+    public void testClearMappedDependentOfDetached() {
+        clearDependentOfDetachedTest(true);
+    }
+
+    public void testClearInverseKeyDependentOfDetached() {
+        clearDependentOfDetachedTest(false);
+    }
+
+    private void clearDependentOfDetachedTest(boolean mapped) {
+        deleteAll(DependentFieldsPC.class);
+
+        DependentFieldsPC owner = new DependentFieldsPC();
+        for (int i = 0; i < 2; i++) {
+            DependentFieldsPC child = new DependentFieldsPC();
+            if (mapped) {
+                owner.getDependentMappedList().add(child);
+                child.setOwner(owner);
+            } else
+                owner.getDependentInverseKeyList().add(child);
+        }
+
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) currentEntityManager();
+        startTx(pm);
+        pm.persist(owner);
+        endTx(pm);
+        Object oid = pm.getObjectId(owner);
+        assertEquals(3,
+            ((Extent) pm.createExtent(DependentFieldsPC.class, true))
+                .list().size());
+        endEm(pm);
+
+        pm = (OpenJPAEntityManager) currentEntityManager();
+        owner = (DependentFieldsPC) pm.find(DependentFieldsPC.class, oid);
+        if (mapped)
+            assertEquals(2, owner.getDependentMappedList().size());
+        else
+            assertEquals(2, owner.getDependentInverseKeyList().size());
+        DependentFieldsPC detached = (DependentFieldsPC) pm.detach(owner);
+        endEm(pm);
+
+        if (mapped) {
+            assertEquals(2, detached.getDependentMappedList().size());
+            detached.getDependentMappedList().clear();
+        } else {
+            assertEquals(2, detached.getDependentInverseKeyList().size());
+            detached.getDependentInverseKeyList().clear();
+        }
+
+        pm = (OpenJPAEntityManager) currentEntityManager();
+        startTx(pm);
+        owner = (DependentFieldsPC) pm.merge(detached);
+        if (mapped)
+            assertEquals(0, owner.getDependentMappedList().size());
+        else
+            assertEquals(0, owner.getDependentInverseKeyList().size());
+        endTx(pm);
+        assertEquals(1,
+            ((Extent) pm.createExtent(DependentFieldsPC.class, true)).
+                list().size());
+        endEm(pm);
+
+        pm = (OpenJPAEntityManager) currentEntityManager();
+        owner = (DependentFieldsPC) pm.find(DependentFieldsPC.class, oid);
+        if (mapped)
+            assertEquals(0, owner.getDependentMappedList().size());
+        else
+            assertEquals(0, owner.getDependentInverseKeyList().size());
+        endEm(pm);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDetachedStateManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDetachedStateManager.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDetachedStateManager.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDetachedStateManager.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,542 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.TreeMap;
+
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.AttachA;
+import org.apache.openjpa.persistence.kernel.common.apps.AttachB;
+import org.apache.openjpa.persistence.kernel.common.apps.AttachD;
+import org.apache.openjpa.persistence.kernel.common.apps.AttachE;
+import org.apache.openjpa.persistence.kernel.common.apps.DetachSMPC;
+
+import org.apache.openjpa.conf.OpenJPAConfiguration;
+import org.apache.openjpa.conf.OpenJPAConfigurationImpl;
+import org.apache.openjpa.enhance.PCEnhancer;
+import org.apache.openjpa.enhance.PersistenceCapable;
+import org.apache.openjpa.kernel.DetachedStateManager;
+import org.apache.openjpa.lib.util.Options;
+import org.apache.openjpa.persistence.DetachStateType;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactory;
+
+public class TestDetachedStateManager extends BaseKernelTest {
+
+    private static boolean enhanced = false;
+
+    private int oid;
+    private int doid;
+
+    /**
+     * Creates a new instance of TestDetachedStateManager
+     */
+    public TestDetachedStateManager(String name) {
+        super(name);
+    }
+
+    private void deleteAll() {
+        deleteAll(AttachA.class);
+        deleteAll(AttachD.class);
+    }
+
+    public OpenJPAEntityManager getPM() {
+        OpenJPAEntityManager pm = super.getPM();
+        //FIXME jthomas
+        //pm.currentTransaction().setRestoreValues(false);
+        return pm;
+    }
+
+    public void setUp() throws Exception {
+        super.setUp();
+
+        deleteAll();
+
+        OpenJPAEntityManager pm = getPM();
+        startTx(pm);
+        AttachB b = new AttachB();
+        pm.persist(b);
+        b.setAint(5);
+        b.setBstr("5");
+        b.getStringIntMap().put("5", new Integer(5));
+
+        AttachE e = new AttachE();
+        e.setEstr("E");
+        e.setEint(5);
+
+        AttachD d = new AttachD();
+        d.setDint(5);
+        d.setEmbeddedE(e);
+        b.getDs().add(d);
+
+        pm.persist(d);
+
+        oid = b.getId();
+        doid = d.getId();
+        endTx(pm);
+        endEm(pm);
+    }
+
+    public void testDetach() {
+        OpenJPAEntityManager pm = getPM();
+        AttachB b = pm.find(AttachB.class, oid);
+
+        assertNotNull("b is null in testDetach", b);
+
+        b = (AttachB) pm.detach(b);
+        endEm(pm);
+
+        assertTrue(pm.isDetached(b));
+        assertEquals(5, b.getAint());
+        assertEquals("5", b.getBstr());
+        assertNull(b.getStringIntMap());
+
+        b.setAint(12);
+        b.setBstr("12");
+        TreeMap map = new TreeMap();
+        map.put("12", new Integer(12));
+        b.setStringIntMap(map);
+
+        pm = getPM();
+        startTx(pm);
+        AttachB attached = (AttachB) pm.merge(b);
+        assertEquals(12, attached.getAint());
+        assertEquals("12", attached.getBstr());
+        assertNull(attached.getStringIntMap().get("12"));
+        assertEquals(new Integer(5), attached.getStringIntMap().get("5"));
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        b = pm.find(AttachB.class, oid);
+        assertEquals(12, b.getAint());
+        assertEquals("12", b.getBstr());
+        assertNull(b.getStringIntMap().get("12"));
+        assertEquals(new Integer(5), b.getStringIntMap().get("5"));
+        endEm(pm);
+    }
+
+    public void testDetachWithGroups() {
+        OpenJPAEntityManager pm = getPM();
+        //FIXME jthomas
+//        pm.getFetchPlan().setDetachmentOptions(FetchPlanImpl.DETACH_LOAD_FIELDS | FetchPlanImpl.DETACH_UNLOAD_FIELDS);
+        pm.setDetachState(DetachStateType.FETCH_GROUPS);
+        pm.getFetchPlan().addFetchGroup("all");
+        AttachB b = pm.find(AttachB.class, oid);
+
+        assertNotNull("b is null in testDetachWithGroups", b);
+
+        b = (AttachB) pm.detach(b);
+        endEm(pm);
+
+        assertTrue(pm.isDetached(b));
+        assertEquals("b.getAint() not 5", 5, b.getAint());
+        assertEquals("b.getAint() not 5str", "5", b.getBstr());
+        assertEquals("b.getStringIntMap().size() not equal to 1", 1,
+            b.getStringIntMap().size());
+
+        b.setAint(12);
+        b.setBstr("12");
+        b.getStringIntMap().put("12", new Integer(12));
+
+        pm = getPM();
+        startTx(pm);
+        AttachB attached = (AttachB) pm.merge(b);
+        assertEquals("not 12", 12, attached.getAint());
+        assertEquals("not 12str", "12", attached.getBstr());
+        assertEquals("not newInteger(12)", new Integer(12),
+            attached.getStringIntMap().get("12"));
+        assertEquals("not newInteger(5)", new Integer(5),
+            attached.getStringIntMap().get("5"));
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        b = (AttachB) pm.find(AttachB.class, oid);
+        assertEquals("not equal 12", 12, b.getAint());
+        assertEquals("not equal 12str", "12", b.getBstr());
+        assertEquals("not equal newinteger(12)", new Integer(12),
+            b.getStringIntMap().get("12"));
+        assertEquals("not equal newInteger(5)", new Integer(5),
+            b.getStringIntMap().get("5"));
+        endEm(pm);
+    }
+
+    public void testDetachNoOverwrite() {
+        OpenJPAEntityManager pm = getPM();
+        AttachB b = (AttachB) pm.find(AttachB.class, oid);
+        b = (AttachB) pm.detach(b);
+        endEm(pm);
+
+        b.setBstr("12");
+
+        pm = getPM();
+        startTx(pm);
+        AttachB orig = pm.find(AttachB.class, oid);
+        orig.setAint(50);
+
+        AttachB attached = (AttachB) pm.merge(b);
+        assertEquals(attached, orig);
+        assertEquals(50, attached.getAint());
+        assertEquals("12", attached.getBstr());
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        b = (AttachB) pm.find(AttachB.class, oid);
+        assertEquals(50, b.getAint());
+        assertEquals("12", b.getBstr());
+        endEm(pm);
+    }
+
+    public void testOptimisticLock() {
+        OpenJPAEntityManager pm = getPM();
+        AttachB b = (AttachB) pm.find(AttachB.class, oid);
+
+        assertNotNull("b is null in testOptimisticLock", b);
+
+        b = (AttachB) pm.detach(b);
+        endEm(pm);
+
+        b.setAint(12);
+        b.setBstr("12");
+        TreeMap map = new TreeMap();
+        map.put("12", new Integer(12));
+        b.setStringIntMap(map);
+
+        pm = getPM();
+        startTx(pm);
+        AttachB b2 = (AttachB) pm.find(AttachB.class, oid);
+        b2.setAint(15);
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        startTx(pm);
+        try {
+            pm.merge(b);
+            endTx(pm);
+            fail("OL expected.");
+        } catch (Exception jove) {
+            rollbackTx(pm);
+        }
+        endEm(pm);
+    }
+
+    public void testEmbedded() {
+        OpenJPAEntityManager pm = getPM();
+        AttachD d = pm.find(AttachD.class, doid);
+
+        assertNotNull("d is null in testEmbedded", d);
+
+        d.getEmbeddedE().getEstr();
+        d = (AttachD) pm.detach(d);
+        endEm(pm);
+
+        d.getEmbeddedE().setEstr("E12");
+        pm = getPM();
+        startTx(pm);
+        AttachD d2 = (AttachD) pm.merge(d);
+        assertNotEquals(d.getEmbeddedE(), d2.getEmbeddedE());
+        assertEquals("E12", d2.getEmbeddedE().getEstr());
+        assertEquals(5, d2.getEmbeddedE().getEint());
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        d2 = (AttachD) pm.find(AttachD.class, doid);
+
+        assertNotNull("d2 is null in testEmbedded", d2);
+
+        assertEquals("E12", d2.getEmbeddedE().getEstr());
+        assertEquals(5, d2.getEmbeddedE().getEint());
+        endEm(pm);
+    }
+
+    public void testNullEmbedded() {
+        OpenJPAEntityManager pm = getPM();
+        AttachD d = (AttachD) pm.find(AttachD.class, doid);
+
+        assertNotNull("d is null in testNullEmbedded", d);
+        d.getEmbeddedE().getEstr();
+        d = (AttachD) pm.detach(d);
+        endEm(pm);
+
+        d.setEmbeddedE(null);
+        pm = getPM();
+        startTx(pm);
+        AttachD d2 = (AttachD) pm.merge(d);
+        assertNull(d2.getEmbeddedE());
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        d2 = (AttachD) pm.find(AttachD.class, doid);
+
+        assertNotNull("d2 is null in testNullEmbedded", d2);
+        // no null ind
+        if (d2.getEmbeddedE() != null) {
+            assertNull(d2.getEmbeddedE().getEstr());
+            assertEquals(0, d2.getEmbeddedE().getEint());
+        }
+        endEm(pm);
+    }
+
+    public void testNullEmbeddedRelated() {
+        OpenJPAEntityManager pm = getPM();
+        AttachD d = (AttachD) pm.find(AttachD.class, doid);
+
+        assertNotNull("d is null in testNullEmbeddedRelated", d);
+
+        d.getEmbeddedE().getEstr();
+        d = (AttachD) pm.detach(d);
+        endEm(pm);
+
+        d.getEmbeddedE().setEstr(null);
+        pm = getPM();
+        startTx(pm);
+        AttachD d2 = (AttachD) pm.merge(d);
+        assertNull("d2.getEmbeddedE().getEstr() is not null",
+            d2.getEmbeddedE().getEstr());
+        assertEquals("d2.getEmbeddedE().getEint() is not equal to 5", 5,
+            d2.getEmbeddedE().getEint());
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        d2 = (AttachD) pm.find(AttachD.class, doid);
+        assertNull("d2.getEmbeddedE().getEstr() is not null",
+            d2.getEmbeddedE().getEstr());
+        assertEquals("d2.getEmbeddedE().getEint() is not 5", 5,
+            d2.getEmbeddedE().getEint());
+        endEm(pm);
+    }
+
+    public void testNullCollection() {
+        OpenJPAEntityManager pm = getPM();
+        AttachB b = (AttachB) pm.find(AttachB.class, oid);
+        b.getDs();
+        b = (AttachB) pm.detach(b);
+        endEm(pm);
+
+        assertEquals(1, b.getDs().size());
+        b.setDs(null);
+
+        pm = getPM();
+        startTx(pm);
+        b = (AttachB) pm.merge(b);
+        assertNull(b.getDs());
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        b = (AttachB) pm.find(AttachB.class, oid);
+        assertTrue(b.getDs() == null || b.getDs().size() == 0);
+        endEm(pm);
+    }
+
+    public void testCollectionAdd() {
+        doCollectionTest(false);
+    }
+
+    public void testCollectionChanges() {
+        doCollectionTest(true);
+    }
+
+    private void doCollectionTest(boolean remove) {
+        OpenJPAEntityManager pm = getPM();
+        AttachB b = (AttachB) pm.find(AttachB.class, oid);
+
+        assertNotNull("b is null in doCollectionTest", b);
+        b.getDs();
+        b = (AttachB) pm.detach(b);
+        endEm(pm);
+
+        assertEquals("b is null in doCollectionTest", 1, b.getDs().size());
+        if (remove) {
+            for (Iterator it = b.getDs().iterator(); it.hasNext();) {
+                it.next();
+                it.remove();
+            }
+        }
+        AttachD d = new AttachD();
+        d.setDint(12);
+        b.getDs().add(d);
+
+        pm = getPM();
+        startTx(pm);
+        b = (AttachB) pm.merge(b);
+        assertSize(remove ? 1 : 2, b.getDs());
+        endTx(pm);
+        endEm(pm);
+
+        pm = getPM();
+        b = (AttachB) pm.find(AttachB.class, oid);
+        assertSize(remove ? 1 : 2, b.getDs());
+        boolean found1 = false;
+        boolean found2 = false;
+        for (Iterator it = b.getDs().iterator(); it.hasNext();) {
+            d = (AttachD) it.next();
+            switch (d.getDint()) {
+                case 5:
+                    if (found1)
+                        fail("Refound.");
+                    found1 = true;
+                    break;
+                case 12:
+                    if (found2)
+                        fail("Refound.");
+                    found2 = true;
+                    break;
+                default:
+                    fail("Unknown d:" + d.getDint());
+            }
+        }
+
+        if (remove)
+            assertFalse(found1);
+
+        endEm(pm);
+    }
+
+    /*
+   //###
+   // No time to get these working right now.  Have to figure out how to
+   // enhance certain classes with different DetachState settings in autobuild.
+   public void testSerialization ()
+       throws Exception
+   {
+       doSerializationTest (false);
+   }
+
+
+   public void testSerializationAuto ()
+       throws Exception
+   {
+       doSerializationTest (true);
+   }
+    */
+
+    private void doSerializationTest(boolean auto) throws Exception {
+        enhance();
+        Map props = new HashMap();
+        props.put("openjpa.DetachState", "DetachedStateField=true");
+
+        OpenJPAEntityManagerFactory factory =
+            (OpenJPAEntityManagerFactory) getEmf(props);
+        OpenJPAEntityManager pm =
+            (OpenJPAEntityManager) factory.createEntityManager();
+
+        startTx(pm);
+        DetachSMPC pc = new DetachSMPC();
+        pc.setIntField(1);
+        DetachSMPC rel = new DetachSMPC();
+        rel.setIntField(2);
+        pc.getRelSet().add(rel);
+        pc.getStringIntMap().put("a", new Integer(99));
+        pm.persist(pc);
+        endTx(pm);
+        Object pcoid = pm.getObjectId(pc);
+        endEm(pm);
+
+        pm = (OpenJPAEntityManager) factory.createEntityManager();
+        pc = (DetachSMPC) pm.find(DetachSMPC.class, pcoid);
+        pc.getRelSet();
+        pc.getStringIntMap();
+        if (!auto) {
+            pc = (DetachSMPC) pm.detach(pc);
+            assertDetachedSM(pc);
+        }
+        pc = (DetachSMPC) roundtrip(pc, false);
+        assertDetachedSM(pc);
+        endEm(pm);
+
+        assertDetachedSM(pc);
+        assertSize(1, pc.getRelSet());
+        assertEquals(1, pc.getStringIntMap().size());
+
+        pc.setIntField(3);
+        ((DetachSMPC) pc.getRelSet().iterator().next()).setIntField(4);
+        pc.getStringIntMap().put("b", new Integer(100));
+
+        pc = (DetachSMPC) roundtrip(pc, false);
+
+        assertDetachedSM(pc);
+        assertEquals(3, pc.getIntField());
+        assertSize(1, pc.getRelSet());
+        //assertDetachedSM (b.getDs ().iterator ().next ());
+        assertEquals(4, ((DetachSMPC) pc.getRelSet().iterator().next())
+            .getIntField());
+        assertEquals(new Integer(100), pc.getStringIntMap().get("b"));
+
+        pm = (OpenJPAEntityManager) factory.createEntityManager();
+        startTx(pm);
+        pc = (DetachSMPC) pm.merge(pc);
+        assertEquals(3, pc.getIntField());
+        assertSize(1, pc.getRelSet());
+        assertEquals(4, ((DetachSMPC) pc.getRelSet().iterator().next())
+            .getIntField());
+        assertEquals(2, pc.getStringIntMap().size());
+        assertEquals(new Integer(100), pc.getStringIntMap().get("b"));
+        endTx(pm);
+        endEm(pm);
+
+        pm = (OpenJPAEntityManager) factory.createEntityManager();
+        pc = (DetachSMPC) pm.find(DetachSMPC.class, pcoid);
+        assertEquals(3, pc.getIntField());
+        assertSize(1, pc.getRelSet());
+        assertEquals(4, ((DetachSMPC) pc.getRelSet().iterator().next())
+            .getIntField());
+        assertEquals(2, pc.getStringIntMap().size());
+        assertEquals(new Integer(100), pc.getStringIntMap().get("b"));
+
+        startTx(pm);
+        deleteAll(DetachSMPC.class, pm);
+        endTx(pm);
+        endEm(pm);
+        factory.close();
+    }
+
+    private void enhance() throws Exception {
+        Properties props = getProperties(new String[]{
+            "openjpa.DetachState", "DetachedStateField=true",
+        });
+        OpenJPAConfiguration conf = new OpenJPAConfigurationImpl(true, false);
+        conf.fromProperties(props);
+
+        Options opts = new Options();
+        opts.put("jdo", "true");
+        PCEnhancer.run(conf, new String[]{
+            "org.apache.openjpa.persistence.kernel.noenhance.DetachSMPC" }, opts);
+    }
+
+    private void assertDetachedSM(Object obj) {
+        OpenJPAEntityManager pm = getPM();
+        assertTrue(pm.isDetached(obj));
+        PersistenceCapable pc = (PersistenceCapable) obj;
+        assertEquals(DetachedStateManager.class,
+            pc.pcGetStateManager().getClass());
+        endEm(pm);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEmbedded.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEmbedded.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEmbedded.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEmbedded.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,246 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import javax.persistence.EntityManager;
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.ComplexEmbeddedPC;
+import org.apache.openjpa.persistence.kernel.common.apps.EmbeddedOwnerPC;
+import org.apache.openjpa.persistence.kernel.common.apps.EmbeddedPC;
+
+public class TestEJBEmbedded extends BaseKernelTest {
+
+    private Object _oid1 = null;
+    private Object _oid2 = null;
+
+    EmbeddedOwnerPC.EmbKey id1;
+    EmbeddedOwnerPC.EmbKey id2;
+
+    public TestEJBEmbedded(String name) {
+        super(name);
+    }
+
+    @SuppressWarnings("unchecked")
+    public void setUp() {
+        deleteAll(EmbeddedOwnerPC.class);
+        deleteAll(EmbeddedPC.class);
+
+        EntityManager em = currentEntityManager();
+        startTx(em);
+
+        EmbeddedOwnerPC pc1 = new EmbeddedOwnerPC(1, 2);
+        pc1.setStringField("string1");
+        EmbeddedPC embed1 = new EmbeddedPC();
+        embed1.setStringField("embedString1");
+        embed1.setIntField(1);
+        pc1.setEmbedded(embed1);
+
+        EmbeddedPC embed2 = new EmbeddedPC();
+        embed2.setStringField("embedString2");
+        embed2.setIntField(2);
+
+        EmbeddedOwnerPC pc2 = new EmbeddedOwnerPC(3, 4);
+
+        em.persist(pc1);
+        em.persist(pc2);
+
+        endTx(em);
+
+        id1 = new EmbeddedOwnerPC.EmbKey("1:2");
+
+        id2 = new EmbeddedOwnerPC.EmbKey();
+        id2.id1 = 3;
+        id2.id2 = 4;
+
+        endEm(em);
+    }
+
+    public void testInsert() {
+        EntityManager pm = currentEntityManager();
+        startTx(pm);
+
+        EmbeddedOwnerPC pc = pm.find(EmbeddedOwnerPC.class, id1);
+        EmbeddedOwnerPC pc2 = pm.find(EmbeddedOwnerPC.class, id2);
+        assertNotNull("pc is null in testInsert", pc);
+        assertNotNull("p2 is null in testInsert", pc2);
+
+        assertEquals("string1", pc.getStringField());
+        assertNotNull(pc.getEmbedded());
+        assertEquals("embedString1", pc.getEmbedded().getStringField());
+        assertEquals(1, pc.getEmbedded().getIntField());
+
+        endTx(pm);
+        endEm(pm);
+    }
+
+    public void testDelete() {
+        EntityManager pm = currentEntityManager();
+        startTx(pm);
+        EmbeddedOwnerPC pc =
+            (EmbeddedOwnerPC) pm.find(EmbeddedOwnerPC.class, id1);
+
+        assertNotNull("pc is null in testDelete", pc);
+
+        EmbeddedPC embed = pc.getEmbedded();
+
+        pc.setEmbedded(null);
+
+        endTx(pm);
+
+        endEm(pm);
+
+        pm = currentEntityManager();
+        startTx(pm);
+        pc = (EmbeddedOwnerPC) pm.find(EmbeddedOwnerPC.class, id1);
+
+        assertEquals("the expt strng is not string1", "string1",
+            pc.getStringField());
+        assertNull("pc's embedded is null", pc.getEmbedded());
+        endTx(pm);
+        endEm(pm);
+    }
+
+    public void testUpdateRetain() {
+        EntityManager pm = currentEntityManager();
+        startTx(pm);
+        EmbeddedOwnerPC pc =
+            (EmbeddedOwnerPC) pm.find(EmbeddedOwnerPC.class, id1);
+
+        assertNotNull("pc is null testUpdateRetain", pc);
+
+        pc.setStringField("string2");
+        pc.getEmbedded().setStringField("embedString2");
+
+        endTx(pm);
+
+        assertEquals("string2", pc.getStringField());
+        assertNotNull(pc.getEmbedded());
+        assertEquals("embedString2", pc.getEmbedded().getStringField());
+        assertEquals(1, pc.getEmbedded().getIntField());
+
+        endEm(pm);
+    }
+
+    public void testReplace() {
+        EntityManager pm = currentEntityManager();
+        startTx(pm);
+
+        EmbeddedOwnerPC pc = pm.find(EmbeddedOwnerPC.class, id1);
+
+        EmbeddedPC newEmbed = new EmbeddedPC();
+        newEmbed.setStringField("embedString2");
+        pc.setEmbedded(newEmbed);
+
+        ComplexEmbeddedPC newComplexEmbed = new ComplexEmbeddedPC();
+        newEmbed = new EmbeddedPC();
+        newEmbed.setStringField("embedString3");
+        newComplexEmbed.setStringField("complexEmbedString3");
+        newComplexEmbed.setOwnerField(new EmbeddedOwnerPC(5, 6));
+        pc.setComplexEmbedded(newComplexEmbed);
+
+        endTx(pm);
+        endEm(pm);
+
+        pm = currentEntityManager();
+        startTx(pm);
+
+        pc = pm.find(EmbeddedOwnerPC.class, id1);
+        assertEquals("string1", pc.getStringField());
+        assertNotNull(pc.getEmbedded());
+        assertEquals("the exp strng is not embedString1", "embedString1",
+            pc.getEmbedded().getStringField());
+        assertEquals("intfield is not 1", 1, pc.getEmbedded().getIntField());
+
+        endTx(pm);
+        endEm(pm);
+    }
+
+    public void testShare() {
+        EntityManager pm = currentEntityManager();
+        startTx(pm);
+
+        EmbeddedOwnerPC pc1 = pm.find(EmbeddedOwnerPC.class, id1);
+        assertNotNull("pc1 is null in testshare", pc1);
+
+        EmbeddedOwnerPC pc2 = pm.find(EmbeddedOwnerPC.class, id2);
+        assertNotNull("pc2 is null in testshare", pc2);
+
+        EmbeddedPC embed1 = pc1.getEmbedded();
+
+        pm.persist(embed1);
+        Integer oid = new Integer(1);
+        endTx(pm);
+
+        assertEquals("embedString1", embed1.getStringField());
+        assertEquals("embedString1", pc1.getEmbedded().getStringField());
+
+        endEm(pm);
+
+        // make sure the changes stick
+        pm = currentEntityManager();
+        startTx(pm);
+        pc1 = pm.find(EmbeddedOwnerPC.class, id1);
+        assertNotNull("pc1 is null in testshare 2nd find", pc1);
+        pc2 = pm.find(EmbeddedOwnerPC.class, id2);
+        assertNotNull("pc2 is null in testshare 2nd find", pc2);
+        embed1 = pm.find(EmbeddedPC.class, oid.intValue());
+
+        if (embed1 != null) {
+            assertEquals("embedString1", embed1.getStringField());
+            assertEquals("embedString1", pc1.getEmbedded().getStringField());
+        }
+
+        endTx(pm);
+        endEm(pm);
+    }
+
+    public void testOptimisticLocking2() {
+        EntityManager pm1 = getPM(true, false);
+        startTx(pm1);
+
+        EmbeddedOwnerPC pc1 = pm1.find(EmbeddedOwnerPC.class, id1);
+        assertNotNull("pc1 is null in testoptlock2", pc1);
+
+        EntityManager pm2 = currentEntityManager();
+
+        startTx(pm2);
+        EmbeddedOwnerPC pc2 = pm1.find(EmbeddedOwnerPC.class, id1);
+
+        assertNotNull("pc2 is null in testoptlock2", pc2);
+
+        EmbeddedPC embed2 = pc2.getEmbedded();
+        embed2.setStringField("xxxx");
+
+        endTx(pm2);
+        endEm(pm2);
+
+        EmbeddedPC embed1 = pc1.getEmbedded();
+        embed1.setStringField("yyyy");
+        try {
+            endTx(pm1);
+            fail("Should have thrown an OL exception.");
+        }
+        catch (Exception ove) {
+        }
+        finally {
+            endEm(pm1);
+        }
+    }
+}
\ No newline at end of file

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEntityManager.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEntityManager.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEntityManager.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBEntityManager.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.persistence.EntityManager;
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest2;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestEJBEntityManager extends AbstractTestCase {
+
+    private Object _id = null;
+
+    private Object _id2 = null;
+
+    public TestEJBEntityManager(String name) {
+        super(name, "kernelcactusapp");
+    }
+
+    public void setUp() throws Exception {
+        deleteAll(RuntimeTest1.class);
+
+        EntityManager em = currentEntityManager();
+        startTx(em);
+
+        RuntimeTest1 a = new RuntimeTest1("STRING", 10);
+        RuntimeTest2 b = new RuntimeTest2("STRING2", 11);
+        em.persist(a);
+        em.persist(b);
+        _id = a.getIntField();
+        _id2 = b.getIntField();
+
+        endTx(em);
+        endEm(em);
+    }
+
+    /**
+     * Tests that the PM throws Exceptions on usage attempts after it has been
+     * closed.
+     */
+    /* Fix Me - aokeke - takes a lot of time to run */
+    // public void testClosed ()
+    // {
+    // EntityManager em = currentEntityManager();
+    // startTx(em);
+    // endEm(em);
+    //
+    // try
+    // {
+    // // this is the only method that should succeed
+    // if(em.isOpen ())
+    // fail("Supposed to be closed...but cannot be closed inside
+    // container..closed at the end of funct");
+    // }
+    // catch (RuntimeException re)
+    // {
+    // fail ("isClosed");
+    // }
+    // try
+    // {
+    // em.find(RuntimeTest1.class, _id);
+    // fail ("find");
+    // }
+    // catch (RuntimeException re)
+    // {
+    // }
+    // }
+    public void testMultipleCloseThreaded() throws Throwable {
+        final EntityManager em = currentEntityManager();
+        final List result = new ArrayList();
+        // EntityTransaction t = em.getTransaction();
+        // t.begin ();
+
+        em.close();
+
+        new Thread() {
+            @SuppressWarnings("unchecked")
+            public void run() {
+                try {
+                    em.close();
+                    result.add(new Integer(0));
+                }
+                catch (Exception jdoe) {
+                    result.add(jdoe);
+                }
+                catch (Throwable t) {
+                    result.add(t);
+                }
+            }
+        }.start();
+
+        while (result.size() == 0)
+            Thread.currentThread().yield(); // wait for results
+        Object ret = result.get(0);
+
+        if (ret instanceof Exception)
+            return; // good
+
+        if (ret instanceof Throwable)
+            throw (Throwable) ret;
+    }
+
+    /**
+     * This method tries to perform operations that should lead to illegal
+     * states, such as persisting instances outside of transactions, etc.
+     */
+    public void testIllegalState() {
+        EntityManager em = currentEntityManager();
+
+        RuntimeTest1 a = new RuntimeTest1("foo", 10);
+        RuntimeTest1 a2 = (RuntimeTest1) em.find(RuntimeTest1.class, _id);
+
+        try {
+            em.persist(a);
+            fail("persist...");
+        }
+        catch (Exception ise) {
+        }
+
+        endEm(em);
+    }
+}
\ No newline at end of file

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBGetObjectByIdValue.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBGetObjectByIdValue.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBGetObjectByIdValue.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBGetObjectByIdValue.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import javax.persistence.EntityManager;
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.AImplB;
+import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestEJBGetObjectByIdValue extends AbstractTestCase {
+
+    private Object[] oids;
+
+    public TestEJBGetObjectByIdValue(String name) {
+        super(name, "kernelcactusapp");
+    }
+
+    public void setUp() throws Exception {
+        deleteAll(RuntimeTest1.class);
+        deleteAll(AImplB.class);
+
+        EntityManager em = currentEntityManager();
+        startTx(em);
+        em.flush();
+        endTx(em);
+        endEm(em);
+    }
+
+    public void testDatastore() {
+        EntityManager em = currentEntityManager();
+        startTx(em);
+
+        RuntimeTest1 pc = new RuntimeTest1(1);
+        pc.setStringField("foo");
+        em.persist(pc);
+
+        endTx(em);
+        endEm(em);
+
+        em = currentEntityManager();
+        pc = (RuntimeTest1) em.find(RuntimeTest1.class, 1);
+        assertEquals("foo", pc.getStringField());
+        em.close();
+
+        em = currentEntityManager();
+        pc = (RuntimeTest1) em.find(RuntimeTest1.class, pc.getIntField());
+        assertEquals("foo", pc.getStringField());
+        endEm(em);
+    }
+
+    public void testAppId() {
+        EntityManager em = currentEntityManager();
+        startTx(em);
+        AImplB pc = new AImplB("foo", 1, "pk");
+        em.persist(pc);
+        AImplB.Idkey oid = new AImplB.Idkey();
+        oid.pk1 = 1;
+        oid.pk2 = "pk";
+        endTx(em);
+        endEm(em);
+
+        em = currentEntityManager();
+        pc = (AImplB) em.find(AImplB.class, oid);
+        assertEquals("foo", pc.getName());
+        endEm(em);
+
+        em = currentEntityManager();
+        pc = (AImplB) em.find(AImplB.class, oid.toString());
+        assertEquals("foo", pc.getName());
+        endEm(em);
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBIncrementalFlushes.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBIncrementalFlushes.java?rev=627979&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBIncrementalFlushes.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestEJBIncrementalFlushes.java Fri Feb 15 01:19:55 2008
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.openjpa.persistence.kernel;
+
+import javax.persistence.EntityManager;
+
+
+import org.apache.openjpa.persistence.kernel.common.apps.InstanceCallbacksTest;
+import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
+import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
+
+public class TestEJBIncrementalFlushes extends AbstractTestCase {
+
+    public TestEJBIncrementalFlushes(String name) {
+        super(name, "kernelcactusapp");
+    }
+
+    public void setUp() {
+        deleteAll(RuntimeTest1.class);
+
+        EntityManager em = currentEntityManager();
+        startTx(em);
+
+        endTx(em);
+        endEm(em);
+    }
+
+//	public void testBasicJdoPreStore ()
+//	{
+//		EntityManager em = getEm ();
+//		em.getTransaction ().begin ();
+//		InstanceCallbacksTest a = new InstanceCallbacksTest ("foo", 10);
+//		em.persist(a);
+//		em.flush ();
+//		assertTrue (a.preStoreCalled);
+//		em.getTransaction ().commit ();
+//	}
+
+    public void testFlushNoChange() {
+        EntityManager em = currentEntityManager();
+        startTx(em);
+        InstanceCallbacksTest a = new InstanceCallbacksTest("foo", 10);
+        em.persist(a);
+        em.flush();
+        endTx(em);
+        //assertTrue (a.preStoreCalled);
+        assertEquals(10, a.getIntField());
+
+        endTx(em);
+        endEm(em);
+    }
+
+    public void testOptimisticLockGivesCorrectError() {
+        EntityManager pm1 = currentEntityManager();
+        EntityManager pm2 = currentEntityManager();
+
+        RuntimeTest1 a1 = new RuntimeTest1("foo", 10);
+        startTx(pm1);
+        pm1.persist(a1);
+        endTx(pm1);
+
+        RuntimeTest1 a2 = (RuntimeTest1) pm2.find(RuntimeTest1.class, 10);
+        startTx(pm2);
+        a2.setStringField("foobar");
+        startTx(pm2);
+
+        startTx(pm1);
+        a1.setStringField("foobarbaz");
+        try {
+            endTx(pm1);
+        }
+        catch (Exception ole) {
+            // expected case
+        }
+        finally {
+            if (pm1.getTransaction().isActive())
+                pm1.getTransaction().rollback();
+
+            endEm(pm1);
+            endEm(pm2);
+        }
+    }
+}



Mime
View raw message