PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 130 lines | 100 code | 11 blank | 19 comment | 0 complexity | de569a9a6b5baea458748e16fb3f9637 MD5 | raw file
  1/*
  2 * /**********************************************************************
  3 * Copyright (c) 2009 Google Inc.
  4 *
  5 * Licensed under the Apache License, Version 2.0 (the "License");
  6 * you may not use this file except in compliance with the License.
  7 * You may obtain a copy of the License at
  8 *
  9 * http://www.apache.org/licenses/LICENSE-2.0
 10 *
 11 * Unless required by applicable law or agreed to in writing, software
 12 * distributed under the License is distributed on an "AS IS" BASIS,
 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14 * See the License for the specific language governing permissions and
 15 * limitations under the License.
 16 * **********************************************************************/
 17package com.google.appengine.datanucleus.jpa;
 18
 19import java.util.Set;
 20
 21import com.google.appengine.api.datastore.Entity;
 22import com.google.appengine.api.datastore.EntityNotFoundException;
 23import com.google.appengine.api.datastore.KeyFactory;
 24import com.google.appengine.datanucleus.Utils;
 25import com.google.appengine.datanucleus.test.jpa.EmbeddableJPA;
 26import com.google.appengine.datanucleus.test.jpa.HasEmbeddableCollection;
 27import com.google.appengine.datanucleus.test.jpa.HasEmbeddedJPA;
 28
 29/**
 30 * @author Max Ross <maxr@google.com>
 31 */
 32public class JPAEmbeddedTest extends JPATestCase {
 33
 34  public void testEmbedded() throws EntityNotFoundException {
 35    HasEmbeddedJPA pojo = new HasEmbeddedJPA();
 36    EmbeddableJPA embedded = new EmbeddableJPA();
 37    embedded.setEmbeddedString("yar");
 38    embedded.setMultiVal(Utils.newArrayList("m1", "m2"));
 39    pojo.setEmbeddable(embedded);
 40    embedded = new EmbeddableJPA();
 41    embedded.setEmbeddedString("yar2");
 42    embedded.setMultiVal(Utils.newArrayList("m3", "m4"));
 43    pojo.setEmbeddable2(embedded);
 44    beginTxn();
 45    em.persist(pojo);
 46    commitTxn();
 47    assertEquals(1, countForClass(HasEmbeddedJPA.class));
 48    Entity e = ds.get(KeyFactory.createKey(pojo.getClass().getSimpleName(), pojo.getId()));
 49    assertEquals("yar", e.getProperty("embeddedString"));
 50    assertEquals(Utils.newArrayList("m1", "m2"), e.getProperty("multiVal"));
 51    assertEquals("yar2", e.getProperty("EMBEDDEDSTRING"));
 52    assertEquals(Utils.newArrayList("m3", "m4"), e.getProperty("MULTIVAL"));
 53    beginTxn();
 54    pojo = em.find(HasEmbeddedJPA.class, pojo.getId());
 55    assertNotNull(pojo.getEmbeddable());
 56    assertEquals("yar", pojo.getEmbeddable().getEmbeddedString());
 57    assertEquals(Utils.newArrayList("m1", "m2"), pojo.getEmbeddable().getMultiVal());
 58    assertEquals("yar2", pojo.getEmbeddable2().getEmbeddedString());
 59    assertEquals(Utils.newArrayList("m3", "m4"), pojo.getEmbeddable2().getMultiVal());
 60    commitTxn();
 61  }
 62
 63  public void testEmbeddedWithKeyPk_NullEmbedded() {
 64    HasEmbeddedJPA pojo = new HasEmbeddedJPA();
 65    beginTxn();
 66    em.persist(pojo);
 67    commitTxn();
 68    em.clear();
 69
 70    beginTxn();
 71    pojo = em.find(HasEmbeddedJPA.class, pojo.getId());
 72    assertNull(pojo.getEmbeddable());
 73    assertNull(pojo.getEmbeddable2());
 74    commitTxn();
 75  }
 76
 77  public void testEmbeddedWithKeyPk_AddEmbeddedToExistingParent() {
 78    HasEmbeddedJPA pojo = new HasEmbeddedJPA();
 79    beginTxn();
 80    em.persist(pojo);
 81    commitTxn();
 82
 83    EmbeddableJPA embeddable = new EmbeddableJPA();
 84    embeddable.setEmbeddedString("yar");
 85    EmbeddableJPA embeddable2 = new EmbeddableJPA();
 86    embeddable2.setEmbeddedString("yar2");
 87    beginTxn();
 88    pojo.setEmbeddable(embeddable);
 89    pojo.setEmbeddable2(embeddable2);
 90    pojo = em.find(HasEmbeddedJPA.class, pojo.getId()); // Finds it in L1 cache from first txn (but with fields set now)
 91    assertNotNull(pojo.getEmbeddable());
 92    assertNotNull(pojo.getEmbeddable().getEmbeddedString());
 93    assertNotNull(pojo.getEmbeddable2());
 94    assertNotNull(pojo.getEmbeddable2().getEmbeddedString());
 95    commitTxn(); // Flushes the changes to embeddable fields
 96
 97    beginTxn();
 98    pojo = em.find(HasEmbeddedJPA.class, pojo.getId());
 99    assertNotNull(pojo.getEmbeddable());
100    assertEquals("yar", pojo.getEmbeddable().getEmbeddedString());
101    assertNotNull(pojo.getEmbeddable2());
102    assertEquals("yar2", pojo.getEmbeddable2().getEmbeddedString());
103    commitTxn();
104  }
105
106  public void testEmbeddedCollectionOfEmbeddables() {
107    HasEmbeddableCollection pojo = new HasEmbeddableCollection();
108    EmbeddableJPA emb1 = new EmbeddableJPA();
109    emb1.setEmbeddedString("The String1");
110    pojo.getTheSet().add(emb1);
111    EmbeddableJPA emb2 = new EmbeddableJPA();
112    emb2.setEmbeddedString("The String2");
113    pojo.getTheSet().add(emb2);
114    beginTxn();
115    em.persist(pojo);
116    commitTxn();
117    Long pk = pojo.getId();
118    em.close();
119
120    em = emf.createEntityManager();
121    beginTxn();
122    HasEmbeddableCollection thePojo = em.find(HasEmbeddableCollection.class, pk);
123    assertNotNull(thePojo);
124    Set<EmbeddableJPA> theEmbColl = thePojo.getTheSet();
125    assertNotNull(thePojo);
126    assertEquals("size of embedded collection is wrong", 2, theEmbColl.size());
127    commitTxn();
128    em.close();
129  }
130}