You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2010/07/21 23:49:43 UTC

svn commit: r966432 - in /openjpa/branches/1.3.x: openjpa-kernel/src/main/java/org/apache/openjpa/util/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/

Author: mikedd
Date: Wed Jul 21 21:49:43 2010
New Revision: 966432

URL: http://svn.apache.org/viewvc?rev=966432&view=rev
Log:
OPENJPA-1501: trim trailing whitespace for entities with String IDs

Added:
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java   (with props)
Modified:
    openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java

Modified: openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java?rev=966432&r1=966431&r2=966432&view=diff
==============================================================================
--- openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java (original)
+++ openjpa/branches/1.3.x/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java Wed Jul 21 21:49:43 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.util;
 
+import org.apache.commons.lang.StringUtils;
+
 /**
  * {@link OpenJPAId} subclass appropriate for String fields.
  *
@@ -27,14 +29,14 @@ public final class StringId extends Open
 
     private final String key;
 
-    public StringId(Class cls, String key) {
+    public StringId(Class<?> cls, String key) {
         super(cls);
-        this.key = (key == null) ? "" : key;
+        this.key = (key == null) ? "" : StringUtils.stripEnd(key, null);
     }
 
-    public StringId(Class cls, String key, boolean subs) {
+    public StringId(Class<?> cls, String key, boolean subs) {
         super(cls, subs);
-        this.key = (key == null) ? "" : key;
+        this.key = (key == null) ? "" : StringUtils.stripEnd(key, null);
     }
 
     public String getId() {

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java?rev=966432&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java Wed Jul 21 21:49:43 2010
@@ -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.identity;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+public class StringIdEntity {
+    @Id
+    @Column(length = 12)
+    private String id;
+    private int data;
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public int getData() {
+        return data;
+    }
+
+    public void setData(int data) {
+        this.data = data;
+    }
+
+    @Override
+    public String toString() {
+        return "StringIdEntity [data=" + data + ", id='" + id + "']";
+    }
+}

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

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java?rev=966432&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java Wed Jul 21 21:49:43 2010
@@ -0,0 +1,112 @@
+/*
+ * 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.identity;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.openjpa.persistence.test.PersistenceTestCase;
+
+public class TestStringId extends PersistenceTestCase {
+    private static EntityManagerFactory _emf;
+
+    public void setUp() {
+        _emf = createEMF(StringIdEntity.class);
+
+        cleanup();
+    }
+
+    public void tearDown() {
+        _emf.close();
+    }
+
+    private void cleanup() {
+        EntityManager em = _emf.createEntityManager();
+        em.getTransaction().begin();
+        em.createQuery("Delete from StringIdEntity").executeUpdate();
+        em.getTransaction().commit();
+        em.close();
+    }
+
+    public void testTrailingWhitespace() {
+        StringIdEntity sie1 = new StringIdEntity();
+        sie1.setId("ABC ");
+
+        EntityManager em = _emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(sie1);
+        em.getTransaction().commit();
+        assertTrue(em.contains(sie1));
+
+        StringIdEntity sie2 = em.find(StringIdEntity.class, "ABC");
+        assertSame("Find should return the same instance with trailing whitespace", sie1, sie2);
+
+        StringIdEntity sie3 = em.find(StringIdEntity.class, "ABC  ");
+        assertSame("Find should return the same instance with trailing whitespace", sie1, sie3);
+
+        assertNotSame("Leading WS should not match", sie1, em.find(StringIdEntity.class, " ABC"));
+
+        em.close();
+    }
+
+    public void testLeadingWhitespace() {
+        StringIdEntity sie1 = new StringIdEntity();
+        sie1.setId(" ABC");
+
+        EntityManager em = _emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(sie1);
+        em.getTransaction().commit();
+        assertTrue(em.contains(sie1));
+
+        StringIdEntity sie2 = em.find(StringIdEntity.class, "ABC");
+        assertNotSame("Find should not return the same instance with leading whitespace", sie1, sie2);
+
+        StringIdEntity sie3 = em.find(StringIdEntity.class, "   ABC");
+        assertNotSame("Find should not return the same instance with leading whitespace", sie1, sie3);
+
+        assertSame(sie1, em.find(StringIdEntity.class, " ABC"));
+
+        assertNotSame("Trailing WS should not match", sie1, em.find(StringIdEntity.class, "ABC "));
+        em.close();
+    }
+
+    public void testInnerWhitespace() {
+        StringIdEntity sie1 = new StringIdEntity();
+        sie1.setId("A B C");
+
+        EntityManager em = _emf.createEntityManager();
+        em.getTransaction().begin();
+        em.persist(sie1);
+        em.getTransaction().commit();
+        assertTrue(em.contains(sie1));
+
+        StringIdEntity sie2 = em.find(StringIdEntity.class, "ABC");
+        assertNotSame("Find should not return the same instance with inner whitespace", sie1, sie2);
+
+        StringIdEntity sie3 = em.find(StringIdEntity.class, "   ABC");
+        assertNotSame("Find should not return the same instance with inner whitespace", sie1, sie3);
+
+        assertSame(sie1, em.find(StringIdEntity.class, "A B C"));
+
+        assertNotSame("Trailing WS should not match", sie1, em.find(StringIdEntity.class, "ABC "));
+
+        em.close();
+    }
+}

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