]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_1_1_07Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_1_1_07Test.java
1 package dst.ass1.jpa.tests;
2
3 import static org.hamcrest.CoreMatchers.isA;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertNotNull;
6 import static org.junit.Assert.assertNull;
7
8 import javax.persistence.PersistenceException;
9
10 import org.hibernate.PropertyValueException;
11 import org.hibernate.exception.ConstraintViolationException;
12 import org.junit.Test;
13
14 import dst.ass1.jpa.model.ICourse;
15 import dst.ass1.jpa.model.IMetadata;
16
17 /**
18  * Tests if the ICourse and IMetadata relation is implemented correctly.
19  */
20 public class Ass1_1_1_07Test extends Ass1_TestBase {
21
22     @Test
23     public void testMetadataAssociation() {
24         ICourse course1 = daoFactory.createCourseDAO().findById(testData.course1Id);
25         assertNotNull(course1);
26         assertEquals(testData.metadata1Id, course1.getMetadata().getId());
27
28         ICourse course2 = daoFactory.createCourseDAO().findById(testData.course2Id);
29         assertNotNull(course2);
30         assertEquals(testData.metadata2Id, course2.getMetadata().getId());
31     }
32
33     @Test
34     public void testMetadataNonOptionalConstraint() {
35         expectedException.expect(PersistenceException.class);
36         expectedException.expectCause(isA(PropertyValueException.class));
37         expectedException.expectMessage("not-null property");
38
39         ICourse course1 = daoFactory.createCourseDAO().findById(testData.course1Id);
40         assertNotNull(course1);
41         course1.setMetadata(null);
42         em.getTransaction().begin();
43         em.persist(course1);
44         em.flush();
45     }
46
47     @Test
48     public void testMetadataUniqueConstraint() throws Exception {
49         ICourse course1 = daoFactory.createCourseDAO().findById(testData.course1Id);
50         IMetadata metadata1 = course1.getMetadata();
51
52         ICourse course5 = modelFactory.createCourse();
53         course5.setName("course5");
54         course5.setMetadata(metadata1);
55
56         em.getTransaction().begin();
57         expectedException.expect(PersistenceException.class);
58         expectedException.expectCause(isA(ConstraintViolationException.class));
59         expectedException.reportMissingExceptionWithMessage(
60                 "Persisting the same metadata object with a different course should result in a constraint violation"
61         );
62         em.persist(course5);
63         em.flush();
64     }
65
66     @Test
67     public void testCourseMetadataDeleteCascade() throws Exception {
68         ICourse course = daoFactory.createCourseDAO().findById(testData.course4Id);
69         Long metadataId = course.getMetadata().getId();
70
71         em.getTransaction().begin();
72         try {
73             em.remove(course);
74             em.flush();
75         } catch (PersistenceException e) {
76             throw new AssertionError("Removing a course should not result in a PersistenceException", e);
77         }
78         em.getTransaction().commit();
79
80         em.getTransaction().begin();
81         IMetadata metadata = em.find(modelFactory.createMetadata().getClass(), metadataId);
82         assertNull("Expected metadata to be null after associated course was deleted", metadata);
83     }
84
85
86 }