PageRenderTime 31ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://datanucleus-appengine.googlecode.com/
Java | 268 lines | 202 code | 48 blank | 18 comment | 0 complexity | 9249855037035400652ffc8e2cddcda2 MD5 | raw file
Possible License(s): Apache-2.0
  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.Key;
  16. import com.google.appengine.api.datastore.KeyFactory;
  17. import com.google.appengine.datanucleus.DatastoreServiceInterceptor;
  18. import com.google.appengine.datanucleus.Utils;
  19. import com.google.appengine.datanucleus.WriteBlocker;
  20. import com.google.appengine.datanucleus.test.jpa.Book;
  21. import com.google.appengine.datanucleus.test.jpa.HasMultiValuePropsJPA;
  22. /**
  23. * @author Max Ross <maxr@google.com>
  24. */
  25. public class JPAFetchTest extends JPATestCase {
  26. @Override
  27. protected void setUp() throws Exception {
  28. super.setUp();
  29. DatastoreServiceInterceptor.install(getStoreManager(), new WriteBlocker());
  30. }
  31. @Override
  32. protected void tearDown() throws Exception {
  33. try {
  34. super.tearDown();
  35. } finally {
  36. DatastoreServiceInterceptor.uninstall();
  37. }
  38. }
  39. @Override
  40. protected EntityManagerFactoryName getEntityManagerFactoryName() {
  41. return EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed;
  42. }
  43. public void testSimpleFetch_Id() {
  44. Key key = ds.put(Book.newBookEntity("max", "47", "yam"));
  45. String keyStr = KeyFactory.keyToString(key);
  46. Book book = em.find(Book.class, keyStr);
  47. assertNotNull(book);
  48. assertEquals(keyStr, book.getId());
  49. assertEquals("max", book.getAuthor());
  50. assertEquals("47", book.getIsbn());
  51. assertEquals("yam", book.getTitle());
  52. }
  53. public void testSimpleFetchWithNonTransactionalDatasource() {
  54. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_not_allowed);
  55. Key key = ds.put(Book.newBookEntity("max", "47", "yam"));
  56. String keyStr = KeyFactory.keyToString(key);
  57. Book book = em.find(Book.class, keyStr);
  58. assertNotNull(book);
  59. assertEquals(keyStr, book.getId());
  60. assertEquals("max", book.getAuthor());
  61. assertEquals("47", book.getIsbn());
  62. assertEquals("yam", book.getTitle());
  63. }
  64. public void testSimpleFetch_Id_LongIdOnly() {
  65. Key key = ds.put(Book.newBookEntity("max", "47", "yam"));
  66. Book book = em.find(Book.class, key.getId());
  67. assertNotNull(book);
  68. String keyStr = KeyFactory.keyToString(key);
  69. assertEquals(keyStr, book.getId());
  70. assertEquals("max", book.getAuthor());
  71. assertEquals("47", book.getIsbn());
  72. assertEquals("yam", book.getTitle());
  73. }
  74. public void testSimpleFetch_Id_LongIdOnly_NotFound() {
  75. assertNull(em.find(Book.class, -1));
  76. }
  77. public void testSimpleFetch_Id_IntIdOnly() {
  78. Key key = ds.put(Book.newBookEntity("max", "47", "yam"));
  79. Book book = em.find(Book.class, Long.valueOf(key.getId()).intValue());
  80. assertNotNull(book);
  81. String keyStr = KeyFactory.keyToString(key);
  82. assertEquals(keyStr, book.getId());
  83. assertEquals("max", book.getAuthor());
  84. assertEquals("47", book.getIsbn());
  85. assertEquals("yam", book.getTitle());
  86. }
  87. public void testSimpleFetchWithNamedKey() {
  88. Key key = ds.put(Book.newBookEntity("named key", "max", "47", "yam"));
  89. String keyStr = KeyFactory.keyToString(key);
  90. Book book = em.find(Book.class, keyStr);
  91. assertNotNull(book);
  92. assertEquals(keyStr, book.getId());
  93. assertEquals("max", book.getAuthor());
  94. assertEquals("47", book.getIsbn());
  95. assertEquals("yam", book.getTitle());
  96. assertEquals("named key", KeyFactory.stringToKey(book.getId()).getName());
  97. }
  98. public void testSimpleFetchWithNamedKey_NameOnly() {
  99. Key key = ds.put(Book.newBookEntity("named key", "max", "47", "yam"));
  100. Book book = em.find(Book.class, key.getName());
  101. assertNotNull(book);
  102. String keyStr = KeyFactory.keyToString(key);
  103. assertEquals(keyStr, book.getId());
  104. assertEquals("max", book.getAuthor());
  105. assertEquals("47", book.getIsbn());
  106. assertEquals("yam", book.getTitle());
  107. assertEquals("named key", KeyFactory.stringToKey(book.getId()).getName());
  108. }
  109. public void testSimpleFetchWithNamedKey_NameOnly_NotFound() {
  110. assertNull(em.find(Book.class, "does not exist"));
  111. }
  112. public void testFetchNonExistent() {
  113. Key key = ds.put(Book.newBookEntity("max", "47", "yam"));
  114. ds.delete(key);
  115. String keyStr = KeyFactory.keyToString(key);
  116. assertNull(em.find(Book.class, keyStr));
  117. }
  118. public void testFetchSet() {
  119. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  120. e.setProperty("strSet", Utils.newArrayList("a", "b", "c"));
  121. ds.put(e);
  122. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  123. assertEquals(Utils.newHashSet("a", "b", "c"), pojo.getStrSet());
  124. }
  125. public void testFetchSetNonTxn() {
  126. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  127. testFetchSet();
  128. }
  129. public void testFetchArrayList() {
  130. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  131. e.setProperty("strArrayList", Utils.newArrayList("a", "b", "c"));
  132. ds.put(e);
  133. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  134. assertEquals(Utils.newArrayList("a", "b", "c"), pojo.getStrArrayList());
  135. }
  136. public void testFetchArrayListNonTxn() {
  137. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  138. testFetchArrayList();
  139. }
  140. public void testFetchList() {
  141. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  142. e.setProperty("strList", Utils.newArrayList("a", "b", "c"));
  143. ds.put(e);
  144. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  145. assertEquals(Utils.newArrayList("a", "b", "c"), pojo.getStrList());
  146. }
  147. public void testFetchListNonTxn() {
  148. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  149. testFetchList();
  150. }
  151. public void testFetchLinkedList() {
  152. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  153. e.setProperty("strLinkedList", Utils.newArrayList("a", "b", "c"));
  154. ds.put(e);
  155. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  156. assertEquals(Utils.newLinkedList("a", "b", "c"), pojo.getStrLinkedList());
  157. }
  158. public void testFetchLinkedListNonTxn() {
  159. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  160. testFetchLinkedList();
  161. }
  162. public void testFetchHashSet() {
  163. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  164. e.setProperty("strHashSet", Utils.newArrayList("a", "b", "c"));
  165. ds.put(e);
  166. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  167. assertEquals(Utils.newHashSet("a", "b", "c"), pojo.getStrHashSet());
  168. }
  169. public void testFetchHashSetNonTxn() {
  170. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  171. testFetchHashSet();
  172. }
  173. public void testFetchLinkedHashSet() {
  174. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  175. e.setProperty("strLinkedHashSet", Utils.newArrayList("a", "b", "c"));
  176. ds.put(e);
  177. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  178. assertEquals(Utils.newHashSet("a", "b", "c"), pojo.getStrLinkedHashSet());
  179. }
  180. public void testFetchLinkedHashSetNonTxn() {
  181. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  182. testFetchLinkedHashSet();
  183. }
  184. public void testFetchSortedSet() {
  185. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  186. e.setProperty("strSortedSet", Utils.newArrayList("c", "b", "a"));
  187. ds.put(e);
  188. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  189. assertEquals(Utils.newHashSet("a", "b", "c"), pojo.getStrSortedSet());
  190. }
  191. public void testFetchSortedSetNonTxn() {
  192. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  193. testFetchSortedSet();
  194. }
  195. public void testFetchTreeSet() {
  196. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  197. e.setProperty("strTreeSet", Utils.newArrayList("c", "b", "a"));
  198. ds.put(e);
  199. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  200. assertEquals(Utils.newTreeSet("a", "b", "c"), pojo.getStrTreeSet());
  201. }
  202. public void testFetchTreeSetNonTxn() {
  203. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  204. testFetchTreeSet();
  205. }
  206. public void testFetchCollection() {
  207. Entity e = new Entity(HasMultiValuePropsJPA.class.getSimpleName());
  208. e.setProperty("intColl", Utils.newArrayList(2, 3, 4));
  209. ds.put(e);
  210. HasMultiValuePropsJPA pojo = em.find(HasMultiValuePropsJPA.class, e.getKey().getId());
  211. assertEquals(Utils.newArrayList(2, 3, 4), pojo.getIntColl());
  212. }
  213. public void testFetchCollectionNonTxn() {
  214. switchDatasource(EntityManagerFactoryName.nontransactional_ds_non_transactional_ops_allowed);
  215. testFetchCollection();
  216. }
  217. }