From d91ed8942c987e4629caa0cebed72e90d6bf85c2 Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Thu, 22 Aug 2013 15:35:52 -0400 Subject: [PATCH] HHH-4699 test case --- .../annotations/enumerated/EntityEnum.java | 19 ++++++++ .../enumerated/EnumeratedTypeTest.java | 45 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EntityEnum.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EntityEnum.java index 8016c10abc..a7c7fce24d 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EntityEnum.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EntityEnum.java @@ -1,16 +1,19 @@ package org.hibernate.test.annotations.enumerated; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.GeneratedValue; import javax.persistence.Id; + import org.hibernate.annotations.Type; import org.hibernate.annotations.TypeDef; import org.hibernate.annotations.TypeDefs; /** * @author Janario Oliveira + * @author Brett Meyer */ @Entity @TypeDefs({ @TypeDef(typeClass = LastNumberType.class, defaultForType = EntityEnum.LastNumber.class) }) @@ -31,6 +34,11 @@ public class EntityEnum { NUMBER_1, NUMBER_2, NUMBER_3 } + enum Trimmed { + + A, B, C + } + @Id @GeneratedValue private long id; @@ -42,6 +50,9 @@ public class EntityEnum { private LastNumber lastNumber; @Enumerated(EnumType.STRING) private LastNumber explicitOverridingImplicit; + @Column(columnDefinition = "char(5)") + @Enumerated(EnumType.STRING) + private Trimmed trimmed; public long getId() { return id; @@ -90,4 +101,12 @@ public class EntityEnum { public void setExplicitOverridingImplicit(LastNumber explicitOverridingImplicit) { this.explicitOverridingImplicit = explicitOverridingImplicit; } + + public Trimmed getTrimmed() { + return trimmed; + } + + public void setTrimmed(Trimmed trimmed) { + this.trimmed = trimmed; + } } diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EnumeratedTypeTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EnumeratedTypeTest.java index 10e2071604..d2d9cf62be 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EnumeratedTypeTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/enumerated/EnumeratedTypeTest.java @@ -1,16 +1,28 @@ package org.hibernate.test.annotations.enumerated; +import static org.junit.Assert.assertEquals; + import java.io.Serializable; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.List; + +import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.cfg.Configuration; import org.hibernate.criterion.Restrictions; +import org.hibernate.engine.spi.SessionImplementor; import org.hibernate.mapping.PersistentClass; +import org.hibernate.test.annotations.enumerated.EntityEnum.Common; +import org.hibernate.test.annotations.enumerated.EntityEnum.FirstLetter; +import org.hibernate.test.annotations.enumerated.EntityEnum.LastNumber; +import org.hibernate.test.annotations.enumerated.EntityEnum.Trimmed; +import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.type.EnumType; import org.hibernate.type.Type; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.hibernate.test.annotations.enumerated.EntityEnum.*; /** * Test type definition for enum @@ -327,6 +339,35 @@ public class EnumeratedTypeTest extends BaseCoreFunctionalTestCase { session.close(); } + + @Test + @TestForIssue(jiraKey = "HHH-4699") + public void testTrimmedEnum() throws SQLException { + // use native SQL to insert, forcing whitespace to occur + final Session s = openSession(); + final Connection connection = ((SessionImplementor)s).connection(); + final Statement statement = connection.createStatement(); + statement.execute("insert into EntityEnum (id, trimmed) values(1, '" + Trimmed.A.name() + "')"); + statement.execute("insert into EntityEnum (id, trimmed) values(2, '" + Trimmed.B.name() + "')"); + + s.getTransaction().begin(); + + // ensure EnumType can do #fromName with the trimming + List resultList = s.createQuery("select e from EntityEnum e").list(); + assertEquals( resultList.size(), 2 ); + assertEquals( resultList.get(0).getTrimmed(), Trimmed.A ); + assertEquals( resultList.get(1).getTrimmed(), Trimmed.B ); + + // ensure querying works + final Query query = s.createQuery("select e from EntityEnum e where e.trimmed=?"); + query.setParameter( 0, Trimmed.A ); + resultList = query.list(); + assertEquals( resultList.size(), 1 ); + assertEquals( resultList.get(0).getTrimmed(), Trimmed.A ); + + s.getTransaction().rollback(); + s.close(); + } @Override protected Class[] getAnnotatedClasses() {