PageRenderTime 37ms CodeModel.GetById 28ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/com/google/appengine/datanucleus/jdo/JDORecursiveParentTest.java

http://datanucleus-appengine.googlecode.com/
Java | 107 lines | 72 code | 17 blank | 18 comment | 3 complexity | 2c5ad4643fd30908ef6cdd89f61e276b 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.jdo;
 17
 18import com.google.appengine.datanucleus.test.jdo.HasRecursiveParentJDO.HasRecursiveParent;
 19
 20/**
 21 * Test recursive relations (Issue 80)
 22 */
 23public class JDORecursiveParentTest extends JDOTestCase {
 24
 25  public void testCreateParentAndChildDifferentTxn() {
 26
 27    HasRecursiveParent p = new HasRecursiveParent();
 28    p.setName("yam");
 29    beginTxn();
 30    pm.makePersistent(p);
 31    String pid = p.getId();
 32    commitTxn();
 33
 34    beginTxn();
 35    HasRecursiveParent c = new HasRecursiveParent();
 36    c.setName("yum");
 37    HasRecursiveParent p2 = pm.getObjectById(HasRecursiveParent.class, pid);
 38    c.setParent(p2);
 39    p2.getChildren().add(c);
 40    pm.makePersistent(c);
 41    String cid = c.getId();
 42    commitTxn();
 43
 44    beginTxn();
 45    HasRecursiveParent c2 = pm.getObjectById(HasRecursiveParent.class, cid);
 46    HasRecursiveParent p3 = c2.getParent();
 47    assertEquals(1, p3.getChildren().size());
 48    assertEquals("yam", p3.getName());
 49    assertEquals(p3.getChildren().iterator().next(), c2);
 50    commitTxn();
 51  }
 52
 53  public void testCreateParentAndChildSameTxn() {
 54
 55    HasRecursiveParent p = new HasRecursiveParent();
 56    p.setName("yam");
 57    HasRecursiveParent c = new HasRecursiveParent();
 58    c.setName("yum");
 59    c.setParent(p);
 60    p.getChildren().add(c);
 61    beginTxn();
 62    pm.makePersistent(p);
 63    String pid = p.getId();
 64    String cid = c.getId();
 65    commitTxn();
 66
 67    beginTxn();
 68    HasRecursiveParent p2 = pm.getObjectById(HasRecursiveParent.class, pid);
 69    assertEquals(1, p2.getChildren().size());
 70    HasRecursiveParent c2 = p2.getChildren().iterator().next();
 71    assertEquals(cid, c2.getId());
 72    assertEquals("yum", c2.getName());
 73    assertEquals(c2.getParent(), p2);
 74    commitTxn();
 75
 76  }
 77
 78  public void testCreateParentHierarchie() {
 79
 80    HasRecursiveParent p, p0 = p = new HasRecursiveParent();
 81    p.setName("yam4");
 82    for (int i = 3; i > 0; i--) {
 83      HasRecursiveParent c = new HasRecursiveParent();
 84      c.setName("yam" + i);
 85      c.setParent(p);
 86      p.getChildren().add(c);
 87      p = c;
 88    }
 89
 90    beginTxn();
 91    pm.makePersistent(p0);
 92    String cid = p.getId();
 93    commitTxn();
 94
 95    beginTxn();
 96    HasRecursiveParent c = pm.getObjectById(HasRecursiveParent.class, cid);
 97    int i = 1;
 98    while (c.getParent() != null) {
 99      assertEquals("yam" + (i++), c.getName());
100      c = c.getParent();
101    }
102
103    commitTxn();
104
105  }
106
107}