Elffuss BraidHub

kikocis/java-driver public

Merge pull request #395 from datastax/java765

JAVA-765: Provide API to retrieve values of a Parameterized SimpleStatement.
Changeseta9e072694f53ecff8dba60779e86510b4cab0e51e769aceed5b4dc4803451ed9
AuthorAlexandre Dutra <alexdut@gmail.com> · 2015-08-03 18:04 +02:00
CommitterAlexandre Dutra <alexdut@gmail.com> · 2015-08-03 18:04 +02:00
Parents743d7237895c, 0e2c5b044515

Browse the files at this changeset

16 files changed

modified changelog/README.md +1 −0
@@ -23,6 +23,7 @@
2323 - [bug] Preserve tracing across retries (JAVA-815)
2424 - [improvement] New RetryDecision.tryNextHost() (JAVA-709)
2525 - [bug] Handle function calls and raw strings as non-idempotent in QueryBuilder (JAVA-733)
26+- [improvement] Provide API to retrieve values of a Parameterized SimpleStatement (JAVA-765)
2627
2728 Merged from 2.0.10_fixes branch:
2829
modified driver-core/src/main/java/com/datastax/driver/core/SimpleStatement.java +21 −7
@@ -24,7 +24,7 @@
2424 public class SimpleStatement extends RegularStatement {
2525
2626 private final String query;
27- private final ByteBuffer[] values;
27+ private final Object[] values;
2828
2929 private volatile ByteBuffer routingKey;
3030 private volatile String keyspace;
@@ -77,16 +77,13 @@
7777 *
7878 * @param query the query string.
7979 * @param values values required for the execution of {@code query}.
80- *
81- * @throws IllegalArgumentException if one of {@code values} is not of a type
82- * corresponding to a CQL3 type, i.e. is not a Class that could be returned
83- * by {@link DataType#asJavaClass}.
80+ * @throws IllegalArgumentException if the number of values is greater than 65535.
8481 */
8582 public SimpleStatement(String query, Object... values) {
8683 if (values.length > 65535)
8784 throw new IllegalArgumentException("Too many values, the maximum allowed is 65535");
8885 this.query = query;
89- this.values = convert(values);
86+ this.values = values;
9087 }
9188
9289 private static ByteBuffer[] convert(Object[] values) {
@@ -117,7 +114,24 @@
117114
118115 @Override
119116 public ByteBuffer[] getValues() {
120- return values;
117+ return values == null ? null : convert(values);
118+ }
119+
120+ /**
121+ * Returns the {@code i}th value as the Java type matching its CQL type.
122+ *
123+ * @param i the index to retrieve.
124+ * @return the value of the {@code i}th value of this statement.
125+ *
126+ * @throws IllegalStateException if this statement does not have values.
127+ * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
128+ */
129+ public Object getObject(int i) {
130+ if (values == null)
131+ throw new IllegalStateException("This statement does not have values");
132+ if (i < 0 || i >= values.length)
133+ throw new ArrayIndexOutOfBoundsException(i);
134+ return values[i];
121135 }
122136
123137 /**
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Assignment.java +6 −7
@@ -15,7 +15,6 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18-import java.nio.ByteBuffer;
1918 import java.util.List;
2019
2120 import static com.datastax.driver.core.querybuilder.Utils.appendName;
@@ -41,7 +40,7 @@
4140 }
4241
4342 @Override
44- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
43+ void appendTo(StringBuilder sb, List<Object> variables) {
4544 appendName(name, sb);
4645 sb.append('=');
4746 appendValue(value, sb, variables);
@@ -75,7 +74,7 @@
7574 }
7675
7776 @Override
78- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
77+ void appendTo(StringBuilder sb, List<Object> variables) {
7978 appendName(name, sb).append('=');
8079 appendName(name, sb).append(isIncr ? "+" : "-");
8180 appendValue(value, sb, variables);
@@ -102,7 +101,7 @@
102101 }
103102
104103 @Override
105- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
104+ void appendTo(StringBuilder sb, List<Object> variables) {
106105 appendName(name, sb).append('=');
107106 appendValue(value, sb, variables);
108107 sb.append('+');
@@ -132,7 +131,7 @@
132131 }
133132
134133 @Override
135- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
134+ void appendTo(StringBuilder sb, List<Object> variables) {
136135 appendName(name, sb).append('[').append(idx).append("]=");
137136 appendValue(value, sb, variables);
138137 }
@@ -166,7 +165,7 @@
166165 }
167166
168167 @Override
169- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
168+ void appendTo(StringBuilder sb, List<Object> variables) {
170169 appendName(name, sb).append('=');
171170 appendName(name, sb).append(isAdd ? "+" : "-");
172171 appendValue(collection, sb, variables);
@@ -195,7 +194,7 @@
195194 }
196195
197196 @Override
198- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
197+ void appendTo(StringBuilder sb, List<Object> variables) {
199198 appendName(name, sb).append('[');
200199 appendValue(key, sb, variables);
201200 sb.append("]=");
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Batch.java +1 −1
@@ -48,7 +48,7 @@
4848 }
4949
5050 @Override
51- StringBuilder buildQueryString(List<ByteBuffer> variables) {
51+ StringBuilder buildQueryString(List<Object> variables) {
5252 StringBuilder builder = new StringBuilder();
5353
5454 builder.append(isCounterOp()
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/BuiltStatement.java +29 −11
@@ -36,7 +36,7 @@
3636
3737 private boolean dirty;
3838 private String cache;
39- private ByteBuffer[] values;
39+ private List<Object> values;
4040
4141 Boolean isCounterOp;
4242 boolean hasNonIdempotentOps;
@@ -64,13 +64,30 @@
6464 return lowercaseId.matcher(ident).matches() ? ident : Metadata.quote(ident);
6565 }
6666
67-
6867 @Override
6968 public String getQueryString() {
7069 maybeRebuildCache();
7170 return cache;
7271 }
7372
73+ /**
74+ * Returns the {@code i}th value as the Java type matching its CQL type.
75+ *
76+ * @param i the index to retrieve.
77+ * @return the value of the {@code i}th value of this statement.
78+ *
79+ * @throws IllegalStateException if this statement does not have values.
80+ * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object.
81+ */
82+ public Object getObject(int i) {
83+ maybeRebuildCache();
84+ if (values == null || values.isEmpty())
85+ throw new IllegalStateException("This statement does not have values");
86+ if (i < 0 || i >= values.size())
87+ throw new ArrayIndexOutOfBoundsException(i);
88+ return values.get(i);
89+ }
90+
7491 private void maybeRebuildCache() {
7592 if (!dirty && cache != null)
7693 return;
@@ -81,14 +98,14 @@
8198 if (hasBindMarkers || forceNoValues) {
8299 sb = buildQueryString(null);
83100 } else {
84- List<ByteBuffer> l = new ArrayList<ByteBuffer>();
85- sb = buildQueryString(l);
101+ values = new ArrayList<Object>();
102+ sb = buildQueryString(values);
86103
87- if (l.size() > 65535)
104+ if (values.size() > 65535)
88105 throw new IllegalArgumentException("Too many values for built statement, the maximum allowed is 65535");
89106
90- if (!l.isEmpty())
91- values = l.toArray(new ByteBuffer[l.size()]);
107+ if (values.isEmpty())
108+ values = null;
92109 }
93110
94111 maybeAddSemicolon(sb);
@@ -111,7 +128,7 @@
111128 return sb;
112129 }
113130
114- abstract StringBuilder buildQueryString(List<ByteBuffer> variables);
131+ abstract StringBuilder buildQueryString(List<Object> variables);
115132
116133 boolean isCounterOp() {
117134 return isCounterOp == null ? false : isCounterOp;
@@ -148,7 +165,8 @@
148165
149166 for (int i = 0; i < partitionKey.size(); i++) {
150167 if (name.equals(partitionKey.get(i).getName()) && Utils.isRawValue(value)) {
151- routingKey[i] = partitionKey.get(i).getType().parse(Utils.toRawString(value));
168+ DataType dt = partitionKey.get(i).getType();
169+ routingKey[i] = dt.serialize(value);
152170 return;
153171 }
154172 }
@@ -176,7 +194,7 @@
176194 @Override
177195 public ByteBuffer[] getValues() {
178196 maybeRebuildCache();
179- return values;
197+ return values == null ? null : Utils.convert(values);
180198 }
181199
182200 @Override
@@ -272,7 +290,7 @@
272290 }
273291
274292 @Override
275- StringBuilder buildQueryString(List<ByteBuffer> values) {
293+ StringBuilder buildQueryString(List<Object> values) {
276294 return statement.buildQueryString(values);
277295 }
278296
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Clause.java +3 −4
@@ -15,7 +15,6 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18-import java.nio.ByteBuffer;
1918 import java.util.List;
2019
2120 public abstract class Clause extends Utils.Appendeable {
@@ -48,7 +47,7 @@
4847 }
4948
5049 @Override
51- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
50+ void appendTo(StringBuilder sb, List<Object> variables) {
5251 Utils.appendName(name, sb).append(op);
5352 Utils.appendValue(value, sb, variables);
5453 }
@@ -79,7 +78,7 @@
7978 }
8079
8180 @Override
82- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
81+ void appendTo(StringBuilder sb, List<Object> variables) {
8382
8483 // We special case the case of just one bind marker because there is little
8584 // reasons to do:
@@ -146,7 +145,7 @@
146145 }
147146
148147 @Override
149- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
148+ void appendTo(StringBuilder sb, List<Object> variables) {
150149 sb.append("(");
151150 for (int i = 0; i < names.size(); i++) {
152151 if (i > 0)
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Delete.java +4 −5
@@ -15,7 +15,6 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18-import java.nio.ByteBuffer;
1918 import java.util.ArrayList;
2019 import java.util.List;
2120
@@ -52,7 +51,7 @@
5251 }
5352
5453 @Override
55- StringBuilder buildQueryString(List<ByteBuffer> variables) {
54+ StringBuilder buildQueryString(List<Object> variables) {
5655 StringBuilder builder = new StringBuilder();
5756
5857 builder.append("DELETE");
@@ -414,7 +413,7 @@
414413 }
415414
416415 @Override
417- void appendTo(StringBuilder sb, List<ByteBuffer> values) {
416+ void appendTo(StringBuilder sb, List<Object> values) {
418417 Utils.appendName(columnName, sb);
419418 }
420419
@@ -426,7 +425,7 @@
426425 @Override
427426 public String toString() {
428427 StringBuilder sb = new StringBuilder();
429- appendTo(sb, new ArrayList<ByteBuffer>());
428+ appendTo(sb, new ArrayList<Object>());
430429 return sb.toString();
431430 }
432431 }
@@ -444,7 +443,7 @@
444443 }
445444
446445 @Override
447- void appendTo(StringBuilder sb, List<ByteBuffer> values) {
446+ void appendTo(StringBuilder sb, List<Object> values) {
448447 super.appendTo(sb, values);
449448 sb.append('[');
450449 Utils.appendFlatValue(key, sb);
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Insert.java +1 −1
@@ -46,7 +46,7 @@
4646 }
4747
4848 @Override
49- StringBuilder buildQueryString(List<ByteBuffer> variables) {
49+ StringBuilder buildQueryString(List<Object> variables) {
5050 StringBuilder builder = new StringBuilder();
5151
5252 builder.append("INSERT INTO ");
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Ordering.java +1 −1
@@ -29,7 +29,7 @@
2929 }
3030
3131 @Override
32- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
32+ void appendTo(StringBuilder sb, List<Object> variables) {
3333 Utils.appendName(name, sb);
3434 sb.append(isDesc ? " DESC" : " ASC");
3535 }
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Select.java +1 −2
@@ -15,7 +15,6 @@
1515 */
1616 package com.datastax.driver.core.querybuilder;
1717
18-import java.nio.ByteBuffer;
1918 import java.util.ArrayList;
2019 import java.util.Arrays;
2120 import java.util.Collections;
@@ -55,7 +54,7 @@
5554 }
5655
5756 @Override
58- StringBuilder buildQueryString(List<ByteBuffer> variables) {
57+ StringBuilder buildQueryString(List<Object> variables) {
5958 StringBuilder builder = new StringBuilder();
6059
6160 builder.append("SELECT ");
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Truncate.java +1 −1
@@ -38,7 +38,7 @@
3838 }
3939
4040 @Override
41- protected StringBuilder buildQueryString(List<ByteBuffer> variables) {
41+ protected StringBuilder buildQueryString(List<Object> variables) {
4242 StringBuilder builder = new StringBuilder();
4343
4444 builder.append("TRUNCATE ");
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Update.java +1 −1
@@ -52,7 +52,7 @@
5252 }
5353
5454 @Override
55- StringBuilder buildQueryString(List<ByteBuffer> variables) {
55+ StringBuilder buildQueryString(List<Object> variables) {
5656 StringBuilder builder = new StringBuilder();
5757
5858 builder.append("UPDATE ");
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Using.java +2 −2
@@ -35,7 +35,7 @@
3535 }
3636
3737 @Override
38- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
38+ void appendTo(StringBuilder sb, List<Object> variables) {
3939 sb.append(optionName).append(' ').append(value);
4040 }
4141
@@ -54,7 +54,7 @@
5454 }
5555
5656 @Override
57- void appendTo(StringBuilder sb, List<ByteBuffer> variables) {
57+ void appendTo(StringBuilder sb, List<Object> variables) {
5858 sb.append(optionName).append(' ').append(marker);
5959 }
6060
modified driver-core/src/main/java/com/datastax/driver/core/querybuilder/Utils.java +49 −58
@@ -25,14 +25,13 @@
2525 import static com.google.common.base.Preconditions.checkNotNull;
2626
2727 import com.datastax.driver.core.DataType;
28-import com.datastax.driver.core.utils.Bytes;
2928
3029 // Static utilities private to the query builder
3130 abstract class Utils {
3231
3332 private static final Pattern cnamePattern = Pattern.compile("\\w+(?:\\[.+\\])?");
3433
35- static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values, List<ByteBuffer> variables) {
34+ static StringBuilder joinAndAppend(StringBuilder sb, String separator, List<? extends Appendeable> values, List<Object> variables) {
3635 for (int i = 0; i < values.size(); i++) {
3736 if (i > 0)
3837 sb.append(separator);
@@ -50,7 +49,7 @@
5049 return sb;
5150 }
5251
53- static StringBuilder joinAndAppendValues(StringBuilder sb, String separator, List<?> values, List<ByteBuffer> variables) {
52+ static StringBuilder joinAndAppendValues(StringBuilder sb, String separator, List<?> values, List<Object> variables) {
5453 for (int i = 0; i < values.size(); i++) {
5554 if (i > 0)
5655 sb.append(separator);
@@ -59,37 +58,42 @@
5958 return sb;
6059 }
6160
62- // Returns null if it's not really serializable (function call, bind markers, ...)
63- static ByteBuffer serializeValue(Object value) {
64- if (value == QueryBuilder.bindMarker() || value instanceof FCall || value instanceof CName)
65- return null;
61+ // Returns false if it's not really serializable (function call, bind markers, ...)
62+ static boolean isSerializable(Object value) {
63+ if (value instanceof BindMarker || value instanceof FCall || value instanceof CName)
64+ return false;
6665
6766 if (value instanceof RawString)
68- return null;
67+ return false;
6968
7069 // We also don't serialize fixed size number types. The reason is that if we do it, we will
7170 // force a particular size (4 bytes for ints, ...) and for the query builder, we don't want
7271 // users to have to bother with that.
7372 if (value instanceof Number && !(value instanceof BigInteger || value instanceof BigDecimal))
74- return null;
75-
76- try {
77- return DataType.serializeValue(value);
78- } catch (IllegalArgumentException e) {
79- return null;
80- }
73+ return false;
74+
75+ return true;
8176 }
8277
83- static StringBuilder appendValue(Object value, StringBuilder sb, List<ByteBuffer> variables) {
84- if (variables == null)
85- return appendValue(value, sb, false);
78+ static ByteBuffer[] convert(List<Object> values) {
79+ ByteBuffer[] serializedValues = new ByteBuffer[values.size()];
80+ for (int i = 0; i < values.size(); i++) {
81+ try {
82+ serializedValues[i] = DataType.serializeValue(values.get(i));
83+ } catch (IllegalArgumentException e) {
84+ // Catch and rethrow to provide a more helpful error message (one that include which value is bad)
85+ throw new IllegalArgumentException(String.format("Value %d of type %s does not correspond to any CQL3 type", i, values.get(i).getClass()));
86+ }
87+ }
88+ return serializedValues;
89+ }
8690
87- ByteBuffer bb = serializeValue(value);
88- if (bb == null)
91+ static StringBuilder appendValue(Object value, StringBuilder sb, List<Object> variables) {
92+ if (variables == null || !isSerializable(value))
8993 return appendValue(value, sb, false);
9094
9195 sb.append('?');
92- variables.add(bb);
96+ variables.add(value);
9397 return sb;
9498 }
9599
@@ -140,13 +144,13 @@
140144 sb.append(value);
141145 return true;
142146 } else if (value instanceof InetAddress) {
143- sb.append('\'').append(((InetAddress)value).getHostAddress()).append('\'');
147+ sb.append(DataType.inet().format(value));
144148 return true;
145149 } else if (value instanceof Date) {
146- sb.append(((Date)value).getTime());
150+ sb.append(DataType.timestamp().format(value));
147151 return true;
148152 } else if (value instanceof ByteBuffer) {
149- sb.append(Bytes.toHexString((ByteBuffer) value));
153+ sb.append(DataType.blob().format(value));
150154 return true;
151155 } else if (value instanceof BindMarker) {
152156 sb.append(value);
@@ -188,23 +192,18 @@
188192 }
189193 }
190194
191- static StringBuilder appendCollection(Object value, StringBuilder sb, List<ByteBuffer> variables) {
192- ByteBuffer bb = variables == null ? null : serializeValue(value);
193- if (bb == null) {
195+ static StringBuilder appendCollection(Object value, StringBuilder sb, List<Object> variables) {
196+ if (variables == null || !isSerializable(value)) {
194197 boolean wasCollection = appendValueIfCollection(value, sb, false);
195198 assert wasCollection;
196199 } else {
197200 sb.append('?');
198- variables.add(bb);
201+ variables.add(value);
199202 }
200203 return sb;
201204 }
202205
203- static StringBuilder appendList(List<?> l, StringBuilder sb) {
204- return appendList(l, sb, false);
205- }
206-
207- private static StringBuilder appendList(List<?> l, StringBuilder sb, boolean rawValue) {
206+ static StringBuilder appendList(List<?> l, StringBuilder sb, boolean rawValue) {
208207 sb.append('[');
209208 for (int i = 0; i < l.size(); i++) {
210209 if (i > 0)
@@ -215,11 +214,7 @@
215214 return sb;
216215 }
217216
218- static StringBuilder appendSet(Set<?> s, StringBuilder sb) {
219- return appendSet(s, sb, false);
220- }
221-
222- private static StringBuilder appendSet(Set<?> s, StringBuilder sb, boolean rawValue) {
217+ static StringBuilder appendSet(Set<?> s, StringBuilder sb, boolean rawValue) {
223218 sb.append('{');
224219 boolean first = true;
225220 for (Object elt : s) {
@@ -230,8 +225,20 @@
230225 return sb;
231226 }
232227
233- static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb) {
234- return appendMap(m, sb, false);
228+ static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb, boolean rawValue) {
229+ sb.append('{');
230+ boolean first = true;
231+ for (Map.Entry<?, ?> entry : m.entrySet()) {
232+ if (first)
233+ first = false;
234+ else
235+ sb.append(',');
236+ appendFlatValue(entry.getKey(), sb, rawValue);
237+ sb.append(':');
238+ appendFlatValue(entry.getValue(), sb, rawValue);
239+ }
240+ sb.append('}');
241+ return sb;
235242 }
236243
237244 static boolean containsBindMarker(Object value) {
@@ -273,24 +280,8 @@
273280 return true;
274281 }
275282
276- private static StringBuilder appendMap(Map<?, ?> m, StringBuilder sb, boolean rawValue) {
277- sb.append('{');
278- boolean first = true;
279- for (Map.Entry<?, ?> entry : m.entrySet()) {
280- if (first)
281- first = false;
282- else
283- sb.append(',');
284- appendFlatValue(entry.getKey(), sb, rawValue);
285- sb.append(':');
286- appendFlatValue(entry.getValue(), sb, rawValue);
287- }
288- sb.append('}');
289- return sb;
290- }
291-
292283 private static StringBuilder appendValueString(String value, StringBuilder sb) {
293- return sb.append('\'').append(replace(value, '\'', "''")).append('\'');
284+ return sb.append(DataType.text().format(value));
294285 }
295286
296287 static boolean isRawValue(Object value) {
@@ -339,7 +330,7 @@
339330 }
340331
341332 static abstract class Appendeable {
342- abstract void appendTo(StringBuilder sb, List<ByteBuffer> values);
333+ abstract void appendTo(StringBuilder sb, List<Object> values);
343334 abstract boolean containsBindMarker();
344335 }
345336
modified driver-core/src/test/java/com/datastax/driver/core/SimpleStatementTest.java +23 −1
@@ -20,10 +20,32 @@
2020
2121 import org.testng.annotations.Test;
2222
23+import static org.assertj.core.api.Assertions.assertThat;
24+
2325 public class SimpleStatementTest {
26+
2427 @Test(groups = "unit", expectedExceptions = { IllegalArgumentException.class })
2528 public void should_fail_if_too_many_variables() {
2629 List<Object> args = Collections.nCopies(1 << 16, (Object)1);
2730 new SimpleStatement("mock query", args.toArray());
2831 }
29-}
No newline at the end of the file
32+
33+ @Test(groups = "unit", expectedExceptions = { IllegalStateException.class })
34+ public void should_throw_ISE_if_getObject_called_on_statement_without_values() {
35+ new SimpleStatement("doesn't matter").getObject(0);
36+ }
37+
38+
39+ @Test(groups = "unit", expectedExceptions = { IndexOutOfBoundsException.class })
40+ public void should_throw_IOOBE_if_getObject_called_with_wrong_index() {
41+ new SimpleStatement("doesn't matter", new Object()).getObject(1);
42+ }
43+
44+ @Test(groups = "unit")
45+ public void should_return_object_at_ith_index() {
46+ Object expected = new Object();
47+ Object actual = new SimpleStatement("doesn't matter", expected).getObject(0);
48+ assertThat(actual).isSameAs(expected);
49+ }
50+
51+}
modified driver-core/src/test/java/com/datastax/driver/core/querybuilder/QueryBuilderTest.java +18 −2
@@ -18,7 +18,6 @@
1818 import java.math.BigDecimal;
1919 import java.math.BigInteger;
2020 import java.net.InetAddress;
21-import java.nio.ByteBuffer;
2221 import java.util.*;
2322
2423 import org.testng.annotations.Test;
@@ -28,7 +27,6 @@
2827 import static org.testng.Assert.assertTrue;
2928 import static org.testng.Assert.fail;
3029
31-import com.datastax.driver.core.Assertions;
3230 import com.datastax.driver.core.ConsistencyLevel;
3331 import com.datastax.driver.core.RegularStatement;
3432 import com.datastax.driver.core.Statement;
@@ -776,4 +774,22 @@
776774 assertThat(select.getQueryString()).doesNotContain("?");
777775 assertThat(select.getValues()).isNull();
778776 }
777+
778+ @Test(groups = "unit", expectedExceptions = { IllegalStateException.class })
779+ public void should_throw_ISE_if_getObject_called_on_statement_without_values() {
780+ select().from("test").where(eq("foo", 42)).getObject(0); // integers are appended to the CQL string
781+ }
782+
783+ @Test(groups = "unit", expectedExceptions = { IndexOutOfBoundsException.class })
784+ public void should_throw_IOOBE_if_getObject_called_with_wrong_index() {
785+ select().from("test").where(eq("foo", new Object())).getObject(1);
786+ }
787+
788+ @Test(groups = "unit")
789+ public void should_return_object_at_ith_index() {
790+ Object expected = new Object();
791+ Object actual = select().from("test").where(eq("foo", expected)).getObject(0);
792+ assertThat(actual).isSameAs(expected);
793+ }
794+
779795 }