Elffuss BraidHub

kikocis/java-driver public

Add test to cover JAVA-977.
Changesetab0e361a495135e1c9c0b4950cd86f797e485443aeb341a931d6f66782110e59
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-09 22:31 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-04-01 14:16 +02:00
Parents6c9eb909ee48

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +44 −14
@@ -18,6 +18,7 @@
1818 import com.datastax.driver.core.*;
1919 import com.datastax.driver.core.exceptions.CodecNotFoundException;
2020 import com.datastax.driver.core.exceptions.InvalidQueryException;
21+import com.datastax.driver.core.exceptions.InvalidTypeException;
2122 import com.datastax.driver.core.utils.Bytes;
2223 import com.datastax.driver.core.utils.CassandraVersion;
2324 import com.google.common.collect.ImmutableList;
@@ -26,14 +27,19 @@
2627 import com.google.common.collect.Sets;
2728 import org.testng.annotations.Test;
2829
30+import java.io.PrintWriter;
31+import java.io.StringWriter;
2932 import java.math.BigDecimal;
3033 import java.math.BigInteger;
3134 import java.net.InetAddress;
35+import java.nio.ByteBuffer;
3236 import java.util.*;
3337
3438 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
3539 import static org.assertj.core.api.Assertions.assertThat;
36-import static org.testng.Assert.*;
40+import static org.assertj.core.api.Assertions.fail;
41+import static org.testng.Assert.assertEquals;
42+import static org.testng.Assert.assertTrue;
3743
3844 public class QueryBuilderTest {
3945
@@ -139,35 +145,35 @@
139145
140146 try {
141147 select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b"));
142- fail();
148+ fail("Expected an IllegalStateException");
143149 } catch (IllegalStateException e) {
144150 assertEquals(e.getMessage(), "An ORDER BY clause has already been provided");
145151 }
146152
147153 try {
148154 select().column("a").all().from("foo");
149- fail();
155+ fail("Expected an IllegalStateException");
150156 } catch (IllegalStateException e) {
151157 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
152158 }
153159
154160 try {
155161 select().column("a").countAll().from("foo");
156- fail();
162+ fail("Expected an IllegalStateException");
157163 } catch (IllegalStateException e) {
158164 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
159165 }
160166
161167 try {
162168 select().all().from("foo").limit(-42);
163- fail();
169+ fail("Expected an IllegalArgumentException");
164170 } catch (IllegalArgumentException e) {
165171 assertEquals(e.getMessage(), "Invalid LIMIT value, must be strictly positive");
166172 }
167173
168174 try {
169175 select().all().from("foo").limit(42).limit(42);
170- fail();
176+ fail("Expected an IllegalStateException");
171177 } catch (IllegalStateException e) {
172178 assertEquals(e.getMessage(), "A LIMIT value has already been provided");
173179 }
@@ -243,7 +249,7 @@
243249
244250 try {
245251 insertInto("foo").values(new String[]{"a", "b"}, new Object[]{1, 2, 3});
246- fail();
252+ fail("Expected an IllegalArgumentException");
247253 } catch (IllegalArgumentException e) {
248254 assertEquals(e.getMessage(), "Got 2 names but 3 values");
249255 }
@@ -323,7 +329,7 @@
323329
324330 try {
325331 update("foo").using(ttl(-400));
326- fail();
332+ fail("Expected an IllegalArgumentException");
327333 } catch (IllegalArgumentException e) {
328334 assertEquals(e.getMessage(), "Invalid ttl, must be positive");
329335 }
@@ -377,14 +383,14 @@
377383
378384 try {
379385 delete().column("a").all().from("foo");
380- fail();
386+ fail("Expected an IllegalStateException");
381387 } catch (IllegalStateException e) {
382388 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
383389 }
384390
385391 try {
386392 delete().from("foo").using(timestamp(-1240003134L));
387- fail();
393+ fail("Expected an IllegalArgumentException");
388394 } catch (IllegalArgumentException e) {
389395 assertEquals(e.getMessage(), "Invalid timestamp, must be positive");
390396 }
@@ -880,7 +886,6 @@
880886 }
881887
882888 @Test(groups = "unit")
883- @CassandraVersion(major = 2.1)
884889 public void should_serialize_collections_of_serializable_elements() {
885890 Set<UUID> set = Sets.newHashSet(UUID.randomUUID());
886891 List<Date> list = Lists.newArrayList(new Date());
@@ -897,7 +902,6 @@
897902 }
898903
899904 @Test(groups = "unit")
900- @CassandraVersion(major = 2.1)
901905 public void should_not_attempt_to_serialize_function_calls_in_collections() {
902906 BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1)));
903907 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});");
@@ -905,7 +909,13 @@
905909 }
906910
907911 @Test(groups = "unit")
908- @CassandraVersion(major = 2.1)
912+ public void should_not_attempt_to_serialize_bind_markers_in_collections() {
913+ BuiltStatement query = insertInto("foo").value("v", Lists.newArrayList(1, 2, bindMarker()));
914+ assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,?]);");
915+ assertThat(query.getValues(ProtocolVersion.NEWEST_SUPPORTED, CodecRegistry.DEFAULT_INSTANCE)).isNullOrEmpty();
916+ }
917+
918+ @Test(groups = "unit")
909919 public void should_not_attempt_to_serialize_raw_values_in_collections() {
910920 BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x")));
911921 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});");
@@ -913,7 +923,6 @@
913923 }
914924
915925 @Test(groups = "unit")
916- @CassandraVersion(major = 2.1)
917926 public void should_not_attempt_to_serialize_collections_containing_numbers() {
918927 BuiltStatement query;
919928 // lists
@@ -933,4 +942,25 @@
933942 assertThat(query.hasValues()).isFalse();
934943 }
935944
945+ @Test(groups = "unit")
946+ public void should_include_original_cause_when_arguments_invalid() {
947+ // Collection elements in protocol v2 must be at most 65535 bytes
948+ ByteBuffer bb = ByteBuffer.allocate(65536); // too big
949+ List<ByteBuffer> value = Lists.newArrayList(bb);
950+
951+ BuiltStatement s = insertInto("foo").value("l", value);
952+ try {
953+ s.getValues(ProtocolVersion.V2, CodecRegistry.DEFAULT_INSTANCE);
954+ fail("Expected an IllegalArgumentException");
955+ } catch (InvalidTypeException e) {
956+ assertThat(e.getCause()).isInstanceOf(IllegalArgumentException.class);
957+ StringWriter writer = new StringWriter();
958+ e.getCause().printStackTrace(new PrintWriter(writer));
959+ String stackTrace = writer.toString();
960+ assertThat(stackTrace).contains(
961+ "Native protocol version 2 supports only elements with size up to 65535 bytes - " +
962+ "but element size is 65536 bytes");
963+ }
964+ }
965+
936966 }