|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.annotations.various; |
6 | 6 |
|
7 | | -import org.hibernate.boot.MetadataSources; |
8 | | -import org.hibernate.boot.registry.StandardServiceRegistry; |
9 | | -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
10 | | -import org.hibernate.boot.spi.MetadataImplementor; |
11 | | -import org.hibernate.testing.orm.junit.BaseUnitTest; |
12 | | -import org.hibernate.testing.util.ServiceRegistryUtil; |
13 | | -import org.hibernate.type.BasicType; |
14 | | -import org.hibernate.type.StandardBasicTypes; |
15 | | -import org.junit.jupiter.api.AfterAll; |
16 | | -import org.junit.jupiter.api.BeforeAll; |
| 7 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 8 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
17 | 10 | import org.junit.jupiter.api.Test; |
18 | 11 |
|
19 | | -import static org.assertj.core.api.Assertions.assertThat; |
| 12 | +import java.sql.Timestamp; |
| 13 | +import java.util.Date; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
20 | 17 |
|
21 | 18 | /** |
22 | 19 | * Test for the @Timestamp annotation. |
23 | 20 | * |
24 | 21 | * @author Hardy Ferentschik |
25 | 22 | */ |
26 | | -@BaseUnitTest |
| 23 | +@SessionFactory |
| 24 | +@DomainModel(annotatedClasses = {VMTimestamped.class, DBTimestamped.class}) |
27 | 25 | public class TimestampTest { |
28 | | - private StandardServiceRegistry ssr; |
29 | | - private MetadataImplementor metadata; |
30 | | - |
31 | | - @BeforeAll |
32 | | - public void setUp() { |
33 | | - ssr = ServiceRegistryUtil.serviceRegistry(); |
34 | | - metadata = (MetadataImplementor) new MetadataSources( ssr ) |
35 | | - .addAnnotatedClass( VMTimestamped.class ) |
36 | | - .addAnnotatedClass( DBTimestamped.class ) |
37 | | - .getMetadataBuilder() |
38 | | - .build(); |
39 | | - } |
40 | | - |
41 | | - @AfterAll |
42 | | - public void tearDown() { |
43 | | - if ( ssr != null ) { |
44 | | - StandardServiceRegistryBuilder.destroy( ssr ); |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - @Test |
49 | | - public void testTimestampSourceIsVM() { |
50 | | - assertTimestampSource( VMTimestamped.class ); |
51 | | - } |
52 | | - |
53 | | - @Test |
54 | | - public void testTimestampSourceIsDB() { |
55 | | - assertTimestampSource( DBTimestamped.class ); |
56 | | - } |
57 | | - |
58 | | - private void assertTimestampSource(Class<?> clazz ) { |
59 | | - assertTimestampSource( clazz, |
60 | | - metadata.getTypeConfiguration().getBasicTypeRegistry() |
61 | | - .resolve( StandardBasicTypes.TIMESTAMP ) ); |
62 | | - } |
| 26 | + @Test void test(SessionFactoryScope scope) { |
| 27 | + scope.inTransaction( session -> { |
| 28 | + var vmTimestamped = new VMTimestamped(); |
| 29 | + session.persist( vmTimestamped ); |
| 30 | + var dbTimestamped = new DBTimestamped(); |
| 31 | + session.persist( dbTimestamped ); |
| 32 | + session.flush(); |
| 33 | + assertNotNull( vmTimestamped.getLastUpdate() ); |
| 34 | + assertNotNull( dbTimestamped.getLastUpdate() ); |
| 35 | + assertInstanceOf( Date.class, vmTimestamped.getLastUpdate() ); |
| 36 | + assertInstanceOf( Timestamp.class, dbTimestamped.getLastUpdate() ); |
| 37 | + } ); |
| 38 | + scope.inTransaction( session -> { |
| 39 | + var vmTimestamped = session.find(VMTimestamped.class, 1); |
| 40 | + var dbTimestamped = session.find(DBTimestamped.class, 1); |
| 41 | + assertNotNull( vmTimestamped.getLastUpdate() ); |
| 42 | + assertNotNull( dbTimestamped.getLastUpdate() ); |
| 43 | + assertInstanceOf( Timestamp.class, vmTimestamped.getLastUpdate() ); |
| 44 | + assertInstanceOf( Timestamp.class, dbTimestamped.getLastUpdate() ); |
| 45 | + } ); |
63 | 46 |
|
64 | | - private void assertTimestampSource(Class<?> clazz, BasicType<?> basicType) { |
65 | | - var persistentClass = metadata.getEntityBinding( clazz.getName() ); |
66 | | - assertThat( persistentClass ).isNotNull(); |
67 | | - var versionProperty = persistentClass.getVersion(); |
68 | | - assertThat( versionProperty ).isNotNull(); |
69 | | - assertThat( versionProperty.getType().getName() ) |
70 | | - .describedAs( "Wrong timestamp type" ) |
71 | | - .isEqualTo( basicType.getName() ); |
72 | 47 | } |
73 | 48 | } |
0 commit comments