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