1 package xuml.tools.jaxb.compiler.test;
2
3 import javax.persistence.EntityManager;
4 import javax.persistence.EntityManagerFactory;
5 import javax.persistence.RollbackException;
6
7 import one_many_to_many_association.A;
8 import one_many_to_many_association.B;
9 import one_many_to_many_association.C;
10 import one_many_to_many_association.Context;
11
12 import org.junit.AfterClass;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15
16 import xuml.tools.model.compiler.runtime.RelationshipNotEstablishedException;
17
18 public class BinaryAssociationOneManyToManyAssociationClassTest {
19
20 @BeforeClass
21 public static void setup() {
22 EntityManagerFactory emf = PersistenceHelper.createEmf("one-many-to-many-association");
23 Context.setEntityManagerFactory(emf, 10);
24 }
25
26 @AfterClass
27 public static void shutdown() {
28 Context.close();
29 }
30
31
32
33 @Test(expected = RelationshipNotEstablishedException.class)
34 public void testCannotCreateBWithoutInstanceOfAViaC() {
35
36 EntityManager em = Context.createEntityManager();
37 try {
38 em.getTransaction().begin();
39 B.create("b").persist(em);
40 } finally {
41 em.close();
42 }
43 }
44
45 @Test
46 public void testCanCreateInstanceOfAWithoutB() {
47
48 EntityManager em = Context.createEntityManager();
49 try {
50 em.getTransaction().begin();
51 A.create("a").persist(em);
52 em.getTransaction().commit();
53 } finally {
54 em.close();
55 }
56 }
57
58 @Test
59 public void testCanCreateInstanceOfBWithA() {
60
61 EntityManager em = Context.createEntityManager();
62 try {
63 em.getTransaction().begin();
64 A a = A.create("a1").persist(em);
65 B b = B.create("b1");
66 C c = C.create("c1");
67 c.setA_R1(a);
68 c.setB_R1(b);
69 c.setDescription("hello");
70 a.getC_R1().add(c);
71 b.getC_R1().add(c);
72 em.persist(b);
73 em.persist(c);
74 em.getTransaction().commit();
75 } finally {
76 em.close();
77 }
78 }
79
80 @Test(expected = RollbackException.class)
81 public void testCannotCreateTwoLinksBetweenAAndB() {
82
83 EntityManager em = Context.createEntityManager();
84 try {
85 em.getTransaction().begin();
86 A a = A.create("a2").persist(em);
87 B b = B.create("b2");
88 C c = C.create("c2");
89 c.setA_R1(a);
90 c.setB_R1(b);
91 c.setDescription("hello");
92 a.getC_R1().add(c);
93 b.getC_R1().add(c);
94 em.persist(b);
95 em.persist(c);
96 C c3 = C.create("c3");
97 c3.setA_R1(a);
98 c3.setB_R1(b);
99 c3.setDescription("hello");
100 a.getC_R1().add(c3);
101 b.getC_R1().add(c3);
102 c3.persist(em);
103 em.getTransaction().commit();
104 } finally {
105 em.close();
106 }
107 }
108 }