Skip to content

Commit 753f735

Browse files
committed
Various code cleanup
1 parent 0cccdcc commit 753f735

File tree

1 file changed

+51
-102
lines changed

1 file changed

+51
-102
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/hql/CoalesceTest.java

Lines changed: 51 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4,131 +4,80 @@
44
*/
55
package org.hibernate.orm.test.hql;
66

7-
import java.util.List;
8-
import jakarta.persistence.Column;
9-
import jakarta.persistence.Entity;
10-
import jakarta.persistence.GeneratedValue;
11-
import jakarta.persistence.Id;
12-
import jakarta.persistence.TypedQuery;
13-
14-
7+
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;
8+
import org.hibernate.testing.orm.junit.DomainModel;
159
import org.hibernate.testing.orm.junit.JiraKey;
16-
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
17-
import org.junit.Before;
18-
import org.junit.Test;
10+
import org.hibernate.testing.orm.junit.SessionFactory;
11+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
12+
import org.junit.jupiter.api.AfterEach;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
1915

20-
import static org.hamcrest.CoreMatchers.hasItem;
21-
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
22-
import static org.junit.Assert.assertThat;
16+
import java.util.List;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
2319

2420
/**
2521
* @author Johannes Buehler
2622
*/
23+
@SuppressWarnings("JUnitMalformedDeclaration")
2724
@JiraKey( value = "HHH-10463")
28-
public class CoalesceTest extends BaseCoreFunctionalTestCase {
29-
30-
private Person person;
25+
@DomainModel(annotatedClasses = org.hibernate.testing.orm.domain.gambit.EntityOfBasics.class)
26+
@SessionFactory
27+
public class CoalesceTest {
28+
final String QRY_STR = "from EntityOfBasics e where e.theString = coalesce(:p , e.theString)";
3129

32-
@Override
33-
protected Class[] getAnnotatedClasses() {
34-
return new Class[] {
35-
Person.class
36-
};
37-
}
30+
@Test
31+
public void testCoalesce(SessionFactoryScope sessions) {
32+
sessions.inTransaction( (session) -> {
33+
final List<EntityOfBasics> resultList = session.createQuery( QRY_STR, EntityOfBasics.class )
34+
.setParameter("p", "a string")
35+
.getResultList();
36+
assertThat( resultList ).hasSize( 1 );
37+
} );
3838

39-
@Before
40-
public void setUp() {
41-
doInHibernate( this::sessionFactory, session -> {
42-
person = new Person();
43-
person.setName("Johannes");
44-
person.setSurname("Buehler");
45-
session.persist(person);
39+
sessions.inTransaction( (session) -> {
40+
final List<EntityOfBasics> resultList = session.createQuery( QRY_STR, EntityOfBasics.class )
41+
.setParameter("p", "$^&@#")
42+
.getResultList();
43+
assertThat( resultList ).hasSize( 0 );
4644
} );
4745
}
4846

4947
@Test
50-
public void HHH_10463_TestCoalesce() {
51-
doInHibernate( this::sessionFactory, session -> {
52-
TypedQuery<Person> query = session.createQuery( "from Person p where p.name = coalesce(:name , p.name) ", Person.class );
53-
query.setParameter("name", "Johannes");
54-
List<Person> resultList = query.getResultList();
55-
assertThat(resultList, hasItem(person));
48+
public void testCoalesceWithNull(SessionFactoryScope sessions) {
49+
sessions.inTransaction( (session) -> {
50+
final List<EntityOfBasics> resultList = session.createQuery( QRY_STR, EntityOfBasics.class )
51+
.setParameter("p", null)
52+
.getResultList();
53+
assertThat( resultList ).hasSize( 1 );
5654
} );
57-
5855
}
5956

6057
@Test
61-
public void HHH_10463_NullInCoalesce() {
62-
doInHibernate( this::sessionFactory, session -> {
63-
TypedQuery<Person> query = session.createQuery( "from Person p where p.name = coalesce(:name, p.name) ", Person.class );
64-
query.setParameter("name", null);
65-
List<Person> resultList = query.getResultList();
66-
assertThat(resultList, hasItem(person));
58+
public void testCoalesceWithCast(SessionFactoryScope sessions) {
59+
final String qryStr = "from EntityOfBasics e where e.theString = coalesce(cast(:p as string) , e.theString)";
60+
sessions.inTransaction( (session) -> {
61+
final List<EntityOfBasics> resultList = session.createQuery( qryStr, EntityOfBasics.class )
62+
.setParameter("p", null)
63+
.getResultList();
64+
assertThat( resultList ).hasSize( 1 );
6765
} );
6866
}
6967

70-
@Test
71-
public void HHH_10463_NullInCoalesce_PostgreSQL_Workaround() {
72-
doInHibernate( this::sessionFactory, session -> {
73-
TypedQuery<Person> query = session.createQuery( "from Person p where p.name = coalesce(cast( :name as string) , p.name) ", Person.class );
74-
query.setParameter("name", null);
75-
List<Person> resultList = query.getResultList();
76-
assertThat(resultList, hasItem(person));
68+
@BeforeEach
69+
public void createTestData(SessionFactoryScope sessions) {
70+
sessions.inTransaction( (session) -> {
71+
final EntityOfBasics entity = new EntityOfBasics( 1 );
72+
entity.setTheString( "a string" );
73+
entity.setTheField( "another string" );
74+
session.persist( entity );
7775
} );
7876
}
7977

80-
@Entity(name = "Person")
81-
public static class Person {
82-
83-
@Id
84-
@GeneratedValue
85-
private Integer id;
86-
87-
@Column
88-
private String name;
89-
90-
@Column
91-
private String surname;
92-
93-
public String getName() {
94-
return name;
95-
}
96-
97-
public void setName(String name) {
98-
this.name = name;
99-
}
100-
101-
public String getSurname() {
102-
return surname;
103-
}
104-
105-
public void setSurname(String surname) {
106-
this.surname = surname;
107-
}
108-
109-
public Integer getId() {
110-
return id;
111-
}
112-
113-
public void setId(Integer id) {
114-
this.id = id;
115-
}
116-
117-
@Override
118-
public boolean equals(Object o) {
119-
if (this == o) return true;
120-
if (!(o instanceof Person )) return false;
121-
122-
Person person = (Person) o;
123-
124-
return id != null ? id.equals(person.id) : person.id == null;
125-
126-
}
127-
128-
@Override
129-
public int hashCode() {
130-
return id != null ? id.hashCode() : 0;
131-
}
78+
@AfterEach
79+
void dropTestData(SessionFactoryScope sessions) {
80+
sessions.dropData();
13281
}
13382

13483
}

0 commit comments

Comments
 (0)