openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strub...@apache.org
Subject svn commit: r1689594 - /openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/
Date Tue, 07 Jul 2015 08:10:56 GMT
Author: struberg
Date: Tue Jul  7 08:10:56 2015
New Revision: 1689594

URL: http://svn.apache.org/r1689594
Log:
OPENJPA-2341 add unit test for Strategy handling

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ImplicitValueStrategyEntity.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/PrincipalValueStrategyHandler.java
  (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestImplicitValueStrategy.java
  (with props)
Modified:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestValueStrategy.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ValueStrategyEntity.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ImplicitValueStrategyEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ImplicitValueStrategyEntity.java?rev=1689594&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ImplicitValueStrategyEntity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ImplicitValueStrategyEntity.java
Tue Jul  7 08:10:56 2015
@@ -0,0 +1,57 @@
+/*
+ * 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.strategy.value;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+
+/**
+ * Similar to {@link ValueStrategyEntity} but this time
+ * we do not annotate any @Strategy but we give it via the
+ * configured MappingDefaults FieldStrategies.
+ */
+@Entity
+public class ImplicitValueStrategyEntity {
+    @Id
+    private String id;
+
+    /**
+     * This field gets AUTOMATICALLY converted by OpenJPA because we registered
+     * the {@link PrincipalValueStrategyHandler} in persistence.xml.
+     * Currently this needs either to be Serializable, so we cannot store Principal but only
the impl :(
+     */
+    private PrincipalValueStrategyHandler.TestPrincipal user;
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public PrincipalValueStrategyHandler.TestPrincipal getUser() {
+        return user;
+    }
+
+    public void setUser(PrincipalValueStrategyHandler.TestPrincipal user) {
+        this.user = user;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ImplicitValueStrategyEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/PrincipalValueStrategyHandler.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/PrincipalValueStrategyHandler.java?rev=1689594&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/PrincipalValueStrategyHandler.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/PrincipalValueStrategyHandler.java
Tue Jul  7 08:10:56 2015
@@ -0,0 +1,87 @@
+/*
+ * 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.strategy.value;
+
+import java.io.Serializable;
+import java.security.Principal;
+
+import org.apache.openjpa.jdbc.kernel.JDBCStore;
+import org.apache.openjpa.jdbc.meta.ValueMapping;
+import org.apache.openjpa.jdbc.meta.strats.AbstractValueHandler;
+import org.apache.openjpa.jdbc.schema.Column;
+import org.apache.openjpa.jdbc.schema.ColumnIO;
+import org.apache.openjpa.meta.JavaTypes;
+
+/**
+ * A ValueHandler which handles a Principal and stores it as String representation.
+ * I use Principal because this class exists in every JDK. A more practical example
+ * could be joda LocalDate or the new Java8 Date classes.
+ */
+public class PrincipalValueStrategyHandler extends AbstractValueHandler {
+
+    private static final long serialVersionUID = 8371304701543038775L;
+
+    private static final PrincipalValueStrategyHandler _instance = new PrincipalValueStrategyHandler();
+
+    public static PrincipalValueStrategyHandler getInstance() {
+        return _instance;
+    }
+
+    @Override
+    public Column[] map(ValueMapping arg0, String name, ColumnIO arg2,
+                        boolean arg3) {
+
+        Column col = new Column();
+        col.setName(name);
+        col.setJavaType(JavaTypes.STRING);
+
+        return new Column[]{col};
+    }
+
+    public Object toDataStoreValue(ValueMapping vm, Object val, JDBCStore store) {
+
+        if (val instanceof Principal) {
+            return ((Principal) val).getName();
+        }
+
+        return null;
+    }
+
+    public Object toObjectValue(ValueMapping vm, final Object val) {
+        if (val == null || !(val instanceof String)) {
+            return null;
+        }
+
+        return new TestPrincipal((String) val);
+    }
+
+
+    public static class TestPrincipal implements Principal, Serializable {
+        private final String name;
+
+        public TestPrincipal(String name) {
+            this.name = name;
+        }
+
+        @Override
+        public String getName() {
+            return name;
+        }
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/PrincipalValueStrategyHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestImplicitValueStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestImplicitValueStrategy.java?rev=1689594&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestImplicitValueStrategy.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestImplicitValueStrategy.java
Tue Jul  7 08:10:56 2015
@@ -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.strategy.value;
+
+import java.security.Principal;
+
+import javax.persistence.EntityManager;
+
+import org.apache.openjpa.persistence.test.SQLListenerTestCase;
+
+public class TestImplicitValueStrategy extends SQLListenerTestCase {
+    public void setUp(){
+        setUp(ImplicitValueStrategyEntity.class, DROP_TABLES,
+                "openjpa.jdbc.MappingDefaults",
+                "ForeignKeyDeleteAction=restrict, JoinForeignKeyDeleteAction=restrict, "
+
+                    "FieldStrategies='java.security.Principal=" +
+                    "org.apache.openjpa.persistence.strategy.value.PrincipalValueStrategyHandler'",
+                "openjpa.RuntimeUnenhancedClasses", "supported"
+                );
+        assertNotNull(emf);
+
+        create();
+    }
+
+    public void testIt() {
+        EntityManager em = emf.createEntityManager();
+        ImplicitValueStrategyEntity se = em.find(ImplicitValueStrategyEntity.class, "id1");
+        assertNotNull(se);
+        assertNotNull(se.getUser());
+        assertEquals("name1", se.getUser().getName());
+
+        em.close();
+    }
+
+    private void create() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        ImplicitValueStrategyEntity stratEnt = new ImplicitValueStrategyEntity();
+        stratEnt.setId("id1");
+        PrincipalValueStrategyHandler.TestPrincipal user = new PrincipalValueStrategyHandler.TestPrincipal("name1");
+        stratEnt.setUser(user);
+
+        em.persist(stratEnt);
+
+        em.getTransaction().commit();
+        em.close();
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestImplicitValueStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestValueStrategy.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestValueStrategy.java?rev=1689594&r1=1689593&r2=1689594&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestValueStrategy.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/TestValueStrategy.java
Tue Jul  7 08:10:56 2015
@@ -24,31 +24,36 @@ import org.apache.openjpa.persistence.te
 
 public class TestValueStrategy extends SQLListenerTestCase {
     public void setUp(){
-      setUp(ValueStrategyEntity.class, DROP_TABLES);
-      assertNotNull(emf);
-      
-      create();
-}
+        setUp(ValueStrategyEntity.class, DROP_TABLES,
+                "openjpa.RuntimeUnenhancedClasses", "supported");
+        assertNotNull(emf);
+
+        create();
+    }
     
     public void testIt() {
         EntityManager em = emf.createEntityManager();
         ValueStrategyEntity se = em.find(ValueStrategyEntity.class, "id1");
         assertNotNull(se);
-        
+        assertEquals("name1", se.getName());
+        assertNotNull(se.getUser());
+        assertEquals("name1", se.getUser().getName());
+
         em.close();
     }
 
-        private void create() {
-            EntityManager em = emf.createEntityManager();
-            em.getTransaction().begin();
-            
-            ValueStrategyEntity stratEnt = new ValueStrategyEntity();
-            stratEnt.setId("id1");
-            stratEnt.setName("name1");
-            
-            em.persist(stratEnt);
-            
-            em.getTransaction().commit();
-            em.close();
-        }
+    private void create() {
+        EntityManager em = emf.createEntityManager();
+        em.getTransaction().begin();
+
+        ValueStrategyEntity stratEnt = new ValueStrategyEntity();
+        stratEnt.setId("id1");
+        stratEnt.setName("name1");
+        stratEnt.setUser(new PrincipalValueStrategyHandler.TestPrincipal("name1"));
+
+        em.persist(stratEnt);
+
+        em.getTransaction().commit();
+        em.close();
+    }
 }

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ValueStrategyEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ValueStrategyEntity.java?rev=1689594&r1=1689593&r2=1689594&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ValueStrategyEntity.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/strategy/value/ValueStrategyEntity.java
Tue Jul  7 08:10:56 2015
@@ -32,6 +32,14 @@ public class ValueStrategyEntity {
     @Strategy ("org.apache.openjpa.persistence.strategy.value.ValueStrategyHandler")
     private String name;
 
+    /**
+     * This field gets AUTOMATICALLY converted by OpenJPA because we registered
+     * the {@link PrincipalValueStrategyHandler} in persistence.xml.
+     * Currently this needs either to be Serializable, so we cannot store Principal but only
the impl :(
+     */
+    @Strategy("org.apache.openjpa.persistence.strategy.value.PrincipalValueStrategyHandler")
+    private PrincipalValueStrategyHandler.TestPrincipal user;
+
     public String getId() {
         return id;
     }
@@ -48,4 +56,11 @@ public class ValueStrategyEntity {
         this.name = name;
     }
 
+    public PrincipalValueStrategyHandler.TestPrincipal getUser() {
+        return user;
+    }
+
+    public void setUser(PrincipalValueStrategyHandler.TestPrincipal user) {
+        this.user = user;
+    }
 }



Mime
View raw message