]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_2_1aTest.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_2_1aTest.java
1 package dst.ass1.jpa.tests;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertSame;
6
7 import javax.persistence.Query;
8
9 import org.junit.Before;
10 import org.junit.Test;
11
12 import dst.ass1.jpa.dao.IParticipantDAO;
13 import dst.ass1.jpa.model.IParticipant;
14 import dst.ass1.jpa.util.Constants;
15
16 public class Ass1_2_1aTest extends Ass1_TestBase {
17
18     private IParticipantDAO dao;
19     private Class<? extends IParticipant> entityClass;
20
21     @Before
22     public void setUp() throws Exception {
23         dao = daoFactory.createParticipantDAO();
24         entityClass = modelFactory.createParticipant().getClass();
25     }
26
27     @Test
28     public void namedQuery_withEmailParameter_returnsNonEmptyResultSet() throws Exception {
29         Query query = em.createNamedQuery(Constants.Q_PARTICIPANT_BY_EMAIL);
30
31         try {
32             query.setParameter("email", TestData.PARTICIPANT_1_EMAIL);
33         } catch (IllegalArgumentException e) {
34             throw new AssertionError("Could not set parameter 'email' in named query " + Constants.Q_PARTICIPANT_BY_EMAIL, e);
35         }
36
37         assertEquals("Expected exactly one result", 1, query.getResultList().size());
38     }
39
40     @Test
41     public void findByEmail_returnsCorrectResult() throws Exception {
42         Long participantId = testData.participant1Id;
43
44         IParticipant actual = dao.findByEmail(TestData.PARTICIPANT_1_EMAIL);
45         IParticipant expected = em.find(entityClass, participantId);
46
47         assertEquals(participantId, actual.getId());
48         assertSame(expected, actual); // note that they should actually be the same object due EntityManager caching!
49     }
50
51     @Test
52     public void findByEmail_withNonExistingEmail_returnsNull() throws Exception {
53         assertNull(dao.findByEmail("non@existing.com"));
54     }
55 }