Skip to content

Commit

Permalink
[hibernate#856] Test conversion of temporal types when JDBC_TIME_ZONE…
Browse files Browse the repository at this point in the history
… is set
  • Loading branch information
DavideD committed Jun 14, 2021
1 parent e0d10f5 commit 6145e5b
Showing 1 changed file with 207 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,227 @@
*/
package org.hibernate.reactive;

import io.vertx.ext.unit.TestContext;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.function.Consumer;
import java.util.function.Supplier;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;

import org.junit.Test;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import io.vertx.ext.unit.TestContext;

public class UTCTest extends BaseReactiveTest {
@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
configuration.setProperty(AvailableSettings.JDBC_TIME_ZONE, "UTC");
configuration.addAnnotatedClass(Thing.class);
return configuration;
}

@Test
public void test(TestContext context) {
Thing thing = new Thing();
thing.dateTime = OffsetDateTime.of(2021, 3, 25, 12, 30, 0, 0, ZoneOffset.ofHours(5));
test(context, getMutinySessionFactory()
.withSession(session -> session.persist(thing).call(session::flush))
final Thing thing = new Thing();

@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
configuration.setProperty( AvailableSettings.JDBC_TIME_ZONE, "UTC" );
configuration.addAnnotatedClass( Thing.class );
return configuration;
}

@Test
public void testDate(TestContext context) {
thing.date = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) )
.getTime();

testField(
context,
"dateType",
thing::getDate,
entity -> context.assertEquals( thing.date, entity.date )
);
}

@Test
public void testCalendar(TestContext context) {
thing.calendar = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );

testField(
context,
"calendarType",
thing::getCalendar,
entity -> context.assertEquals( thing.calendar.toInstant(), entity.calendar.toInstant() )
);
}

@Test
public void testLocalDate(TestContext context) {
thing.localDate = LocalDate.now();

testField(
context,
"localDateType",
thing::getLocalDate,
entity -> context.assertEquals( thing.localDate, entity.localDate )
);
}

@Test
public void testLocalTime(TestContext context) {
thing.localTime = LocalTime.MAX.truncatedTo( ChronoUnit.SECONDS );

testField(
context,
"localTimeType",
thing::getLocalTime,
entity -> context.assertEquals( thing.localTime, entity.localTime )
);
}

@Test
public void testLocalDateTime(TestContext context) {
thing.localDateTime = LocalDateTime.now()
.truncatedTo( ChronoUnit.MILLIS );

testField(
context,
"localDateTimeType",
thing::getLocalDateTime,
entity -> context.assertEquals( thing.localDateTime, entity.localDateTime )
);
}

@Test
public void testOffsetDateTime(TestContext context) {
final ZoneOffset zoneOffset = ZoneOffset.ofHours( 5 );
LocalDateTime dateTime = LocalDateTime.of( 2021, 3, 25, 12, 30 );
thing.offsetDateTime = OffsetDateTime.of( dateTime, zoneOffset );

testField(
context,
"offsetDateTimeType",
thing::getOffsetDateTime,
entity -> {
context.assertEquals( entity.offsetDateTime.getOffset(), ZoneOffset.UTC );
context.assertEquals( thing.offsetDateTime,
entity.offsetDateTime.toInstant().atZone( zoneOffset ).toOffsetDateTime() );
}
);
}

@Test
public void testOffsetTime(TestContext context) {
thing.offsetTime = OffsetTime.now( ZoneOffset.ofHours( 7 ) )
.truncatedTo( ChronoUnit.SECONDS );

testField(
context,
"offsetTimeType",
thing::getOffsetTime,
entity -> context.assertEquals( thing.offsetTime.toLocalTime(), entity.offsetTime.toLocalTime() )
);
}

@Test
public void testZonedDateTime(TestContext context) {
final ZoneOffset zoneOffset = ZoneOffset.ofHours( 7 );
thing.zonedDateTime = ZonedDateTime.now( zoneOffset );

testField(
context,
"zonedDateTimeType",
thing::getZonedDateTime,
entity -> context.assertEquals( thing.zonedDateTime, entity.zonedDateTime.withZoneSameInstant( zoneOffset ) )
);
}

private void testField(TestContext context, String columnName, Supplier<?> fieldValue, Consumer<Thing> assertion) {
test( context, getMutinySessionFactory()
.withSession( session -> session.persist( thing ).call( session::flush ).invoke( session::clear ) )
.chain( () -> getMutinySessionFactory()
.withSession(session -> session.find(Thing.class, thing.id))
.invoke(t -> {
context.assertNotNull(t);
context.assertTrue( thing.dateTime.isEqual(t.dateTime) );
})
.withSession( session -> session.find( Thing.class, thing.id ) )
.invoke( t -> {
context.assertNotNull( t );
assertion.accept( t );
} )
)
.chain( () -> getMutinySessionFactory()
.withSession(session -> session.createQuery("from ThingInUTC where dateTime=:dt")
.setParameter("dt", thing.dateTime).getSingleResult())
.invoke(context::assertNotNull)
.withSession( session -> session.createQuery( "from ThingInUTC where " + columnName + "=:dt", Thing.class )
.setParameter( "dt", fieldValue.get() ).getSingleResult() )
.invoke( assertion )
)
);
}

@Entity(name="ThingInUTC")
static class Thing {
@Id
@GeneratedValue
long id;
@Entity(name = "ThingInUTC")
static class Thing {
@Id
@GeneratedValue
long id;

@Column(name = "dateType")
Date date;

@Column(name = "calendarType")
Calendar calendar;

@Column(name = "offsetDateTimeType")
OffsetDateTime offsetDateTime;

@Column(name = "offsetTimeType")
OffsetTime offsetTime;

@Column(name = "zonedDateTimeType")
ZonedDateTime zonedDateTime;

@Column(name = "localDateType")
LocalDate localDate;

@Column(name = "localTimeType")
LocalTime localTime;

@Column(name = "localDateTimeType")
LocalDateTime localDateTime;

public Date getDate() {
return date;
}

public Calendar getCalendar() {
return calendar;
}

public OffsetDateTime getOffsetDateTime() {
return offsetDateTime;
}

public OffsetTime getOffsetTime() {
return offsetTime;
}

public ZonedDateTime getZonedDateTime() {
return zonedDateTime;
}

public LocalDate getLocalDate() {
return localDate;
}

public LocalTime getLocalTime() {
return localTime;
}

OffsetDateTime dateTime;
public LocalDateTime getLocalDateTime() {
return localDateTime;
}
}
}

0 comments on commit 6145e5b

Please sign in to comment.