From 15d20113e75ed7d4895af9df4730ea9a69377faf Mon Sep 17 00:00:00 2001 From: Gavin Date: Fri, 28 Apr 2023 15:29:15 +0200 Subject: [PATCH] HHH-16125 make tests work with postgres enums --- .../orm/test/annotations/collectionelement/Boy.java | 6 +++--- .../{Character.java => CharacterTrait.java} | 2 +- .../DefaultNamingCollectionElementTest.java | 12 ++++++------ .../hibernate/orm/test/query/hql/FunctionTests.java | 11 +++++++++-- .../orm/test/query/hql/StandardFunctionTests.java | 11 +++++++++-- 5 files changed, 28 insertions(+), 14 deletions(-) rename hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/{Character.java => CharacterTrait.java} (93%) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Boy.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Boy.java index 9bf1c47021..cc652ad1ca 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Boy.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Boy.java @@ -48,7 +48,7 @@ public class Boy { private Map scorePerPreferredName = new HashMap<>(); private int[] favoriteNumbers; private Set favoriteToys = new HashSet<>(); - private Set characters = new HashSet<>(); + private Set characters = new HashSet<>(); private Map foods = new HashMap<>(); private Set countryAttitudes = new HashSet<>(); @@ -155,11 +155,11 @@ public class Boy { @ElementCollection @Enumerated(EnumType.STRING) @Column(name = "`characters`") - public Set getCharacters() { + public Set getCharacters() { return characters; } - public void setCharacters(Set characters) { + public void setCharacters(Set characters) { this.characters = characters; } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Character.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/CharacterTrait.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Character.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/CharacterTrait.java index 4cd9c317ce..17aa9dc962 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/Character.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/CharacterTrait.java @@ -10,7 +10,7 @@ package org.hibernate.orm.test.annotations.collectionelement; /** * @author Emmanuel Bernard */ -public enum Character { +public enum CharacterTrait { GENTLE, NORMAL, AGGRESSIVE, diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/DefaultNamingCollectionElementTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/DefaultNamingCollectionElementTest.java index 0dfb7b7db7..864cc67d04 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/DefaultNamingCollectionElementTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/collectionelement/DefaultNamingCollectionElementTest.java @@ -80,8 +80,8 @@ public class DefaultNamingCollectionElementTest { favNbrs[index] = index * 3; } boy.setFavoriteNumbers( favNbrs ); - boy.getCharacters().add( Character.GENTLE ); - boy.getCharacters().add( Character.CRAFTY ); + boy.getCharacters().add( CharacterTrait.GENTLE ); + boy.getCharacters().add( CharacterTrait.CRAFTY ); HashMap foods = new HashMap<>(); foods.put( "breakfast", FavoriteFood.PIZZA ); @@ -103,7 +103,7 @@ public class DefaultNamingCollectionElementTest { assertEquals( Integer.valueOf( 5 ), boy.getScorePerNickName().get( "Thing" ) ); assertNotNull( boy.getFavoriteNumbers() ); assertEquals( 3, boy.getFavoriteNumbers()[1] ); - assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); + assertTrue( boy.getCharacters().contains( CharacterTrait.CRAFTY ) ); assertTrue( boy.getFavoriteFood().get( "dinner" ).equals( FavoriteFood.SUSHI ) ); assertTrue( boy.getFavoriteFood().get( "lunch" ).equals( FavoriteFood.KUNGPAOCHICKEN ) ); assertTrue( boy.getFavoriteFood().get( "breakfast" ).equals( FavoriteFood.PIZZA ) ); @@ -199,8 +199,8 @@ public class DefaultNamingCollectionElementTest { favNbrs[index] = index * 3; } boy.setFavoriteNumbers( favNbrs ); - boy.getCharacters().add( Character.GENTLE ); - boy.getCharacters().add( Character.CRAFTY ); + boy.getCharacters().add( CharacterTrait.GENTLE ); + boy.getCharacters().add( CharacterTrait.CRAFTY ); session.persist( boy ); session.getTransaction().commit(); @@ -215,7 +215,7 @@ public class DefaultNamingCollectionElementTest { assertEquals( new Integer( 5 ), boy.getScorePerNickName().get( "Thing" ) ); assertNotNull( boy.getFavoriteNumbers() ); assertEquals( 3, boy.getFavoriteNumbers()[1] ); - assertTrue( boy.getCharacters().contains( Character.CRAFTY ) ); + assertTrue( boy.getCharacters().contains( CharacterTrait.CRAFTY ) ); List result = session.createQuery( "select boy from Boy boy join boy.nickNames names where names = :name" ) .setParameter( "name", "Thing" ).list(); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java index ff3bc69e8d..1a4297e7f6 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/FunctionTests.java @@ -54,6 +54,7 @@ import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.isOneOf; +import static org.hibernate.testing.orm.domain.gambit.EntityOfBasics.Gender.FEMALE; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -409,9 +410,15 @@ public class FunctionTests { public void testCoalesceFunction(SessionFactoryScope scope) { scope.inTransaction( session -> { - session.createQuery("select coalesce(nullif('',''), e.gender, e.convertedGender) from EntityOfBasics e", EntityOfBasics.Gender.class) + //Derby does not like literal nulls :-/ +// session.createQuery("select coalesce(null, e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.MALE) from EntityOfBasics e", EntityOfBasics.Gender.class) +// .list(); + session.createQuery("select coalesce(nullif(e.gender,org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.FEMALE), e.gender) from EntityOfBasics e", EntityOfBasics.Gender.class) .list(); - session.createQuery("select ifnull(e.gender, e.convertedGender) from EntityOfBasics e", EntityOfBasics.Gender.class) + session.createQuery("select coalesce(nullif(e.gender,?1), e.gender) from EntityOfBasics e", EntityOfBasics.Gender.class) + .setParameter(1, FEMALE) + .list(); + session.createQuery("select ifnull(e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.FEMALE) from EntityOfBasics e", EntityOfBasics.Gender.class) .list(); assertThat( session.createQuery("select coalesce(nullif('',''), nullif('bye','bye'), 'hello', 'oops')", String.class).getSingleResult(), is("hello") ); assertThat( session.createQuery("select ifnull(nullif('bye','bye'), 'hello')", String.class).getSingleResult(), is("hello") ); diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/StandardFunctionTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/StandardFunctionTests.java index 1163bd07b7..0bcc113f5b 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/StandardFunctionTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/hql/StandardFunctionTests.java @@ -36,6 +36,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.isOneOf; +import static org.hibernate.testing.orm.domain.gambit.EntityOfBasics.Gender.FEMALE; /** * @author Steve Ebersole @@ -181,9 +182,15 @@ public class StandardFunctionTests { public void testCoalesceFunction(SessionFactoryScope scope) { scope.inTransaction( session -> { - session.createQuery("select coalesce(nullif('',''), e.gender, e.convertedGender) from EntityOfBasics e") + //Derby does not like literal nulls :-/ +// session.createQuery("select coalesce(null, e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.MALE) from EntityOfBasics e") +// .list(); + session.createQuery("select coalesce(nullif(e.gender,org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.FEMALE), e.gender) from EntityOfBasics e") .list(); - session.createQuery("select ifnull(e.gender, e.convertedGender) from EntityOfBasics e") + session.createQuery("select coalesce(nullif(e.gender,?1), e.gender) from EntityOfBasics e") + .setParameter(1, FEMALE) + .list(); + session.createQuery("select ifnull(e.gender, org.hibernate.testing.orm.domain.gambit.EntityOfBasics$Gender.MALE) from EntityOfBasics e") .list(); } );