1 package dst.ass1.jpa.tests;
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;
8 import javax.persistence.PersistenceException;
10 import org.hibernate.PropertyValueException;
11 import org.hibernate.exception.ConstraintViolationException;
12 import org.junit.Test;
14 import dst.ass1.jpa.model.ICourse;
15 import dst.ass1.jpa.model.IMetadata;
18 * Tests if the ICourse and IMetadata relation is implemented correctly.
20 public class Ass1_1_1_07Test extends Ass1_TestBase {
23 public void testMetadataAssociation() {
24 ICourse course1 = daoFactory.createCourseDAO().findById(testData.course1Id);
25 assertNotNull(course1);
26 assertEquals(testData.metadata1Id, course1.getMetadata().getId());
28 ICourse course2 = daoFactory.createCourseDAO().findById(testData.course2Id);
29 assertNotNull(course2);
30 assertEquals(testData.metadata2Id, course2.getMetadata().getId());
34 public void testMetadataNonOptionalConstraint() {
35 expectedException.expect(PersistenceException.class);
36 expectedException.expectCause(isA(PropertyValueException.class));
37 expectedException.expectMessage("not-null property");
39 ICourse course1 = daoFactory.createCourseDAO().findById(testData.course1Id);
40 assertNotNull(course1);
41 course1.setMetadata(null);
42 em.getTransaction().begin();
48 public void testMetadataUniqueConstraint() throws Exception {
49 ICourse course1 = daoFactory.createCourseDAO().findById(testData.course1Id);
50 IMetadata metadata1 = course1.getMetadata();
52 ICourse course5 = modelFactory.createCourse();
53 course5.setName("course5");
54 course5.setMetadata(metadata1);
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"
67 public void testCourseMetadataDeleteCascade() throws Exception {
68 ICourse course = daoFactory.createCourseDAO().findById(testData.course4Id);
69 Long metadataId = course.getMetadata().getId();
71 em.getTransaction().begin();
75 } catch (PersistenceException e) {
76 throw new AssertionError("Removing a course should not result in a PersistenceException", e);
78 em.getTransaction().commit();
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);