From 22054669db9d80652424ae22d04859c00494c7e3 Mon Sep 17 00:00:00 2001 From: Fay Wang Date: Mon, 19 Oct 2009 22:41:21 +0000 Subject: [PATCH] OPENJPA-871: derived identity test case using orm xml git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@826848 13f79535-47bb-0310-9956-ffa450edef68 --- .../enhance/identity/Dependent1Xml.java | 62 +++++++++ .../enhance/identity/DependentId1Xml.java | 65 +++++++++ .../enhance/identity/Employee1Xml.java | 86 ++++++++++++ .../enhance/identity/TestMapsIdXml.java | 130 ++++++++++++++++++ .../test/resources/META-INF/persistence.xml | 14 ++ .../enhance/identity/mapsId-orm.xml | 55 ++++++++ 6 files changed, 412 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1Xml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1Xml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1Xml.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMapsIdXml.java create mode 100644 openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1Xml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1Xml.java new file mode 100644 index 000000000..7bee10704 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent1Xml.java @@ -0,0 +1,62 @@ +/* + * 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.enhance.identity; + + +public class Dependent1Xml { + DependentId1Xml id; + + Employee1Xml emp; + + public Employee1Xml getEmp() { + return emp; + } + + public void setEmp(Employee1Xml emp) { + this.emp = emp; + } + + public DependentId1Xml getId() { + return id; + } + + public void setId(DependentId1Xml id) { + this.id = id; + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof Dependent1Xml)) return false; + Dependent1Xml d0 = (Dependent1Xml)o; + DependentId1Xml id0 = d0.getId(); + if (!id.equals(id0)) return false; + Employee1Xml e0 = d0.getEmp(); + if (emp != null && !emp.equals(e0)) return false; + if (emp == null && e0 != null) return false; + return true; + } + + public int hashCode() { + int ret = 0; + ret = ret * 31 + id.hashCode(); + ret = ret * 31 + emp.hashCode(); + return ret; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1Xml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1Xml.java new file mode 100644 index 000000000..b40280980 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId1Xml.java @@ -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.enhance.identity; + +public class DependentId1Xml { + String name; + long empPK; + + public DependentId1Xml() {} + + public DependentId1Xml(String name, long empPK) { + this.name = name; + this.empPK = empPK; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setEmpPK(long empPK) { + this.empPK = empPK; + } + + public long getEmpPK() { + return empPK; + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof DependentId1Xml)) return false; + DependentId1Xml d = (DependentId1Xml)o; + if (empPK != d.getEmpPK()) return false; + if (name != null && !name.equals(d.getName())) return false; + if (name == null && d.getName() != null) return false; + return true; + } + + public int hashCode() { + int ret = 0; + ret = (int) (ret * 31 + empPK); + ret = ret * 31 + name.hashCode(); + return ret; + } + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1Xml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1Xml.java new file mode 100644 index 000000000..6d68ce72f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee1Xml.java @@ -0,0 +1,86 @@ +/* + * 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.enhance.identity; + +import java.util.ArrayList; +import java.util.List; + +public class Employee1Xml { + int empId; + + String name; + + List dependents = new ArrayList(); + + public int getEmpId() { + return empId; + } + + public void setEmpId(int empId) { + this.empId = empId; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getDependents() { + return dependents; + } + + public void setDependents(List dependents) { + this.dependents = dependents; + } + + public void addDependent(Dependent1Xml d) { + dependents.add(d); + } + + public boolean equals(Object o) { + if (o == null) return false; + if (!(o instanceof Employee1Xml)) return false; + Employee1Xml e = (Employee1Xml)o; + if (empId != e.getEmpId()) return false; + if (name != null && !name.equals(e.getName())) return false; + if (name == null && e.getName() != null) return false; + List ds0 = e.getDependents(); + if (ds0 != null && ds0.size() != 0 && dependents == null) return false; + if (ds0 == null && dependents != null && dependents.size() != 0) + return false; + if (ds0 == null && dependents == null) return true; + if (ds0 != null && dependents != null) { + if (ds0.size() != dependents.size()) return false; + } + return true; + } + + public int hashCode() { + int ret = 0; + ret = ret * 31 + empId; + ret = ret * 31 + name.hashCode(); + if (dependents != null) + for (Dependent1Xml d : dependents) + ret = ret * 31 + d.id.hashCode(); + return ret; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMapsIdXml.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMapsIdXml.java new file mode 100644 index 000000000..fc74daef5 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestMapsIdXml.java @@ -0,0 +1,130 @@ +/* + * 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.enhance.identity; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.persistence.EntityManager; +import javax.persistence.EntityTransaction; +import javax.persistence.Query; + +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +public class TestMapsIdXml extends SingleEMFTestCase { + public int numEmployees = 4; + public int numDependentsPerEmployee = 2; + public int numPersons = 4; + + public Map emps1xml = new HashMap(); + public Map deps1xml = new HashMap(); + public int eId1 = 1; + public int dId1 = 1; + + public void setUp() throws Exception { + super.setUp(CLEAR_TABLES); + } + + @Override + protected String getPersistenceUnitName() { + return "mapsId-pu"; + } + + + /** + * This is spec 2.4.1.2 Example 1, case(b) + */ + public void testMapsId1Xml() { + createObj1Xml(); + findObj1Xml(); + queryObj1Xml(); + } + + public void createObj1Xml() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + for (int i = 0; i < numEmployees; i++) + createEmployee1Xml(em, eId1++); + tran.begin(); + em.flush(); + tran.commit(); + em.close(); + } + + public Employee1Xml createEmployee1Xml(EntityManager em, int id) { + Employee1Xml e = new Employee1Xml(); + e.setEmpId(id); + e.setName("emp_" + id); + for (int i = 0; i < numDependentsPerEmployee; i++) { + Dependent1Xml d = createDependent1Xml(em, dId1++, e); + e.addDependent(d); + em.persist(d); + } + em.persist(e); + emps1xml.put(id, e); + return e; + } + + public Dependent1Xml createDependent1Xml(EntityManager em, int id, Employee1Xml e) { + Dependent1Xml d = new Dependent1Xml(); + DependentId1Xml did = new DependentId1Xml(); + did.setName("dep_" + id); + d.setId(did); + d.setEmp(e); + deps1xml.put(did.getName(), d); + return d; + } + + public void findObj1Xml() { + EntityManager em = emf.createEntityManager(); + Employee1Xml e = em.find(Employee1Xml.class, 1); + List ds = e.getDependents(); + assertEquals(numDependentsPerEmployee, ds.size()); + Employee1Xml e0 = emps1xml.get(1); + assertEquals(e0, e); + } + + public void queryObj1Xml() { + queryDependent1Xml(); + } + + public void queryDependent1Xml() { + EntityManager em = emf.createEntityManager(); + EntityTransaction tran = em.getTransaction(); + tran.begin(); + String jpql = "select d from Dependent1Xml d where d.id.name = 'dep_1' " + + "AND d.emp.name = 'emp_1'"; + Query q = em.createQuery(jpql); + List ds = q.getResultList(); + for (Dependent1Xml d : ds) { + assertDependent1Xml(d); + } + tran.commit(); + em.close(); + } + + public void assertDependent1Xml(Dependent1Xml d) { + DependentId1Xml id = d.getId(); + Dependent1Xml d0 = deps1xml.get(id.getName()); + if (d0.id.empPK == 0) + d0.id.empPK = d0.emp.getEmpId(); + assertEquals(d0, d); + } +} diff --git a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml index 0221dd31c..10a530eba 100644 --- a/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml +++ b/openjpa-persistence-jdbc/src/test/resources/META-INF/persistence.xml @@ -52,6 +52,7 @@ org/apache/openjpa/persistence/orm.xml org/apache/openjpa/persistence/embed/embed-assoc-over-orm.xml org/apache/openjpa/persistence/detach/detach-orm.xml + org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml @@ -255,5 +256,18 @@ org.apache.openjpa.persistence.meta.MdrTestEntity + + org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml + org.apache.openjpa.persistence.enhance.identity.Employee1Xml + org.apache.openjpa.persistence.enhance.identity.Dependent1Xml + org.apache.openjpa.persistence.enhance.identity.DependentId1Xml + + + + + + diff --git a/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml new file mode 100644 index 000000000..c40567c69 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/resources/org/apache/openjpa/persistence/enhance/identity/mapsId-orm.xml @@ -0,0 +1,55 @@ + + + + + mapsIdXml + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file