]> git.somenet.org - pub/jan/dst18.git/blob - ass1-jpa/src/test/java/dst/ass1/jpa/tests/Ass1_1_2_02Test.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-jpa / src / test / java / dst / ass1 / jpa / tests / Ass1_1_2_02Test.java
1 package dst.ass1.jpa.tests;
2
3 import static org.hamcrest.CoreMatchers.isA;
4 import static org.junit.Assert.assertFalse;
5
6 import java.sql.SQLException;
7
8 import javax.persistence.PersistenceException;
9
10 import org.hibernate.PropertyValueException;
11 import org.junit.Rule;
12 import org.junit.Test;
13 import org.junit.rules.ExpectedException;
14
15 import dst.ass1.jpa.ORMService;
16 import dst.ass1.jpa.model.IParticipant;
17 import dst.ass1.jpa.util.Constants;
18
19 /**
20  * Tests the constraint for IParticipant email.
21  */
22 public class Ass1_1_2_02Test {
23
24     @Rule
25     public ORMService orm = new ORMService();
26
27     @Rule
28     public ExpectedException expectedException = ExpectedException.none();
29
30     @Test
31     public void testUniqueConstraint() {
32         new UniqueConstraintTester<>(() -> orm.getModelFactory().createParticipant(), e -> e.setEmail("unique@example.com"))
33                 .run(orm.getEntityManager());
34     }
35
36     @Test
37     public void testNotNullConstraint() {
38         expectedException.expect(PersistenceException.class);
39         expectedException.expectCause(isA(PropertyValueException.class));
40         expectedException.expectMessage("not-null property");
41
42         IParticipant e1 = orm.getModelFactory().createParticipant();
43         e1.setEmail(null);
44         orm.em().getTransaction().begin();
45         orm.em().persist(e1);
46         orm.em().flush();
47     }
48
49     @Test
50     public void testPersonEmailNotNullConstraintJdbc() throws SQLException {
51         assertFalse(orm.getDatabaseGateway().isNullable(Constants.T_PARTICIPANT, Constants.M_PERSON_EMAIL));
52     }
53
54 }