]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_3_1Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_3_1Test.java
1 package dst.ass1.jpa.tests;
2
3 import static org.hamcrest.Matchers.greaterThan;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertThat;
6
7 import java.util.Date;
8
9 import javax.persistence.EntityManager;
10 import javax.persistence.EntityTransaction;
11
12 import org.junit.Rule;
13 import org.junit.Test;
14
15 import dst.ass1.jpa.ORMService;
16 import dst.ass1.jpa.model.IMaterial;
17
18 public class Ass1_3_1Test {
19
20     @Rule
21     public ORMService orm = new ORMService();
22
23     @Test
24     public void entityListener_prePersistSetsPropertiesCorrectly() throws InterruptedException {
25         Date then = new Date();
26         Thread.sleep(50);
27
28         EntityManager em = orm.getEntityManager();
29
30         IMaterial material1 = orm.getModelFactory().createMaterial();
31
32         material1.setUrn("material1");
33
34
35         EntityTransaction tx;
36         // persist
37         tx = em.getTransaction();
38         tx.begin();
39         em.persist(material1);
40         em.flush();
41         tx.commit();
42
43         assertNotNull(material1.getUploaded());
44         assertNotNull(material1.getLastUpdate());
45
46         assertThat(material1.getUploaded(), greaterThan(then));
47         assertThat(material1.getLastUpdate(), greaterThan(then));
48     }
49
50     @Test
51     public void entityListener_preUpdateSetsPropertiesCorrectly() throws InterruptedException {
52         EntityManager em = orm.getEntityManager();
53
54         IMaterial material1 = orm.getModelFactory().createMaterial();
55
56         material1.setUrn("material1");
57
58         EntityTransaction tx;
59         // persist
60         tx = em.getTransaction();
61         tx.begin();
62         em.persist(material1);
63         em.flush();
64         tx.commit();
65
66         Date then = new Date();
67
68         Thread.sleep(50);
69
70         // update
71         tx = em.getTransaction();
72         tx.begin();
73         material1.setType("sometype");
74         em.persist(material1);
75         em.flush();
76         tx.commit();
77
78         assertNotNull(material1.getLastUpdate());
79         assertThat(material1.getLastUpdate(), greaterThan(then));
80     }
81
82 }