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