PageRenderTime 287ms CodeModel.GetById 248ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 124 lines | 96 code | 10 blank | 18 comment | 1 complexity | 0bcf84f54b81f2a0be11045bed13b362 MD5 | raw file
  1/**********************************************************************
  2Copyright (c) 2009 Google Inc.
  3
  4Licensed under the Apache License, Version 2.0 (the "License");
  5you may not use this file except in compliance with the License.
  6You may obtain a copy of the License at
  7
  8http://www.apache.org/licenses/LICENSE-2.0
  9
 10Unless required by applicable law or agreed to in writing, software
 11distributed under the License is distributed on an "AS IS" BASIS,
 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13See the License for the specific language governing permissions and
 14limitations under the License.
 15**********************************************************************/
 16package com.google.appengine.datanucleus.jpa;
 17
 18import com.google.appengine.api.datastore.Entity;
 19import com.google.appengine.api.datastore.EntityNotFoundException;
 20import com.google.appengine.api.datastore.KeyFactory;
 21import com.google.appengine.datanucleus.Utils;
 22import com.google.appengine.datanucleus.test.jpa.NullDataJPA;
 23
 24import java.util.List;
 25import java.util.Set;
 26
 27/**
 28 * @author Max Ross <maxr@google.com>
 29 */
 30public class JPANullValueTest extends JPATestCase {
 31
 32  public void testFetchNullData() {
 33    Entity e = new Entity(NullDataJPA.class.getSimpleName());
 34    ds.put(e);
 35    beginTxn();
 36    NullDataJPA pojo = em.find(NullDataJPA.class, KeyFactory.keyToString(e.getKey()));
 37    assertNotNull(pojo.getArray());
 38    assertEquals(0, pojo.getArray().length);
 39    assertNotNull(pojo.getList());
 40    assertTrue(pojo.getList().isEmpty());
 41    assertTrue(pojo.getSet().isEmpty());
 42    assertTrue(pojo.getIntegerList().isEmpty());
 43    assertTrue(pojo.getIntegerSet().isEmpty());
 44    assertNull(pojo.getString());
 45    commitTxn();
 46  }
 47
 48  public void testFetchNullValue() {
 49    Entity e = new Entity(NullDataJPA.class.getSimpleName());
 50    e.setProperty("list", null);
 51    ds.put(e);
 52    beginTxn();
 53    NullDataJPA pojo = em.find(NullDataJPA.class, e.getKey());
 54    assertNotNull(pojo.getList());
 55    assertNotNull(pojo.getSet());
 56    assertNotNull(pojo.getIntegerList());
 57    assertNotNull(pojo.getIntegerSet());
 58    assertTrue(pojo.getList().isEmpty());
 59    assertTrue(pojo.getSet().isEmpty());
 60    assertTrue(pojo.getIntegerList().isEmpty());
 61    assertTrue(pojo.getIntegerSet().isEmpty());
 62  }
 63
 64  public void testFetchMultiValuePropWithOneNullEntry() {
 65    Entity e = new Entity(NullDataJPA.class.getSimpleName());
 66    e.setProperty("list", Utils.newArrayList((String) null));
 67    e.setProperty("set", Utils.newArrayList((String) null));
 68    e.setProperty("integerList", Utils.newArrayList((Integer) null));
 69    e.setProperty("integerSet", Utils.newArrayList((Integer) null));
 70    ds.put(e);
 71    beginTxn();
 72    NullDataJPA pojo = em.find(NullDataJPA.class, e.getKey());
 73    assertNotNull(pojo.getList());
 74    assertEquals(1, pojo.getList().size());
 75    assertNull(pojo.getList().get(0));
 76    assertNotNull(pojo.getSet());
 77    assertEquals(1, pojo.getSet().size());
 78    assertNull(pojo.getSet().iterator().next());
 79
 80    assertNotNull(pojo.getIntegerList());
 81    assertEquals(1, pojo.getIntegerList().size());
 82    assertNull(pojo.getIntegerList().get(0));
 83    assertNotNull(pojo.getIntegerSet());
 84    assertEquals(1, pojo.getIntegerSet().size());
 85    assertNull(pojo.getIntegerSet().iterator().next());
 86  }
 87
 88
 89  public void testInsertNullData() throws EntityNotFoundException {
 90    NullDataJPA pojo = new NullDataJPA();
 91    beginTxn();
 92    em.persist(pojo);
 93    commitTxn();
 94    Entity e = ds.get(KeyFactory.createKey(NullDataJPA.class.getSimpleName(), pojo.getId()));
 95    Set<String> props = e.getProperties().keySet();
 96    assertEquals(Utils.newHashSet("string", "array", "list", "set", "integerList", "integerSet"), props);
 97    for (Object val : e.getProperties().values()) {
 98      assertNull(val);
 99    }
100  }
101
102  public void testInsertContainersWithOneNullElement() throws EntityNotFoundException {
103    NullDataJPA pojo = new NullDataJPA();
104    List<String> list = Utils.newArrayList();
105    list.add(null);
106    pojo.setList(list);
107    Set<String> set = Utils.newHashSet();
108    set.add(null);
109    pojo.setSet(set);
110    List<Integer> integerList = Utils.newArrayList();
111    integerList.add(null);
112    pojo.setIntegerList(integerList);
113    Set<Integer> integerSet = Utils.newHashSet();
114    integerSet.add(null);
115    pojo.setIntegerSet(integerSet);
116    pojo.setArray(new String[] {null});
117    beginTxn();
118    em.persist(pojo);
119    commitTxn();
120    Entity e = ds.get(KeyFactory.createKey(NullDataJPA.class.getSimpleName(), pojo.getId()));
121    assertEquals(1, ((List<?>)e.getProperty("array")).size());
122    assertEquals(1, ((List<?>)e.getProperty("list")).size());
123  }
124}