Skip to content

Commit

Permalink
HHH-17507 HHH-17574 Fixed wrap() & unwrap() & updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
H-Lo authored and beikov committed Jan 4, 2024
1 parent ec60a5a commit 3ca2bd8
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public <X> X unwrap(Currency value, Class<X> type, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( Currency.class.isAssignableFrom( type ) ) {
return (X) value;
}
if ( String.class.isAssignableFrom( type ) ) {
return (X) value.getCurrencyCode();
}
Expand All @@ -50,6 +53,9 @@ public <X> Currency wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( value instanceof Currency ) {
return (Currency) value;
}
if (value instanceof String) {
return Currency.getInstance( (String) value );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public <X> Year wrap(X value, WrapperOptions options) {
return null;
}

if ( value instanceof Year) {
return (Year) value;
}

if ( value instanceof Number ) {
return Year.of( ( (Number) value ).intValue() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@
*/
package org.hibernate.orm.test.mapping.basic;

import java.sql.Types;
import java.util.Currency;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import org.assertj.core.api.Assertions;
import org.hibernate.HibernateException;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.NonContextualLobCreator;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.metamodel.mapping.JdbcMapping;
import org.hibernate.metamodel.mapping.PluralAttributeMapping;
import org.hibernate.metamodel.mapping.internal.BasicAttributeMapping;
import org.hibernate.metamodel.mapping.internal.BasicValuedCollectionPart;
import org.hibernate.metamodel.spi.MappingMetamodelImplementor;
import org.hibernate.orm.test.mapping.type.java.YearMappingTests;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.CurrencyJavaType;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeRegistry;
import org.junit.jupiter.api.Test;

import java.sql.Types;
import java.time.Year;
import java.util.Currency;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

Expand All @@ -31,6 +47,7 @@
*/
@DomainModel(annotatedClasses = CurrencyMappingTests.EntityWithCurrency.class)
@SessionFactory
@JiraKey("HHH-17574")
public class CurrencyMappingTests {

@Test
Expand All @@ -46,17 +63,129 @@ public void verifyMappings(SessionFactoryScope scope) {
assertThat(jdbcMapping.getJavaTypeDescriptor().getJavaTypeClass(), equalTo(Currency.class));
assertThat( jdbcMapping.getJdbcType(), equalTo( jdbcRegistry.getDescriptor( Types.VARCHAR)));

final EntityWithCurrency entity = createEntityWithCurrency();

scope.inTransaction(
(session) -> {
session.persist(new EntityWithCurrency(1, Currency.getInstance("USD")));
}
(session) -> session.persist(entity)
);

scope.inTransaction(
(session) -> session.find(EntityWithCurrency.class, 1)
(session) -> session.find(EntityWithCurrency.class, 1)
);
}

@Test
public void basicAssertions(final SessionFactoryScope scope) {
final SessionFactoryImplementor sessionFactory = scope.getSessionFactory();
final JdbcTypeRegistry jdbcTypeRegistry = sessionFactory.getTypeConfiguration().getJdbcTypeRegistry();
final EntityPersister entityDescriptor = sessionFactory.getMappingMetamodel().getEntityDescriptor(
EntityWithCurrency.class );
{
final BasicAttributeMapping currencyAttribute = (BasicAttributeMapping) entityDescriptor.findAttributeMapping(
"currency");
Assertions.assertThat(currencyAttribute.getJdbcMapping().getJdbcType())
.isEqualTo(jdbcTypeRegistry.getDescriptor(Types.VARCHAR));
Assertions.assertThat(currencyAttribute.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass()).isEqualTo(
Currency.class );
}
{
final PluralAttributeMapping currenciesAttribute = (PluralAttributeMapping) entityDescriptor.findAttributeMapping(
"currencies" );
final BasicValuedCollectionPart elementDescriptor = (BasicValuedCollectionPart) currenciesAttribute.getElementDescriptor();
Assertions.assertThat( elementDescriptor.getJdbcMapping().getJdbcType() )
.isEqualTo( jdbcTypeRegistry.getDescriptor( Types.VARCHAR ) );
Assertions.assertThat( elementDescriptor.getJdbcMapping().getJavaTypeDescriptor().getJavaTypeClass() ).isEqualTo(
Currency.class );
}
}

@Test
public void testUnwrapPass() {
final CurrencyJavaType currencyJavaType = new CurrencyJavaType();
final Currency currency = Currency.getInstance("CHF");
{
final Currency c = currencyJavaType.unwrap(currency, Currency.class, null);
Assertions.assertThat( c ).isEqualTo( currency );
}
{
final String c = currencyJavaType.unwrap(currency, String.class, null);
Assertions.assertThat( c ).isEqualTo( "CHF" );
}
}

@Test
public void testUnwrapFail() {
final CurrencyJavaType currencyJavaType = new CurrencyJavaType();
final Currency currency = Currency.getInstance("CHF");
{
Assertions.assertThatThrownBy( () ->
currencyJavaType.unwrap(currency, Boolean.class, null)
).isInstanceOf( HibernateException.class );
}
}

@Test
public void testWrapPass() {
final CurrencyJavaType currencyJavaType = new CurrencyJavaType();
{
final Currency usingNull = currencyJavaType.wrap(null, null);
Assertions.assertThat(usingNull).isNull();
}
{
final Currency usingString = currencyJavaType.wrap("CHF", null);
Assertions.assertThat(usingString).isNotNull();
}
{
final Currency usingCurrency = currencyJavaType.wrap(Currency.getInstance("CHF"), null);
Assertions.assertThat(usingCurrency).isNotNull();
}
}

@Test
public void testWrapFail() {
final CurrencyJavaType currencyJavaType = new CurrencyJavaType();
{
final String usingEmptyString = "";
Assertions.assertThatThrownBy(() ->
currencyJavaType.wrap(usingEmptyString, null)
).isInstanceOf(IllegalArgumentException.class);
}
{
final Integer usingInteger = Integer.valueOf(269);
Assertions.assertThatThrownBy(() ->
currencyJavaType.wrap(usingInteger, null)
).isInstanceOf(HibernateException.class);
}
{
final CurrencyJavaType usingSelf = new CurrencyJavaType();
Assertions.assertThatThrownBy(() ->
currencyJavaType.wrap(usingSelf, null)
).isInstanceOf(HibernateException.class);
}
}

@Test
public void testUsage(final SessionFactoryScope scope) {
final EntityWithCurrency entity = createEntityWithCurrency();
scope.inTransaction((session) -> session.persist(entity));
try {
scope.inTransaction(session -> session.createQuery("from EntityWithCurrency", EntityWithCurrency.class).list());
}
finally {
scope.inTransaction( session -> session.remove( entity ) );
}
}

private static EntityWithCurrency createEntityWithCurrency() {
final Currency currency = Currency.getInstance("USD");

final Set<Currency> currencies = new HashSet<>();
currencies.add(Currency.getInstance("CHF"));
currencies.add(Currency.getInstance("EUR"));

return new EntityWithCurrency( 1, currency, currencies );
}

@Entity(name = "EntityWithCurrency")
@Table(name = "EntityWithCurrency")
public static class EntityWithCurrency {
Expand All @@ -68,12 +197,18 @@ public static class EntityWithCurrency {
private Currency currency;
//end::basic-Currency-example[]

public EntityWithCurrency() {
}
@ElementCollection
private Set<Currency> currencies;

public EntityWithCurrency(Integer id, Currency currency) {
this.id = id;
this.currency = currency;
public EntityWithCurrency() {
//
}

public EntityWithCurrency(final Integer id, final Currency currency, final Set<Currency> currencies) {
this.id = id;
this.currency = currency;
this.currencies = currencies;
}
}

}

0 comments on commit 3ca2bd8

Please sign in to comment.