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