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 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 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;
@ -95,13 +97,13 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
public void writeThenRead() {
withDefaultTimeZone( () -> {
inTransaction( session -> {
session.persist( createEntity( 1 ) );
session.persist( createEntityForHibernateWrite( 1 ) );
} );
inTransaction( session -> {
T read = getActualPropertyValue( session.find( getEntityType(), 1 ) );
assertEquals(
"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() {
withDefaultTimeZone( () -> {
inTransaction( session -> {
session.persist( createEntity( 1 ) );
session.persist( createEntityForHibernateWrite( 1 ) );
} );
inTransaction( session -> {
session.doWork( connection -> {
@ -126,7 +128,7 @@ abstract class AbstractJavaTimeTypeTest<T, E> extends BaseCoreFunctionalTestCase
Object nativeRead = getActualJdbcValue( resultSet, 1 );
assertEquals(
"Values written by Hibernate ORM should match the original value (same day, hour, ...)",
getExpectedJdbcValue(),
getExpectedJdbcValueAfterHibernateWrite(),
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) {
TimeZone timeZoneBefore = TimeZone.getDefault();
TimeZone.setDefault( TimeZone.getTimeZone( env.defaultJvmTimeZone ) );

View File

@ -6,6 +6,7 @@
*/
package org.hibernate.test.type;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
@ -85,12 +86,12 @@ public class InstantTest extends AbstractJavaTimeTypeTest<Instant, InstantTest.E
}
@Override
protected EntityWithInstant createEntity(int id) {
return new EntityWithInstant( id, getExpectedPropertyValue() );
protected EntityWithInstant createEntityForHibernateWrite(int id) {
return new EntityWithInstant( id, getExpectedPropertyValueAfterHibernateRead() );
}
@Override
protected Instant getExpectedPropertyValue() {
protected Instant getExpectedPropertyValueAfterHibernateRead() {
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
protected Object getExpectedJdbcValue() {
LocalDateTime dateTimeInDefaultTimeZone = getExpectedPropertyValue().atZone( ZoneId.systemDefault() )
protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
statement.setTimestamp( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
}
@Override
protected Timestamp getExpectedJdbcValueAfterHibernateWrite() {
LocalDateTime dateTimeInDefaultTimeZone = getExpectedPropertyValueAfterHibernateRead().atZone( ZoneId.systemDefault() )
.toLocalDateTime();
return new Timestamp(
dateTimeInDefaultTimeZone.getYear() - 1900, dateTimeInDefaultTimeZone.getMonthValue() - 1,

View File

@ -7,6 +7,7 @@
package org.hibernate.test.type;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDate;
@ -68,12 +69,12 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
}
@Override
protected EntityWithLocalDate createEntity(int id) {
return new EntityWithLocalDate( id, getExpectedPropertyValue() );
protected EntityWithLocalDate createEntityForHibernateWrite(int id) {
return new EntityWithLocalDate( id, getExpectedPropertyValueAfterHibernateRead() );
}
@Override
protected LocalDate getExpectedPropertyValue() {
protected LocalDate getExpectedPropertyValueAfterHibernateRead() {
return LocalDate.of( year, month, day );
}
@ -83,7 +84,12 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
}
@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 );
}

View File

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

View File

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

View File

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