From ab88266f2aa9f0a95baa64f264efeb3ff79665b2 Mon Sep 17 00:00:00 2001 From: Patrick Linskey Date: Wed, 19 Dec 2007 20:19:49 +0000 Subject: [PATCH] OPENJPA-470. Added test case for custom sequence implementations, and updated docs with the current parentheses limitation. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@605679 13f79535-47bb-0310-9956-ffa450edef68 --- .../openjpa/lib/conf/ConfigurationImpl.java | 14 ++++-- .../persistence/generationtype/CustomSeq.java | 47 +++++++++++++++++++ .../generationtype/GeneratedValues.java | 19 ++++++++ .../generationtype/TestGeneratedValues.java | 24 ++++++++++ .../src/doc/manual/ref_guide_runtime.xml | 5 +- 5 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/CustomSeq.java diff --git a/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java b/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java index 4816be519..a64a02262 100644 --- a/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java +++ b/openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/ConfigurationImpl.java @@ -18,7 +18,7 @@ */ package org.apache.openjpa.lib.conf; -import java.awt.Image; +import java.awt.*; import java.beans.BeanDescriptor; import java.beans.BeanInfo; import java.beans.EventSetDescriptor; @@ -40,13 +40,13 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.AccessController; -import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.MissingResourceException; @@ -622,6 +622,14 @@ public class ConfigurationImpl _globals = false; } + // copy the input to avoid mutation issues + if (map instanceof HashMap) + map = (Map) ((HashMap) map).clone(); + else if (map instanceof Properties) + map = (Map) ((Properties) map).clone(); + else + map = new LinkedHashMap(map); + Map remaining = new HashMap(map); boolean ser = true; Value val; @@ -646,7 +654,7 @@ public class ConfigurationImpl // .properties System property; remove that property so we // we don't warn about it Configurations.removeProperty("properties", remaining); - + // now warn if there are any remaining properties that there // is an unhandled prop, and remove the unknown properties Map.Entry entry; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/CustomSeq.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/CustomSeq.java new file mode 100644 index 000000000..3e138bd25 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/CustomSeq.java @@ -0,0 +1,47 @@ +/* + * 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.generationtype; + +import org.apache.openjpa.kernel.Seq; +import org.apache.openjpa.kernel.StoreContext; +import org.apache.openjpa.meta.ClassMetaData; + +public class CustomSeq implements Seq { + + private int i = 1; + + public void setType(int type) { + if (type == Seq.TYPE_TRANSACTIONAL) + throw new UnsupportedOperationException(); + } + + public Object next(StoreContext ctx, ClassMetaData cls) { + return i++; + } + + public Object current(StoreContext ctx, ClassMetaData cls) { + return i; + } + + public void allocate(int additional, StoreContext ctx, ClassMetaData cls) { + } + + public void close() { + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/GeneratedValues.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/GeneratedValues.java index 3311808ed..e8c56da44 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/GeneratedValues.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/GeneratedValues.java @@ -20,7 +20,9 @@ package org.apache.openjpa.persistence.generationtype; import javax.persistence.Entity; import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.SequenceGenerator; @Entity public class GeneratedValues { @@ -31,6 +33,16 @@ public class GeneratedValues { @GeneratedValue private long field; +// @GeneratedValue(strategy= GenerationType.SEQUENCE, +// generator="org.apache.openjpa.persistence.generationtype.CustomSeq") +// private int customSeqField; + + @GeneratedValue(strategy= GenerationType.SEQUENCE, + generator="GeneratedValues.SequenceGeneratorCustomSeq") + @SequenceGenerator(name="GeneratedValues.SequenceGeneratorCustomSeq", + sequenceName="org.apache.openjpa.persistence.generationtype.CustomSeq()") + private int customSeqWithIndirectionField; + public GeneratedValues() { super(); } @@ -57,4 +69,11 @@ public class GeneratedValues { this.field = field; } +// public int getCustomSeqField() { +// return customSeqField; +// } + + public int getCustomSeqWithIndirectionField() { + return customSeqWithIndirectionField; + } } diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestGeneratedValues.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestGeneratedValues.java index a031705f5..837465443 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestGeneratedValues.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/generationtype/TestGeneratedValues.java @@ -102,4 +102,28 @@ public class TestGeneratedValues extends SingleEMFTestCase { // should not get here... fail(); } + +// public void testCustomSequenceGenerator() { +// EntityManager em = emf.createEntityManager(); +// +// GeneratedValues gv = new GeneratedValues(); +// +// em.getTransaction().begin(); +// em.persist(gv); +// em.getTransaction().commit(); +// +// assertNotEquals(0, gv.getCustomSeqField()); +// } + + public void testCustomSequenceGeneratorWithIndirection() { + EntityManager em = emf.createEntityManager(); + + GeneratedValues gv = new GeneratedValues(); + + em.getTransaction().begin(); + em.persist(gv); + em.getTransaction().commit(); + + assertNotEquals(0, gv.getCustomSeqWithIndirectionField()); + } } diff --git a/openjpa-project/src/doc/manual/ref_guide_runtime.xml b/openjpa-project/src/doc/manual/ref_guide_runtime.xml index df2862ff0..62a7c37f6 100644 --- a/openjpa-project/src/doc/manual/ref_guide_runtime.xml +++ b/openjpa-project/src/doc/manual/ref_guide_runtime.xml @@ -1649,7 +1649,10 @@ suitable for single-JVM environments. You can use JPA SequenceGenerators to describe any built-in Seqs or your own Seq implementation. Set the sequenceName attribute to a plugin -string describing your choice. See +string describing your choice. If specifying your own class name, you must +include parentheses at the end of the class name, even if you have no plugin +properties to configure. +(E.g., sequenceName="com.example.SeqImpl()". See in the JPA Overview for details on defining SequenceGenerators.