View Javadoc
1   package xuml.tools.jaxb.compiler.test;
2   
3   import javax.persistence.EntityManager;
4   
5   import org.junit.AfterClass;
6   import org.junit.BeforeClass;
7   import org.junit.Test;
8   
9   import specialization.A;
10  import specialization.B;
11  import specialization.C;
12  import specialization.Context;
13  import xuml.tools.model.compiler.runtime.RelationshipNotEstablishedException;
14  import xuml.tools.model.compiler.runtime.TooManySpecializationsException;
15  
16  public class SpecializationTest {
17  
18      @BeforeClass
19      public static void setup() {
20          Context.setEntityManagerFactory(PersistenceHelper.createEmf("specialization"), 10);
21      }
22  
23      @AfterClass
24      public static void close() {
25          Context.stop();
26          Context.close();
27      }
28  
29      @Test
30      public void testCanCreate() {
31          EntityManager em = Context.createEntityManager();
32          try {
33              em.getTransaction().begin();
34              A a = A.create("something");
35              B b = B.create("hello");
36              a.setTwo(2);
37              a.setB_R1(b);
38              b.setA_R1(a);
39              b.setNumber(3);
40              a.persist(em);
41              b.persist(em);
42              em.getTransaction().commit();
43          } finally {
44              em.close();
45          }
46      }
47  
48      @Test(expected = RelationshipNotEstablishedException.class)
49      public void testCannotCreateAWithoutOneSpecialization() {
50          EntityManager em = Context.createEntityManager();
51          try {
52              em.getTransaction().begin();
53              A a = A.create("something2");
54              a.setTwo(2);
55              a.persist(em);
56              em.getTransaction().commit();
57          } finally {
58              em.close();
59          }
60      }
61  
62      @Test(expected = TooManySpecializationsException.class)
63      public void testCannotCreateAWithTwoSpecializations() {
64          EntityManager em = Context.createEntityManager();
65          try {
66              em.getTransaction().begin();
67              A a = A.create("something3");
68              B b = B.create("hello3");
69              C c = C.create("there3");
70              a.setTwo(2);
71              a.setB_R1(b);
72              a.setC_R1(c);
73              c.setA_R1(a);
74              b.setA_R1(a);
75              b.setNumber(3);
76              a.persist(em);
77              b.persist(em);
78              em.getTransaction().commit();
79          } finally {
80              em.close();
81          }
82      }
83  
84      // TODO cannot create A with both B and C
85  }