Skip to content

Commit

Permalink
HHH-13266 Test reading of values written without Hibernate ORM in Abs…
Browse files Browse the repository at this point in the history
…tractJavaTimeTypeTest
  • Loading branch information
yrodiere committed Mar 14, 2019
1 parent 30e50a9 commit acbefe5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 30 deletions.
Expand Up @@ -73,13 +73,15 @@ protected final Class<?>[] getAnnotatedClasses() {

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;

Expand All @@ -95,13 +97,13 @@ public void cleanup() {
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
);
} );
} );
Expand All @@ -112,7 +114,7 @@ public void writeThenRead() {
public void writeThenNativeRead() {
withDefaultTimeZone( () -> {
inTransaction( session -> {
session.persist( createEntity( 1 ) );
session.persist( createEntityForHibernateWrite( 1 ) );
} );
inTransaction( session -> {
session.doWork( connection -> {
Expand All @@ -126,14 +128,39 @@ public void writeThenNativeRead() {
Object nativeRead = getActualJdbcValue( resultSet, 1 );
assertEquals(
"Values written by Hibernate ORM should match the original value (same day, hour, ...)",
getExpectedJdbcValue(),
getExpectedJdbcValueAfterHibernateWrite(),
nativeRead
);
} );
} );
} );
}

@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 ) );
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -85,12 +86,12 @@ protected Class<EntityWithInstant> getEntityType() {
}

@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();
}

Expand All @@ -100,8 +101,13 @@ protected Instant getActualPropertyValue(EntityWithInstant entity) {
}

@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,
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -68,12 +69,12 @@ protected Class<EntityWithLocalDate> getEntityType() {
}

@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 );
}

Expand All @@ -83,7 +84,12 @@ protected LocalDate getActualPropertyValue(EntityWithLocalDate entity) {
}

@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 );
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -80,12 +81,12 @@ protected Class<EntityWithLocalDateTime> getEntityType() {
}

@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 );
}

Expand All @@ -95,7 +96,12 @@ protected LocalDateTime getActualPropertyValue(EntityWithLocalDateTime entity) {
}

@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
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -114,7 +115,7 @@ protected Class<EntityWithOffsetDateTime> getEntityType() {
}

@Override
protected EntityWithOffsetDateTime createEntity(int id) {
protected EntityWithOffsetDateTime createEntityForHibernateWrite(int id) {
return new EntityWithOffsetDateTime( id, getOriginalOffsetDateTime() );
}

Expand All @@ -123,7 +124,7 @@ private OffsetDateTime getOriginalOffsetDateTime() {
}

@Override
protected OffsetDateTime getExpectedPropertyValue() {
protected OffsetDateTime getExpectedPropertyValueAfterHibernateRead() {
return getOriginalOffsetDateTime().atZoneSameInstant( ZoneId.systemDefault() ).toOffsetDateTime();
}

Expand All @@ -133,7 +134,12 @@ protected OffsetDateTime getActualPropertyValue(EntityWithOffsetDateTime entity)
}

@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(
Expand Down Expand Up @@ -163,8 +169,8 @@ public void testRetrievingEntityByOffsetDateTime() {
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 ) );
} );
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -126,7 +127,7 @@ protected Class<EntityWithZonedDateTime> getEntityType() {
}

@Override
protected EntityWithZonedDateTime createEntity(int id) {
protected EntityWithZonedDateTime createEntityForHibernateWrite(int id) {
return new EntityWithZonedDateTime(
id,
getOriginalZonedDateTime()
Expand All @@ -138,7 +139,7 @@ private ZonedDateTime getOriginalZonedDateTime() {
}

@Override
protected ZonedDateTime getExpectedPropertyValue() {
protected ZonedDateTime getExpectedPropertyValueAfterHibernateRead() {
return getOriginalZonedDateTime().withZoneSameInstant( ZoneId.systemDefault() );
}

Expand All @@ -148,7 +149,12 @@ protected ZonedDateTime getActualPropertyValue(EntityWithZonedDateTime entity) {
}

@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(
Expand Down Expand Up @@ -178,8 +184,8 @@ public void testRetrievingEntityByZonedDateTime() {
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 ) );
} );
}

Expand Down

0 comments on commit acbefe5

Please sign in to comment.