25
25
import com .google .common .collect .Sets ;
26
26
import org .testng .annotations .Test ;
27
27
28
+ import java .io .PrintWriter ;
29
+ import java .io .StringWriter ;
28
30
import java .math .BigDecimal ;
29
31
import java .math .BigInteger ;
30
32
import java .net .InetAddress ;
33
+ import java .nio .ByteBuffer ;
31
34
import java .util .*;
32
35
33
36
import static com .datastax .driver .core .DataType .cint ;
34
37
import static com .datastax .driver .core .querybuilder .QueryBuilder .*;
35
38
import static org .assertj .core .api .Assertions .assertThat ;
36
- import static org .testng .Assert .*;
39
+ import static org .assertj .core .api .Assertions .fail ;
40
+ import static org .testng .Assert .assertEquals ;
41
+ import static org .testng .Assert .assertTrue ;
37
42
38
43
public class QueryBuilderTest {
39
44
@@ -140,35 +145,35 @@ public void selectTest() throws Exception {
140
145
141
146
try {
142
147
select ().countAll ().from ("foo" ).orderBy (asc ("a" ), desc ("b" )).orderBy (asc ("a" ), desc ("b" ));
143
- fail ();
148
+ fail ("Expected an IllegalStateException" );
144
149
} catch (IllegalStateException e ) {
145
150
assertEquals (e .getMessage (), "An ORDER BY clause has already been provided" );
146
151
}
147
152
148
153
try {
149
154
select ().column ("a" ).all ().from ("foo" );
150
- fail ();
155
+ fail ("Expected an IllegalStateException" );
151
156
} catch (IllegalStateException e ) {
152
157
assertEquals (e .getMessage (), "Some columns ([a]) have already been selected." );
153
158
}
154
159
155
160
try {
156
161
select ().column ("a" ).countAll ().from ("foo" );
157
- fail ();
162
+ fail ("Expected an IllegalStateException" );
158
163
} catch (IllegalStateException e ) {
159
164
assertEquals (e .getMessage (), "Some columns ([a]) have already been selected." );
160
165
}
161
166
162
167
try {
163
168
select ().all ().from ("foo" ).limit (-42 );
164
- fail ();
169
+ fail ("Expected an IllegalArgumentException" );
165
170
} catch (IllegalArgumentException e ) {
166
171
assertEquals (e .getMessage (), "Invalid LIMIT value, must be strictly positive" );
167
172
}
168
173
169
174
try {
170
175
select ().all ().from ("foo" ).limit (42 ).limit (42 );
171
- fail ();
176
+ fail ("Expected an IllegalStateException" );
172
177
} catch (IllegalStateException e ) {
173
178
assertEquals (e .getMessage (), "A LIMIT value has already been provided" );
174
179
}
@@ -244,7 +249,7 @@ public void insertTest() throws Exception {
244
249
245
250
try {
246
251
insertInto ("foo" ).values (new String []{"a" , "b" }, new Object []{1 , 2 , 3 });
247
- fail ();
252
+ fail ("Expected an IllegalArgumentException" );
248
253
} catch (IllegalArgumentException e ) {
249
254
assertEquals (e .getMessage (), "Got 2 names but 3 values" );
250
255
}
@@ -327,7 +332,7 @@ public void updateTest() throws Exception {
327
332
328
333
try {
329
334
update ("foo" ).using (ttl (-400 ));
330
- fail ();
335
+ fail ("Expected an IllegalArgumentException" );
331
336
} catch (IllegalArgumentException e ) {
332
337
assertEquals (e .getMessage (), "Invalid ttl, must be positive" );
333
338
}
@@ -381,14 +386,14 @@ public void deleteTest() throws Exception {
381
386
382
387
try {
383
388
delete ().column ("a" ).all ().from ("foo" );
384
- fail ();
389
+ fail ("Expected an IllegalStateException" );
385
390
} catch (IllegalStateException e ) {
386
391
assertEquals (e .getMessage (), "Some columns ([a]) have already been selected." );
387
392
}
388
393
389
394
try {
390
395
delete ().from ("foo" ).using (timestamp (-1240003134L ));
391
- fail ();
396
+ fail ("Expected an IllegalArgumentException" );
392
397
} catch (IllegalArgumentException e ) {
393
398
assertEquals (e .getMessage (), "Invalid timestamp, must be positive" );
394
399
}
@@ -895,7 +900,6 @@ public void should_return_object_at_ith_index() {
895
900
}
896
901
897
902
@ Test (groups = "unit" )
898
- @ CassandraVersion (major = 2.1 )
899
903
public void should_serialize_collections_of_serializable_elements () {
900
904
Set <UUID > set = Sets .newHashSet (UUID .randomUUID ());
901
905
List <Date > list = Lists .newArrayList (new Date ());
@@ -912,31 +916,27 @@ public void should_serialize_collections_of_serializable_elements() {
912
916
}
913
917
914
918
@ Test (groups = "unit" )
915
- @ CassandraVersion (major = 2.1 )
916
919
public void should_not_attempt_to_serialize_function_calls_in_collections () {
917
920
BuiltStatement query = insertInto ("foo" ).value ("v" , Sets .newHashSet (fcall ("func" , 1 )));
918
921
assertThat (query .getQueryString ()).isEqualTo ("INSERT INTO foo (v) VALUES ({func(1)});" );
919
922
assertThat (query .getValues (ProtocolVersion .V3 )).isNullOrEmpty ();
920
923
}
921
924
922
925
@ Test (groups = "unit" )
923
- @ CassandraVersion (major = 2.1 )
924
926
public void should_not_attempt_to_serialize_bind_markers_in_collections () {
925
927
BuiltStatement query = insertInto ("foo" ).value ("v" , Lists .newArrayList (1 , 2 , bindMarker ()));
926
928
assertThat (query .getQueryString ()).isEqualTo ("INSERT INTO foo (v) VALUES ([1,2,?]);" );
927
929
assertThat (query .getValues (ProtocolVersion .V3 )).isNullOrEmpty ();
928
930
}
929
931
930
932
@ Test (groups = "unit" )
931
- @ CassandraVersion (major = 2.1 )
932
933
public void should_not_attempt_to_serialize_raw_values_in_collections () {
933
934
BuiltStatement query = insertInto ("foo" ).value ("v" , ImmutableMap .of (1 , raw ("x" )));
934
935
assertThat (query .getQueryString ()).isEqualTo ("INSERT INTO foo (v) VALUES ({1:x});" );
935
936
assertThat (query .getValues (ProtocolVersion .V3 )).isNullOrEmpty ();
936
937
}
937
938
938
939
@ Test (groups = "unit" )
939
- @ CassandraVersion (major = 2.1 )
940
940
public void should_not_attempt_to_serialize_collections_containing_numbers () {
941
941
BuiltStatement query ;
942
942
// lists
@@ -956,4 +956,24 @@ public void should_not_attempt_to_serialize_collections_containing_numbers() {
956
956
assertThat (query .hasValues ()).isFalse ();
957
957
}
958
958
959
+ @ Test (groups = "unit" )
960
+ public void should_include_original_cause_when_arguments_invalid () {
961
+ // Collection elements in protocol v2 must be at most 65535 bytes
962
+ ByteBuffer bb = ByteBuffer .allocate (65536 ); // too big
963
+ List <ByteBuffer > value = Lists .newArrayList (bb );
964
+
965
+ BuiltStatement s = insertInto ("foo" ).value ("l" , value );
966
+ try {
967
+ s .getValues (ProtocolVersion .V2 );
968
+ fail ("Expected an IllegalArgumentException" );
969
+ } catch (IllegalArgumentException e ) {
970
+ StringWriter writer = new StringWriter ();
971
+ e .printStackTrace (new PrintWriter (writer ));
972
+ String stackTrace = writer .toString ();
973
+ assertThat (stackTrace ).contains (
974
+ "Native protocol version 2 supports only elements with size up to 65535 bytes - " +
975
+ "but element size is 65536 bytes" );
976
+ }
977
+ }
978
+
959
979
}
0 commit comments