From 912b9a37f7b7803d656c2ca1098f043491d49089 Mon Sep 17 00:00:00 2001 From: Pinaki Poddar Date: Mon, 25 Aug 2008 18:10:59 +0000 Subject: [PATCH] OPENJPA-704:Adding test case git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@688809 13f79535-47bb-0310-9956-ffa450edef68 --- .../kernel/TestDynamicFetchPlan.java | 160 ++++++++++++++++++ .../kernel/common/apps/FetchA.java | 46 +++++ .../kernel/common/apps/FetchB.java | 36 ++++ .../kernel/common/apps/FetchBase.java | 52 ++++++ 4 files changed, 294 insertions(+) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDynamicFetchPlan.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchA.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchB.java create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchBase.java diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDynamicFetchPlan.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDynamicFetchPlan.java new file mode 100644 index 000000000..b2508afc6 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDynamicFetchPlan.java @@ -0,0 +1,160 @@ +/* + * 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.FetchPlan; +import org.apache.openjpa.persistence.OpenJPAEntityManager; +import org.apache.openjpa.persistence.kernel.common.apps.FetchA; +import org.apache.openjpa.persistence.kernel.common.apps.FetchB; +import org.apache.openjpa.persistence.kernel.common.apps.FetchBase; +import org.apache.openjpa.persistence.test.SingleEMFTestCase; + +/** + * Tests behavior of FetchPlan constructed dynamically by adding fields. + * + * Originally reported by Michael Vorburger in + * OpenJPA + * user group + * + * @author Pinaki Poddar + * + */ +public class TestDynamicFetchPlan extends SingleEMFTestCase { + private static final String JPQL = "select a from FetchA a"; + + public void setUp() { + super.setUp(CLEAR_TABLES, FetchBase.class, FetchA.class, FetchB.class); + createData(); + } + + public void createData() { + OpenJPAEntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + FetchA a1 = new FetchA(); + a1.setText("a1"); + FetchB b1 = new FetchB(); + b1.setText("b1"); + a1.setB(b1); + em.persist(a1); + em.persist(b1); + em.getTransaction().commit(); + } + + public void testFetchBySubClassFieldB() { + OpenJPAEntityManager em = emf.createEntityManager(); + FetchPlan fp = em.getFetchPlan(); + fp.clearFetchGroups(); + fp.clearFields(); + fp.addField(FetchA.class, "b"); + fp.addField(FetchB.class, "text"); + + FetchA a = (FetchA) em.createQuery(JPQL).getSingleResult(); + em.close(); + + FetchB b = a.getB(); + assertNotNull(b); + assertNull(a.getText()); + assertEquals("b1", b.getText()); + } + + public void testFetchBySubClassFieldA() { + OpenJPAEntityManager em = emf.createEntityManager(); + FetchPlan fp = em.getFetchPlan(); + fp.clearFetchGroups(); + fp.clearFields(); + fp.addField(FetchA.class, "b"); + fp.addField(FetchA.class, "text"); + + FetchA a = (FetchA) em.createQuery(JPQL).getSingleResult(); + em.close(); + + FetchB b = a.getB(); + assertNotNull(b); + assertEquals("a1", a.getText()); + assertNull(b.getText()); + } + + public void testFetchBySuperClassField() { + OpenJPAEntityManager em = emf.createEntityManager(); + FetchPlan fp = em.getFetchPlan(); + fp.clearFetchGroups(); + fp.clearFields(); + fp.addField(FetchA.class, "b"); + fp.addField(FetchBase.class, "text"); + + FetchA a = (FetchA) em.createQuery(JPQL).getSingleResult(); + em.close(); + + FetchB b = a.getB(); + assertNotNull(b); + assertEquals("a1", a.getText()); + assertEquals("b1", b.getText()); + } + + public void testFetchBySubClassFieldNameB() { + OpenJPAEntityManager em = emf.createEntityManager(); + FetchPlan fp = em.getFetchPlan(); + fp.clearFetchGroups(); + fp.clearFields(); + fp.addField(FetchA.class.getName() + ".b"); + fp.addField(FetchB.class.getName() + ".text"); + + FetchA a = (FetchA) em.createQuery(JPQL).getSingleResult(); + em.close(); + + FetchB b = a.getB(); + assertNotNull(b); + assertNull(a.getText()); + assertEquals("b1", b.getText()); + } + + public void testFetchBySubClassFieldNameA() { + OpenJPAEntityManager em = emf.createEntityManager(); + FetchPlan fp = em.getFetchPlan(); + fp.clearFetchGroups(); + fp.clearFields(); + fp.addField(FetchA.class.getName() + ".b"); + fp.addField(FetchA.class.getName() + ".text"); + + FetchA a = (FetchA) em.createQuery(JPQL).getSingleResult(); + em.close(); + + FetchB b = a.getB(); + assertNotNull(b); + assertEquals("a1", a.getText()); + assertNull(b.getText()); + } + + public void testFetchBySuperClassFieldName() { + OpenJPAEntityManager em = emf.createEntityManager(); + FetchPlan fp = em.getFetchPlan(); + fp.clearFetchGroups(); + fp.clearFields(); + fp.addField(FetchA.class.getName() + ".b"); + fp.addField(FetchBase.class.getName() + ".text"); + + FetchA a = (FetchA) em.createQuery(JPQL).getSingleResult(); + em.close(); + + FetchB b = a.getB(); + assertNotNull(b); + assertEquals("a1", a.getText()); + assertEquals("b1", b.getText()); + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchA.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchA.java new file mode 100644 index 000000000..a815a6250 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchA.java @@ -0,0 +1,46 @@ +/* + * 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.common.apps; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.OneToOne; + +/** + * A persistent entity to verify behavior of dynamically constructed FetchPlan + * by adding fields. + * + * @author Pinaki Poddar + * + */ +@Entity +@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) +public class FetchA extends FetchBase { + @OneToOne + private FetchB b; + + public FetchB getB() { + return b; + } + + public void setB(FetchB b) { + this.b = b; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchB.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchB.java new file mode 100644 index 000000000..bcbddff7c --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchB.java @@ -0,0 +1,36 @@ +/* + * 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.common.apps; + +import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; + +/** + * A persistent entity to verify behavior of dynamically constructed FetchPlan + * by adding fields. + * + * @author Pinaki Poddar + * + */ +@Entity +@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) +public class FetchB extends FetchBase { + +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchBase.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchBase.java new file mode 100644 index 000000000..9921f7ee0 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/FetchBase.java @@ -0,0 +1,52 @@ +/* + * 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.common.apps; + +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; + +/** + * A base entity to verify behavior of dynamically constructed FetchPlan by + * adding fields. + * + * @author Pinaki Poddar + * + */ + +@MappedSuperclass +public class FetchBase { + @Id + @GeneratedValue + private long id; + + private String text; + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public long getId() { + return id; + } +}