Elffuss BraidHub

kikocis/java-driver public

Merge pull request #619 from datastax/java977

JAVA-977: Preserve original cause when BuiltStatement value can't be serialized.
Changeset6ae8ffe222d712d279f90abbd263abfa517b208d86b089df313f3204f51ff7ca
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-09 22:31 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-03-09 22:31 +01:00
Parents6cbc334fa86f, 34b058666d9d

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -36,6 +36,7 @@
3636 - [bug] JAVA-764: Retry with the normal consistency level (not the serial one) when a write times out on the Paxos phase.
3737 - [bug] JAVA-727: Allow monotonic timestamp generators to drift in the future + use microsecond precision when possible.
3838 - [improvement] JAVA-444: Add Java process information to UUIDs.makeNode() hash.
39+- [improvement] JAVA-977: Preserve original cause when BuiltStatement value can't be serialized.
3940
4041 Merged from 2.0 branch:
4142
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +3 −1
@@ -68,7 +68,9 @@
6868 serializedValues[i] = DataType.serializeValue(values.get(i), protocolVersion);
6969 } catch (IllegalArgumentException e) {
7070 // Catch and rethrow to provide a more helpful error message (one that include which value is bad)
71- throw new IllegalArgumentException(String.format("Value %d of type %s does not correspond to any CQL3 type", i, values.get(i).getClass()));
71+ throw new IllegalArgumentException(
72+ String.format("Value %d of type %s does not correspond to any CQL3 type",
73+ i, values.get(i).getClass()), e);
7274 }
7375 }
7476 return serializedValues;
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +35 −15
@@ -25,15 +25,20 @@
2525 import com.google.common.collect.Sets;
2626 import org.testng.annotations.Test;
2727
28+import java.io.PrintWriter;
29+import java.io.StringWriter;
2830 import java.math.BigDecimal;
2931 import java.math.BigInteger;
3032 import java.net.InetAddress;
33+import java.nio.ByteBuffer;
3134 import java.util.*;
3235
3336 import static com.datastax.driver.core.DataType.cint;
3437 import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
3538 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;
3742
3843 public class QueryBuilderTest {
3944
@@ -140,35 +145,35 @@
140145
141146 try {
142147 select().countAll().from("foo").orderBy(asc("a"), desc("b")).orderBy(asc("a"), desc("b"));
143- fail();
148+ fail("Expected an IllegalStateException");
144149 } catch (IllegalStateException e) {
145150 assertEquals(e.getMessage(), "An ORDER BY clause has already been provided");
146151 }
147152
148153 try {
149154 select().column("a").all().from("foo");
150- fail();
155+ fail("Expected an IllegalStateException");
151156 } catch (IllegalStateException e) {
152157 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
153158 }
154159
155160 try {
156161 select().column("a").countAll().from("foo");
157- fail();
162+ fail("Expected an IllegalStateException");
158163 } catch (IllegalStateException e) {
159164 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
160165 }
161166
162167 try {
163168 select().all().from("foo").limit(-42);
164- fail();
169+ fail("Expected an IllegalArgumentException");
165170 } catch (IllegalArgumentException e) {
166171 assertEquals(e.getMessage(), "Invalid LIMIT value, must be strictly positive");
167172 }
168173
169174 try {
170175 select().all().from("foo").limit(42).limit(42);
171- fail();
176+ fail("Expected an IllegalStateException");
172177 } catch (IllegalStateException e) {
173178 assertEquals(e.getMessage(), "A LIMIT value has already been provided");
174179 }
@@ -244,7 +249,7 @@
244249
245250 try {
246251 insertInto("foo").values(new String[]{"a", "b"}, new Object[]{1, 2, 3});
247- fail();
252+ fail("Expected an IllegalArgumentException");
248253 } catch (IllegalArgumentException e) {
249254 assertEquals(e.getMessage(), "Got 2 names but 3 values");
250255 }
@@ -327,7 +332,7 @@
327332
328333 try {
329334 update("foo").using(ttl(-400));
330- fail();
335+ fail("Expected an IllegalArgumentException");
331336 } catch (IllegalArgumentException e) {
332337 assertEquals(e.getMessage(), "Invalid ttl, must be positive");
333338 }
@@ -381,14 +386,14 @@
381386
382387 try {
383388 delete().column("a").all().from("foo");
384- fail();
389+ fail("Expected an IllegalStateException");
385390 } catch (IllegalStateException e) {
386391 assertEquals(e.getMessage(), "Some columns ([a]) have already been selected.");
387392 }
388393
389394 try {
390395 delete().from("foo").using(timestamp(-1240003134L));
391- fail();
396+ fail("Expected an IllegalArgumentException");
392397 } catch (IllegalArgumentException e) {
393398 assertEquals(e.getMessage(), "Invalid timestamp, must be positive");
394399 }
@@ -895,7 +900,6 @@
895900 }
896901
897902 @Test(groups = "unit")
898- @CassandraVersion(major = 2.1)
899903 public void should_serialize_collections_of_serializable_elements() {
900904 Set<UUID> set = Sets.newHashSet(UUID.randomUUID());
901905 List<Date> list = Lists.newArrayList(new Date());
@@ -912,7 +916,6 @@
912916 }
913917
914918 @Test(groups = "unit")
915- @CassandraVersion(major = 2.1)
916919 public void should_not_attempt_to_serialize_function_calls_in_collections() {
917920 BuiltStatement query = insertInto("foo").value("v", Sets.newHashSet(fcall("func", 1)));
918921 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({func(1)});");
@@ -920,7 +923,6 @@
920923 }
921924
922925 @Test(groups = "unit")
923- @CassandraVersion(major = 2.1)
924926 public void should_not_attempt_to_serialize_bind_markers_in_collections() {
925927 BuiltStatement query = insertInto("foo").value("v", Lists.newArrayList(1, 2, bindMarker()));
926928 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ([1,2,?]);");
@@ -928,7 +930,6 @@
928930 }
929931
930932 @Test(groups = "unit")
931- @CassandraVersion(major = 2.1)
932933 public void should_not_attempt_to_serialize_raw_values_in_collections() {
933934 BuiltStatement query = insertInto("foo").value("v", ImmutableMap.of(1, raw("x")));
934935 assertThat(query.getQueryString()).isEqualTo("INSERT INTO foo (v) VALUES ({1:x});");
@@ -936,7 +937,6 @@
936937 }
937938
938939 @Test(groups = "unit")
939- @CassandraVersion(major = 2.1)
940940 public void should_not_attempt_to_serialize_collections_containing_numbers() {
941941 BuiltStatement query;
942942 // lists
@@ -956,4 +956,24 @@
956956 assertThat(query.hasValues()).isFalse();
957957 }
958958
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+
959979 }