From 74683912e3c0de85f4c003e132af6a50a42511ba Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Sat, 17 Nov 2007 05:28:58 +0000 Subject: [PATCH] New test case for enums and JPQL (including some disabled failing tests related to OPENJPA-200); simplification to JDBCStoreManager. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@595910 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/jdbc/kernel/JDBCStoreQuery.java | 12 +-- .../persistence/fields/TestEnumsInJPQL.java | 95 +++++++++++++++++++ 2 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fields/TestEnumsInJPQL.java diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java index b21290ff4..50bb22ad0 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java @@ -28,7 +28,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Iterator; import org.apache.openjpa.event.LifecycleEventManager; import org.apache.openjpa.jdbc.kernel.exps.ExpContext; @@ -63,7 +62,6 @@ import org.apache.openjpa.lib.rop.ResultObjectProvider; import org.apache.openjpa.lib.util.Localizer; import org.apache.openjpa.meta.ClassMetaData; import org.apache.openjpa.meta.ValueMetaData; -import org.apache.openjpa.meta.FieldMetaData; import org.apache.openjpa.util.UserException; import serp.util.Numbers; @@ -422,20 +420,20 @@ public class JDBCStoreQuery protected Number executeDelete(Executor ex, ClassMetaData base, ClassMetaData[] metas, boolean subclasses, ExpressionFactory[] facts, QueryExpressions[] exps, Object[] params) { - return executeBulkOperation(ex, base, metas, subclasses, facts, exps, + return executeBulkOperation(metas, subclasses, facts, exps, params, null); } protected Number executeUpdate(Executor ex, ClassMetaData base, ClassMetaData[] metas, boolean subclasses, ExpressionFactory[] facts, QueryExpressions[] exps, Object[] params) { - return executeBulkOperation(ex, base, metas, subclasses, facts, exps, + return executeBulkOperation(metas, subclasses, facts, exps, params, exps[0].updates); } - private Number executeBulkOperation(Executor ex, ClassMetaData base, - ClassMetaData[] metas, boolean subclasses, ExpressionFactory[] facts, - QueryExpressions[] exps, Object[] params, Map updates) { + private Number executeBulkOperation(ClassMetaData[] metas, + boolean subclasses, ExpressionFactory[] facts, QueryExpressions[] exps, + Object[] params, Map updates) { // we cannot execute a bulk delete statement when have mappings in // multiple tables, so indicate we want to use in-memory with null ClassMapping[] mappings = (ClassMapping[]) metas; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fields/TestEnumsInJPQL.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fields/TestEnumsInJPQL.java new file mode 100644 index 000000000..26858b5cb --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/fields/TestEnumsInJPQL.java @@ -0,0 +1,95 @@ +/* + * 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.fields; + +import javax.persistence.Query; + +import org.apache.openjpa.persistence.test.SingleEMTestCase; + +public class TestEnumsInJPQL + extends SingleEMTestCase { + + public void setUp() { + setUp(EnumFieldType.class, CLEAR_TABLES); + + EnumFieldType o = new EnumFieldType(); + o.setEnumField(SampleEnum.BAR); + em.getTransaction().begin(); + em.persist(o); + em.getTransaction().commit(); + em.close(); + em = emf.createEntityManager(); + } + + public void testEnumLiteralInSelect() { + Query q = em.createQuery("select count(o) from EnumFieldType o where " + + "o.enumField = org.apache.openjpa.persistence.fields.SampleEnum.BAR"); + assertEquals(1, ((Number) q.getSingleResult()).intValue()); + } + + // fails currently + public void xtestEnumLiteralInUpdate() { + testEnumLiteralInSelect(); + em.getTransaction().begin(); + Query q = em.createQuery("update EnumFieldType o set " + + "o.enumField = org.apache.openjpa.persistence.fields.SampleEnum.BAZ"); + assertEquals(1, ((Number) q.executeUpdate()).intValue()); + em.getTransaction().commit(); + postUpdateCheck(); + } + + private void postUpdateCheck() { + Query q = em.createQuery("select count(o) from EnumFieldType o where " + + "o.enumField = org.apache.openjpa.persistence.fields.SampleEnum.BAR"); + assertEquals(0, ((Number) q.getSingleResult()).intValue()); + + q = em.createQuery("select count(o) from EnumFieldType o where " + + "o.enumField = org.apache.openjpa.persistence.fields.SampleEnum.BAZ"); + assertEquals(1, ((Number) q.getSingleResult()).intValue()); + } + + public void testEnumPositionalParamInSelect() { + Query q = em.createQuery("select count(o) from EnumFieldType o where " + + "o.enumField = ?1"); + q.setParameter(1, SampleEnum.BAR); + assertEquals(1, ((Number) q.getSingleResult()).intValue()); + } + + public void testEnumNamedParamInSelect() { + Query q = em.createQuery("select count(o) from EnumFieldType o where " + + "o.enumField = :e"); + q.setParameter("e", SampleEnum.BAR); + assertEquals(1, ((Number) q.getSingleResult()).intValue()); + } + + // fails currently + public void xtestEnumParamInUpdate() { + testEnumLiteralInSelect(); + em.getTransaction().begin(); + Query q = em.createQuery("update EnumFieldType o set o.enumField = :e"); + q.setParameter("e", SampleEnum.BAZ); + assertEquals(1, ((Number) q.executeUpdate()).intValue()); + em.getTransaction().commit(); + postUpdateCheck(); + } + + public void xtestInMemory() { + fail("unimplemented"); + } +} \ No newline at end of file