PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/tests_bugs/com/google/appengine/datanucleus/bugs/jpa/Issue167Test.java

http://datanucleus-appengine.googlecode.com/
Java | 92 lines | 76 code | 16 blank | 0 comment | 5 complexity | 041866aad1e3b5b6a60e9bb0309ae3cd MD5 | raw file
 1package com.google.appengine.datanucleus.bugs.jpa;
 2
 3import java.util.UUID;
 4
 5import javax.persistence.EntityManager;
 6
 7import org.datanucleus.util.NucleusLogger;
 8
 9import com.google.appengine.datanucleus.bugs.test.Issue167Child;
10import com.google.appengine.datanucleus.bugs.test.Issue167Parent;
11
12public class Issue167Test extends JPABugTestCase {
13  public void testRun() {
14    Issue167Parent p = new Issue167Parent(UUID.randomUUID().toString());
15
16    EntityManager em = emf.createEntityManager();
17    try {
18      p.getChildren().add(new Issue167Child("1"));
19      p.getChildren().add(new Issue167Child("2"));
20      p.getChildren().add(new Issue167Child("3"));
21
22      em.getTransaction().begin();
23      em.persist(p);
24      em.getTransaction().commit();
25
26      NucleusLogger.GENERAL.debug(">> Parent " + p.getName() + " stored succesfully");
27    } catch (Exception e) {
28      NucleusLogger.GENERAL.error("Exception in persist", e);
29      fail("Exception in test : " + e.getMessage());
30      return;
31    } finally {
32      if (em.getTransaction().isActive()) {
33        em.getTransaction().rollback();
34      }
35      em.close();
36    }
37
38    for (Issue167Child c : p.getChildren()) {
39      NucleusLogger.GENERAL.debug("Parent contains child " + c.getData());
40    }
41
42    p.getChildren().remove(1); // "2"
43    p.getChildren().remove(1); // "3"
44    p.getChildren().remove(0); // "1"
45    NucleusLogger.GENERAL.debug("Children removed, now contains " + p.getChildren().size() + " children");
46
47    em = emf.createEntityManager();
48    try {
49      em.getTransaction().begin();
50      p = em.merge(p);
51      em.getTransaction().commit();
52
53      NucleusLogger.GENERAL.debug(">> Parent " + p.getName()
54          + " stored succesfully with " + p.getChildren().size() + " children");
55    } catch (Exception e) {
56      NucleusLogger.GENERAL.error("Exception in merge", e);
57      fail("Exception in test : " + e.getMessage());
58      return;
59    } finally {
60      if (em.getTransaction().isActive()) {
61        em.getTransaction().rollback();
62      }
63      em.close();
64    }
65
66    em = emf.createEntityManager();
67
68    try {
69      em.getTransaction().begin();
70      p = (Issue167Parent) em.createQuery(
71          "select p from " + Issue167Parent.class.getName() + " p where p.name='"
72          + p.getName() + "'").getResultList().get(0);
73      p.getChildren(); // Touch children field to get it detached
74      em.getTransaction().commit();
75
76      NucleusLogger.GENERAL.debug(">> Parent " + p.getName() + " fetched succesfully");
77    } catch (Exception e) {
78      NucleusLogger.GENERAL.error("Exception in fetch", e);
79      fail("Exception in test : " + e.getMessage());
80      return;
81    } finally {
82      if (em.getTransaction().isActive()) {
83        em.getTransaction().rollback();
84      }
85      em.close();
86    }
87
88    if (p.getChildren().size() > 0) {
89      fail("Should not have any children but has " + p.getChildren().size());
90    }
91  }
92}