Skip to content

Commit 658e9bc

Browse files
committed
HHH-17693 Add test for issue
1 parent e9946db commit 658e9bc

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.mapping.converted.converter;
8+
9+
import java.time.Duration;
10+
import java.util.Set;
11+
12+
import org.hibernate.testing.orm.junit.DomainModel;
13+
import org.hibernate.testing.orm.junit.Jira;
14+
import org.hibernate.testing.orm.junit.SessionFactory;
15+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
import jakarta.persistence.AttributeConverter;
21+
import jakarta.persistence.Convert;
22+
import jakarta.persistence.Converter;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.GeneratedValue;
25+
import jakarta.persistence.Id;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* @author Marco Belladelli
31+
*/
32+
@DomainModel( annotatedClasses = ConvertedAttributesTypecheckTest.TestEntity.class )
33+
@SessionFactory
34+
@Jira( "https://hibernate.atlassian.net/browse/HHH-17693" )
35+
public class ConvertedAttributesTypecheckTest {
36+
@BeforeAll
37+
public void setUp(SessionFactoryScope scope) {
38+
scope.inTransaction( session -> session.persist( new TestEntity(
39+
Set.of( "one", "two" ),
40+
"123",
41+
String.valueOf( Duration.ofDays( 3 ).toMillis() )
42+
) ) );
43+
}
44+
45+
@AfterAll
46+
public void tearDown(SessionFactoryScope scope) {
47+
scope.inTransaction( session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate() );
48+
}
49+
50+
@Test
51+
public void testLikeOnConvertedString(SessionFactoryScope scope) {
52+
scope.inTransaction( session -> {
53+
final TestEntity result = session.createQuery(
54+
"from TestEntity where convertedString like 'one%'",
55+
TestEntity.class
56+
).getSingleResult();
57+
assertThat( result.getConvertedString() ).contains( "one" );
58+
} );
59+
}
60+
61+
@Test
62+
public void testUnaryExpressionOnConvertedNumber(SessionFactoryScope scope) {
63+
scope.inTransaction( session -> {
64+
final String result = session.createQuery(
65+
"select -convertedNumber from TestEntity",
66+
String.class
67+
).getSingleResult();
68+
assertThat( result ).isEqualTo( "-123" );
69+
} );
70+
}
71+
72+
@Test
73+
public void testFromDurationExpressionOnConvertedDuration(SessionFactoryScope scope) {
74+
scope.inTransaction( session -> {
75+
final String result = session.createQuery(
76+
"select convertedDuration by day from TestEntity",
77+
String.class
78+
).getSingleResult();
79+
assertThat( Long.parseLong( result ) ).isEqualTo( Duration.ofDays( 3 ).toMillis() );
80+
} );
81+
}
82+
83+
@Entity( name = "TestEntity" )
84+
public static class TestEntity {
85+
@Id
86+
@GeneratedValue
87+
public Long id;
88+
89+
@Convert( converter = SetConverter.class )
90+
public Set<String> convertedString;
91+
92+
@Convert( converter = StringConverter.class )
93+
public String convertedNumber;
94+
95+
@Convert( converter = DurationConverter.class )
96+
public String convertedDuration;
97+
98+
public TestEntity() {
99+
}
100+
101+
public TestEntity(Set<String> convertedString, String convertedNumber, String convertedDuration) {
102+
this.convertedString = convertedString;
103+
this.convertedNumber = convertedNumber;
104+
this.convertedDuration = convertedDuration;
105+
}
106+
107+
public Set<String> getConvertedString() {
108+
return convertedString;
109+
}
110+
111+
public String getConvertedNumber() {
112+
return convertedNumber;
113+
}
114+
115+
public String getConvertedDuration() {
116+
return convertedDuration;
117+
}
118+
}
119+
120+
@Converter
121+
public static class SetConverter implements AttributeConverter<Set<String>, String> {
122+
@Override
123+
public String convertToDatabaseColumn(final Set<String> attribute) {
124+
return attribute == null ? null : String.join( ",", attribute );
125+
}
126+
127+
@Override
128+
public Set<String> convertToEntityAttribute(final String dbData) {
129+
return dbData == null ? null : Set.of( dbData.split( "," ) );
130+
}
131+
}
132+
133+
@Converter
134+
public static class StringConverter implements AttributeConverter<String, Integer> {
135+
@Override
136+
public Integer convertToDatabaseColumn(final String attribute) {
137+
return attribute == null ? null : Integer.valueOf( attribute );
138+
}
139+
140+
@Override
141+
public String convertToEntityAttribute(final Integer dbData) {
142+
return dbData == null ? null : dbData.toString();
143+
}
144+
}
145+
146+
@Converter
147+
public static class DurationConverter implements AttributeConverter<String, Duration> {
148+
@Override
149+
public Duration convertToDatabaseColumn(final String attribute) {
150+
return attribute == null ? null : Duration.ofMillis( Long.parseLong( attribute ) );
151+
}
152+
153+
@Override
154+
public String convertToEntityAttribute(final Duration dbData) {
155+
return dbData == null ? null : String.valueOf( dbData.toMillis() );
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)