OPENJPA-1501: trim trailing whitespace for entities with String IDs

git-svn-id: https://svn.apache.org/repos/asf/openjpa/branches/2.0.x@966225 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2010-07-21 13:42:53 +00:00
parent a2d0e05649
commit e34e85396e
3 changed files with 170 additions and 4 deletions

View File

@ -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() {

View File

@ -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 + "']";
}
}

View File

@ -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();
}
}