/tests/com/google/appengine/datanucleus/jdo/JDOPrimaryKeyTest.java
Java | 407 lines | 326 code | 51 blank | 30 comment | 0 complexity | 1b3de3b2111de3842633909b225a2e3f 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.jdo; 17 18import com.google.appengine.api.datastore.Entity; 19import com.google.appengine.api.datastore.EntityNotFoundException; 20import com.google.appengine.api.datastore.Key; 21import com.google.appengine.api.datastore.KeyFactory; 22import com.google.appengine.datanucleus.TestUtils; 23import com.google.appengine.datanucleus.Utils; 24import com.google.appengine.datanucleus.test.jdo.HasEncodedStringPkJDO; 25import com.google.appengine.datanucleus.test.jdo.HasEncodedStringPkSeparateIdFieldJDO; 26import com.google.appengine.datanucleus.test.jdo.HasEncodedStringPkSeparateNameFieldJDO; 27import com.google.appengine.datanucleus.test.jdo.HasKeyPkJDO; 28import com.google.appengine.datanucleus.test.jdo.HasLongPkJDO; 29import com.google.appengine.datanucleus.test.jdo.HasLongPrimitivePkJDO; 30import com.google.appengine.datanucleus.test.jdo.HasStringUUIDHexPkJDO; 31import com.google.appengine.datanucleus.test.jdo.HasStringUUIDPkJDO; 32import com.google.appengine.datanucleus.test.jdo.HasUnencodedStringPkJDO; 33 34import java.util.List; 35import java.util.Set; 36 37import javax.jdo.JDOFatalUserException; 38import javax.jdo.Query; 39 40/** 41 * @author Max Ross <maxr@google.com> 42 */ 43public class JDOPrimaryKeyTest extends JDOTestCase { 44 45 public void testLongPrimitivePk() throws EntityNotFoundException { 46 HasLongPrimitivePkJDO pojo = new HasLongPrimitivePkJDO(); 47 beginTxn(); 48 pm.makePersistent(pojo); 49 commitTxn(); 50 51 assertNotNull(pojo.getId()); 52 Entity e = ds.get(KeyFactory.createKey(HasLongPrimitivePkJDO.class.getSimpleName(), pojo.getId())); 53 54 beginTxn(); 55 pm.getObjectById(HasLongPrimitivePkJDO.class, e.getKey().getId()); 56 pm.getObjectById(HasLongPrimitivePkJDO.class, e.getKey()); 57 pm.getObjectById(HasLongPrimitivePkJDO.class, KeyFactory.keyToString(e.getKey())); 58 commitTxn(); 59 } 60 61 public void testStringUUIDPk() throws EntityNotFoundException { 62 HasStringUUIDPkJDO pojo = new HasStringUUIDPkJDO("First"); 63 beginTxn(); 64 pm.makePersistent(pojo); 65 commitTxn(); 66 67 assertNotNull(pojo.getId()); 68 Entity e = ds.get(KeyFactory.createKey(HasStringUUIDPkJDO.class.getSimpleName(), pojo.getId())); 69 70 beginTxn(); 71 pm.getObjectById(HasStringUUIDPkJDO.class, e.getKey()); 72 pm.getObjectById(HasStringUUIDPkJDO.class, e.getKey().getName()); 73 pm.getObjectById(HasStringUUIDPkJDO.class, KeyFactory.keyToString(e.getKey())); 74 commitTxn(); 75 } 76 77 public void testStringUUIDHexPk() throws EntityNotFoundException { 78 HasStringUUIDHexPkJDO pojo = new HasStringUUIDHexPkJDO("First"); 79 beginTxn(); 80 pm.makePersistent(pojo); 81 commitTxn(); 82 83 assertNotNull(pojo.getId()); 84 Entity e = ds.get(KeyFactory.createKey(HasStringUUIDHexPkJDO.class.getSimpleName(), pojo.getId())); 85 86 beginTxn(); 87 pm.getObjectById(HasStringUUIDHexPkJDO.class, e.getKey()); 88 pm.getObjectById(HasStringUUIDHexPkJDO.class, e.getKey().getName()); 89 pm.getObjectById(HasStringUUIDHexPkJDO.class, KeyFactory.keyToString(e.getKey())); 90 commitTxn(); 91 } 92 93 public void testLongPk() throws EntityNotFoundException { 94 HasLongPkJDO pojo = new HasLongPkJDO(); 95 beginTxn(); 96 pm.makePersistent(pojo); 97 commitTxn(); 98 99 assertNotNull(pojo.getId()); 100 Entity e = ds.get(KeyFactory.createKey(HasLongPkJDO.class.getSimpleName(), pojo.getId())); 101 102 beginTxn(); 103 pm.getObjectById(HasLongPkJDO.class, e.getKey().getId()); 104 pm.getObjectById(HasLongPkJDO.class, e.getKey()); 105 pm.getObjectById(HasLongPkJDO.class, KeyFactory.keyToString(e.getKey())); 106 commitTxn(); 107 } 108 109 public void testLongPk_UserProvided() throws EntityNotFoundException { 110 HasLongPkJDO pojo = new HasLongPkJDO(); 111 pojo.setId(33L); 112 beginTxn(); 113 pm.makePersistent(pojo); 114 commitTxn(); 115 // the fact that this doesn't throw an exception is the test 116 ds.get(KeyFactory.createKey(HasLongPkJDO.class.getSimpleName(), 33)); 117 } 118 119 public void testUnencodedStringPk() throws EntityNotFoundException { 120 HasUnencodedStringPkJDO pojo = new HasUnencodedStringPkJDO(); 121 pojo.setId("a name"); 122 beginTxn(); 123 pm.makePersistent(pojo); 124 commitTxn(); 125 126 assertEquals("a name", pojo.getId()); 127 Entity e = ds.get(KeyFactory.createKey(HasUnencodedStringPkJDO.class.getSimpleName(), pojo.getId())); 128 129 beginTxn(); 130 pm.getObjectById(HasUnencodedStringPkJDO.class, e.getKey().getName()); 131 pm.getObjectById(HasUnencodedStringPkJDO.class, e.getKey()); 132 pm.getObjectById(HasUnencodedStringPkJDO.class, KeyFactory.keyToString(e.getKey())); 133 commitTxn(); 134 } 135 136 public void testUnencodedStringPk_NullValue() { 137 HasUnencodedStringPkJDO pojo = new HasUnencodedStringPkJDO(); 138 beginTxn(); 139 try { 140 pm.makePersistent(pojo); 141 fail("expected exception"); 142 } catch (JDOFatalUserException e) { 143 // good 144 rollbackTxn(); 145 } 146 } 147 148 public void testEncodedStringPk() throws EntityNotFoundException { 149 HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO(); 150 Key key = KeyFactory.createKey(HasEncodedStringPkJDO.class.getSimpleName(), "a name"); 151 pojo.setId(KeyFactory.keyToString(key)); 152 beginTxn(); 153 pm.makePersistent(pojo); 154 commitTxn(); 155 156 assertEquals(KeyFactory.keyToString(key), pojo.getId()); 157 Entity e = ds.get(KeyFactory.stringToKey(pojo.getId())); 158 159 beginTxn(); 160 pm.getObjectById(HasEncodedStringPkJDO.class, e.getKey().getName()); 161 pm.getObjectById(HasEncodedStringPkJDO.class, e.getKey()); 162 pm.getObjectById(HasEncodedStringPkJDO.class, KeyFactory.keyToString(e.getKey())); 163 commitTxn(); 164 } 165 166 public void testEncodedStringPk_NullValue() throws EntityNotFoundException { 167 HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO(); 168 beginTxn(); 169 pm.makePersistent(pojo); 170 commitTxn(); 171 assertNotNull(pojo.getId()); 172 Key key = KeyFactory.stringToKey(pojo.getId()); 173 Entity e = ds.get(key); 174 175 beginTxn(); 176 pm.getObjectById(HasEncodedStringPkJDO.class, e.getKey().getId()); 177 pm.getObjectById(HasEncodedStringPkJDO.class, e.getKey()); 178 pm.getObjectById(HasEncodedStringPkJDO.class, KeyFactory.keyToString(e.getKey())); 179 commitTxn(); 180 } 181 182 public void testEncodedStringPk_NonKeyValue() throws EntityNotFoundException { 183 HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO(); 184 pojo.setId("yar"); 185 beginTxn(); 186 try { 187 pm.makePersistent(pojo); 188 fail("expected exception"); 189 } catch (JDOFatalUserException e) { 190 // good 191 rollbackTxn(); 192 } 193 } 194 195 public void testEncodedStringPk_SeparateNameField() throws EntityNotFoundException { 196 HasEncodedStringPkSeparateNameFieldJDO pojo = new HasEncodedStringPkSeparateNameFieldJDO(); 197 pojo.setName("a name"); 198 beginTxn(); 199 pm.makePersistent(pojo); 200 commitTxn(); 201 202 assertEquals(TestUtils.createKey(pojo, "a name"), KeyFactory.stringToKey(pojo.getId())); 203 Entity e = ds.get(KeyFactory.stringToKey(pojo.getId())); 204 205 beginTxn(); 206 pojo = pm.getObjectById(HasEncodedStringPkSeparateNameFieldJDO.class, e.getKey().getName()); 207 assertEquals("a name", pojo.getName()); 208 pm.getObjectById(HasEncodedStringPkSeparateNameFieldJDO.class, e.getKey()); 209 assertEquals("a name", pojo.getName()); 210 pm.getObjectById(HasEncodedStringPkSeparateNameFieldJDO.class, KeyFactory.keyToString(e.getKey())); 211 assertEquals("a name", pojo.getName()); 212 commitTxn(); 213 214 } 215 216 public void testEncodedStringPk_SeparateIdField() throws EntityNotFoundException { 217 HasEncodedStringPkSeparateIdFieldJDO pojo = new HasEncodedStringPkSeparateIdFieldJDO(); 218 pojo.setId(34L); 219 beginTxn(); 220 pm.makePersistent(pojo); 221 assertNotNull(pojo.getId()); 222 commitTxn(); 223 beginTxn(); 224 assertNotNull(pojo.getId()); 225 assertNotNull(pojo.getKey()); 226 Entity e = ds.get(TestUtils.createKey(pojo, pojo.getId())); 227 long id = e.getKey().getId(); 228 assertEquals(34L, id); 229 pojo = pm.getObjectById(HasEncodedStringPkSeparateIdFieldJDO.class, e.getKey().getId()); 230 assertEquals(id, pojo.getId().longValue()); 231 pm.getObjectById(HasEncodedStringPkSeparateIdFieldJDO.class, e.getKey()); 232 assertEquals(id, pojo.getId().longValue()); 233 pm.getObjectById(HasEncodedStringPkSeparateIdFieldJDO.class, KeyFactory.keyToString(e.getKey())); 234 assertEquals(id, pojo.getId().longValue()); 235 commitTxn(); 236 237 } 238 239 public void testKeyPk_UserProvidedId() throws EntityNotFoundException { 240 HasKeyPkJDO pojo = new HasKeyPkJDO(); 241 pojo.setKey(TestUtils.createKey(pojo, 34)); 242 beginTxn(); 243 pm.makePersistent(pojo); 244 commitTxn(); 245 // the fact that this doesn't throw an exception is the test 246 ds.get(KeyFactory.createKey(HasKeyPkJDO.class.getSimpleName(), 34)); 247 } 248 249 public void testCannotChangeLongPk() { 250 HasLongPkJDO pojo = new HasLongPkJDO(); 251 beginTxn(); 252 pm.makePersistent(pojo); 253 commitTxn(); 254 beginTxn(); 255 pojo = pm.getObjectById(HasLongPkJDO.class, pojo.getId()); 256 pojo.setId(88L); 257 try { 258 commitTxn(); 259 fail("expected exception"); 260 } catch (JDOFatalUserException e) { 261 // good 262 rollbackTxn(); 263 } 264 } 265 266 public void testEntityWithLongPkMappedToPojoWithUnencodedStringPk() { 267 Entity entity = new Entity(HasUnencodedStringPkJDO.class.getSimpleName()); 268 Key key = ds.put(entity); 269 beginTxn(); 270 try { 271 pm.getObjectById(HasUnencodedStringPkJDO.class, key); 272 fail("expected exception"); 273 } catch (JDOFatalUserException e) { 274 // good 275 rollbackTxn(); 276 } 277 } 278 279 public void testEntityWithStringPkMappedToPojoWithLongPk() { 280 Entity entity = new Entity(HasLongPkJDO.class.getSimpleName(), "yar"); 281 Key key = ds.put(entity); 282 beginTxn(); 283 try { 284 pm.getObjectById(HasLongPkJDO.class, key); 285 fail("expected exception"); 286 } catch (JDOFatalUserException e) { 287 // good 288 rollbackTxn(); 289 } 290 } 291 292 public void testEntityWithParentMappedToPojoWithUnencodedStringPk() { 293 Key parent = KeyFactory.createKey("parent kind", 88); 294 Entity entity = new Entity(HasUnencodedStringPkJDO.class.getSimpleName(), "yar", parent); 295 Key key = ds.put(entity); 296 beginTxn(); 297 try { 298 pm.getObjectById(HasUnencodedStringPkJDO.class, key); 299 fail("expected exception"); 300 } catch (JDOFatalUserException e) { 301 // good 302 rollbackTxn(); 303 } 304 } 305 306 public void testEntityWithParentMappedToPojoWithLongPk() { 307 Key parent = KeyFactory.createKey("parent kind", 88); 308 Entity entity = new Entity(HasLongPkJDO.class.getSimpleName(), parent); 309 Key key = ds.put(entity); 310 beginTxn(); 311 try { 312 pm.getObjectById(HasLongPkJDO.class, key); 313 fail("expected exception"); 314 } catch (JDOFatalUserException e) { 315 // good 316 rollbackTxn(); 317 } 318 } 319 320 public void testUnencodedStringPk_BatchGet() throws EntityNotFoundException { 321 switchDatasource(PersistenceManagerFactoryName.nontransactional); 322 HasUnencodedStringPkJDO pojo = new HasUnencodedStringPkJDO(); 323 beginTxn(); 324 pojo.setId("yar1"); 325 pm.makePersistent(pojo); 326 commitTxn(); 327 328 HasUnencodedStringPkJDO pojo2 = new HasUnencodedStringPkJDO(); 329 beginTxn(); 330 pojo2.setId("yar2"); 331 pm.makePersistent(pojo2); 332 commitTxn(); 333 334 assertNotNull(pojo.getId()); 335 assertNotNull(pojo2.getId()); 336 337 beginTxn(); 338 Query q = pm.newQuery("select from " + HasUnencodedStringPkJDO.class.getName() + " where id == :ids"); 339 List<HasUnencodedStringPkJDO> pojos = 340 (List<HasUnencodedStringPkJDO>) q.execute(Utils.newArrayList(pojo.getId(), pojo2.getId())); 341 assertEquals(2, pojos.size()); 342 // we should preserve order but right now we don't 343 Set<String> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId()); 344 assertEquals(pks, Utils.newHashSet("yar1", "yar2")); 345 commitTxn(); 346 } 347 348 public void testEncodedStringPk_BatchGet() throws EntityNotFoundException { 349 switchDatasource(PersistenceManagerFactoryName.nontransactional); 350 HasEncodedStringPkJDO pojo = new HasEncodedStringPkJDO(); 351 beginTxn(); 352 String key1 = new KeyFactory.Builder("parent", 44) 353 .addChild(HasEncodedStringPkJDO.class.getSimpleName(), "yar1").getString(); 354 pojo.setId(key1); 355 pm.makePersistent(pojo); 356 commitTxn(); 357 358 HasEncodedStringPkJDO pojo2 = new HasEncodedStringPkJDO(); 359 beginTxn(); 360 String key2 = new KeyFactory.Builder("parent", 44) 361 .addChild(HasEncodedStringPkJDO.class.getSimpleName(), "yar2").getString(); 362 pojo2.setId(key2); 363 pm.makePersistent(pojo2); 364 commitTxn(); 365 366 assertNotNull(pojo.getId()); 367 assertNotNull(pojo2.getId()); 368 369 beginTxn(); 370 Query q = pm.newQuery("select from " + HasEncodedStringPkJDO.class.getName() + " where id == :ids"); 371 List<HasEncodedStringPkJDO> pojos = 372 (List<HasEncodedStringPkJDO>) q.execute(Utils.newArrayList(pojo.getId(), pojo2.getId())); 373 assertEquals(2, pojos.size()); 374 // we should preserve order but right now we don't 375 Set<String> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId()); 376 assertEquals(pks, Utils.newHashSet(key1, key2)); 377 commitTxn(); 378 } 379 380 public void testUnencodedLongPk_BatchGet() throws EntityNotFoundException { 381 switchDatasource(PersistenceManagerFactoryName.nontransactional); 382 HasLongPkJDO pojo = new HasLongPkJDO(); 383 beginTxn(); 384 pojo.setId(1L); 385 pm.makePersistent(pojo); 386 commitTxn(); 387 388 HasLongPkJDO pojo2 = new HasLongPkJDO(); 389 beginTxn(); 390 pojo2.setId(2L); 391 pm.makePersistent(pojo2); 392 commitTxn(); 393 394 assertNotNull(pojo.getId()); 395 assertNotNull(pojo2.getId()); 396 397 beginTxn(); 398 Query q = pm.newQuery("select from " + HasLongPkJDO.class.getName() + " where id == :ids"); 399 List<HasLongPkJDO> pojos = 400 (List<HasLongPkJDO>) q.execute(Utils.newArrayList(pojo.getId(), pojo2.getId())); 401 assertEquals(2, pojos.size()); 402 // we should preserve order but right now we don't 403 Set<Long> pks = Utils.newHashSet(pojos.get(0).getId(), pojos.get(1).getId()); 404 assertEquals(pks, Utils.newHashSet(1L, 2L)); 405 commitTxn(); 406 } 407}