-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
158 additions
and
0 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
.../org/hibernate/orm/test/mapping/converted/converter/ConvertedAttributesTypecheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html | ||
*/ | ||
package org.hibernate.orm.test.mapping.converted.converter; | ||
|
||
import java.time.Duration; | ||
import java.util.Set; | ||
|
||
import org.hibernate.testing.orm.junit.DomainModel; | ||
import org.hibernate.testing.orm.junit.Jira; | ||
import org.hibernate.testing.orm.junit.SessionFactory; | ||
import org.hibernate.testing.orm.junit.SessionFactoryScope; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Convert; | ||
import jakarta.persistence.Converter; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Marco Belladelli | ||
*/ | ||
@DomainModel( annotatedClasses = ConvertedAttributesTypecheckTest.TestEntity.class ) | ||
@SessionFactory | ||
@Jira( "https://hibernate.atlassian.net/browse/HHH-17693" ) | ||
public class ConvertedAttributesTypecheckTest { | ||
@BeforeAll | ||
public void setUp(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> session.persist( new TestEntity( | ||
Set.of( "one", "two" ), | ||
"123", | ||
String.valueOf( Duration.ofDays( 3 ).toMillis() ) | ||
) ) ); | ||
} | ||
|
||
@AfterAll | ||
public void tearDown(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> session.createMutationQuery( "delete from TestEntity" ).executeUpdate() ); | ||
} | ||
|
||
@Test | ||
public void testLikeOnConvertedString(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final TestEntity result = session.createQuery( | ||
"from TestEntity where convertedString like 'one%'", | ||
TestEntity.class | ||
).getSingleResult(); | ||
assertThat( result.getConvertedString() ).contains( "one" ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testUnaryExpressionOnConvertedNumber(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final String result = session.createQuery( | ||
"select -convertedNumber from TestEntity", | ||
String.class | ||
).getSingleResult(); | ||
assertThat( result ).isEqualTo( "-123" ); | ||
} ); | ||
} | ||
|
||
@Test | ||
public void testFromDurationExpressionOnConvertedDuration(SessionFactoryScope scope) { | ||
scope.inTransaction( session -> { | ||
final String result = session.createQuery( | ||
"select convertedDuration by day from TestEntity", | ||
String.class | ||
).getSingleResult(); | ||
assertThat( Long.parseLong( result ) ).isEqualTo( Duration.ofDays( 3 ).toMillis() ); | ||
} ); | ||
} | ||
|
||
@Entity( name = "TestEntity" ) | ||
public static class TestEntity { | ||
@Id | ||
@GeneratedValue | ||
public Long id; | ||
|
||
@Convert( converter = SetConverter.class ) | ||
public Set<String> convertedString; | ||
|
||
@Convert( converter = StringConverter.class ) | ||
public String convertedNumber; | ||
|
||
@Convert( converter = DurationConverter.class ) | ||
public String convertedDuration; | ||
|
||
public TestEntity() { | ||
} | ||
|
||
public TestEntity(Set<String> convertedString, String convertedNumber, String convertedDuration) { | ||
this.convertedString = convertedString; | ||
this.convertedNumber = convertedNumber; | ||
this.convertedDuration = convertedDuration; | ||
} | ||
|
||
public Set<String> getConvertedString() { | ||
return convertedString; | ||
} | ||
|
||
public String getConvertedNumber() { | ||
return convertedNumber; | ||
} | ||
|
||
public String getConvertedDuration() { | ||
return convertedDuration; | ||
} | ||
} | ||
|
||
@Converter | ||
public static class SetConverter implements AttributeConverter<Set<String>, String> { | ||
@Override | ||
public String convertToDatabaseColumn(final Set<String> attribute) { | ||
return attribute == null ? null : String.join( ",", attribute ); | ||
} | ||
|
||
@Override | ||
public Set<String> convertToEntityAttribute(final String dbData) { | ||
return dbData == null ? null : Set.of( dbData.split( "," ) ); | ||
} | ||
} | ||
|
||
@Converter | ||
public static class StringConverter implements AttributeConverter<String, Integer> { | ||
@Override | ||
public Integer convertToDatabaseColumn(final String attribute) { | ||
return attribute == null ? null : Integer.valueOf( attribute ); | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(final Integer dbData) { | ||
return dbData == null ? null : dbData.toString(); | ||
} | ||
} | ||
|
||
@Converter | ||
public static class DurationConverter implements AttributeConverter<String, Duration> { | ||
@Override | ||
public Duration convertToDatabaseColumn(final String attribute) { | ||
return attribute == null ? null : Duration.ofMillis( Long.parseLong( attribute ) ); | ||
} | ||
|
||
@Override | ||
public String convertToEntityAttribute(final Duration dbData) { | ||
return dbData == null ? null : String.valueOf( dbData.toMillis() ); | ||
} | ||
} | ||
} |