1 package dst.ass1.jpa.tests;
3 import static org.hamcrest.core.IsCollectionContaining.hasItem;
4 import static org.hamcrest.core.IsCollectionContaining.hasItems;
5 import static org.junit.Assert.assertEquals;
6 import static org.junit.Assert.assertFalse;
7 import static org.junit.Assert.assertNotNull;
8 import static org.junit.Assert.assertThat;
9 import static org.junit.Assert.assertTrue;
11 import java.sql.ResultSet;
12 import java.sql.Statement;
13 import java.util.List;
15 import javax.persistence.EntityManager;
17 import org.hibernate.Session;
18 import org.junit.Test;
20 import dst.ass1.jpa.dao.ILessonDAO;
21 import dst.ass1.jpa.dao.impl.DAOFactory;
22 import dst.ass1.jpa.model.ILesson;
23 import dst.ass1.jpa.util.Constants;
26 * Tests the self reference of ILesson.
28 public class Ass1_1_1_10Test extends Ass1_TestBase {
31 public void testLessonSelfAssociation() {
32 EntityManager em = orm.createEntityManager();
33 ILessonDAO dao = new DAOFactory(em).createLessonDAO();
35 ILesson lesson1 = dao.findById(testData.lesson1Id);
36 ILesson lesson2 = dao.findById(testData.lesson2Id);
37 ILesson lesson3 = dao.findById(testData.lesson3Id);
38 ILesson lesson4 = dao.findById(testData.lesson4Id);
39 assertNotNull(lesson1.getRequiredByLessons());
40 assertNotNull(lesson2.getRequiredLessons());
41 assertNotNull(lesson2.getRequiredByLessons());
42 assertNotNull(lesson3.getRequiredLessons());
43 assertNotNull(lesson3.getRequiredByLessons());
44 assertNotNull(lesson4.getRequiredLessons());
46 List<Long> lesson1requiredBy = map(lesson1.getRequiredByLessons(), ILesson::getId);
47 List<Long> lesson2required = map(lesson2.getRequiredLessons(), ILesson::getId);
48 List<Long> lesson2requiredBy = map(lesson2.getRequiredByLessons(), ILesson::getId);
49 List<Long> lesson3required = map(lesson3.getRequiredLessons(), ILesson::getId);
50 List<Long> lesson3requiredBy = map(lesson3.getRequiredByLessons(), ILesson::getId);
51 List<Long> lesson4required = map(lesson4.getRequiredLessons(), ILesson::getId);
53 assertThat(lesson1requiredBy, hasItems(testData.lesson2Id, testData.lesson3Id));
54 assertThat(lesson2required, hasItem(testData.lesson1Id));
55 assertThat(lesson3required, hasItem(testData.lesson1Id));
56 assertThat(lesson2requiredBy, hasItem(testData.lesson4Id));
57 assertThat(lesson3requiredBy, hasItem(testData.lesson4Id));
58 assertThat(lesson4required, hasItems(testData.lesson2Id, testData.lesson3Id));
62 public void testLessonSelfAssociationJdbc() {
63 String sql = "SELECT " + Constants.I_LESSON_REQUIRED + ", " + Constants.I_LESSON_REQUIRED_BY +
64 " FROM " + Constants.J_LESSONS_REQUIRED +
65 " ORDER BY " + Constants.I_LESSON_REQUIRED + ", " + Constants.I_LESSON_REQUIRED_BY;
67 em.unwrap(Session.class).doWork(connection -> {
69 try (Statement stmt = connection.createStatement()) {
70 ResultSet rs = stmt.executeQuery(sql);
71 assertTrue(rs.next());
73 assertEquals((long) testData.lesson1Id, rs.getLong(Constants.I_LESSON_REQUIRED));
74 assertEquals((long) testData.lesson2Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
76 assertTrue(rs.next());
78 assertEquals((long) testData.lesson1Id, rs.getLong(Constants.I_LESSON_REQUIRED));
79 assertEquals((long) testData.lesson3Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
81 assertTrue(rs.next());
83 assertEquals((long) testData.lesson2Id, rs.getLong(Constants.I_LESSON_REQUIRED));
84 assertEquals((long) testData.lesson4Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
86 assertTrue(rs.next());
88 assertEquals((long) testData.lesson3Id, rs.getLong(Constants.I_LESSON_REQUIRED));
89 assertEquals((long) testData.lesson4Id, rs.getLong(Constants.I_LESSON_REQUIRED_BY));
91 assertFalse(rs.next());