HHH-15711 Add test for issue

This commit is contained in:
Andrea Boriero 2022-11-21 17:01:44 +01:00 committed by Andrea Boriero
parent 2171d3b24a
commit 8ef73781ba
2 changed files with 196 additions and 0 deletions

View File

@ -0,0 +1,155 @@
package org.hibernate.orm.test.query.hql;
import java.util.List;
import java.util.Locale;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
@DomainModel(
annotatedClasses = FunctionAndEnumsTest.TestEntity.class
)
@SessionFactory
public class FunctionAndEnumsTest {
@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
EmbeddableThirdLevel embeddableThirdLevel = new EmbeddableThirdLevel( Level.THIRD );
EmbeddableSecondLevel embeddableSecondLevel = new EmbeddableSecondLevel(
Level.SECOND,
embeddableThirdLevel
);
EmbeddableFirstLevel embeddableFirstLevel = new EmbeddableFirstLevel(
Level.FIRST,
embeddableSecondLevel
);
TestEntity testEntity = new TestEntity( 1l, embeddableFirstLevel );
session.persist( testEntity );
}
);
}
@Test
public void testLowerFunction(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
List<String> results = session.createQuery(
"select lower(e.embeddableFirstLevel.embeddableSecondLevel.embeddableThirdLevel.lastLevel) from TestEntity e",
String.class
).list();
assertThat( results.size() ).isEqualTo( 1 );
assertThat( results.get( 0 ) ).isEqualTo( Level.THIRD.name().toLowerCase( Locale.ROOT ) );
results = session.createQuery(
"select lower(e.embeddableFirstLevel.embeddableSecondLevel.anotherLevel) from TestEntity e",
String.class
).list();
assertThat( results.size() ).isEqualTo( 1 );
assertThat( results.get( 0 ) ).isEqualTo( Level.SECOND.name().toLowerCase( Locale.ROOT ) );
results = session.createQuery(
"select lower(e.embeddableFirstLevel.level) from TestEntity e",
String.class
)
.list();
assertThat( results.size() ).isEqualTo( 1 );
assertThat( results.get( 0 ) ).isEqualTo( Level.FIRST.name().toLowerCase( Locale.ROOT ) );
}
);
}
@Entity(name = "TestEntity")
public static class TestEntity {
@Id
private Long id;
private EmbeddableFirstLevel embeddableFirstLevel;
public TestEntity() {
}
public TestEntity(Long id, EmbeddableFirstLevel embeddableFirstLevel) {
this.id = id;
this.embeddableFirstLevel = embeddableFirstLevel;
}
public Long getId() {
return id;
}
}
@Embeddable
public static class EmbeddableFirstLevel {
@Enumerated(EnumType.STRING)
private Level level;
private EmbeddableSecondLevel embeddableSecondLevel;
public EmbeddableFirstLevel() {
}
public EmbeddableFirstLevel(Level level, EmbeddableSecondLevel embeddableSecondLevel) {
this.level = level;
this.embeddableSecondLevel = embeddableSecondLevel;
}
}
@Embeddable
public static class EmbeddableSecondLevel {
@Enumerated(EnumType.STRING)
private Level anotherLevel;
private EmbeddableThirdLevel embeddableThirdLevel;
public EmbeddableSecondLevel() {
}
public EmbeddableSecondLevel(Level anotherLevel, EmbeddableThirdLevel embeddableThirdLevel) {
this.anotherLevel = anotherLevel;
this.embeddableThirdLevel = embeddableThirdLevel;
}
}
@Embeddable
public static class EmbeddableThirdLevel {
@Enumerated(EnumType.STRING)
private Level lastLevel;
public EmbeddableThirdLevel() {
}
public EmbeddableThirdLevel(Level lastLevel) {
this.lastLevel = lastLevel;
}
}
public enum Level {
FIRST,
SECOND,
THIRD
}
}

View File

@ -13,6 +13,8 @@ import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.MariaDBDialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.dialect.TiDBDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.domain.StandardDomainModel;
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;
import org.hibernate.testing.orm.domain.gambit.EntityOfLists;
@ -45,6 +47,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isOneOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author Gavin King
@ -89,6 +92,44 @@ public class FunctionTests {
);
}
@Test
@TestForIssue( jiraKey = "HHH-15711")
public void testLowerUpperFunctionsWithEnums(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery("select lower(e.gender) from EntityOfBasics e", String.class)
.list();
session.createQuery("select upper(e.gender) from EntityOfBasics e", String.class)
.list();
}
);
}
@Test
@TestForIssue( jiraKey = "HHH-15711")
public void testLowerUpperFunctionsWithConvertedEnums(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery("select lower(e.convertedGender) from EntityOfBasics e", String.class)
.list();
session.createQuery("select upper(e.convertedGender) from EntityOfBasics e", String.class)
.list();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-15711")
public void testLowerFunctionsOrdinalEnumsShouldFail(SessionFactoryScope scope) {
scope.inTransaction(
session ->
assertThrows( IllegalArgumentException.class, () ->
session.createQuery( "select lower(e.ordinalGender) from EntityOfBasics e" )
.list()
)
);
}
@Test
public void testIdVersionFunctions(SessionFactoryScope scope) {
scope.inTransaction(