openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mik...@apache.org
Subject svn commit: r960983 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/util/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/
Date Tue, 06 Jul 2010 19:06:15 GMT
Author: mikedd
Date: Tue Jul  6 19:06:14 2010
New Revision: 960983

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

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

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java?rev=960983&r1=960982&r2=960983&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java (original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java Tue Jul
 6 19:06:14 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/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java?rev=960983&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java
Tue Jul  6 19:06:14 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/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java?rev=960983&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java
(added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java
Tue Jul  6 19:06:14 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.AbstractPersistenceTestCase;
+
+public class TestStringId extends AbstractPersistenceTestCase {
+    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/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message