@@ -377,26 +377,76 @@ public void should_be_able_to_use_udtCodec_standalone() {
377377 assertThat (otherAddresses ).containsOnly (MapEntry .entry ("condo" , expectedOtherAddress ));
378378 }
379379
380+ /**
381+ * Ensures that if a table is altered after a {@link Mapper} is created that it continues to work as long as
382+ * the change is compatible. A new column is added to the table in this case, which is a compatible change.
383+ * <p>
384+ * It also ensures that after the change is made that requesting a new {@link Mapper} returns a different instance
385+ * instead of the existing one.
386+ *
387+ * @jira_ticket JAVA-1126
388+ * @test_category object_mapper
389+ */
380390 @ Test (groups = "short" )
381391 public void should_work_normally_when_when_table_is_altered_but_remains_compatible () {
392+ MappingManager manager = new MappingManager (session ());
382393 User expected = new User ("Paul" , new Address ("12 4th Street" , "Springfield" , 12345 , "12341343" , "435423245" ));
383- Mapper <User > m = new MappingManager (session ()).mapper (User .class );
394+ Mapper <User > m = manager .mapper (User .class );
395+ Mapper <User > m1 = manager .mapper (User .class );
396+ assertThat (m1 ).isSameAs (m );
384397 session ().execute ("ALTER TABLE users ADD foo text" );
385398 m .save (expected );
386399 User retrieved = m .get (expected .getUserId ());
387400 assertThat (retrieved ).isEqualTo (expected );
401+
402+ // Mapper should be recreated since table changed.
403+ Mapper <User > m2 = manager .mapper (User .class );
404+ assertThat (m2 ).isNotSameAs (m );
388405 }
389406
407+ /**
408+ * Ensures that if a UDT is altered after a {@link Mapper} is created that has a UDT that it continues to work as
409+ * long as the change is compatible. A new field is added to the table in this case, which is a compatible change.
410+ * <p>
411+ * It also ensures that after the change is made that requesting a new {@link TypeCodec} for that UDT and a new
412+ * {@link Mapper} for a table using that UDT returns different instances instead of the existing ones.
413+ *
414+ * @jira_ticket JAVA-1126
415+ * @test_category object_mapper
416+ */
390417 @ Test (groups = "short" )
391418 public void should_work_normally_when_udt_is_altered_but_remains_compatible () {
419+ MappingManager manager = new MappingManager (session ());
392420 User expected = new User ("Paul" , new Address ("12 4th Street" , "Springfield" , 12345 , "12341343" , "435423245" ));
393- Mapper <User > m = new MappingManager (session ()).mapper (User .class );
421+ Mapper <User > m = manager .mapper (User .class );
422+ TypeCodec c = manager .udtCodec (Address .class );
423+ TypeCodec c1 = manager .udtCodec (Address .class );
424+ assertThat (c1 ).isSameAs (c );
394425 session ().execute ("ALTER TYPE address ADD foo text" );
395426 m .save (expected );
396427 User retrieved = m .get (expected .getUserId ());
397428 assertThat (retrieved ).isEqualTo (expected );
429+
430+ // Codec should be recreated when requested since UDT and thus the table changed.
431+ Mapper m2 = manager .mapper (User .class );
432+ assertThat (m2 ).isNotSameAs (m );
433+
434+ // Codec should be recreated when requested since UDT changed.
435+ TypeCodec c2 = manager .udtCodec (Address .class );
436+ assertThat (c2 ).isNotSameAs (c );
398437 }
399438
439+ /**
440+ * Ensures that if a table is dropped after a {@link Mapper} is created that the {@link Mapper} can no longer
441+ * successfully make queries and that attempting to create a new {@link Mapper} throws an
442+ * {@link IllegalArgumentException}.
443+ * <p>
444+ * It also ensures that requesting a {@link Mapper} fails as the previous {@link Mapper} was evicted and a new
445+ * one cannot be created as the table has been dropped.
446+ *
447+ * @jira_ticket JAVA-1126
448+ * @test_category object_mapper
449+ */
400450 @ Test (groups = "short" )
401451 public void should_throw_error_when_table_is_dropped () {
402452 User user = new User ("Paul" , new Address ("12 4th Street" , "Springfield" , 12345 , "12341343" , "435423245" ));
@@ -417,7 +467,6 @@ public void should_throw_error_when_table_is_dropped() {
417467 assertThat (e .getMessage ()).isEqualTo ("unconfigured table users" );
418468 }
419469 // trying to use a new mapper
420- manager = new MappingManager (session ());
421470 try {
422471 manager .mapper (User .class );
423472 fail ("Expected IllegalArgumentException" );
@@ -426,6 +475,16 @@ public void should_throw_error_when_table_is_dropped() {
426475 }
427476 }
428477
478+ /**
479+ * Ensures that if a table is altered after a {@link Mapper} is created that it no longer continues to work if
480+ * the change is not compatible. A column is dropped from the table in this case, which is an incompatible change.
481+ * <p>
482+ * It also ensures that requesting a {@link Mapper} fails as the previous {@link Mapper} was evicted and a new
483+ * one cannot be created as a declared column is missing from the schema but is present in the class definition.
484+ *
485+ * @jira_ticket JAVA-1126
486+ * @test_category object_mapper
487+ */
429488 @ Test (groups = "short" )
430489 public void should_throw_error_when_table_is_altered_and_is_not_compatible_anymore () {
431490 User user = new User ("Paul" , new Address ("12 4th Street" , "Springfield" , 12345 , "12341343" , "435423245" ));
@@ -446,7 +505,6 @@ public void should_throw_error_when_table_is_altered_and_is_not_compatible_anymo
446505 assertThat (e .getMessage ()).isEqualTo ("Undefined name mainaddress in selection clause" );
447506 }
448507 // trying to use a new mapper
449- manager = new MappingManager (session ());
450508 try {
451509 manager .mapper (User .class );
452510 fail ("Expected IllegalArgumentException" );
@@ -455,6 +513,19 @@ public void should_throw_error_when_table_is_altered_and_is_not_compatible_anymo
455513 }
456514 }
457515
516+ /**
517+ * Ensures that if a UDT is altered after a {@link Mapper} is created that it no longer continues to work if
518+ * the change made to the UDT is not compatible. A field is renamed in the UDT in this case, which is not a
519+ * compatible change.
520+ * <p>
521+ * It also ensures that requesting a {@link Mapper} fails as the previous {@link Mapper} was evicted and a new
522+ * one cannot be created as a declared field is missing from the schema but is present in the class definition.
523+ * <p>
524+ * Also verifies that a {@link TypeCodec} cannot be requested for the UDT as well.
525+ *
526+ * @jira_ticket JAVA-1126
527+ * @test_category object_mapper
528+ */
458529 @ Test (groups = "short" )
459530 public void should_throw_error_when_udt_is_altered_and_is_not_compatible_anymore () {
460531 User user = new User ("Paul" , new Address ("12 4th Street" , "Springfield" , 12345 , "12341343" , "435423245" ));
@@ -477,7 +548,6 @@ public void should_throw_error_when_udt_is_altered_and_is_not_compatible_anymore
477548 // ok, codec could not be created
478549 }
479550 // trying to use a new mapper
480- manager = new MappingManager (session ());
481551 try {
482552 manager .mapper (User .class );
483553 fail ("Expected IllegalArgumentException" );
@@ -492,5 +562,170 @@ public void should_throw_error_when_udt_is_altered_and_is_not_compatible_anymore
492562 }
493563 }
494564
565+ @ UDT (name = "address" )
566+ public static class AddressUnknownField {
567+
568+ private String city ;
569+
570+ private String street ;
571+
572+ @ Field (name = "ZIP_code" , caseSensitive = true )
573+ private int zipCode ;
574+
575+ private Set <String > phones ;
576+
577+ public String province ;
578+
579+ public AddressUnknownField () {
580+ }
581+
582+ public String getStreet () {
583+ return street ;
584+ }
585+
586+ public void setStreet (String street ) {
587+ this .street = street ;
588+ }
589+
590+ public String getCity () {
591+ return city ;
592+ }
593+
594+ public void setCity (String city ) {
595+ this .city = city ;
596+ }
597+
598+ public int getZipCode () {
599+ return zipCode ;
600+ }
601+
602+ public void setZipCode (int zipCode ) {
603+ this .zipCode = zipCode ;
604+ }
605+
606+ public Set <String > getPhones () {
607+ return phones ;
608+ }
609+
610+ public void setPhones (Set <String > phones ) {
611+ this .phones = phones ;
612+ }
613+
614+ public String getProvince () {
615+ return province ;
616+ }
617+
618+ public void setProvince (String province ) {
619+ this .province = province ;
620+ }
621+ }
622+
623+
624+ /**
625+ * Ensures that when attempting to create a {@link TypeCodec} from a class that has a field that does not exist in
626+ * the UDT that an {@link IllegalArgumentException} is thrown.
627+ *
628+ * @jira_ticket JAVA-1126
629+ * @test_category object_mapper
630+ */
631+ @ Test (groups = "short" , expectedExceptions = IllegalArgumentException .class )
632+ public void should_fail_to_create_codec_if_class_has_field_not_in_udt () {
633+ MappingManager manager = new MappingManager (session ());
634+ manager .getUDTCodec (AddressUnknownField .class );
635+ }
636+
637+ @ UDT (name = "nonexistent" )
638+ public static class NonExistentUDT {
639+ public String name ;
640+
641+ public void setName (String name ) {
642+ this .name = name ;
643+ }
644+
645+ public String getName () {
646+ return this .name ;
647+ }
648+ }
649+
650+ /**
651+ * Ensures that when attempting to create a {@link TypeCodec} from a class that has a {@link UDT} annotation with
652+ * a name that doesn't exist in the current keyspace that an {@link IllegalArgumentException} is thrown.
653+ *
654+ * @jira_ticket JAVA-1126
655+ * @test_category object_mapper
656+ */
657+ @ Test (groups = "short" , expectedExceptions = IllegalArgumentException .class )
658+ public void should_fail_to_create_codec_if_udt_does_not_exist () {
659+ MappingManager manager = new MappingManager (session ());
660+ manager .getUDTCodec (NonExistentUDT .class );
661+ }
662+
663+ @ Table (name = "users" )
664+ public static class UserWithAddressUnknownField {
665+ @ PartitionKey
666+ @ Column (name = "user_id" )
667+ private UUID userId ;
668+
669+ private String name ;
670+
671+ @ Frozen
672+ private AddressUnknownField mainAddress ;
673+
674+ @ Column (name = "other_addresses" )
675+ @ FrozenValue
676+ private Map <String , Address > otherAddresses = Maps .newHashMap ();
677+
678+ public UserWithAddressUnknownField () {
679+ }
680+
681+ public UUID getUserId () {
682+ return userId ;
683+ }
684+
685+ public void setUserId (UUID userId ) {
686+ this .userId = userId ;
687+ }
688+
689+ public String getName () {
690+ return name ;
691+ }
692+
693+ public void setName (String name ) {
694+ this .name = name ;
695+ }
696+
697+ public AddressUnknownField getMainAddress () {
698+ return mainAddress ;
699+ }
700+
701+ public void setMainAddress (AddressUnknownField address ) {
702+ this .mainAddress = address ;
703+ }
704+
705+ public Map <String , Address > getOtherAddresses () {
706+ return otherAddresses ;
707+ }
708+
709+ public void setOtherAddresses (Map <String , Address > otherAddresses ) {
710+ this .otherAddresses = otherAddresses ;
711+ }
712+
713+ public void addOtherAddress (String name , Address address ) {
714+ this .otherAddresses .put (name , address );
715+ }
716+ }
717+
718+ /**
719+ * Ensures that when attempting to create a {@link Mapper} from a class that has a field that is a class mapping to
720+ * a user type that has a field that does not exist in that UDT that an {@link IllegalArgumentException} is thrown.
721+ *
722+ * @jira_ticket JAVA-1126
723+ * @test_category object_mapper
724+ */
725+ @ Test (groups = "short" , expectedExceptions = IllegalArgumentException .class )
726+ public void should_fail_to_create_mapper_if_class_has_udt_field_class_that_has_field_not_in_udt () {
727+ MappingManager manager = new MappingManager (session ());
728+ manager .mapper (UserWithAddressUnknownField .class );
729+ }
495730
496731}
0 commit comments