HHH-13266 Test reading of values written without Hibernate ORM in AbstractJavaTimeTypeTest

This commit is contained in:
Yoann Rodière 2019-03-12 12:34:30 +01:00 committed by gbadner
parent ece5f1a180
commit a065bffe90
6 changed files with 87 additions and 30 deletions

View File

@ -73,13 +73,15 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
protected abstract Class<E> getEntityType(); protected abstract Class<E> getEntityType();
protected abstract E createEntity(int id); protected abstract E createEntityForHibernateWrite(int id);
protected abstract T getExpectedPropertyValue(); protected abstract T getExpectedPropertyValueAfterHibernateRead();
protected abstract T getActualPropertyValue(E entity); protected abstract T getActualPropertyValue(E entity);
protected abstract Object getExpectedJdbcValue(); protected abstract void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException;
protected abstract Object getExpectedJdbcValueAfterHibernateWrite();
protected abstract Object getActualJdbcValue(ResultSet resultSet, int columnIndex) throws SQLException; protected abstract Object getActualJdbcValue(ResultSet resultSet, int columnIndex) throws SQLException;
@ -95,13 +97,13 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
public void writeThenRead() { public void writeThenRead() {
withDefaultTimeZone( () -> { withDefaultTimeZone( () -> {
inTransaction( session -> { inTransaction( session -> {
session.persist( createEntity( 1 ) ); session.persist( createEntityForHibernateWrite( 1 ) );
} ); } );
inTransaction( session -> { inTransaction( session -> {
T read = getActualPropertyValue( session.find( getEntityType(), 1 ) ); T read = getActualPropertyValue( session.find( getEntityType(), 1 ) );
assertEquals( assertEquals(
"Writing then reading a value should return the original value", "Writing then reading a value should return the original value",
getExpectedPropertyValue(), read getExpectedPropertyValueAfterHibernateRead(), read
); );
} ); } );
} ); } );
@ -112,7 +114,7 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
public void writeThenNativeRead() { public void writeThenNativeRead() {
withDefaultTimeZone( () -> { withDefaultTimeZone( () -> {
inTransaction( session -> { inTransaction( session -> {
session.persist( createEntity( 1 ) ); session.persist( createEntityForHibernateWrite( 1 ) );
} ); } );
inTransaction( session -> { inTransaction( session -> {
session.doWork( connection -> { session.doWork( connection -> {
@ -126,7 +128,7 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
Object nativeRead = getActualJdbcValue( resultSet, 1 ); Object nativeRead = getActualJdbcValue( resultSet, 1 );
assertEquals( assertEquals(
"Values written by Hibernate ORM should match the original value (same day, hour, ...)", "Values written by Hibernate ORM should match the original value (same day, hour, ...)",
getExpectedJdbcValue(), getExpectedJdbcValueAfterHibernateWrite(),
nativeRead nativeRead
); );
} ); } );
@ -134,6 +136,31 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
} ); } );
} }
@Test
@TestForIssue(jiraKey = "HHH-13266")
public void nativeWriteThenRead() {
withDefaultTimeZone( () -> {
inTransaction( session -> {
session.doWork( connection -> {
final PreparedStatement statement = connection.prepareStatement(
"INSERT INTO " + ENTITY_NAME + " (" + ID_COLUMN_NAME + ", " + PROPERTY_COLUMN_NAME + ") "
+ " VALUES ( ? , ? )"
);
statement.setInt( 1, 1 );
setJdbcValueForNonHibernateWrite( statement, 2 );
statement.execute();
} );
} );
inTransaction( session -> {
T read = getActualPropertyValue( session.find( getEntityType(), 1 ) );
assertEquals(
"Values written without Hibernate ORM should be read correctly by Hibernate ORM",
getExpectedPropertyValueAfterHibernateRead(), read
);
} );
} );
}
protected final void withDefaultTimeZone(Runnable runnable) { protected final void withDefaultTimeZone(Runnable runnable) {
TimeZone timeZoneBefore = TimeZone.getDefault(); TimeZone timeZoneBefore = TimeZone.getDefault();
TimeZone.setDefault( TimeZone.getTimeZone( env.defaultJvmTimeZone ) ); TimeZone.setDefault( TimeZone.getTimeZone( env.defaultJvmTimeZone ) );

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.test.type; package org.hibernate.test.type;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -85,12 +86,12 @@ public class InstantTest extends AbstractJavaTimeTypeTest<Instant, InstantTest.E
} }
@Override @Override
protected EntityWithInstant createEntity(int id) { protected EntityWithInstant createEntityForHibernateWrite(int id) {
return new EntityWithInstant( id, getExpectedPropertyValue() ); return new EntityWithInstant( id, getExpectedPropertyValueAfterHibernateRead() );
} }
@Override @Override
protected Instant getExpectedPropertyValue() { protected Instant getExpectedPropertyValueAfterHibernateRead() {
return OffsetDateTime.of( year, month, day, hour, minute, second, nanosecond, ZoneOffset.UTC ).toInstant(); return OffsetDateTime.of( year, month, day, hour, minute, second, nanosecond, ZoneOffset.UTC ).toInstant();
} }
@ -100,8 +101,13 @@ public class InstantTest extends AbstractJavaTimeTypeTest<Instant, InstantTest.E
} }
@Override @Override
protected Object getExpectedJdbcValue() { protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
LocalDateTime dateTimeInDefaultTimeZone = getExpectedPropertyValue().atZone( ZoneId.systemDefault() ) statement.setTimestamp( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
}
@Override
protected Timestamp getExpectedJdbcValueAfterHibernateWrite() {
LocalDateTime dateTimeInDefaultTimeZone = getExpectedPropertyValueAfterHibernateRead().atZone( ZoneId.systemDefault() )
.toLocalDateTime(); .toLocalDateTime();
return new Timestamp( return new Timestamp(
dateTimeInDefaultTimeZone.getYear() - 1900, dateTimeInDefaultTimeZone.getMonthValue() - 1, dateTimeInDefaultTimeZone.getYear() - 1900, dateTimeInDefaultTimeZone.getMonthValue() - 1,

View File

@ -7,6 +7,7 @@
package org.hibernate.test.type; package org.hibernate.test.type;
import java.sql.Date; import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDate; import java.time.LocalDate;
@ -68,12 +69,12 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
} }
@Override @Override
protected EntityWithLocalDate createEntity(int id) { protected EntityWithLocalDate createEntityForHibernateWrite(int id) {
return new EntityWithLocalDate( id, getExpectedPropertyValue() ); return new EntityWithLocalDate( id, getExpectedPropertyValueAfterHibernateRead() );
} }
@Override @Override
protected LocalDate getExpectedPropertyValue() { protected LocalDate getExpectedPropertyValueAfterHibernateRead() {
return LocalDate.of( year, month, day ); return LocalDate.of( year, month, day );
} }
@ -83,7 +84,12 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
} }
@Override @Override
protected Object getExpectedJdbcValue() { protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
statement.setDate( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
}
@Override
protected Date getExpectedJdbcValueAfterHibernateWrite() {
return new Date( year - 1900, month - 1, day ); return new Date( year - 1900, month - 1, day );
} }

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.test.type; package org.hibernate.test.type;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -80,12 +81,12 @@ public class LocalDateTimeTest extends AbstractJavaTimeTypeTest<LocalDateTime, L
} }
@Override @Override
protected EntityWithLocalDateTime createEntity(int id) { protected EntityWithLocalDateTime createEntityForHibernateWrite(int id) {
return new EntityWithLocalDateTime( id, getExpectedPropertyValue() ); return new EntityWithLocalDateTime( id, getExpectedPropertyValueAfterHibernateRead() );
} }
@Override @Override
protected LocalDateTime getExpectedPropertyValue() { protected LocalDateTime getExpectedPropertyValueAfterHibernateRead() {
return LocalDateTime.of( year, month, day, hour, minute, second, nanosecond ); return LocalDateTime.of( year, month, day, hour, minute, second, nanosecond );
} }
@ -95,7 +96,12 @@ public class LocalDateTimeTest extends AbstractJavaTimeTypeTest<LocalDateTime, L
} }
@Override @Override
protected Object getExpectedJdbcValue() { protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
statement.setTimestamp( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
}
@Override
protected Timestamp getExpectedJdbcValueAfterHibernateWrite() {
return new Timestamp( return new Timestamp(
year - 1900, month - 1, day, year - 1900, month - 1, day,
hour, minute, second, nanosecond hour, minute, second, nanosecond

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.test.type; package org.hibernate.test.type;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -114,7 +115,7 @@ public class OffsetDateTimeTest extends AbstractJavaTimeTypeTest<OffsetDateTime,
} }
@Override @Override
protected EntityWithOffsetDateTime createEntity(int id) { protected EntityWithOffsetDateTime createEntityForHibernateWrite(int id) {
return new EntityWithOffsetDateTime( id, getOriginalOffsetDateTime() ); return new EntityWithOffsetDateTime( id, getOriginalOffsetDateTime() );
} }
@ -123,7 +124,7 @@ public class OffsetDateTimeTest extends AbstractJavaTimeTypeTest<OffsetDateTime,
} }
@Override @Override
protected OffsetDateTime getExpectedPropertyValue() { protected OffsetDateTime getExpectedPropertyValueAfterHibernateRead() {
return getOriginalOffsetDateTime().atZoneSameInstant( ZoneId.systemDefault() ).toOffsetDateTime(); return getOriginalOffsetDateTime().atZoneSameInstant( ZoneId.systemDefault() ).toOffsetDateTime();
} }
@ -133,7 +134,12 @@ public class OffsetDateTimeTest extends AbstractJavaTimeTypeTest<OffsetDateTime,
} }
@Override @Override
protected Object getExpectedJdbcValue() { protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
statement.setTimestamp( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
}
@Override
protected Timestamp getExpectedJdbcValueAfterHibernateWrite() {
LocalDateTime dateTimeInDefaultTimeZone = getOriginalOffsetDateTime().atZoneSameInstant( ZoneId.systemDefault() ) LocalDateTime dateTimeInDefaultTimeZone = getOriginalOffsetDateTime().atZoneSameInstant( ZoneId.systemDefault() )
.toLocalDateTime(); .toLocalDateTime();
return new Timestamp( return new Timestamp(
@ -163,8 +169,8 @@ public class OffsetDateTimeTest extends AbstractJavaTimeTypeTest<OffsetDateTime,
assertThat( list.size(), is( 1 ) ); assertThat( list.size(), is( 1 ) );
} ); } );
checkOneMatch.accept( getOriginalOffsetDateTime() ); checkOneMatch.accept( getOriginalOffsetDateTime() );
checkOneMatch.accept( getExpectedPropertyValue() ); checkOneMatch.accept( getExpectedPropertyValueAfterHibernateRead() );
checkOneMatch.accept( getExpectedPropertyValue().withOffsetSameInstant( ZoneOffset.UTC ) ); checkOneMatch.accept( getExpectedPropertyValueAfterHibernateRead().withOffsetSameInstant( ZoneOffset.UTC ) );
} ); } );
} }

