Skip to content

Commit a4a918f

Browse files
committed
HHH-18532 Test cases checking that value is unwrapped to requested type
1 parent 4dcfdb5 commit a4a918f

File tree

1 file changed

+252
-0
lines changed

1 file changed

+252
-0
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
6+
*/
7+
package org.hibernate.orm.test.mapping.type.java;
8+
9+
import java.sql.Time;
10+
import java.sql.Timestamp;
11+
import java.time.Instant;
12+
import java.time.LocalDate;
13+
import java.time.LocalDateTime;
14+
import java.time.LocalTime;
15+
import java.time.OffsetDateTime;
16+
import java.time.OffsetTime;
17+
import java.time.ZonedDateTime;
18+
import java.util.Calendar;
19+
import java.util.Date;
20+
import java.util.TimeZone;
21+
22+
import org.hibernate.HibernateException;
23+
import org.hibernate.dialect.Dialect;
24+
import org.hibernate.dialect.H2Dialect;
25+
import org.hibernate.engine.jdbc.LobCreator;
26+
import org.hibernate.engine.jdbc.NonContextualLobCreator;
27+
import org.hibernate.engine.spi.SessionFactoryImplementor;
28+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
29+
import org.hibernate.type.descriptor.WrapperOptions;
30+
import org.hibernate.type.descriptor.java.CalendarDateJavaType;
31+
import org.hibernate.type.descriptor.java.CalendarJavaType;
32+
import org.hibernate.type.descriptor.java.CalendarTimeJavaType;
33+
import org.hibernate.type.descriptor.java.DateJavaType;
34+
import org.hibernate.type.descriptor.java.InstantJavaType;
35+
import org.hibernate.type.descriptor.java.JdbcDateJavaType;
36+
import org.hibernate.type.descriptor.java.JdbcTimeJavaType;
37+
import org.hibernate.type.descriptor.java.JdbcTimestampJavaType;
38+
import org.hibernate.type.descriptor.java.LocalDateJavaType;
39+
import org.hibernate.type.descriptor.java.LocalDateTimeJavaType;
40+
import org.hibernate.type.descriptor.java.LocalTimeJavaType;
41+
import org.hibernate.type.descriptor.java.OffsetDateTimeJavaType;
42+
import org.hibernate.type.descriptor.java.OffsetTimeJavaType;
43+
import org.hibernate.type.descriptor.java.ZonedDateTimeJavaType;
44+
45+
import org.junit.jupiter.api.Assertions;
46+
import org.junit.jupiter.api.Test;
47+
48+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
49+
import static org.junit.jupiter.api.Assertions.assertThrows;
50+
51+
public class DateSubclassesUnwrapTest {
52+
53+
private static final WrapperOptions wrapperOptions = new WrapperOptions() {
54+
@Override
55+
public SharedSessionContractImplementor getSession() {
56+
return null;
57+
}
58+
59+
@Override
60+
public SessionFactoryImplementor getSessionFactory() {
61+
return null;
62+
}
63+
64+
public boolean useStreamForLobBinding() {
65+
return false;
66+
}
67+
68+
@Override
69+
public int getPreferredSqlTypeCodeForBoolean() {
70+
return 0;
71+
}
72+
73+
public LobCreator getLobCreator() {
74+
return NonContextualLobCreator.INSTANCE;
75+
}
76+
77+
@Override
78+
public TimeZone getJdbcTimeZone() {
79+
return null;
80+
}
81+
82+
private final Dialect dialect = new H2Dialect() {
83+
@Override
84+
public boolean useConnectionToCreateLob() {
85+
return false;
86+
}
87+
};
88+
89+
@Override
90+
public Dialect getDialect() {
91+
return dialect;
92+
}
93+
};
94+
95+
@Test
96+
void testJdbcTimestampJavaType() {
97+
final JdbcTimestampJavaType javaType = JdbcTimestampJavaType.INSTANCE;
98+
final Date date = new Date();
99+
100+
assertInstanceOf( Timestamp.class, javaType.unwrap( date, Timestamp.class, wrapperOptions ) );
101+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( date, java.sql.Date.class, wrapperOptions ) );
102+
assertInstanceOf( Time.class, javaType.unwrap( date, Time.class, wrapperOptions ) );
103+
assertInstanceOf( Date.class, javaType.unwrap( date, Date.class, wrapperOptions ) );
104+
}
105+
106+
@Test
107+
void testJdbcDateJavaType() {
108+
final JdbcDateJavaType javaType = JdbcDateJavaType.INSTANCE;
109+
final Date date = new Date();
110+
111+
assertInstanceOf( Timestamp.class, javaType.unwrap( date, Timestamp.class, wrapperOptions ) );
112+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( date, java.sql.Date.class, wrapperOptions ) );
113+
assertThrows( IllegalArgumentException.class, () -> javaType.unwrap( date, Time.class, wrapperOptions ) );
114+
assertInstanceOf( Date.class, javaType.unwrap( date, Date.class, wrapperOptions ) );
115+
}
116+
117+
@Test
118+
void testJdbcTimeJavaType() {
119+
final JdbcTimeJavaType javaType = JdbcTimeJavaType.INSTANCE;
120+
final Date date = new Date();
121+
122+
assertInstanceOf( Timestamp.class, javaType.unwrap( date, Timestamp.class, wrapperOptions ) );
123+
Assertions.assertThrows(
124+
IllegalArgumentException.class,
125+
() -> javaType.unwrap( date, java.sql.Date.class, wrapperOptions )
126+
);
127+
assertInstanceOf( Time.class, javaType.unwrap( date, Time.class, wrapperOptions ) );
128+
assertInstanceOf( Date.class, javaType.unwrap( date, Date.class, wrapperOptions ) );
129+
}
130+
131+
@Test
132+
void testDateJavaType() {
133+
final DateJavaType javaType = DateJavaType.INSTANCE;
134+
final Date date = new Date();
135+
136+
assertInstanceOf( Timestamp.class, javaType.unwrap( date, Timestamp.class, wrapperOptions ) );
137+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( date, java.sql.Date.class, wrapperOptions ) );
138+
assertInstanceOf( Time.class, javaType.unwrap( date, Time.class, wrapperOptions ) );
139+
assertInstanceOf( Date.class, javaType.unwrap( date, Date.class, wrapperOptions ) );
140+
}
141+
142+
@Test
143+
void testInstantJavaType() {
144+
final InstantJavaType javaType = InstantJavaType.INSTANCE;
145+
final Instant instant = Instant.now();
146+
147+
assertInstanceOf( Timestamp.class, javaType.unwrap( instant, Timestamp.class, wrapperOptions ) );
148+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( instant, java.sql.Date.class, wrapperOptions ) );
149+
assertInstanceOf( Time.class, javaType.unwrap( instant, Time.class, wrapperOptions ) );
150+
assertInstanceOf( Date.class, javaType.unwrap( instant, Date.class, wrapperOptions ) );
151+
}
152+
153+
@Test
154+
void testLocalDateJavaType() {
155+
final LocalDateJavaType javaType = LocalDateJavaType.INSTANCE;
156+
final LocalDate date = LocalDate.now();
157+
158+
assertInstanceOf( Timestamp.class, javaType.unwrap( date, Timestamp.class, wrapperOptions ) );
159+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( date, java.sql.Date.class, wrapperOptions ) );
160+
assertThrows( HibernateException.class, () -> javaType.unwrap( date, Time.class, wrapperOptions ) );
161+
assertInstanceOf( Date.class, javaType.unwrap( date, Date.class, wrapperOptions ) );
162+
}
163+
164+
@Test
165+
void testLocalDateTimeJavaType() {
166+
final LocalDateTimeJavaType javaType = LocalDateTimeJavaType.INSTANCE;
167+
final LocalDateTime dateTime = LocalDateTime.now();
168+
169+
assertInstanceOf( Timestamp.class, javaType.unwrap( dateTime, Timestamp.class, wrapperOptions ) );
170+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( dateTime, java.sql.Date.class, wrapperOptions ) );
171+
assertInstanceOf( Time.class, javaType.unwrap( dateTime, Time.class, wrapperOptions ) );
172+
assertInstanceOf( Date.class, javaType.unwrap( dateTime, Date.class, wrapperOptions ) );
173+
}
174+
175+
@Test
176+
void testLocalTimeJavaType() {
177+
final LocalTimeJavaType javaType = LocalTimeJavaType.INSTANCE;
178+
final LocalTime time = LocalTime.now();
179+
180+
assertInstanceOf( Timestamp.class, javaType.unwrap( time, Timestamp.class, wrapperOptions ) );
181+
assertThrows( HibernateException.class, () -> javaType.unwrap( time, java.sql.Date.class, wrapperOptions ) );
182+
assertInstanceOf( Time.class, javaType.unwrap( time, Time.class, wrapperOptions ) );
183+
assertInstanceOf( Date.class, javaType.unwrap( time, Date.class, wrapperOptions ) );
184+
}
185+
186+
@Test
187+
void testOffsetDateTimeJavaType() {
188+
final OffsetDateTimeJavaType javaType = OffsetDateTimeJavaType.INSTANCE;
189+
final OffsetDateTime dateTime = OffsetDateTime.now();
190+
191+
assertInstanceOf( Timestamp.class, javaType.unwrap( dateTime, Timestamp.class, wrapperOptions ) );
192+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( dateTime, java.sql.Date.class, wrapperOptions ) );
193+
assertInstanceOf( Time.class, javaType.unwrap( dateTime, Time.class, wrapperOptions ) );
194+
assertInstanceOf( Date.class, javaType.unwrap( dateTime, Date.class, wrapperOptions ) );
195+
}
196+
197+
@Test
198+
void testOffsetTimeJavaType() {
199+
final OffsetTimeJavaType javaType = OffsetTimeJavaType.INSTANCE;
200+
final OffsetTime time = OffsetTime.now();
201+
202+
assertInstanceOf( Timestamp.class, javaType.unwrap( time, Timestamp.class, wrapperOptions ) );
203+
assertThrows( IllegalArgumentException.class,
204+
() -> javaType.unwrap( time, java.sql.Date.class, wrapperOptions ) );
205+
assertInstanceOf( Time.class, javaType.unwrap( time, Time.class, wrapperOptions ) );
206+
assertInstanceOf( Date.class, javaType.unwrap( time, Date.class, wrapperOptions ) );
207+
}
208+
209+
@Test
210+
void testZonedDateTimeJavaType() {
211+
final ZonedDateTimeJavaType javaType = ZonedDateTimeJavaType.INSTANCE;
212+
final ZonedDateTime dateTime = ZonedDateTime.now();
213+
214+
assertInstanceOf( Timestamp.class, javaType.unwrap( dateTime, Timestamp.class, wrapperOptions ) );
215+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( dateTime, java.sql.Date.class, wrapperOptions ) );
216+
assertInstanceOf( Time.class, javaType.unwrap( dateTime, Time.class, wrapperOptions ) );
217+
assertInstanceOf( Date.class, javaType.unwrap( dateTime, Date.class, wrapperOptions ) );
218+
}
219+
220+
@Test
221+
void testCalendarDateJavaType() {
222+
final CalendarDateJavaType javaType = CalendarDateJavaType.INSTANCE;
223+
final Calendar calendar = Calendar.getInstance();
224+
225+
assertInstanceOf( Timestamp.class, javaType.unwrap( calendar, Timestamp.class, wrapperOptions ) );
226+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( calendar, java.sql.Date.class, wrapperOptions ) );
227+
assertInstanceOf( Time.class, javaType.unwrap( calendar, Time.class, wrapperOptions ) );
228+
assertInstanceOf( Date.class, javaType.unwrap( calendar, Date.class, wrapperOptions ) );
229+
}
230+
231+
@Test
232+
void testCalendarJavaType() {
233+
final CalendarJavaType javaType = CalendarJavaType.INSTANCE;
234+
final Calendar calendar = Calendar.getInstance();
235+
236+
assertInstanceOf( Timestamp.class, javaType.unwrap( calendar, Timestamp.class, wrapperOptions ) );
237+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( calendar, java.sql.Date.class, wrapperOptions ) );
238+
assertInstanceOf( Time.class, javaType.unwrap( calendar, Time.class, wrapperOptions ) );
239+
assertInstanceOf( Date.class, javaType.unwrap( calendar, Date.class, wrapperOptions ) );
240+
}
241+
242+
@Test
243+
void testCalendarTimeJavaType() {
244+
final CalendarTimeJavaType javaType = CalendarTimeJavaType.INSTANCE;
245+
final Calendar calendar = Calendar.getInstance();
246+
247+
assertInstanceOf( Timestamp.class, javaType.unwrap( calendar, Timestamp.class, wrapperOptions ) );
248+
assertInstanceOf( java.sql.Date.class, javaType.unwrap( calendar, java.sql.Date.class, wrapperOptions ) );
249+
assertInstanceOf( Time.class, javaType.unwrap( calendar, Time.class, wrapperOptions ) );
250+
assertInstanceOf( Date.class, javaType.unwrap( calendar, Date.class, wrapperOptions ) );
251+
}
252+
}

0 commit comments

Comments
 (0)