Skip to content

Commit

Permalink
HHH-17635 - Add test for issue
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
  • Loading branch information
jrenaat authored and mbladel committed Jan 18, 2024
1 parent d6f09d6 commit 29346e6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
public class MonetaryAmountUserType implements CompositeUserType<MonetaryAmount> {

public static final MonetaryAmountUserType INSTANCE = new MonetaryAmountUserType();

@Override
public Object getPropertyValue(MonetaryAmount component, int property) throws HibernateException {
switch ( property ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
package org.hibernate.orm.test.type.contributor.usertype;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Currency;

import org.hibernate.boot.model.TypeContributor;
import org.hibernate.orm.test.mapping.basic.MonetaryAmount;
import org.hibernate.type.CustomType;
import org.hibernate.type.Type;
import org.hibernate.type.UserComponentType;

import org.hibernate.testing.orm.junit.BootstrapServiceRegistry;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.Jira;
import org.hibernate.testing.orm.junit.JiraKey;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
Expand All @@ -29,11 +35,26 @@
annotatedClasses = {
ContributedUserTypeTest.StringWrapperTestEntity.class,
ContributedUserTypeTest.MyCompositeValueTestEntity.class,
ContributedUserTypeTest.Wallet.class
},
typeContributors = { StringWrapperTypeContributor.class, MyCompositeValueTypeContributor.class }
)
@SessionFactory
@BootstrapServiceRegistry(
javaServices = {
@BootstrapServiceRegistry.JavaService( role = TypeContributor.class, impl = ServiceLoadedCustomUserTypeTypeContributor.class)
}
)
public class ContributedUserTypeTest {

public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createMutationQuery( "delete from Wallet" ).executeUpdate();
}
);
}

@Test
@JiraKey( "HHH-14408" )
public void test(SessionFactoryScope scope) {
Expand Down Expand Up @@ -81,6 +102,28 @@ public void testCompositeParameter(SessionFactoryScope scope) {
);
}

@Test
@Jira( value = "https://hibernate.atlassian.net/browse/HHH-17635" )
public void testServiceLoadedCustomUserType(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Wallet wallet = new Wallet();
wallet.setId( 1L );
wallet.setMoney( new MonetaryAmount( new BigDecimal( 1000 ), Currency.getInstance("EUR")) );
session.persist( wallet );
}
);
scope.inTransaction(
session -> {
Wallet w = session.createSelectionQuery( "from Wallet", Wallet.class ).getSingleResult();
MonetaryAmount amount = w.getMoney();
Assertions.assertNotNull( amount );
Assertions.assertEquals( 1000, amount.getAmount().intValue() );
Assertions.assertEquals( "EUR", amount.getCurrency().getCurrencyCode() );
}
);
}

@Entity( name = "StringWrapperTestEntity" )
public static class StringWrapperTestEntity implements Serializable {
@Id
Expand All @@ -94,4 +137,30 @@ public static class MyCompositeValueTestEntity implements Serializable {
private Integer id;
private MyCompositeValue compositeValue;
}

@Entity(name = "Wallet")
public static class Wallet {

@Id
private Long id;

private MonetaryAmount money;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public MonetaryAmount getMoney() {
return money;
}

public void setMoney(MonetaryAmount money) {
this.money = money;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.type.contributor.usertype;

import org.hibernate.boot.model.TypeContributions;
import org.hibernate.boot.model.TypeContributor;
import org.hibernate.orm.test.mapping.basic.MonetaryAmountUserType;
import org.hibernate.service.ServiceRegistry;

/**
* @author Jan Schatteman
*/
public class ServiceLoadedCustomUserTypeTypeContributor implements TypeContributor {

@Override
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
typeContributions.contributeType( MonetaryAmountUserType.INSTANCE );
}
}

0 comments on commit 29346e6

Please sign in to comment.