openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p..@apache.org
Subject svn commit: r590529 [2/2] - in /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence: jdbc/annotations/ test/
Date Wed, 31 Oct 2007 01:27:25 GMT
Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestExtensionAnnotations.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestExtensionAnnotations.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestExtensionAnnotations.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestExtensionAnnotations.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,132 @@
+/*
+ * 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.jdbc.annotations;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
+import org.apache.openjpa.meta.FetchGroup;
+import org.apache.openjpa.meta.JavaTypes;
+import org.apache.openjpa.meta.UpdateStrategies;
+import org.apache.openjpa.meta.ValueMetaData;
+import org.apache.openjpa.meta.ValueStrategies;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * <p>Test the parsing of Kodo metadata extension annotations.</p>
+ *
+ * @author Abe White
+ */
+public class TestExtensionAnnotations
+    extends SingleEMFTestCase {
+
+    private ClassMapping _mapping;
+
+    public void setUp() {
+        setUp(ExtensionsEntity.class);
+        _mapping = ((JDBCConfiguration) emf.getConfiguration()).
+            getMappingRepositoryInstance().getMapping(ExtensionsEntity.class,
+            null, true);
+    }
+
+    public void testDataStoreId() {
+        assertEquals(ClassMapping.ID_DATASTORE, _mapping.getIdentityType());
+        assertEquals(ValueStrategies.SEQUENCE, _mapping.getIdentityStrategy());
+        assertEquals("system", _mapping.getIdentitySequenceName());
+    }
+
+    public void testDataCache() {
+        assertNull(_mapping.getDataCacheName());
+    }
+
+    public void testFetchGroups() {
+        FetchGroup fg = _mapping.getFetchGroup("detail");
+        assertNotNull(fg);
+        assertFalse(fg.isPostLoad());
+        FieldMapping fm = _mapping.getFieldMapping("rel");
+        assertEquals(1, fm.getCustomFetchGroups().length);
+        assertEquals("detail", fm.getCustomFetchGroups()[0]);
+        assertEquals(-1, fg.getRecursionDepth(fm));
+        fm = _mapping.getFieldMapping("seq");
+        assertEquals(1, fm.getCustomFetchGroups().length);
+        assertEquals("detail", fm.getCustomFetchGroups()[0]);
+        assertEquals(1, fg.getRecursionDepth(fm));
+    }
+
+    public void testValueStrategy() {
+        assertEquals(ValueStrategies.UUID_HEX,
+            _mapping.getField("uuid").getValueStrategy());
+        FieldMapping seq = _mapping.getFieldMapping("seq");
+        assertEquals(ValueStrategies.SEQUENCE, seq.getValueStrategy());
+        assertEquals("system", seq.getValueSequenceName());
+    }
+
+    public void testReadOnly() {
+        assertEquals(UpdateStrategies.RESTRICT,
+            _mapping.getField("seq").getUpdateStrategy());
+    }
+
+    public void testInverseLogical() {
+        assertEquals("owner", _mapping.getField("rel").getInverse());
+        assertNull(_mapping.getField("owner").getInverse());
+    }
+
+    public void testDependent() {
+        assertEquals(ValueMetaData.CASCADE_AUTO,
+            _mapping.getField("rel").getCascadeDelete());
+        assertEquals(ValueMetaData.CASCADE_NONE,
+            _mapping.getField("eager").getCascadeDelete());
+        assertEquals(ValueMetaData.CASCADE_AUTO,
+            _mapping.getField("eager").getElement().getCascadeDelete());
+    }
+
+    public void testLRS() {
+        assertTrue(_mapping.getField("lrs").isLRS());
+        assertFalse(_mapping.getField("eager").isLRS());
+    }
+
+    public void testClassCriteria() {
+        assertTrue(_mapping.getFieldMapping("eager").getElementMapping().
+            getUseClassCriteria());
+        assertFalse(_mapping.getFieldMapping("eager").getUseClassCriteria());
+        assertFalse(_mapping.getFieldMapping("lrs").getElementMapping().
+            getUseClassCriteria());
+    }
+
+    public void testExternalValues() {
+        FieldMapping externalValues = _mapping.getFieldMapping
+            ("externalValues");
+        assertEquals(JavaTypes.CHAR, externalValues.getDeclaredTypeCode());
+        assertEquals(JavaTypes.INT, externalValues.getTypeCode());
+        assertEquals(new Integer(1), externalValues.getExternalValueMap().
+            get(new Character('M')));
+        assertEquals(new Integer(2), externalValues.getExternalValueMap().
+            get(new Character('F')));
+    }
+
+    public void testExternalizer() {
+        FieldMapping externalizer = _mapping.getFieldMapping("externalizer");
+        assertEquals(JavaTypes.OBJECT, externalizer.getDeclaredTypeCode());
+        assertEquals(JavaTypes.STRING, externalizer.getTypeCode());
+        assertEquals("java.lang.String", externalizer.getExternalValue
+            (String.class, null));
+        assertEquals(String.class, externalizer.getFieldValue
+            (String.class.getName(), null));
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestFlatInheritance.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestFlatInheritance.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestFlatInheritance.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestFlatInheritance.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,58 @@
+/*
+ * 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.jdbc.annotations;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for InheritanceType.SINGLE_TABLE
+ *
+ * @author Steve Kim
+ */
+public class TestFlatInheritance extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(Flat1.class, Flat2.class, CLEAR_TABLES);
+    }
+
+    public void testInheritance() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        Flat1 pc = new Flat1(1);
+        Flat2 pc2 = new Flat2(2);
+        pc.setBasic(10);
+        pc2.setBasic(20);
+        pc2.setBasic2("DOG");
+        em.persist(pc);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(Flat1.class, 1);
+        assertEquals(10, pc.getBasic());
+        pc = em.find(Flat1.class, 2);
+        assertEquals(Flat2.class, pc.getClass());
+        assertEquals(20, pc.getBasic());
+        assertEquals("DOG", ((Flat2) pc).getBasic2());
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestJoinedInheritance.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestJoinedInheritance.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestJoinedInheritance.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestJoinedInheritance.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,65 @@
+/*
+ * 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.jdbc.annotations;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for InheritanceType.JOINED
+ *
+ * @author Steve Kim
+ */
+public class TestJoinedInheritance extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(AnnoTest1.class, AnnoTest2.class, AnnoTest3.class, Flat1.class,
+            CLEAR_TABLES);
+    }
+
+    public void testInheritance() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest3 pc = new AnnoTest3();
+        pc.setPk(new Long(1));
+        pc.setBasic(10);
+        pc.setBasic2(20);
+        em.persist(pc);
+
+        AnnoTest2 pc2 = new AnnoTest2();
+        pc2.setPk1(2);
+        pc2.setPk2("bar");
+        pc2.setInverseOneOne(pc);
+        pc.setSubOneOne(pc2);
+        em.persist(pc2);
+
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc2 = em.find(AnnoTest2.class, new AnnoTest2.Oid(2, "bar"));
+        pc = (AnnoTest3) pc2.getInverseOneOne();
+        assertEquals(pc.getInverseOwnerOneOne(), pc2);
+        assertEquals(10, pc.getBasic());
+        assertEquals(20, pc.getBasic2());
+        assertEquals(pc2, pc.getSubOneOne());
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestManyToMany.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestManyToMany.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestManyToMany.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestManyToMany.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,110 @@
+/*
+ * 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.jdbc.annotations;
+
+import java.util.Set;
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for m-m
+ *
+ * @author Steve Kim
+ */
+public class TestManyToMany
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class, CLEAR_TABLES);
+    }
+
+    public void testManyToMany() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(4);
+        em.persist(pc);
+        AnnoTest2 pc2;
+        for (int i = 0; i < 3; i++) {
+            pc2 = new AnnoTest2(5 + i, "foo" + i);
+            pc.getManyMany().add(pc2);
+            em.persist(pc2);
+        }
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(4));
+        Set<AnnoTest2> many = pc.getManyMany();
+        assertEquals(3, many.size());
+        for (AnnoTest2 manyPc2 : many) {
+            switch ((int) manyPc2.getPk1()) {
+                case 5:
+                    assertEquals("foo0", manyPc2.getPk2());
+                    break;
+                case 6:
+                    assertEquals("foo1", manyPc2.getPk2());
+                    break;
+                case 7:
+                    assertEquals("foo2", manyPc2.getPk2());
+                    break;
+                default:
+                    fail("bad pk:" + manyPc2.getPk1());
+            }
+        }
+        em.close();
+    }
+
+    public void testInverseOwnerManyToMany() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(4);
+        em.persist(pc);
+        AnnoTest2 pc2;
+        for (int i = 0; i < 3; i++) {
+            pc2 = new AnnoTest2(5 + i, "foo" + i);
+            pc2.getManyMany().add(pc);
+            em.persist(pc2);
+        }
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(4));
+        Set<AnnoTest2> many = pc.getInverseOwnerManyMany();
+        assertEquals(3, many.size());
+        for (AnnoTest2 manyPc2 : many) {
+            assertTrue(manyPc2.getManyMany().contains(pc));
+            switch ((int) manyPc2.getPk1()) {
+                case 5:
+                    assertEquals("foo0", manyPc2.getPk2());
+                    break;
+                case 6:
+                    assertEquals("foo1", manyPc2.getPk2());
+                    break;
+                case 7:
+                    assertEquals("foo2", manyPc2.getPk2());
+                    break;
+                default:
+                    fail("bad pk:" + manyPc2.getPk1());
+            }
+        }
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestMapKey.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestMapKey.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestMapKey.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestMapKey.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,123 @@
+/*
+ * 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.jdbc.annotations;
+
+import javax.persistence.EntityManager;
+import javax.persistence.MapKey;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.FieldMapping;
+import org.apache.openjpa.jdbc.meta.strats.RelationMapInverseKeyFieldStrategy;
+import org.apache.openjpa.jdbc.meta.strats.RelationMapTableFieldStrategy;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * <p>Test the use of the {@link MapKey} annotation.  Note that we have
+ * more thorough Kodo core tests for the mappings themselves.  See
+ * {@link kodo.jdbc.meta.TestMappedByKeyMaps}.
+ *
+ * @author Abe White
+ */
+public class TestMapKey
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(Flat1.class, AnnoTest1.class, AnnoTest2.class, CLEAR_TABLES);
+
+    }
+
+    public void testDefaultMapKeyMapping() {
+        ClassMapping mapping = ((JDBCConfiguration) emf.getConfiguration())
+            .getMappingRepositoryInstance().getMapping(AnnoTest1.class,
+                null, true);
+        FieldMapping fm = mapping.getFieldMapping("defaultMapKey");
+        assertTrue(fm.getStrategy() instanceof RelationMapTableFieldStrategy);
+        assertEquals("pk", fm.getKey().getValueMappedBy());
+        assertEquals(mapping.getRepository().getMetaData(Flat1.class, null,
+            true).getField("pk"), fm.getKey().getValueMappedByMetaData());
+    }
+
+    public void testNamedMapKeyMapping() {
+        ClassMapping mapping = ((JDBCConfiguration) emf.getConfiguration())
+            .getMappingRepositoryInstance().getMapping(AnnoTest1.class,
+                null, true);
+        FieldMapping fm = mapping.getFieldMapping("namedMapKey");
+        assertTrue(fm.getStrategy() instanceof RelationMapTableFieldStrategy);
+        assertEquals("basic", fm.getKey().getValueMappedBy());
+        assertEquals(mapping.getRepository().getMetaData(Flat1.class, null,
+            true).getField("basic"), fm.getKey().getValueMappedByMetaData());
+    }
+
+    public void testInverseOwnerMapKeyMapping() {
+        ClassMapping mapping = ((JDBCConfiguration) emf.getConfiguration())
+            .getMappingRepositoryInstance().getMapping(AnnoTest1.class,
+                null, true);
+        FieldMapping fm = mapping.getFieldMapping("inverseOwnerMapKey");
+        assertTrue(fm.getStrategy() instanceof
+            RelationMapInverseKeyFieldStrategy);
+        assertEquals("basic", fm.getKey().getValueMappedBy());
+        assertEquals(mapping.getRepository().getMetaData(AnnoTest2.class,
+            null, true).getField("basic"), fm.getKey().
+            getValueMappedByMetaData());
+    }
+
+    public void testInsertAndRetrieve() {
+        Flat1 f1 = new Flat1(1);
+        f1.setBasic(100);
+        Flat1 f2 = new Flat1(2);
+        f2.setBasic(200);
+        AnnoTest2 a1 = new AnnoTest2(1L, "1");
+        a1.setBasic("100");
+        AnnoTest2 a2 = new AnnoTest2(2L, "2");
+        a2.setBasic("200");
+
+        AnnoTest1 pc = new AnnoTest1(1L);
+        pc.getDefaultMapKey().put(f1.getPk(), f1);
+        pc.getDefaultMapKey().put(f2.getPk(), f2);
+        pc.getNamedMapKey().put(f1.getBasic(), f1);
+        pc.getNamedMapKey().put(f2.getBasic(), f2);
+        pc.getInverseOwnerMapKey().put(a1.getBasic(), a1);
+        pc.getInverseOwnerMapKey().put(a2.getBasic(), a2);
+        a1.setOneManyOwner(pc);
+        a2.setOneManyOwner(pc);
+
+        OpenJPAEntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persistAll(new Object[]{ pc, f1, f2, a1, a2 });
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(1));
+        assertEquals(2, pc.getDefaultMapKey().size());
+        assertEquals(1, pc.getDefaultMapKey().get(1).getPk());
+        assertEquals(2, pc.getDefaultMapKey().get(2).getPk());
+        assertEquals(2, pc.getNamedMapKey().size());
+        assertEquals(100, pc.getNamedMapKey().get(100).getBasic());
+        assertEquals(200, pc.getNamedMapKey().get(200).getBasic());
+        assertEquals(2, pc.getInverseOwnerMapKey().size());
+        assertEquals("100", pc.getInverseOwnerMapKey().get("100").
+            getBasic());
+        assertEquals("200", pc.getInverseOwnerMapKey().get("200").
+            getBasic());
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToMany.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToMany.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToMany.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToMany.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,102 @@
+/*
+ * 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.jdbc.annotations;
+
+import java.util.Collection;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for 1-m
+ *
+ * @author Steve Kim
+ */
+public class TestOneToMany
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class, CLEAR_TABLES);
+    }
+
+    public void testOneToMany() {
+        OpenJPAEntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        pc.getOneMany().add(new AnnoTest2(15, "foo"));
+        pc.getOneMany().add(new AnnoTest2(20, "foobar"));
+        em.persist(pc);
+        em.persistAll(pc.getOneMany());
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        Collection<AnnoTest2> many = pc.getOneMany();
+        assertEquals(2, many.size());
+        for (AnnoTest2 pc2 : many) {
+            switch ((int) pc2.getPk1()) {
+                case 15:
+                    assertEquals("foo", pc2.getPk2());
+                    break;
+                case 20:
+                    assertEquals("foobar", pc2.getPk2());
+                    break;
+                default:
+                    fail("unknown element:" + pc2.getPk1());
+            }
+        }
+        em.close();
+    }
+
+    public void testInverseOwnerOneToMany() {
+        OpenJPAEntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        AnnoTest2 pc2 = new AnnoTest2(15, "foo");
+        pc.getInverseOwnerOneMany().add(pc2);
+        pc2.setOneManyOwner(pc);
+        pc2 = new AnnoTest2(20, "foobar");
+        pc.getInverseOwnerOneMany().add(pc2);
+        pc2.setOneManyOwner(pc);
+        em.persist(pc);
+        em.persistAll(pc.getInverseOwnerOneMany());
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        Collection<AnnoTest2> many = pc.getInverseOwnerOneMany();
+        assertEquals(2, many.size());
+        for (AnnoTest2 pc3 : many) {
+            assertEquals(pc, pc3.getOneManyOwner());
+            switch ((int) pc3.getPk1()) {
+                case 15:
+                    assertEquals("foo", pc3.getPk2());
+                    break;
+                case 20:
+                    assertEquals("foobar", pc3.getPk2());
+                    break;
+                default:
+                    fail("unknown element:" + pc3.getPk1());
+            }
+        }
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToOne.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToOne.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToOne.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestOneToOne.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,135 @@
+/*
+ * 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.jdbc.annotations;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for 1-1
+ *
+ * @author Steve Kim
+ */
+public class TestOneToOne
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class, CLEAR_TABLES);
+    }
+
+    public void testOneToOne() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        AnnoTest2 pc2 = new AnnoTest2(15, "foo");
+        pc.setOneOne(pc2);
+        em.persist(pc);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        pc2 = pc.getOneOne();
+        assertNotNull(pc2);
+        assertEquals(15, pc2.getPk1());
+        assertEquals("foo", pc2.getPk2());
+        em.close();
+    }
+
+    public void testSelfOneToOne() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        AnnoTest1 pc2 = new AnnoTest1(15);
+        pc.setSelfOneOne(pc2);
+        em.persist(pc);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        pc2 = pc.getSelfOneOne();
+        assertNotNull(pc2);
+        assertEquals(new Long(15), pc2.getPk());
+        em.close();
+    }
+
+    public void testPKJoinSelfOneToOne() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        AnnoTest1 pc2 = new AnnoTest1(15);
+        pc.setSelfOneOne(pc2);
+        em.persist(pc);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        pc2 = pc.getSelfOneOne();
+        assertNotNull(pc2);
+        assertEquals(new Long(15), pc2.getPk());
+        em.close();
+    }
+
+    public void testOtherTableOneToOne() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        AnnoTest2 pc2 = new AnnoTest2(15, "foo");
+        pc.setOtherTableOneOne(pc2);
+        em.persist(pc);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        pc2 = pc.getOtherTableOneOne();
+        assertNotNull(pc2);
+        assertEquals(15, pc2.getPk1());
+        assertEquals("foo", pc2.getPk2());
+        em.close();
+    }
+
+    public void testInverseOneToOne() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc = new AnnoTest1(5);
+        AnnoTest2 pc2 = new AnnoTest2(15, "foo");
+        pc2.setInverseOneOne(pc);
+        em.persist(pc);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        pc2 = pc.getInverseOwnerOneOne();
+        assertNotNull(pc2);
+        assertEquals(15, pc2.getPk1());
+        assertEquals("foo", pc2.getPk2());
+        assertEquals(pc, pc2.getInverseOneOne());
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestPropertyAccess.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestPropertyAccess.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestPropertyAccess.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestPropertyAccess.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,92 @@
+/*
+ * 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.jdbc.annotations;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestPropertyAccess
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(PropertyAccess1.class, CLEAR_TABLES);
+    }
+
+    public void testPropertyAccessBasicCreation() {
+        OpenJPAEntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        PropertyAccess1 pa1_1 = new PropertyAccess1(10);
+        pa1_1.setName("foo");
+        em.persist(pa1_1);
+        em.getTransaction().commit();
+
+        // getting a new EM should not be necessary once the extended PC stuff
+        // is complete.
+        em = emf.createEntityManager();
+
+        PropertyAccess1 pa1_2 = em.find(PropertyAccess1.class, 10);
+        assertNotSame(pa1_1, pa1_2);
+        assertNotNull(pa1_2);
+        assertEquals(10, pa1_2.getId());
+        assertEquals("foo", pa1_2.getName());
+    }
+
+    public void testPropertyAccessBasicMutation() {
+        OpenJPAEntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        PropertyAccess1 pa1_1 = new PropertyAccess1(10);
+        pa1_1.setName("foo");
+        em.persist(pa1_1);
+        em.getTransaction().commit();
+
+        // getting a new EM should not be necessary once the extended PC stuff
+        // is complete.
+        em = emf.createEntityManager();
+
+        em.getTransaction().begin();
+        PropertyAccess1 pa1_2 = em.find(PropertyAccess1.class, 10);
+        pa1_2.setName(pa1_2.getName() + "bar");
+        em.getTransaction().commit();
+
+        em = emf.createEntityManager();
+        PropertyAccess1 pa1_3 = em.find(PropertyAccess1.class, 10);
+        assertNotSame(pa1_2, pa1_3);
+        assertEquals("foobar", pa1_3.getName());
+    }
+
+    public void testJPQL() {
+        EntityManager em = emf.createEntityManager();
+        em.createQuery("select o from PropertyAccess1 o where " +
+            "o.name = 'foo'").getResultList();
+        em.createQuery("select o from PropertyAccess1 o order by " +
+            "o.name asc").getResultList();
+        em.close();
+    }
+
+    public void testJPQLWithFieldNameMismatch() {
+        EntityManager em = emf.createEntityManager();
+        em.createQuery("select o from PropertyAccess1 o where " +
+            "o.intValue = 0").getResultList();
+        em.createQuery("select o from PropertyAccess1 o order by " +
+            "o.intValue asc").getResultList();
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestSerializedLobs.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestSerializedLobs.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestSerializedLobs.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestSerializedLobs.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,137 @@
+/*
+ * 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.jdbc.annotations;
+
+import java.sql.Blob;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Date;
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for serialized, clob, and lob types.
+ *
+ * @author Steve Kim
+ */
+public class TestSerializedLobs extends SingleEMFTestCase {
+
+    private static final Date DATE = new Date();
+
+    public void setUp() {
+        setUp(AnnoTest1.class, AnnoTest2.class, Flat1.class, CLEAR_TABLES);
+    }
+
+    // Serialized fields not being read properly
+    public void testSerialized() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 pc1 = new AnnoTest1(1);
+        AnnoTest1 pc2 = new AnnoTest1(2);
+        pc1.setSerialized("ASDASD");
+        pc2.setSerialized(DATE);
+        em.persist(pc1);
+        em.persist(pc2);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc1 = em.find(AnnoTest1.class, new Long(1));
+        pc2 = em.find(AnnoTest1.class, new Long(2));
+        assertEquals("ASDASD", pc1.getSerialized());
+        assertEquals(DATE, pc2.getSerialized());
+        em.close();
+    }
+
+    public void testBlob()
+        throws Exception {
+        OpenJPAEntityManagerSPI em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        AnnoTest1 pc = new AnnoTest1(1);
+        pc.setBlob("Not Null".getBytes());
+        em.persist(pc);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(1));
+        assertEquals("Not Null", new String(pc.getBlob()));
+        Connection conn = (Connection) em.getConnection();
+        Statement stmnt = conn.createStatement();
+        ResultSet rs = stmnt.executeQuery("SELECT BLOBVAL FROM ANNOTEST1 "
+            + "WHERE PK = 1");
+        assertTrue(rs.next());
+
+        JDBCConfiguration conf = (JDBCConfiguration) em.getConfiguration();
+        DBDictionary dict = conf.getDBDictionaryInstance();
+        if (dict.useGetBytesForBlobs)
+            rs.getBytes(1);
+        else if (dict.useGetObjectForBlobs)
+            rs.getObject(1);
+        else {
+            Blob blob = rs.getBlob(1);
+            blob.getBytes(1L, (int) blob.length());
+        }
+        assertEquals("Not Null", new String(pc.getBlob()));
+
+        try {
+            rs.close();
+        } catch (SQLException e) {
+        }
+        try {
+            stmnt.close();
+        } catch (SQLException e) {
+        }
+        try {
+            conn.close();
+        } catch (SQLException e) {
+        }
+        em.close();
+    }
+
+    public void testClob()
+        throws Exception {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        AnnoTest1 pc = new AnnoTest1(1);
+        StringBuffer buf = new StringBuffer();
+        for (int i = 0; i < 1000; i++)
+            buf.append((char) ('a' + (i % 24)));
+        pc.setClob(buf.toString());
+        em.persist(pc);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(1));
+        String str = pc.getClob();
+        assertEquals(1000, str.length());
+        for (int i = 0; i < str.length(); i++)
+            assertEquals('a' + (i % 24), str.charAt(i));
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestTablePerClassInheritance.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestTablePerClassInheritance.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestTablePerClassInheritance.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestTablePerClassInheritance.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,101 @@
+/*
+ * 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.jdbc.annotations;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.strats.FullClassStrategy;
+import org.apache.openjpa.jdbc.meta.strats.NoneDiscriminatorStrategy;
+import org.apache.openjpa.persistence.OpenJPAEntityManager;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * <p>Test that InheritanceType.TABLE_PER_CLASS JPA mapping is translated
+ * correctly.  See the <code>kodo.jdbc.meta.tableperclass</code> test package
+ * for more detailed tests of functionality.</p>
+ *
+ * @author Abe White
+ */
+public class TestTablePerClassInheritance
+    extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(TablePerClass1.class, TablePerClass2.class, EmbedOwner.class,
+            EmbedValue.class, CLEAR_TABLES);
+    }
+
+    public void testMapping() {
+        ClassMapping mapping = ((JDBCConfiguration) emf.getConfiguration()).
+            getMappingRepositoryInstance().getMapping(TablePerClass2.class,
+            null, true);
+        assertTrue(mapping.getStrategy() instanceof FullClassStrategy);
+        assertTrue(mapping.getDiscriminator().getStrategy()
+            instanceof NoneDiscriminatorStrategy);
+        assertNull(mapping.getJoinForeignKey());
+        assertNull(mapping.getJoinablePCSuperclassMapping());
+        assertEquals("TPC_BASIC", mapping.getFieldMapping("basic").
+            getColumns()[0].getName());
+        ClassMapping embed = mapping.getFieldMapping("embed").
+            getEmbeddedMapping();
+        assertEquals("TPC_EMB_BASIC", embed.getFieldMapping("basic").
+            getColumns()[0].getName());
+
+        ClassMapping sup = mapping.getPCSuperclassMapping();
+        assertEquals(TablePerClass1.class, sup.getDescribedType());
+        assertTrue(sup.getStrategy() instanceof FullClassStrategy);
+        assertTrue(sup.getDiscriminator().getStrategy()
+            instanceof NoneDiscriminatorStrategy);
+        assertEquals("TPC_BASIC", sup.getFieldMapping("basic").
+            getColumns()[0].getName());
+        embed = sup.getFieldMapping("embed").getEmbeddedMapping();
+        assertEquals("TPC_EMB_BASIC", embed.getFieldMapping("basic").
+            getColumns()[0].getName());
+    }
+
+    public void testInsertAndRetrieve() {
+        OpenJPAEntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        TablePerClass1 tpc1 = new TablePerClass1();
+        tpc1.setBasic(1);
+        EmbedValue ev = new EmbedValue();
+        ev.setBasic("11");
+        tpc1.setEmbed(ev);
+        TablePerClass2 tpc2 = new TablePerClass2();
+        tpc2.setBasic(2);
+        tpc2.setBasic2("2");
+        ev = new EmbedValue();
+        ev.setBasic("22");
+        tpc2.setEmbed(ev);
+        em.persistAll(tpc1, tpc2);
+        em.getTransaction().commit();
+        int id1 = tpc1.getPk();
+        int id2 = tpc2.getPk();
+        em.close();
+
+        em = emf.createEntityManager();
+        tpc1 = em.find(TablePerClass1.class, id1);
+        assertEquals(1, tpc1.getBasic());
+        assertEquals("11", tpc1.getEmbed().getBasic());
+        tpc2 = (TablePerClass2) em.find(TablePerClass1.class, id2);
+        assertEquals(2, tpc2.getBasic());
+        assertEquals("2", tpc2.getBasic2());
+        assertEquals("22", tpc2.getEmbed().getBasic());
+        em.close();
+    }
+}

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java?rev=590529&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/annotations/TestVersion.java Tue Oct 30 18:27:23 2007
@@ -0,0 +1,184 @@
+/*
+ * 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.jdbc.annotations;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.meta.ClassMapping;
+import org.apache.openjpa.jdbc.meta.strats.NoneVersionStrategy;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Test for opt-lock
+ *
+ * @author Steve Kim
+ */
+public class TestVersion extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(AnnoTest1.class, AnnoTest2.class, AnnoTest3.class, Flat1.class,
+            EmbedOwner.class, EmbedValue.class, CLEAR_TABLES);
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        AnnoTest1 test1 = new AnnoTest1();
+        test1.setPk(new Long(5));
+        test1.setBasic(50);
+        test1.setTransient(500);
+        em.persist(test1);
+
+        AnnoTest2 test2 = new AnnoTest2();
+        test2.setPk1(5);
+        test2.setPk2("bar");
+        test2.setBasic("50");
+        em.persist(test2);
+
+        AnnoTest3 test3 = new AnnoTest3();
+        test3.setPk(new Long(3));
+        test3.setBasic2(50);
+        em.persist(test3);
+        em.getTransaction().commit();
+        em.close();
+    }
+
+    public void testVersionNumeric() {
+        EntityManager em1 = emf.createEntityManager();
+        em1.getTransaction().begin();
+        EntityManager em2 = emf.createEntityManager();
+        em2.getTransaction().begin();
+
+        AnnoTest1 pc1 = em1.find(AnnoTest1.class, new Long(5));
+        AnnoTest1 pc2 = em2.find(AnnoTest1.class, new Long(5));
+        assertEquals(1, pc1.getVersion());
+        assertEquals(1, pc2.getVersion());
+        assertEquals(0, pc1.getTransient());
+        pc1.setBasic(75);
+        pc2.setBasic(75);
+        em1.getTransaction().commit();
+        em1.close();
+
+        em1 = emf.createEntityManager();
+        pc1 = em1.find(AnnoTest1.class, new Long(5));
+        assertEquals(2, pc1.getVersion());
+        em1.close();
+        try {
+            em2.getTransaction().commit();
+            fail("Optimistic fail");
+        } catch (Exception e) {
+        } finally {
+            em2.close();
+        }
+    }
+
+    public void testVersionTimestamp() {
+        EntityManager em1 = emf.createEntityManager();
+        em1.getTransaction().begin();
+        EntityManager em2 = emf.createEntityManager();
+        em2.getTransaction().begin();
+
+        AnnoTest2 pc1 = em1.find(AnnoTest2.class,
+            new AnnoTest2.Oid(5, "bar"));
+        AnnoTest2 pc2 = em2.find(AnnoTest2.class,
+            new AnnoTest2.Oid(5, "bar"));
+        assertNotNull(pc1.getVersion());
+        assertEquals(pc1.getVersion(), pc2.getVersion());
+        pc1.setBasic("75");
+        pc2.setBasic("75");
+        em1.getTransaction().commit();
+        em1.close();
+
+        em1 = emf.createEntityManager();
+        pc1 = em1.find(AnnoTest2.class,
+            new AnnoTest2.Oid(5, "bar"));
+        assertTrue(pc1.getVersion().compareTo(pc2.getVersion()) > 0);
+        em1.close();
+        try {
+            em2.getTransaction().commit();
+            fail("Optimistic fail");
+        } catch (Exception e) {
+        } finally {
+            em2.close();
+        }
+    }
+
+    public void testVersionSubclass() {
+        EntityManager em1 = emf.createEntityManager();
+        em1.getTransaction().begin();
+        EntityManager em2 = emf.createEntityManager();
+        em2.getTransaction().begin();
+
+        AnnoTest3 pc1 = em1.find(AnnoTest3.class, new Long(3));
+        AnnoTest3 pc2 = em2.find(AnnoTest3.class, new Long(3));
+        assertEquals(1, pc1.getVersion());
+        assertEquals(1, pc2.getVersion());
+        pc1.setBasic2(75);
+        pc2.setBasic2(75);
+        em1.getTransaction().commit();
+        em1.close();
+
+        em1 = emf.createEntityManager();
+        pc1 = em1.find(AnnoTest3.class, new Long(3));
+        assertEquals(2, pc1.getVersion());
+        em1.close();
+        try {
+            em2.getTransaction().commit();
+            fail("Optimistic fail");
+        } catch (Exception e) {
+        } finally {
+            em2.close();
+        }
+    }
+
+    public void testVersionNoChange() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        AnnoTest1 pc = em.find(AnnoTest1.class, new Long(5));
+        assertEquals(1, pc.getVersion());
+        assertEquals(0, pc.getTransient());
+        pc.setTransient(750);
+        em.getTransaction().commit();
+        em.close();
+
+        em = emf.createEntityManager();
+        pc = em.find(AnnoTest1.class, new Long(5));
+        assertEquals(1, pc.getVersion());
+        assertEquals(0, pc.getTransient());
+        em.close();
+    }
+
+    public void testNoDefaultVersionWithoutFieldOrColumn() {
+        ClassMapping cls = ((JDBCConfiguration) emf.getConfiguration()).
+            getMappingRepositoryInstance().getMapping(EmbedOwner.class, 
+            null, true);
+        assertEquals(NoneVersionStrategy.getInstance(),
+            cls.getVersion().getStrategy());
+        assertEquals(0, cls.getVersion().getColumns().length);
+    }
+
+    public void testVersionWithField() {
+        ClassMapping cls = ((JDBCConfiguration) emf.getConfiguration()).
+            getMappingRepositoryInstance().getMapping(AnnoTest1.class, 
+            null, true);
+        assertTrue(NoneVersionStrategy.getInstance() !=
+            cls.getVersion().getStrategy());
+        assertEquals(1, cls.getVersion().getColumns().length);
+    }
+}

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java?rev=590529&r1=590528&r2=590529&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/test/PersistenceTestCase.java Tue Oct 30 18:27:23 2007
@@ -171,4 +171,13 @@
         ClassMetaData meta = JPAFacadeHelper.getMetaData(emf, c);
         return (meta == null) ? null : meta.getTypeAlias();
     }
+
+    public static void assertNotEquals(Object o1, Object o2) {
+        if (o1 == o2)
+            fail("expected args to be different; were the same instance.");
+        else if (o1 == null || o2 == null)
+            return;
+        else if (o1.equals(o2))
+            fail("expected args to be different; compared equal.");
+    }
 }



Mime
View raw message