| 
 | 1 | +package org.hibernate.orm.test.mapping.type.java;  | 
 | 2 | + | 
 | 3 | +import java.sql.Time;  | 
 | 4 | +import java.time.LocalDate;  | 
 | 5 | +import java.time.LocalTime;  | 
 | 6 | +import java.time.ZoneId;  | 
 | 7 | +import java.util.TimeZone;  | 
 | 8 | + | 
 | 9 | +import org.hibernate.annotations.JavaType;  | 
 | 10 | +import org.hibernate.community.dialect.InformixDialect;  | 
 | 11 | +import org.hibernate.type.descriptor.java.JdbcTimeJavaType;  | 
 | 12 | + | 
 | 13 | +import org.hibernate.testing.orm.junit.DomainModel;  | 
 | 14 | +import org.hibernate.testing.orm.junit.SessionFactory;  | 
 | 15 | +import org.hibernate.testing.orm.junit.SessionFactoryScope;  | 
 | 16 | +import org.hibernate.testing.orm.junit.SkipForDialect;  | 
 | 17 | +import org.junit.jupiter.api.BeforeEach;  | 
 | 18 | +import org.junit.jupiter.api.Disabled;  | 
 | 19 | +import org.junit.jupiter.api.Test;  | 
 | 20 | + | 
 | 21 | +import jakarta.persistence.Entity;  | 
 | 22 | +import jakarta.persistence.Id;  | 
 | 23 | +import jakarta.persistence.Table;  | 
 | 24 | + | 
 | 25 | +import static org.junit.jupiter.api.Assertions.assertEquals;  | 
 | 26 | +import static org.junit.jupiter.api.Assertions.assertTrue;  | 
 | 27 | + | 
 | 28 | +/**  | 
 | 29 | + * Test case for @see org.hibernate.type.descriptor.java.JdbcTimeJavaType checking that retrieved value of  | 
 | 30 | + *  | 
 | 31 | + * @see java.sql.Time field is equal to saved one.  | 
 | 32 | + * <p>  | 
 | 33 | + * Date/time used for testing is June 20th 2024 at midnight.  | 
 | 34 | + * <p>  | 
 | 35 | + * Three identical tests are created, for three different time zones:  | 
 | 36 | + * - good : America/Chihuahua - time zone offset at test time is identical to offset at January 1st 1970; this test is successfull for all databases  | 
 | 37 | + * - bad :  America/Cancun - time zone offsets at test time is different from offset at January 1st 1970; this test is failing for some databases  | 
 | 38 | + * - ugly : America/Hermosillo - there is gap at midnight of January 1st 1970, so that year 1970 starts at 01:00 instead at 00:00; all tests with local time where local time is zero will fail  | 
 | 39 | + *  | 
 | 40 | + * [IANA Time Zone Database version 2024b (2024-09-04) removed 'uglay' time zones]  | 
 | 41 | + */  | 
 | 42 | + | 
 | 43 | +@DomainModel(annotatedClasses = GoodBadUglyTest.Times.class)  | 
 | 44 | +@SessionFactory  | 
 | 45 | +@SkipForDialect(dialectClass = InformixDialect.class)  | 
 | 46 | +@Disabled  | 
 | 47 | +public class GoodBadUglyTest {  | 
 | 48 | + | 
 | 49 | +	@BeforeEach  | 
 | 50 | +	void clearData(final SessionFactoryScope scope) {  | 
 | 51 | +		scope.inTransaction( session -> session.createMutationQuery( "delete from SqlTimeTest" ).executeUpdate() );  | 
 | 52 | +	}  | 
 | 53 | + | 
 | 54 | +	@Test  | 
 | 55 | +	void good(final SessionFactoryScope scope) {  | 
 | 56 | +		test( scope, "America/Chihuahua" );  | 
 | 57 | +	}  | 
 | 58 | + | 
 | 59 | +	@Test  | 
 | 60 | +	void bad(final SessionFactoryScope scope) {  | 
 | 61 | +		test( scope, "America/Cancun" );  | 
 | 62 | +	}  | 
 | 63 | + | 
 | 64 | +	@Test  | 
 | 65 | +	@Disabled  | 
 | 66 | +	void ugly(final SessionFactoryScope scope) {  | 
 | 67 | +		test( scope, "America/Hermosillo" );  | 
 | 68 | +	}  | 
 | 69 | + | 
 | 70 | +	private static void test(final SessionFactoryScope scope, final String zoneId) {  | 
 | 71 | +		final var original = TimeZone.getDefault();  | 
 | 72 | +		TimeZone.setDefault( TimeZone.getTimeZone( zoneId ) );  | 
 | 73 | +		try {  | 
 | 74 | +			final var now = LocalDate.of( 2024, 6, 20 ).atStartOfDay( ZoneId.systemDefault() );  | 
 | 75 | +			final var expected = scope.fromTransaction( session -> {  | 
 | 76 | +				final var entity = new Times( 1 );  | 
 | 77 | +				entity.sqlTime = new Time( now.toInstant().toEpochMilli() );  | 
 | 78 | +				session.persist( entity );  | 
 | 79 | +				return entity;  | 
 | 80 | +			} );  | 
 | 81 | + | 
 | 82 | +			final var actual = scope.fromSession( session -> session.get( Times.class, expected.id ) );  | 
 | 83 | + | 
 | 84 | +			assertTrue(  | 
 | 85 | +					JdbcTimeJavaType.INSTANCE.areEqual( expected.sqlTime, actual.sqlTime ),  | 
 | 86 | +					"expected %s, but was %s".formatted( expected.sqlTime, actual.sqlTime )  | 
 | 87 | +			);  | 
 | 88 | + | 
 | 89 | +			assertEquals(  | 
 | 90 | +					LocalTime.MIDNIGHT,  | 
 | 91 | +					actual.sqlTime.toLocalTime(),  | 
 | 92 | +					"Expecting that LocalTime.MIDNIGHT is 00:00"  | 
 | 93 | +			);  | 
 | 94 | +		}  | 
 | 95 | +		finally {  | 
 | 96 | +			TimeZone.setDefault( original );  | 
 | 97 | +		}  | 
 | 98 | +	}  | 
 | 99 | + | 
 | 100 | +	@Entity(name = "SqlTimeTest")  | 
 | 101 | +	@Table(name = "sql_time_test")  | 
 | 102 | +	public static class Times {  | 
 | 103 | +		@Id  | 
 | 104 | +		private Integer id;  | 
 | 105 | + | 
 | 106 | +		@JavaType(JdbcTimeJavaType.class)  | 
 | 107 | +		private Time sqlTime;  | 
 | 108 | + | 
 | 109 | + | 
 | 110 | +		public Times() {  | 
 | 111 | +		}  | 
 | 112 | + | 
 | 113 | +		public Times(final Integer id) {  | 
 | 114 | +			this.id = id;  | 
 | 115 | +		}  | 
 | 116 | + | 
 | 117 | +		public Integer getId() {  | 
 | 118 | +			return id;  | 
 | 119 | +		}  | 
 | 120 | + | 
 | 121 | +		public void setId(final Integer id) {  | 
 | 122 | +			this.id = id;  | 
 | 123 | +		}  | 
 | 124 | + | 
 | 125 | +		public Time getSqlTime() {  | 
 | 126 | +			return sqlTime;  | 
 | 127 | +		}  | 
 | 128 | + | 
 | 129 | +		public void setSqlTime(final Time sqlTime) {  | 
 | 130 | +			this.sqlTime = sqlTime;  | 
 | 131 | +		}  | 
 | 132 | + | 
 | 133 | +		@Override  | 
 | 134 | +		public String toString() {  | 
 | 135 | +			return "Times{id=%d, sqlTime=%s}".formatted( id, sqlTime );  | 
 | 136 | +		}  | 
 | 137 | +	}  | 
 | 138 | +}  | 
0 commit comments