|
| 1 | +package com.baeldung.hibernate.creationupdatetimestamp; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | + |
| 7 | +import java.time.Instant; |
| 8 | + |
| 9 | +import org.h2.Driver; |
| 10 | +import org.hibernate.Session; |
| 11 | +import org.hibernate.SessionFactory; |
| 12 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 13 | +import org.hibernate.cfg.Configuration; |
| 14 | +import org.hibernate.dialect.H2Dialect; |
| 15 | +import org.hibernate.service.ServiceRegistry; |
| 16 | +import org.junit.jupiter.api.AfterAll; |
| 17 | +import org.junit.jupiter.api.BeforeAll; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import com.baeldung.hibernate.creationupdatetimestamp.model.Book; |
| 21 | + |
| 22 | +class HibernateCreationUpdateTimestampIntegrationTest { |
| 23 | + |
| 24 | + private static SessionFactory sessionFactory; |
| 25 | + |
| 26 | + private Session session; |
| 27 | + |
| 28 | + @BeforeAll |
| 29 | + public static void beforeTests() { |
| 30 | + Configuration configuration = new Configuration().addAnnotatedClass(Book.class) |
| 31 | + .setProperty("hibernate.dialect", H2Dialect.class.getName()) |
| 32 | + .setProperty("hibernate.connection.driver_class", Driver.class.getName()) |
| 33 | + .setProperty("hibernate.connection.url", "jdbc:h2:mem:test") |
| 34 | + .setProperty("hibernate.connection.username", "sa") |
| 35 | + .setProperty("hibernate.connection.password", "") |
| 36 | + .setProperty("hibernate.hbm2ddl.auto", "update"); |
| 37 | + |
| 38 | + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()) |
| 39 | + .build(); |
| 40 | + |
| 41 | + sessionFactory = configuration.buildSessionFactory(serviceRegistry); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void whenCreatingEntity_ThenCreatedOnIsSet() { |
| 46 | + session = sessionFactory.openSession(); |
| 47 | + session.beginTransaction(); |
| 48 | + Book book = new Book(); |
| 49 | + |
| 50 | + session.save(book); |
| 51 | + session.getTransaction() |
| 52 | + .commit(); |
| 53 | + session.close(); |
| 54 | + |
| 55 | + assertNotNull(book.getCreatedOn()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void whenCreatingEntity_ThenCreatedOnAndLastUpdatedOnAreBothSet() { |
| 60 | + session = sessionFactory.openSession(); |
| 61 | + session.beginTransaction(); |
| 62 | + Book book = new Book(); |
| 63 | + |
| 64 | + session.save(book); |
| 65 | + session.getTransaction() |
| 66 | + .commit(); |
| 67 | + session.close(); |
| 68 | + |
| 69 | + assertNotNull(book.getCreatedOn()); |
| 70 | + assertNotNull(book.getLastUpdatedOn()); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void whenCreatingEntity_ThenCreatedOnAndLastUpdatedOnAreNotEqual() { |
| 75 | + session = sessionFactory.openSession(); |
| 76 | + session.beginTransaction(); |
| 77 | + Book book = new Book(); |
| 78 | + |
| 79 | + session.save(book); |
| 80 | + session.getTransaction() |
| 81 | + .commit(); |
| 82 | + session.close(); |
| 83 | + |
| 84 | + assertNotEquals(book.getCreatedOn(), book.getLastUpdatedOn()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void whenUpdatingEntity_ThenLastUpdatedOnIsUpdatedAndCreatedOnStaysTheSame() { |
| 89 | + session = sessionFactory.openSession(); |
| 90 | + session.beginTransaction(); |
| 91 | + Book book = new Book(); |
| 92 | + session.save(book); |
| 93 | + session.flush(); |
| 94 | + Instant createdOnAfterCreation = book.getCreatedOn(); |
| 95 | + Instant lastUpdatedOnAfterCreation = book.getLastUpdatedOn(); |
| 96 | + |
| 97 | + String newName = "newName"; |
| 98 | + book.setTitle(newName); |
| 99 | + session.getTransaction() |
| 100 | + .commit(); |
| 101 | + session.close(); |
| 102 | + Instant createdOnAfterUpdate = book.getCreatedOn(); |
| 103 | + Instant lastUpdatedOnAfterUpdate = book.getLastUpdatedOn(); |
| 104 | + |
| 105 | + assertEquals(newName, book.getTitle()); |
| 106 | + assertNotNull(createdOnAfterUpdate); |
| 107 | + assertNotNull(lastUpdatedOnAfterUpdate); |
| 108 | + assertEquals(createdOnAfterCreation, createdOnAfterUpdate); |
| 109 | + assertNotEquals(lastUpdatedOnAfterCreation, lastUpdatedOnAfterUpdate); |
| 110 | + } |
| 111 | + |
| 112 | + @AfterAll |
| 113 | + static void afterTests() { |
| 114 | + sessionFactory.close(); |
| 115 | + } |
| 116 | +} |
0 commit comments