1919 */
2020package org .apache .cassandra .db .marshal ;
2121
22+ import java .util .Arrays ;
23+
24+ import org .apache .cassandra .cql3 .FieldIdentifier ;
2225import org .apache .cassandra .serializers .MarshalException ;
26+ import org .apache .cassandra .utils .ByteBufferUtil ;
2327import org .junit .Test ;
2428
29+ import static org .junit .Assert .assertFalse ;
30+ import static org .junit .Assert .assertTrue ;
31+
2532public class BytesTypeTest
2633{
2734 private static final String INVALID_HEX = "33AG45F" ; // Invalid (has a G)
@@ -38,4 +45,68 @@ public void testFromStringWithValidString()
3845 {
3946 BytesType .instance .fromString (VALID_HEX );
4047 }
48+
49+ @ Test
50+ public void testValueCompatibilityWithScalarTypes ()
51+ {
52+ // BytesType should be value-compatible with simple scalar types
53+ assertTrue ("BytesType should be value-compatible with AsciiType" ,
54+ BytesType .instance .isValueCompatibleWith (AsciiType .instance ));
55+ assertTrue ("BytesType should be value-compatible with UTF8Type" ,
56+ BytesType .instance .isValueCompatibleWith (UTF8Type .instance ));
57+ assertTrue ("BytesType should be value-compatible with Int32Type" ,
58+ BytesType .instance .isValueCompatibleWith (Int32Type .instance ));
59+ assertTrue ("BytesType should be value-compatible with LongType" ,
60+ BytesType .instance .isValueCompatibleWith (LongType .instance ));
61+ assertTrue ("BytesType should be value-compatible with UUIDType" ,
62+ BytesType .instance .isValueCompatibleWith (UUIDType .instance ));
63+ assertTrue ("BytesType should be value-compatible with BooleanType" ,
64+ BytesType .instance .isValueCompatibleWith (BooleanType .instance ));
65+ assertTrue ("BytesType should be value-compatible with itself" ,
66+ BytesType .instance .isValueCompatibleWith (BytesType .instance ));
67+ }
68+
69+ @ Test
70+ public void testSerializationIncompatibilityWithCollections ()
71+ {
72+ // BytesType should NOT be serialization-compatible with collections (even frozen ones)
73+ // because converting a collection to raw bytes for schema changes is nonsensical
74+ ListType <?> frozenList = ListType .getInstance (Int32Type .instance , false );
75+ SetType <?> frozenSet = SetType .getInstance (Int32Type .instance , false );
76+ MapType <?, ?> frozenMap = MapType .getInstance (Int32Type .instance , UTF8Type .instance , false );
77+
78+ assertFalse ("BytesType should NOT be serialization-compatible with frozen<list>" ,
79+ BytesType .instance .isSerializationCompatibleWith (frozenList ));
80+ assertFalse ("BytesType should NOT be serialization-compatible with frozen<set>" ,
81+ BytesType .instance .isSerializationCompatibleWith (frozenSet ));
82+ assertFalse ("BytesType should NOT be serialization-compatible with frozen<map>" ,
83+ BytesType .instance .isSerializationCompatibleWith (frozenMap ));
84+
85+ // Also test with multi-cell collections
86+ ListType <?> multiCellList = ListType .getInstance (Int32Type .instance , true );
87+ SetType <?> multiCellSet = SetType .getInstance (Int32Type .instance , true );
88+ MapType <?, ?> multiCellMap = MapType .getInstance (Int32Type .instance , UTF8Type .instance , true );
89+
90+ assertFalse ("BytesType should NOT be serialization-compatible with list" ,
91+ BytesType .instance .isSerializationCompatibleWith (multiCellList ));
92+ assertFalse ("BytesType should NOT be serialization-compatible with set" ,
93+ BytesType .instance .isSerializationCompatibleWith (multiCellSet ));
94+ assertFalse ("BytesType should NOT be serialization-compatible with map" ,
95+ BytesType .instance .isSerializationCompatibleWith (multiCellMap ));
96+ }
97+
98+ @ Test
99+ public void testSerializationIncompatibilityWithUDT ()
100+ {
101+ // BytesType should NOT be serialization-compatible with User Defined Types
102+ // because converting a UDT to raw bytes for schema changes is nonsensical
103+ UserType udt = new UserType ("ks" ,
104+ ByteBufferUtil .bytes ("myType" ),
105+ Arrays .asList (FieldIdentifier .forQuoted ("field1" ), FieldIdentifier .forQuoted ("field2" )),
106+ Arrays .asList (Int32Type .instance , UTF8Type .instance ),
107+ true );
108+
109+ assertFalse ("BytesType should NOT be serialization-compatible with UDT" ,
110+ BytesType .instance .isSerializationCompatibleWith (udt ));
111+ }
41112}
0 commit comments