1 package dst.ass1.jpa.tests;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertSame;
7 import javax.persistence.Query;
9 import org.junit.Before;
10 import org.junit.Test;
12 import dst.ass1.jpa.dao.IParticipantDAO;
13 import dst.ass1.jpa.model.IParticipant;
14 import dst.ass1.jpa.util.Constants;
16 public class Ass1_2_1aTest extends Ass1_TestBase {
18 private IParticipantDAO dao;
19 private Class<? extends IParticipant> entityClass;
22 public void setUp() throws Exception {
23 dao = daoFactory.createParticipantDAO();
24 entityClass = modelFactory.createParticipant().getClass();
28 public void namedQuery_withEmailParameter_returnsNonEmptyResultSet() throws Exception {
29 Query query = em.createNamedQuery(Constants.Q_PARTICIPANT_BY_EMAIL);
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);
37 assertEquals("Expected exactly one result", 1, query.getResultList().size());
41 public void findByEmail_returnsCorrectResult() throws Exception {
42 Long participantId = testData.participant1Id;
44 IParticipant actual = dao.findByEmail(TestData.PARTICIPANT_1_EMAIL);
45 IParticipant expected = em.find(entityClass, participantId);
47 assertEquals(participantId, actual.getId());
48 assertSame(expected, actual); // note that they should actually be the same object due EntityManager caching!
52 public void findByEmail_withNonExistingEmail_returnsNull() throws Exception {
53 assertNull(dao.findByEmail("non@existing.com"));