You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by dw...@apache.org on 2009/09/17 20:45:05 UTC

svn commit: r816331 - in /openjpa/branches/1.3.x: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/ openjpa-project/src/doc/manual/

Author: dwoods
Date: Thu Sep 17 18:45:05 2009
New Revision: 816331

URL: http://svn.apache.org/viewvc?rev=816331&view=rev
Log:
OPENJPA-1213 Additional Compatibility tests (patch #2) showing change in query.setParameter() behaviour.  Contributed by Tim McConnell, but with some package name and OpenJPAVersion checks added.

Added:
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity2.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestDupNamedQuery.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestMethodQLQuery.java   (with props)
Modified:
    openjpa/branches/1.3.x/openjpa-project/src/doc/manual/migration_considerations.xml

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity.java?rev=816331&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity.java Thu Sep 17 18:45:05 2009
@@ -0,0 +1,108 @@
+/*
+ * 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.compat;
+
+import java.io.Serializable;
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.EntityResult;
+import javax.persistence.FieldResult;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedNativeQueries;
+import javax.persistence.NamedNativeQuery;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.SqlResultSetMapping;
+import javax.persistence.Table;
+
+@NamedQuery(name="FindXTwo", query="select s from simple s where s.name = :fname")
+
+@NamedQueries( {
+    @NamedQuery(name="FindOne", query="select s from simple s where s.name = :fname"),
+    @NamedQuery(name="FindOne", query="select s from simple s where s.name = :fname"),
+    @NamedQuery(name="FindAll", query="select s from simple s")
+})
+
+@NamedNativeQueries( { 
+    @NamedNativeQuery(name = "findSimpleEntitites",
+        query = "SELECT ID, NAME, VALUE FROM SIMPLE_ENTITY", 
+        resultSetMapping = "simpleEntitiesResult") })
+
+@SqlResultSetMapping(name = "simpleEntitiesResult",
+    entities = @EntityResult(
+    entityClass = org.apache.openjpa.persistence.query.SimpleEntity.class, 
+    fields = {@FieldResult(name = "id", column = "ID"),
+        @FieldResult(name = "name", column = "NAME"),
+        @FieldResult(name = "value", column = "VALUE") }))
+@Entity(name = "simple")
+@Table(name = "SIMPLE_ENTITY")
+public class SimpleEntity implements Serializable {
+
+    public static final String NAMED_QUERY_WITH_POSITIONAL_PARAMS = "SelectWithPositionalParameter";
+    public static final String NAMED_QUERY_WITH_NAMED_PARAMS = "SelectWithNamedParameter";
+
+    @Id
+    @GeneratedValue
+    @Column(name = "ID")
+    private long id;
+
+    @Basic
+    @Column(name = "NAME")
+    private String name;
+
+    @Basic
+    @Column(name = "VALUE")
+    private String value;
+
+    public SimpleEntity() {
+    }
+
+    public SimpleEntity(String name, String value) {
+        this();
+        this.name = name;
+        this.value = value;
+    }
+
+    public long getId() {
+        return this.id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity2.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity2.java?rev=816331&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity2.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity2.java Thu Sep 17 18:45:05 2009
@@ -0,0 +1,88 @@
+/*
+ * 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.compat;
+
+import javax.persistence.Basic;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
+import javax.persistence.Table;
+
+@NamedQuery(name="FindXTwo", query="select s from simple2 s where s.name = :fname")
+
+@NamedQueries( {
+    @NamedQuery(name="FindOne", query="select s from simple2 s where s.name = :fname"),
+    @NamedQuery(name="Find2One", query="select s from simple2 s where s.name = :fname"),
+    @NamedQuery(name="Find2All", query="select s from simple2 s")
+})
+
+@Entity(name = "simple2")
+@Table(name = "SIMPLE_ENTITY2")
+public class SimpleEntity2 {
+
+    @Id
+    @GeneratedValue
+    @Column(name = "ID")
+    private long id;
+
+    @Basic
+    @Column(name = "NAME")
+    private String name;
+
+    @Basic
+    @Column(name = "VALUE")
+    private String value;
+
+    public SimpleEntity2() {
+    }
+
+    public SimpleEntity2(String name, String value) {
+        this();
+        this.name = name;
+        this.value = value;
+    }
+
+    public long getId() {
+        return this.id;
+    }
+
+    public void setId(long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getValue() {
+        return value;
+    }
+
+    public void setValue(String value) {
+        this.value = value;
+    }
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/SimpleEntity2.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestDupNamedQuery.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestDupNamedQuery.java?rev=816331&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestDupNamedQuery.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestDupNamedQuery.java Thu Sep 17 18:45:05 2009
@@ -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.compat;
+
+import java.util.Iterator;
+import java.util.List;
+import javax.persistence.EntityManager;
+
+import junit.textui.TestRunner;
+import org.apache.openjpa.conf.OpenJPAVersion;
+import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+/**
+ * <b>Compatible testcases</b> are used to test various backwards compatibility scenarios between JPA 2.0 and JPA 1.2
+ * 
+ * <p>The following scenarios are tested:
+ * <ol>
+ * <li>query.setParameter()
+ * </ol>
+ * <p> 
+ * <b>Note(s):</b>
+ * <ul>
+ * <li>The proper openjpa.Compatibility value(s) must be provided in order for the testcase(s) to succeed
+ * </ul>
+ */
+public class TestDupNamedQuery extends SingleEMFTestCase {
+
+    public void setUp() {
+        setUp(SimpleEntity.class, SimpleEntity2.class, CLEAR_TABLES);
+
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(new SimpleEntity("Name One", "Value One"));
+        em.persist(new SimpleEntity("Name Two", "Value Two"));
+        em.persist(new SimpleEntity2("Name2 One", "Value2 One"));
+        em.persist(new SimpleEntity2("Name2 Two", "Value2 Two"));
+        em.getTransaction().commit();
+        em.close();
+    }
+
+    public void testSimpleQuery() {
+        simpleQuery(false, "FindOne", "FindAll", "Name One", "Value One",
+            "Name Two", "Value Two");
+        simpleQuery(true, "Find2One", "Find2All", "Name2 One", "Value2 One",
+            "Name2 Two", "Value2 Two");
+
+        simpleQuery(false, "FindXTwo", null, "Name Two", "Value Two", null,
+            null);
+    }
+
+    private void simpleQuery(boolean simple2, String findOneQName,
+        String findAllQName, String nameOne, String ValueOne, String nameTwo,
+        String ValueTwo) {
+        EntityManager em = emf.createEntityManager();
+
+        try {
+            List list = em.createNamedQuery(findOneQName).setParameter(1, nameOne).getResultList();
+            assertNotNull(list);
+            assertEquals(list.size(), 1);
+            Object o = list.get(0);
+            assertSame(o.getClass(), simple2 ? SimpleEntity2.class : SimpleEntity.class);
+            assertEquals(simple2 ? ((SimpleEntity2) o).getValue()
+                : ((SimpleEntity) o).getValue(), ValueOne);
+
+            if (findAllQName != null) {
+                list = em.createNamedQuery(findAllQName).getResultList();
+                assertNotNull(list);
+                assertEquals(list.size(), 2);
+                for (Iterator resultIter = list.iterator(); resultIter.hasNext();) {
+                    o = resultIter.next();
+                    assertSame(o.getClass(), simple2 ? SimpleEntity2.class
+                        : SimpleEntity.class);
+                    String n = null;
+                    String v = null;
+                    if (simple2) {
+                        n = ((SimpleEntity2) o).getName();
+                        v = ((SimpleEntity2) o).getValue();
+                    } else {
+                        n = ((SimpleEntity) o).getName();
+                        v = ((SimpleEntity) o).getValue();
+                    }
+                    if (n.equals(nameOne)) {
+                        assertTrue(v.equals(ValueOne));
+                    } else if (n.equals(nameTwo)) {
+                        assertTrue(v.equals(ValueTwo));
+                    } else {
+                        assertTrue(false);
+                    }
+                }
+            }
+        } catch (ArgumentException ae) {
+            if ((OpenJPAVersion.MAJOR_RELEASE == 1) &&
+                 (OpenJPAVersion.MINOR_RELEASE >= 3)) {
+                // new behavior - expected exception
+            } else {
+                // unexpected exception
+               throw ae;
+            }
+        } catch (IllegalArgumentException iae) {
+            if (OpenJPAVersion.MAJOR_RELEASE >= 2) {
+                // expected exception for new behavior
+            } else {
+                // unexpected exception
+                throw iae;
+            }
+        } finally {
+            em.close();
+        }
+    }
+
+    public static void main(String[] args) {
+        TestRunner.run(TestDupNamedQuery.class);
+    }
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestDupNamedQuery.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestMethodQLQuery.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestMethodQLQuery.java?rev=816331&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestMethodQLQuery.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestMethodQLQuery.java Thu Sep 17 18:45:05 2009
@@ -0,0 +1,97 @@
+/*
+ * 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.compat;
+
+import java.util.Map;
+import java.util.ArrayList;
+
+import org.apache.openjpa.conf.OpenJPAVersion;
+import org.apache.openjpa.kernel.QueryLanguages;
+import org.apache.openjpa.kernel.StoreContext;
+import org.apache.openjpa.kernel.FetchConfiguration;
+import org.apache.openjpa.lib.rop.ResultObjectProvider;
+import org.apache.openjpa.lib.rop.ListResultObjectProvider;
+import org.apache.openjpa.meta.ClassMetaData;
+import org.apache.openjpa.persistence.ArgumentException;
+import org.apache.openjpa.persistence.OpenJPAQuery;
+import org.apache.openjpa.persistence.QueryImpl;
+import org.apache.openjpa.persistence.test.SingleEMTestCase;
+
+/**
+ * <b>Compatible testcases</b> are used to test various backwards compatibility scenarios between JPA 2.0 and JPA 1.2
+ * 
+ * <p>The following scenarios are tested:
+ * <ol>
+ * <li>query.setParameter()
+ * </ol>
+ * <p> 
+ * <b>Note(s):</b>
+ * <ul>
+ * <li>The proper openjpa.Compatibility value(s) must be provided in order for the testcase(s) to succeed
+ * </ul>
+ */
+public class TestMethodQLQuery extends SingleEMTestCase {
+
+    @Override
+    public void setUp() {
+        setUp(SimpleEntity.class);
+    }
+
+    public void testMethodQLWithoutParametersDeclared() {
+        try {
+            OpenJPAQuery q = em.createQuery(QueryLanguages.LANG_METHODQL,
+                getClass().getName() + ".echo");
+            ((QueryImpl) q).getDelegate().setCandidateType(
+                SimpleEntity.class, true);
+            q.setParameter("param", 5);
+            ((QueryImpl) q).getDelegate().declareParameters("Integer param");
+            assertEquals(5, q.getResultList().get(0));
+        } catch (ArgumentException ae) {
+            if ((OpenJPAVersion.MAJOR_RELEASE == 1) &&
+                 (OpenJPAVersion.MINOR_RELEASE >= 3)) {
+                // new behavior - expected exception
+            } else {
+                // unexpected exception
+               throw ae;
+            }
+        } catch (IllegalArgumentException iae) {
+            if (OpenJPAVersion.MAJOR_RELEASE >= 2) {
+                // expected exception for new behavior
+            } else {
+                // unexpected exception
+                throw iae;
+            }
+        }
+    } 
+
+    public static ResultObjectProvider echo(StoreContext ctx,
+        ClassMetaData meta, boolean subs, Map params, FetchConfiguration conf) {
+        return new ListResultObjectProvider(new ArrayList(params.values()));
+    }
+
+    public static void voidMeth(StoreContext ctx,
+        ClassMetaData meta, boolean subs, Map params, FetchConfiguration conf) {
+    }
+
+    public static Object invalidReturnMeth(StoreContext ctx,
+        ClassMetaData meta, boolean subs, Map params, FetchConfiguration conf) {
+        return null;
+    }
+}
+

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/compat/TestMethodQLQuery.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/branches/1.3.x/openjpa-project/src/doc/manual/migration_considerations.xml
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-project/src/doc/manual/migration_considerations.xml?rev=816331&r1=816330&r2=816331&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-project/src/doc/manual/migration_considerations.xml (original)
+++ openjpa/branches/1.3.x/openjpa-project/src/doc/manual/migration_considerations.xml Thu Sep 17 18:45:05 2009
@@ -39,7 +39,8 @@
                     The Query interface setParameters() method behavior has
                     changed to throw an ArgumentException if more
                     parameter substitutions are supplied than defined in the
-                    createQuery() call, as required by the JPA2 specification.
+                    createQuery() or createNamedQuery() call, as required by
+                    the JPA2 specification.
                     OpenJPA 1.2.x and prior versions would silently ignore the
                     supplied parameter substitutions and allow the Query to be
                     processed.