PageRenderTime 223ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jpa/JPARecursiveParentTest.java

http://datanucleus-appengine.googlecode.com/
Java | 104 lines | 71 code | 15 blank | 18 comment | 3 complexity | e948482f8dcc4218f7e77df885f5fbc2 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**********************************************************************
  2. Copyright (c) 2011 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus.jpa;
  14. import com.google.appengine.datanucleus.test.jpa.HasRecursiveParentJPA.HasRecursiveParent;
  15. /**
  16. * Test recursive relations (Issue 80)
  17. */
  18. public class JPARecursiveParentTest extends JPATestCase {
  19. public void testCreateParentAndChildDifferentTxn() {
  20. HasRecursiveParent p = new HasRecursiveParent();
  21. p.setName("yam");
  22. beginTxn();
  23. em.persist(p);
  24. commitTxn();
  25. String pid = p.getId();
  26. beginTxn();
  27. HasRecursiveParent p2 = em.find(HasRecursiveParent.class, pid);
  28. HasRecursiveParent c = new HasRecursiveParent();
  29. c.setName("yum");
  30. c.setParent(p2);
  31. p2.getChildren().add(c);
  32. commitTxn();
  33. String cid = c.getId();
  34. beginTxn();
  35. HasRecursiveParent c2 = em.find(HasRecursiveParent.class, cid);
  36. HasRecursiveParent p3 = c2.getParent();
  37. assertEquals(1, p3.getChildren().size());
  38. assertEquals("yam", p3.getName());
  39. assertEquals(p3.getChildren().iterator().next(), c2);
  40. commitTxn();
  41. }
  42. public void testCreateParentAndChildSameTxn() {
  43. HasRecursiveParent p = new HasRecursiveParent();
  44. p.setName("yam");
  45. HasRecursiveParent c = new HasRecursiveParent();
  46. c.setName("yum");
  47. c.setParent(p);
  48. p.getChildren().add(c);
  49. beginTxn();
  50. em.persist(p);
  51. commitTxn();
  52. String pid = p.getId();
  53. String cid = c.getId();
  54. beginTxn();
  55. HasRecursiveParent p2 = em.find(HasRecursiveParent.class, pid);
  56. assertEquals(1, p2.getChildren().size());
  57. HasRecursiveParent c2 = p2.getChildren().iterator().next();
  58. assertEquals(cid, c2.getId());
  59. assertEquals("yum", c2.getName());
  60. assertEquals(c2.getParent(), p2);
  61. commitTxn();
  62. }
  63. public void testCreateParentHierarchie() {
  64. HasRecursiveParent p, p0 = p = new HasRecursiveParent();
  65. p.setName("yam4");
  66. for (int i = 3; i > 0; i--) {
  67. HasRecursiveParent c = new HasRecursiveParent();
  68. c.setName("yam" + i);
  69. c.setParent(p);
  70. p.getChildren().add(c);
  71. p = c;
  72. }
  73. beginTxn();
  74. em.persist(p0);
  75. commitTxn();
  76. String cid = p.getId();
  77. beginTxn();
  78. HasRecursiveParent c = em.find(HasRecursiveParent.class, cid);
  79. int i = 1;
  80. while (c.getParent() != null) {
  81. assertEquals("yam" + (i++), c.getName());
  82. c = c.getParent();
  83. }
  84. commitTxn();
  85. }
  86. }