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