litesoft
@ HEAD
litesoft / trunk / Java / GAE / DiscoveryTests / DatastoreServiceTests / src / org / litesoft / gae / datastoreservice / DatastoreServiceTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
package org.litesoft.gae.datastoreservice; import org.litesoft.core.typeutils.Objects; import java.util.*; import junit.framework.TestCase; import junit.framework.TestSuite; import com.google.appengine.api.datastore.DatastoreService; import com.google.appengine.api.datastore.DatastoreServiceConfig; import com.google.appengine.api.datastore.DatastoreServiceFactory; import com.google.appengine.api.datastore.Entity; import com.google.appengine.api.datastore.EntityNotFoundException; import com.google.appengine.api.datastore.FetchOptions; import com.google.appengine.api.datastore.ImplicitTransactionManagementPolicy; import com.google.appengine.api.datastore.Key; import com.google.appengine.api.datastore.KeyFactory; import com.google.appengine.api.datastore.PreparedQuery; import com.google.appengine.api.datastore.Query; import com.google.appengine.api.datastore.Transaction; import com.google.appengine.api.datastore.ReadPolicy.Consistency; import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; import com.google.appengine.tools.development.testing.LocalServiceTestHelper; public class DatastoreServiceTest extends TestCase { private static final String KIND = "TheFlintstones"; private final LocalServiceTestHelper mHelper = new LocalServiceTestHelper( new LocalDatastoreServiceTestConfig() ); public static TestSuite suite() { return new TestSuite( DatastoreServiceTest.class ); } public DatastoreServiceTest( String name ) { super( name ); } public static void main( String[] args ) { junit.textui.TestRunner.run( suite() ); } @Override protected void setUp() throws Exception { super.setUp(); mHelper.setUp(); mConfig = DatastoreServiceConfig.Builder.withDefaults() // Deadline=null, // ImplicitTransactionManagementPolicy=NONE, // ReadPolicy.Consistency=STRONG .deadline( 5 ); // Deadline=5 secs mDatastoreService = DatastoreServiceFactory.getDatastoreService( mConfig ); mGOD_Apollo = KeyFactory.createKey( "GODS", "Apollo" ); mGOD_Venus = KeyFactory.createKey( "GODS", "Venus" ); } @Override public void tearDown() { mHelper.tearDown(); } private DatastoreServiceConfig mConfig; private DatastoreService mDatastoreService; private Key mGOD_Apollo, mGOD_Venus; private Entity createEntity( Key pGOD, String pName ) { Entity zEntity = new Entity( KIND, pName, pGOD ); zEntity.setUnindexedProperty( "Version", 1L ); zEntity.setProperty( "Name", pName ); zEntity.setUnindexedProperty( "Children", 0L ); return zEntity; } private void inc( Entity pEntity, String pProperty ) { Long zLong = (Long) pEntity.getProperty( pProperty ); pEntity.setUnindexedProperty( pProperty, zLong + 1 ); } private Entity updateChildren( Entity pEntity ) { inc( pEntity, "Version" ); inc( pEntity, "Children" ); return pEntity; } public void test_DataStoreConfig() throws Exception { assertEquals( 5.0d, mConfig.getDeadline(), 0.0001 ); assertEquals( ImplicitTransactionManagementPolicy.NONE, mConfig.getImplicitTransactionManagementPolicy() ); assertEquals( Consistency.STRONG, mConfig.getReadPolicy().getConsistency() ); } public void test_NonConflictingDifferentGodsOverlapingTransactionsNew() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBarney = createEntity( mGOD_Apollo, "Barney" ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zApolloTransaction.commit(); zVenusTransaction.commit(); readAndValidate( null, zEntityFred, "Fred", 1, 0 ); readAndValidate( null, zEntityWilma, "Wilma", 1, 0 ); readAndValidate( null, zEntityBarney, "Barney", 1, 0 ); readAndValidate( null, zEntityBetty, "Betty", 1, 0 ); } public void test_TransactionReadMultiGOD() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); zApolloTransaction.commit(); zVenusTransaction.commit(); Transaction zTransaction = mDatastoreService.beginTransaction(); mDatastoreService.get( zTransaction, zEntityFred.getKey() ); // will // succeed! try { mDatastoreService.get( zTransaction, zEntityWilma.getKey() ); // will // Fail! fail( "Fred & Wilma - Did NOT Fail with different GODS (Entity Groups)" ); } catch ( IllegalArgumentException expected ) { String zMessage = expected.getMessage(); assertTrue( zMessage, zMessage .startsWith( "can't operate on multiple entity groups in a single transaction." ) ); } zTransaction.rollback(); } public void test_TransactionUpdate() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBarney = createEntity( mGOD_Apollo, "Barney" ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zApolloTransaction.commit(); zVenusTransaction.commit(); zVenusTransaction = mDatastoreService.beginTransaction(); zEntityWilma = updateChildren( readAndValidate( zVenusTransaction, zEntityWilma, "Wilma", 1, 0 ) ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityPebbles = createEntity( mGOD_Venus, "Pebbles" ); mDatastoreService.put( zVenusTransaction, zEntityPebbles ); // Can't Update Fred! zVenusTransaction.commit(); zApolloTransaction = mDatastoreService.beginTransaction(); zEntityBarney = updateChildren( readAndValidate( zApolloTransaction, zEntityBarney, "Barney", 1, 0 ) ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBammBamm = createEntity( mGOD_Apollo, "BammBamm" ); mDatastoreService.put( zApolloTransaction, zEntityBammBamm ); // Can't Update Betty! zApolloTransaction.commit(); readAndValidate( null, zEntityFred, "Fred", 1, 0 ); readAndValidate( null, zEntityWilma, "Wilma", 2, 1 ); readAndValidate( null, zEntityPebbles, "Pebbles", 1, 0 ); readAndValidate( null, zEntityBarney, "Barney", 2, 1 ); readAndValidate( null, zEntityBetty, "Betty", 1, 0 ); readAndValidate( null, zEntityBammBamm, "BammBamm", 1, 0 ); } public void test_NonConflictingDifferentGodsOverlapingTransactionsUpdate() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); zApolloTransaction.commit(); zVenusTransaction.commit(); zApolloTransaction = mDatastoreService.beginTransaction(); zVenusTransaction = mDatastoreService.beginTransaction(); zEntityWilma = updateChildren( readAndValidate( zVenusTransaction, zEntityWilma, "Wilma", 1, 0 ) ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityPebbles = createEntity( mGOD_Venus, "Pebbles" ); mDatastoreService.put( zVenusTransaction, zEntityPebbles ); zEntityFred = updateChildren( readAndValidate( zApolloTransaction, zEntityFred, "Fred", 1, 0 ) ); mDatastoreService.put( zApolloTransaction, zEntityFred ); zApolloTransaction.commit(); zVenusTransaction.commit(); readAndValidate( null, zEntityFred, "Fred", 2, 1 ); readAndValidate( null, zEntityWilma, "Wilma", 2, 1 ); readAndValidate( null, zEntityPebbles, "Pebbles", 1, 0 ); } public void test_ConflictingSameEntityOverlapingTransactionsUpdate() throws Exception { Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); zVenusTransaction.commit(); zVenusTransaction = mDatastoreService.beginTransaction(); zEntityWilma = updateChildren( readAndValidate( zVenusTransaction, zEntityWilma, "Wilma", 1, 0 ) ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityPebbles = createEntity( mGOD_Venus, "Pebbles" ); mDatastoreService.put( zVenusTransaction, zEntityPebbles ); Transaction zVenus2Transaction = mDatastoreService.beginTransaction(); Entity zEntityWilma2 = updateChildren( readAndValidate2( zVenus2Transaction, zEntityWilma, "Wilma", 1, 0 ) ); mDatastoreService.put( zVenus2Transaction, zEntityWilma2 ); zVenusTransaction.commit(); try { zVenus2Transaction.commit(); fail( "Two Transaction Wilma Update - Did NOT Fail" ); } catch ( ConcurrentModificationException expected ) { String zMessage = expected.getMessage(); assertTrue( zMessage, zMessage.startsWith( "too much contention on these datastore entities. please try again." ) ); } readAndValidate( null, zEntityWilma, "Wilma", 2, 1 ); } public void test_NonConflictingSameGodDifferentEntityOverlapingTransactionsUpdate() throws Exception { Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zVenusTransaction.commit(); zVenusTransaction = mDatastoreService.beginTransaction(); zEntityWilma = updateChildren( readAndValidate( zVenusTransaction, zEntityWilma, "Wilma", 1, 0 ) ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Transaction zVenus2Transaction = mDatastoreService.beginTransaction(); zEntityBetty = updateChildren( readAndValidate( zVenus2Transaction, zEntityBetty, "Betty", 1, 0 ) ); mDatastoreService.put( zVenus2Transaction, zEntityBetty ); zVenusTransaction.commit(); try { zVenus2Transaction.commit(); fail( "Two Transaction Wilma & Betty Update - Did NOT Fail" ); } catch ( ConcurrentModificationException expected ) { String zMessage = expected.getMessage(); assertTrue( zMessage, zMessage.startsWith( "too much contention on these datastore entities. please try again." ) ); } readAndValidate( null, zEntityWilma, "Wilma", 2, 1 ); readAndValidate2( null, zEntityBetty, "Betty", 1, 0 ); } public void test_QueryAllDifferentGods() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBarney = createEntity( mGOD_Apollo, "Barney" ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zApolloTransaction.commit(); zVenusTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( KIND ).addSort( "Name" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 4, zList.size() ); validate( zList.get( 0 ), "Barney", 1, 0 ); validate( zList.get( 1 ), "Betty", 1, 0 ); validate( zList.get( 2 ), "Fred", 1, 0 ); validate( zList.get( 3 ), "Wilma", 1, 0 ); } public void test_QuerySpecificGod() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBarney = createEntity( mGOD_Apollo, "Barney" ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zApolloTransaction.commit(); zVenusTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( KIND, mGOD_Apollo ).addSort( "Name" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 2, zList.size() ); validate( zList.get( 0 ), "Barney", 1, 0 ); validate( zList.get( 1 ), "Fred", 1, 0 ); } public void test_QueryEqualsFilter() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBarney = createEntity( mGOD_Apollo, "Barney" ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zApolloTransaction.commit(); zVenusTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( KIND ).addSort( "Name" ).addFilter( "Name", Query.FilterOperator.EQUAL, "Barney") ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 1, zList.size() ); validate( zList.get( 0 ), "Barney", 1, 0 ); } public void test_QueryLessThanFilter() throws Exception { Transaction zApolloTransaction = mDatastoreService.beginTransaction(); Transaction zVenusTransaction = mDatastoreService.beginTransaction(); Entity zEntityFred = createEntity( mGOD_Apollo, "Fred" ); mDatastoreService.put( zApolloTransaction, zEntityFred ); Entity zEntityWilma = createEntity( mGOD_Venus, "Wilma" ); mDatastoreService.put( zVenusTransaction, zEntityWilma ); Entity zEntityBarney = createEntity( mGOD_Apollo, "Barney" ); mDatastoreService.put( zApolloTransaction, zEntityBarney ); Entity zEntityBetty = createEntity( mGOD_Venus, "Betty" ); mDatastoreService.put( zVenusTransaction, zEntityBetty ); zApolloTransaction.commit(); zVenusTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( KIND ).addSort( "Name" ).addFilter( "Name", Query.FilterOperator.LESS_THAN, "C") ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 2, zList.size() ); validate( zList.get( 0 ), "Barney", 1, 0 ); validate( zList.get( 1 ), "Betty", 1, 0 ); } public void test_NullKey() throws Exception { try { KeyFactory.createKey( KIND, null ); fail( "null Key did NOT fail" ); } catch ( IllegalArgumentException expected ) { String zMessage = expected.getMessage(); assertTrue( zMessage, zMessage.startsWith( "name cannot be null or empty" ) ); } // new Entity( KIND, null, pGOD ); // Does Not Fail, but // implicitly creates a auto-generated id key!! } public void test_EmptyKey() throws Exception { try { KeyFactory.createKey( KIND, "" ); fail( "empty Key did NOT fail" ); } catch ( IllegalArgumentException expected ) { String zMessage = expected.getMessage(); assertTrue( zMessage, zMessage.startsWith( "name cannot be null or empty" ) ); } } public void test_IndexingString() throws Exception { Transaction zTransaction = mDatastoreService.beginTransaction(); String v1, v2; mDatastoreService.put( zTransaction, createIndexingEntity( "Strings", null ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Strings", v1 = "" ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Strings", v2 = "Fred" ) ); zTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( "Strings" ).addSort( "FieldValue" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 3, zList.size() ); validate( zList.get( 0 ), null ); validate( zList.get( 1 ), v1 ); validate( zList.get( 2 ), v2 ); } public void test_IndexingBoolean() throws Exception { Transaction zTransaction = mDatastoreService.beginTransaction(); Boolean v1, v2; mDatastoreService.put( zTransaction, createIndexingEntity( "Booleans", null ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Booleans", v1 = Boolean.FALSE ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Booleans", v2 = Boolean.TRUE ) ); zTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( "Booleans" ).addSort( "FieldValue" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 3, zList.size() ); validate( zList.get( 0 ), null ); validate( zList.get( 1 ), v1 ); validate( zList.get( 2 ), v2 ); } public void test_IndexingLong() throws Exception { Transaction zTransaction = mDatastoreService.beginTransaction(); Long v1, v2; mDatastoreService.put( zTransaction, createIndexingEntity( "Longs", null ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Longs", v1 = 1L ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Longs", v2 = 100L ) ); zTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( "Longs" ).addSort( "FieldValue" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 3, zList.size() ); validate( zList.get( 0 ), null ); validate( zList.get( 1 ), v1 ); validate( zList.get( 2 ), v2 ); } public void test_IndexingDouble() throws Exception { Transaction zTransaction = mDatastoreService.beginTransaction(); Double v1, v2; mDatastoreService.put( zTransaction, createIndexingEntity( "Doubles", null ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Doubles", v1 = 1.1D ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Doubles", v2 = 11.2D ) ); zTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( "Doubles" ).addSort( "FieldValue" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 3, zList.size() ); validate( zList.get( 0 ), null ); validate( zList.get( 1 ), v1 ); validate( zList.get( 2 ), v2 ); } public void test_IndexingDate() throws Exception { Transaction zTransaction = mDatastoreService.beginTransaction(); Date v1, v2; mDatastoreService.put( zTransaction, createIndexingEntity( "Dates", null ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Dates", v1 = createDate( 1957, 1, 4, 11, 12, 13, 14 ) ) ); mDatastoreService.put( zTransaction, createIndexingEntity( "Dates", v2 = createDate( 2000, 2, 29, 0, 0, 0, 0 ) ) ); zTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( "Dates" ).addSort( "FieldValue" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 3, zList.size() ); validate( zList.get( 0 ), null ); validate( zList.get( 1 ), v1 ); validate( zList.get( 2 ), v2 ); } public void test_LargeCollections() throws Exception { Transaction zTransaction = mDatastoreService.beginTransaction(); long zMaxLongsThatWork = 62645L; Entity zEntity; Set<Long> zLongs; try { zLongs = createLongs( zMaxLongsThatWork + 1 ); // Too Big! zEntity = new Entity( "LargeCollections", "Test" ); zEntity.setProperty( "FieldValue", "Test" ); zEntity.setUnindexedProperty( "Longs", zLongs ); mDatastoreService.put( zTransaction, zEntity ); fail( "Too Many did not fail w/ too large" ); } catch ( Exception expected ) { assertNotNull( expected ); // com.google.apphosting.api.ApiProxy$RequestTooLargeException: The request to API call datastore_v3.Put() was too large. } zLongs = createLongs( zMaxLongsThatWork ); // Just Small Enough! zEntity = new Entity( "LargeCollections", "Test" ); zEntity.setProperty( "FieldValue", "Test" ); zEntity.setUnindexedProperty( "Longs", zLongs ); mDatastoreService.put( zTransaction, zEntity ); zTransaction.commit(); PreparedQuery zQuery = mDatastoreService.prepare( new Query( "LargeCollections" ).addSort( "FieldValue" ) ); List<Entity> zList = zQuery.asList( FetchOptions.Builder.withOffset( 0 ) ); assertEquals( "Entities", 1, zList.size() ); zEntity = zList.get( 0 ); assertEquals( "FieldValue", "Test", zEntity.getProperty( "FieldValue" ) ); validate( zLongs, zEntity.getProperty( "Longs" ) ); } private Set<Long> createLongs( long pLongsToCreate ) { HashSet<Long> zSet = new HashSet<Long>(); while ( --pLongsToCreate >= 0 ) { zSet.add( pLongsToCreate ); } return zSet; } @SuppressWarnings("unchecked") private void validate( Set<Long> zExpected, Object pFieldValue ) { List<Long> zReadList = (List<Long>)pFieldValue; for (Long zLong : zReadList) { if ( !zExpected.remove( zLong ) ) { fail( "Found value (" + zLong + "), not written" ); } } if ( !zExpected.isEmpty() ) { fail( "Did NOT read: " + zExpected ); } } @SuppressWarnings("deprecation") private Date createDate( int pYear, int pMonth, int pDay, int pHour, int pMin, int pSec, int pMilliSecs ) { Date zDate = new Date( pYear - 1900, pMonth - 1, pDay, pHour, pMin, pSec ); if ( pMilliSecs != 0 ) { zDate = new Date( zDate.getTime() + pMilliSecs ); } return zDate; } @SuppressWarnings("deprecation") private String simpleDateToString( Date pDate ) { int zYear = pDate.getYear() + 1900; int zMonth = pDate.getMonth() + 1; int zDay = pDate.getDate(); int zHour = pDate.getHours(); int zMin = pDate.getMinutes(); int zSec = pDate.getSeconds(); int zMilliSecs = (int)(pDate.getTime() - new Date( zYear - 1900, zMonth - 1, zDay, zHour, zMin, zSec ).getTime()); return "" + zYear + "/" + zMonth + "/" + zDay + "-" + zHour + ":" + zMin + ":" + zSec + "." + zMilliSecs; } private String createIndexKey( Object pFieldValue ) { if ( pFieldValue == null ) { return " "; } if ( pFieldValue instanceof String ) { return pFieldValue + " "; } if ( pFieldValue instanceof Date ) { return simpleDateToString( (Date)pFieldValue ); } return pFieldValue.toString(); } private Entity createIndexingEntity( String pKind, Object pFieldValue ) { Entity zEntity = new Entity( pKind, createIndexKey( pFieldValue ), mGOD_Apollo ); zEntity.setProperty( "FieldValue", pFieldValue ); return zEntity; } private void validate( Entity pEntity, Object pExpectedFieldValue ) { validate( pEntity, createIndexKey( pExpectedFieldValue ), pExpectedFieldValue ); } private void validate( Entity pEntity, String pExpectedKey, Object pExpectedFieldValue ) { assertEquals( "Entity Key", pExpectedKey, pEntity.getKey().getName() ); Object zFieldValue = pEntity.getProperty( "FieldValue" ); if ( pExpectedFieldValue == null ) { assertNull( "FieldValue", zFieldValue ); } else { assertEquals( "Entity FieldValue", pExpectedFieldValue, zFieldValue ); assertEquals( "Entity FieldValue Class", pExpectedFieldValue.getClass(), zFieldValue.getClass() ); } } private Entity readAndValidate( Transaction pTransaction, Entity pEntity, String pName, long pCurrentVersion, long pCurrentChildren ) throws EntityNotFoundException { validate( pEntity, pName, pCurrentVersion, pCurrentChildren ); return readAndValidate2( pTransaction, pEntity, pName, pCurrentVersion, pCurrentChildren ); } private void validate( Entity pEntity, String pName, long pCurrentVersion, long pCurrentChildren ) { assertEquals( "Current Entity Version", pCurrentVersion, pEntity.getProperty( "Version" ) ); assertEquals( "Current Entity Name", pName, pEntity.getProperty( "Name" ) ); assertEquals( "Current Entity Children", pCurrentChildren, pEntity.getProperty( "Children" ) ); } private Entity readAndValidate2( Transaction pTransaction, Entity pEntity, String pName, long pCurrentVersion, long pCurrentChildren ) throws EntityNotFoundException { Key zKey = pEntity.getKey(); return readAndValidate3( pTransaction, zKey, pName, pCurrentVersion, pCurrentChildren ); } private Entity readAndValidate3( Transaction pTransaction, Key zKey, String pName, long pCurrentVersion, long pCurrentChildren ) throws EntityNotFoundException { assertEquals( "Key Kind", KIND, zKey.getKind() ); assertEquals( "Key Name (Our Key Value)", pName, zKey.getName() ); Entity zEntity = mDatastoreService.get( pTransaction, zKey ); if ( pTransaction == null ) { Object zVersion = zEntity.getProperty( "Version" ); assertNotNull( "Read Entity Version", zVersion ); if ( !zVersion.equals( pCurrentVersion ) ) { Transaction zTransaction = mDatastoreService.beginTransaction(); zEntity = mDatastoreService.get( zTransaction, zKey ); zTransaction.rollback(); } } assertEquals( "Read Entity Version", pCurrentVersion, zEntity.getProperty( "Version" ) ); assertEquals( "Read Entity Name", pName, zEntity.getProperty( "Name" ) ); assertEquals( "Read Entity Children", pCurrentChildren, zEntity.getProperty( "Children" ) ); return zEntity; } } |