@Test
  public void testTwoLevelsOfFetch() throws Exception {
    fact.register(ListNode.class);

    TestObjectify ofy = fact.begin();

    ListNode node3 = new ListNode();
    node3.foo = "foo3";
    ofy.put(node3);

    ListNode node2 = new ListNode();
    node2.foo = "foo2";
    node2.next = Ref.create(node3);
    ofy.put(node2);

    ListNode node1 = new ListNode();
    node1.foo = "foo1";
    node1.next = Ref.create(node2);
    ofy.put(node1);

    ofy.clear();
    ListNode fetched = ofy.get(Key.create(node1));

    assert fetched.foo.equals(node1.foo);
    assert fetched.next.get().id.equals(node2.id);
    assert fetched.next.get().foo.equals(node2.foo);
    assert fetched.next.get().next.get().id.equals(node3.id);
    assert fetched.next.get().next.get().foo.equals(node3.foo);
    assert fetched.next.get().next.get().next == null;
  }
  @Test
  public void testGrouping() throws Exception {
    fact.register(HasEntitiesWithGroups.class);

    HasEntitiesWithGroups he = new HasEntitiesWithGroups();
    he.single = Ref.create(k1);
    he.multi.add(Ref.create(k1));
    he.multi.add(Ref.create(k2));
    HasEntitiesWithGroups fetched = this.putClearGet(he);

    Key<HasEntitiesWithGroups> hekey = Key.create(he);

    assert fetched.single.key().equals(k1);
    assertRefUninitialzied(fetched.single);

    assert fetched.multi.get(0).equals(fetched.single);
    for (Ref<Trivial> ref : fetched.multi) assertRefUninitialzied(ref);

    TestObjectify ofy = fact.begin();

    ofy.clear();
    fetched = ofy.load().group(Single.class).key(hekey).get();
    assert fetched.single.get().getId().equals(t1.getId());
    assert fetched.single.get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(0).equals(fetched.single);
    for (Ref<Trivial> ref : fetched.multi) assertRefUninitialzied(ref);

    ofy.clear();
    fetched = ofy.load().group(Multi.class).key(hekey).get();
    assert fetched.multi.get(0).get().getId().equals(t1.getId());
    assert fetched.multi.get(0).get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());

    assert fetched.multi.get(0).equals(fetched.single);
    assertRefUninitialzied(fetched.single);

    ofy.clear();
    fetched = ofy.load().group(Single.class).group(Multi.class).key(hekey).get();
    assert fetched.multi.get(0).get().getId().equals(t1.getId());
    assert fetched.multi.get(0).get().getSomeString().equals(t1.getSomeString());
    assert fetched.multi.get(1).get().getId().equals(t2.getId());
    assert fetched.multi.get(1).get().getSomeString().equals(t2.getSomeString());
    assert fetched.single.get() == fetched.multi.get(0).get();
  }
  @Test
  public void testBasicLists() throws Exception {
    fact.register(HasCollections.class);
    TestObjectify ofy = this.fact.begin();

    HasCollections hc = new HasCollections();
    hc.integerList = Arrays.asList(1, 2, 3);

    hc.integerArrayList = new ArrayList<Integer>(hc.integerList);
    hc.integerLinkedList = new LinkedList<Integer>(hc.integerList);

    Key<HasCollections> key = ofy.save().entity(hc).now();
    ofy.clear();
    hc = ofy.load().key(key).get();

    assertContains123(hc.integerList, ArrayList.class);
    assertContains123(hc.integerArrayList, ArrayList.class);
    assertContains123(hc.integerLinkedList, LinkedList.class);
  }
  @Test
  public void testMissingTail() throws Exception {
    fact.register(ListNode.class);

    TestObjectify ofy = fact.begin();

    // Node2 should not exist but should have a concrete id for node1
    ListNode node2 = new ListNode();
    node2.id = 999L;

    ListNode node1 = new ListNode();
    node1.foo = "foo1";
    node1.next = Ref.create(Key.create(node2));
    ofy.put(node1);

    ofy.clear();
    ListNode fetched = ofy.get(Key.create(node1));

    assert fetched.foo.equals(node1.foo);
    assert fetched.next.get() == null; // it was fetched, so this should be initialized and null.
  }
  /** Test rule: never store an empty Collection, leaves value as null */
  @Test
  public void testEmptyCollections() throws Exception {
    fact.register(HasCollections.class);
    TestObjectify ofy = this.fact.begin();

    HasCollections hc = new HasCollections();
    hc.integerList = new ArrayList<Integer>();

    Key<HasCollections> key = ofy.save().entity(hc).now();
    ofy.clear();
    hc = ofy.load().key(key).get();

    System.out.println(ds().get(fact.getRawKey(hc)));

    // This wouldn't be valid if we didn't clear the session
    assert hc.integerList == null;

    Entity e = ds().get(fact.getRawKey(key));
    // rule : never store an empty collection
    assert !e.hasProperty("integerList");

    assert hc.initializedList != null;
    assert hc.initializedList instanceof LinkedList<?>;
  }