Author: ppoddar
Date: Fri Aug  1 09:57:52 2008
New Revision: 681747

URL: http://svn.apache.org/viewvc?rev=681747&view=rev
Log:
Add test case for IDENTITY generation strategy

Added:
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObject.java
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObjectNative.java
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/TestAutoIncrement.java

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObject.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObject.java?rev=681747&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObject.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObject.java
 Fri Aug  1 09:57:52 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.oracle;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+/**
+ * A persistent entity to verify IDENTITY strategy for primary key value
+ * generation with Oracle Database.
+ * 
+ * @author Pinaki Poddar
+ *
+ */
[EMAIL PROTECTED]
+public class PObject {
+       @Id
+       @GeneratedValue(strategy=GenerationType.IDENTITY)
+       private long id;
+       
+       private String name;
+       
+       public long getId() {
+               return id;
+       }
+       
+       public String getName() {
+               return name;
+       }
+       
+       public void setName(String s) {
+               this.name = s;
+       }
+       
+       
+}

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObjectNative.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObjectNative.java?rev=681747&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObjectNative.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/PObjectNative.java
 Fri Aug  1 09:57:52 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.oracle;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+
+/**
+ * A persistent entity to verify IDENTITY strategy for primary key value
+ * generation with Oracle Database.
+ * 
+ * @author Pinaki Poddar
+ *
+ */
[EMAIL PROTECTED]
+public class PObjectNative {
+       @Id
+       @GeneratedValue(strategy=GenerationType.IDENTITY)
+       private long id;
+       
+       private String name;
+       
+       public long getId() {
+               return id;
+       }
+       
+       public String getName() {
+               return name;
+       }
+       
+       public void setName(String s) {
+               this.name = s;
+       }
+       
+       
+}

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/TestAutoIncrement.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/TestAutoIncrement.java?rev=681747&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/TestAutoIncrement.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/oracle/TestAutoIncrement.java
 Fri Aug  1 09:57:52 2008
@@ -0,0 +1,100 @@
+/*
+ * 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.oracle;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * Tests identity value assignment with IDENTITY strategy specifically for
+ * Oracle database. IDENTITY strategy for most database platform is supported
+ * with auto-increment capabilities. As Oracle does not natively support
+ * auto-increment, the same effect is achieved by a combination of a database
+ * sequence and a pre-insert database trigger [1].
+ * 
+ * This test verifies that a persistence entity using IDENTITY generation type
+ * is allocated identities in monotonic sequence on Oracle platform.
+ * 
+ * [1] http://jen.fluxcapacitor.net/geek/autoincr.html
+ * 
+ * @author Pinaki Poddar
+ * 
+ */
+public class TestAutoIncrement extends SingleEMFTestCase {
+       private static String PLATFORM = "oracle";
+
+       public void setUp() throws Exception {
+               if (!isTargetPlatform(PLATFORM)) {
+                       System.err.println("*** " + getName() + " skipped. Run 
with "
+                                       + "-Dplatform=" + PLATFORM + " to 
activate");
+                       return;
+               }
+               if 
("testAutoIncrementIdentityWithNamedSequence".equals(getName())) {
+                       super.setUp(CLEAR_TABLES, PObject.class,
+                           "openjpa.jdbc.DBDictionary",
+                           
"oracle(UseTriggersForAutoAssign=true,autoAssignSequenceName=autoIncrementSequence)",
+                           "openjpa.Log", "SQL=TRACE");
+               } else {
+                       super.setUp(CLEAR_TABLES, PObjectNative.class,
+                                       "openjpa.jdbc.DBDictionary",
+                                       "oracle(UseTriggersForAutoAssign=true)",
+                                       "openjpa.Log", "SQL=TRACE");
+               }
+       }
+
+       boolean isTargetPlatform(String target) {
+               String url = getPlatform();
+               return url != null && url.indexOf(target) != -1;
+       }
+
+       public String getPlatform() {
+               return System.getProperty("platform", "derby");
+       }
+
+       public void testAutoIncrementIdentityWithNamedSequence() {
+               if (!isTargetPlatform(PLATFORM))
+                       return;
+
+               EntityManager em = emf.createEntityManager();
+               em.getTransaction().begin();
+               PObject pc1 = new PObject();
+               PObject pc2 = new PObject();
+               em.persist(pc1);
+               em.persist(pc2);
+               em.getTransaction().commit();
+
+               assertEquals(1, Math.abs(pc1.getId() - pc2.getId()));
+       }
+
+       public void testAutoIncrementIdentityWithNativeSequence() {
+               if (!isTargetPlatform(PLATFORM))
+                       return;
+
+               EntityManager em = emf.createEntityManager();
+               em.getTransaction().begin();
+               PObjectNative pc1 = new PObjectNative();
+               PObjectNative pc2 = new PObjectNative();
+               em.persist(pc1);
+               em.persist(pc2);
+               em.getTransaction().commit();
+
+               assertEquals(1, Math.abs(pc1.getId() - pc2.getId()));
+       }
+}


Reply via email to