HHH-13266 Test LocalDate serialization when dates are remapped as Timestamp, in particular around 1900-01-01
This commit is contained in:
parent
a3c318523d
commit
9a8d4f0e5d
|
@ -10,6 +10,8 @@ import java.sql.Date;
|
||||||
import java.sql.PreparedStatement;
|
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.Types;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -18,6 +20,8 @@ import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||||
|
|
||||||
import org.hibernate.testing.TestForIssue;
|
import org.hibernate.testing.TestForIssue;
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
|
|
||||||
|
@ -36,6 +40,7 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
|
||||||
@Parameterized.Parameters(name = "{1}-{2}-{3} {0}")
|
@Parameterized.Parameters(name = "{1}-{2}-{3} {0}")
|
||||||
public static List<Object[]> data() {
|
public static List<Object[]> data() {
|
||||||
return new ParametersBuilder()
|
return new ParametersBuilder()
|
||||||
|
.alsoTestRemappingsWithH2( DateAsTimestampRemappingH2Dialect.class )
|
||||||
// Not affected by HHH-13266 (JDK-8061577)
|
// Not affected by HHH-13266 (JDK-8061577)
|
||||||
.add( 2017, 11, 6, ZONE_UTC_MINUS_8 )
|
.add( 2017, 11, 6, ZONE_UTC_MINUS_8 )
|
||||||
.add( 2017, 11, 6, ZONE_PARIS )
|
.add( 2017, 11, 6, ZONE_PARIS )
|
||||||
|
@ -44,7 +49,7 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
|
||||||
.add( 1900, 1, 1, ZONE_OSLO )
|
.add( 1900, 1, 1, ZONE_OSLO )
|
||||||
.add( 1900, 1, 2, ZONE_PARIS )
|
.add( 1900, 1, 2, ZONE_PARIS )
|
||||||
.add( 1900, 1, 2, ZONE_AMSTERDAM )
|
.add( 1900, 1, 2, ZONE_AMSTERDAM )
|
||||||
// Could have been affected by HHH-13266 (JDK-8061577), but was not
|
// Affected by HHH-13266 (JDK-8061577), but only when remapping dates as timestamps
|
||||||
.add( 1892, 1, 1, ZONE_OSLO )
|
.add( 1892, 1, 1, ZONE_OSLO )
|
||||||
.add( 1900, 1, 1, ZONE_PARIS )
|
.add( 1900, 1, 1, ZONE_PARIS )
|
||||||
.add( 1900, 1, 1, ZONE_AMSTERDAM )
|
.add( 1900, 1, 1, ZONE_AMSTERDAM )
|
||||||
|
@ -85,17 +90,32 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
|
protected void setJdbcValueForNonHibernateWrite(PreparedStatement statement, int parameterIndex) throws SQLException {
|
||||||
statement.setDate( parameterIndex, getExpectedJdbcValueAfterHibernateWrite() );
|
if ( DateAsTimestampRemappingH2Dialect.class.equals( getRemappingDialectClass() ) ) {
|
||||||
|
statement.setTimestamp( parameterIndex, new Timestamp( year - 1900, month - 1, day, 0, 0, 0, 0 ) );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
statement.setDate( parameterIndex, new Date( year - 1900, month - 1, day ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Date getExpectedJdbcValueAfterHibernateWrite() {
|
protected Object getExpectedJdbcValueAfterHibernateWrite() {
|
||||||
return new Date( year - 1900, month - 1, day );
|
if ( DateAsTimestampRemappingH2Dialect.class.equals( getRemappingDialectClass() ) ) {
|
||||||
|
return new Timestamp( year - 1900, month - 1, day, 0, 0, 0, 0 );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return new Date( year - 1900, month - 1, day );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getActualJdbcValue(ResultSet resultSet, int columnIndex) throws SQLException {
|
protected Object getActualJdbcValue(ResultSet resultSet, int columnIndex) throws SQLException {
|
||||||
return resultSet.getDate( columnIndex );
|
if ( DateAsTimestampRemappingH2Dialect.class.equals( getRemappingDialectClass() ) ) {
|
||||||
|
return resultSet.getTimestamp( columnIndex );
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return resultSet.getDate( columnIndex );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Entity(name = ENTITY_NAME)
|
@Entity(name = ENTITY_NAME)
|
||||||
|
@ -116,4 +136,10 @@ public class LocalDateTest extends AbstractJavaTimeTypeTest<LocalDate, LocalDate
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class DateAsTimestampRemappingH2Dialect extends AbstractRemappingH2Dialect {
|
||||||
|
public DateAsTimestampRemappingH2Dialect() {
|
||||||
|
super( Types.DATE, TimestampTypeDescriptor.INSTANCE );
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue