diff --git a/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java b/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java index 2d80f5d4b..d5148a0fb 100644 --- a/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java +++ b/openjpa-kernel/src/main/java/org/apache/openjpa/util/StringId.java @@ -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 OpenJPAId { 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() { diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java new file mode 100644 index 000000000..625459db9 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/StringIdEntity.java @@ -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 + "']"; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java new file mode 100644 index 000000000..6ab295b1f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/identity/TestStringId.java @@ -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(); + } +}