View File

@ -6,6 +6,7 @@
*/ */
package org.hibernate.test.type; package org.hibernate.test.type;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
@ -126,7 +127,7 @@ public class ZonedDateTimeTest extends AbstractJavaTimeTypeTest<ZonedDateTime, Z
} }
@Override @Override
protected EntityWithZonedDateTime createEntity(int id) { protected EntityWithZonedDateTime createEntityForHibernateWrite(int id) {
return new EntityWithZonedDateTime( return new EntityWithZonedDateTime(
id, id,
getOriginalZonedDateTime() getOriginalZonedDateTime()
@ -138,7 +139,7 @@ public class ZonedDateTimeTest extends AbstractJavaTimeTypeTest<ZonedDateTime, Z
} }
@Override @Override
protected ZonedDateTime getExpectedPropertyValue() { protected ZonedDateTime getExpectedPropertyValueAfterHibernateRead() {
return getOriginalZonedDateTime().withZoneSameInstant( ZoneId.systemDefault() ); return getOriginalZonedDateTime().withZoneSameInstant( ZoneId.systemDefault() );
} }
@ -148,7 +149,12 @@ public class ZonedDateTimeTest extends AbstractJavaTimeTypeTest<ZonedDateTime, Z
} }
@Override @Override
protected Object getExpectedJdbcValue() { protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
statement.setTimestamp( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
}
@Override
protected Timestamp getExpectedJdbcValueAfterHibernateWrite() {
LocalDateTime dateTimeInDefaultTimeZone = getOriginalZonedDateTime().withZoneSameInstant( ZoneId.systemDefault() ) LocalDateTime dateTimeInDefaultTimeZone = getOriginalZonedDateTime().withZoneSameInstant( ZoneId.systemDefault() )
.toLocalDateTime(); .toLocalDateTime();
return new Timestamp( return new Timestamp(
@ -178,8 +184,8 @@ public class ZonedDateTimeTest extends AbstractJavaTimeTypeTest<ZonedDateTime, Z
assertThat( list.size(), is( 1 ) ); assertThat( list.size(), is( 1 ) );
} ); } );
checkOneMatch.accept( getOriginalZonedDateTime() ); checkOneMatch.accept( getOriginalZonedDateTime() );
checkOneMatch.accept( getExpectedPropertyValue() ); checkOneMatch.accept( getExpectedPropertyValueAfterHibernateRead() );
checkOneMatch.accept( getExpectedPropertyValue().withZoneSameInstant( ZoneOffset.UTC ) ); checkOneMatch.accept( getExpectedPropertyValueAfterHibernateRead().withZoneSameInstant( ZoneOffset.UTC ) );
} ); } );
} }