-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPersonTest.java
99 lines (86 loc) · 3.56 KB
/
PersonTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
*
*/
package org.uli.jpa;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author uli
*
*/
public class PersonTest {
private static final String PERSISTENCE_UNIT_NAME = "jpa";
private static EntityManagerFactory entityManagerFactory;
private static Map<Integer, Person> persons = new HashMap<Integer, Person>();
@BeforeClass
static public void initEm() {
Map<String,String> persistenceProperties = new HashMap<String,String>();
persistenceProperties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
persistenceProperties.put("javax.persistence.jdbc.url","jdbc:derby:memory:derbyDb;create=true");
entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME, persistenceProperties);
}
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("select p from Person p");
if (query.getResultList().isEmpty()) {
// Create new persons
entityManager.getTransaction().begin();
for (int i=0; i<40; i++) {
Person person = new Person();
person.setFirstName("firstName-"+i);
person.setLastName("lastName-"+i);
entityManager.persist(person);
persons.put(person.getPersonId(), person);
}
entityManager.getTransaction().commit();
}
entityManager.close();
}
@Test
public void testNotEmpty() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Query query = entityManager.createQuery("select p from Person p");
@SuppressWarnings("unchecked")
List<Object> resultList = query.getResultList();
assertFalse(resultList.isEmpty());
assertEquals(persons.size(), resultList.size());
entityManager.close();
}
@Test
public void testFind() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
for (Person p : persons.values()) {
Integer personId = p.getPersonId();
Person dbPerson = entityManager.find(Person.class, personId);
assertEquals("Person-"+personId+", personId:", personId, dbPerson.getPersonId());
assertEquals("Person-"+personId+", firstName:", p.getFirstName(), dbPerson.getFirstName());
assertEquals("Person-"+personId+", lastName:", p.getLastName(), dbPerson.getLastName());
}
entityManager.close();
}
@Test
public void testGetReference() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
for (Person p : persons.values()) {
Integer personId = p.getPersonId();
Person dbPerson = entityManager.getReference(Person.class, personId);
assertEquals("Person-"+personId+", personId:", personId, dbPerson.getPersonId());
assertEquals("Person-"+personId+", firstName:", p.getFirstName(), dbPerson.getFirstName());
assertEquals("Person-"+personId+", lastName:", p.getLastName(), dbPerson.getLastName());
}
entityManager.close();
}
}