10000 JAVA-977: Preserve original cause when BuiltStatement value can't be … · kecmu/java-driver@3cfe204 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3cfe204

Browse files
committed
JAVA-977: Preserve original cause when BuiltStatement value can't be serialized.
1 parent da7d95b commit 3cfe204

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

changelog/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ static ByteBuffer[] convert(List<Object> values, ProtocolVersion protocolVersion
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;

driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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 @@ public void selectTest() throws Exception {
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 @@ public void insertTest() throws Exception {
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 @@ public void updateTest() throws Exception {
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 @@ public void deleteTest() throws Exception {
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 @@ public void should_return_object_at_ith_index() {
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,31 +916,27 @@ public void should_serialize_collections_of_serializable_elements() {
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)});");
919922
assertThat(query.getValues(ProtocolVersion.V3)).isNullOrEmpty();
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,?]);");
927929
assertThat(query.getValues(ProtocolVersion.V3)).isNullOrEmpty();
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});");
935936
assertThat(query.getValues(ProtocolVersion.V3)).isNullOrEmpty();
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 @@ public void should_not_attempt_to_serialize_collections_containing_numbers() {
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
}

0 commit comments

Comments
 (0)
0