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