Elffuss BraidHub

kikocis/java-driver public

Add GettableData/SettableData tests for JAVA-940.
Changeset2764f2e0c54fb0d64bcf3279c39c16a6f041fb7a4d67e8a8761370d271e4b73c
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-13 23:34 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-10-14 22:37 +02:00
Parentsaecf0027996e

Browse the files at this changeset

3 files changed

added driver-core/src/test/java/com/datastax/driver/core/GettableDataIntegrationTest.java +497 −0
@@ -0,0 +1,497 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import java.math.BigDecimal;
19+import java.math.BigInteger;
20+import java.net.InetAddress;
21+import java.net.UnknownHostException;
22+import java.nio.ByteBuffer;
23+import java.util.Collection;
24+import java.util.Date;
25+import java.util.List;
26+import java.util.Map;
27+import java.util.Set;
28+import java.util.UUID;
29+import java.util.concurrent.atomic.AtomicInteger;
30+
31+import com.google.common.collect.ImmutableMap;
32+import com.google.common.collect.ImmutableSet;
33+import com.google.common.reflect.TypeToken;
34+import org.testng.annotations.BeforeClass;
35+import org.testng.annotations.Test;
36+
37+import static com.google.common.collect.Lists.newArrayList;
38+import static org.assertj.core.api.Assertions.assertThat;
39+
40+import static com.datastax.driver.core.CodecUtils.listOf;
41+import static com.datastax.driver.core.TestUtils.getValue;
42+import static com.datastax.driver.core.TestUtils.setValue;
43+
44+public class GettableDataIntegrationTest extends CCMBridge.PerClassSingleNodeCluster {
45+
46+ boolean is21 = cassandraVersion.compareTo(VersionNumber.parse("2.1.3")) > 0;
47+ boolean is20 = cassandraVersion.compareTo(VersionNumber.parse("2.0")) > 0;
48+
49+ CodecRegistry registry = new CodecRegistry();
50+
51+ // Used for generating unique keys.
52+ AtomicInteger keyCounter = new AtomicInteger(0);
53+
54+ @Override
55+ protected Collection<String> getTableDefinitions() {
56+ // only add tuples / nested collections at > 2.1.3.
57+ return newArrayList("CREATE TABLE codec_mapping (k int PRIMARY KEY, "
58+ + "v int, l list<int>, m map<int,int>" +
59+ (is21 ? ", t tuple<int,int>, s set<frozen<list<int>>>)" : ")"));
60+ }
61+
62+ @Override
63+ protected Cluster.Builder configure(Cluster.Builder builder) {
64+ return super.configure(builder).withCodecRegistry(registry);
65+ }
66+
67+ @BeforeClass(groups = "short")
68+ public void setUpRegistry() {
69+ for(TypeMapping<?> mapping : mappings) {
70+ registry.register(mapping.codec);
71+ }
72+ }
73+
74+ static final ByteBuffer intBuf = ByteBuffer.allocate(4);
75+ static {
76+ intBuf.putInt(1);
77+ intBuf.flip();
78+ }
79+
80+ static InetAddress localhost;
81+ static {
82+ try {
83+ localhost = InetAddress.getLocalHost();
84+ } catch(UnknownHostException e) {
85+ localhost = null;
86+ }
87+ }
88+
89+ // Mappings of Codecs, Data Type to access as (used for determining what get|set method to call), value to set.
90+ private TypeMapping<?>[] mappings = {
91+ // set|getString
92+ new TypeMapping<String>(new IntToStringCodec(), DataType.varchar(), "1"),
93+ // set|getLong
94+ new TypeMapping<Long>(new IntToLongCodec(), DataType.bigint(), 1L),
95+ // set|getBytes
96+ new TypeMapping<ByteBuffer>(new IntToByteBufferCodec(), DataType.blob(), intBuf),
97+ // set|getBool
98+ new TypeMapping<Boolean>(new IntToBooleanCodec(), DataType.cboolean(), true),
99+ // set|getDecimal
100+ new TypeMapping<BigDecimal>(new IntToBigDecimalCodec(), DataType.decimal(), new BigDecimal(1)),
101+ // set|getDouble
102+ new TypeMapping<Double>(new IntToDoubleCodec(), DataType.cdouble(), 1.0d),
103+ // set|getFloat
104+ new TypeMapping<Float>(new IntToFloatCodec(), DataType.cfloat(), 1.0f),
105+ // set|getInet
106+ new TypeMapping<InetAddress>(new IntToInetAddressCodec(), DataType.inet(), localhost),
107+ // set|getTime
108+ new TypeMapping<Long>(new IntToLongCodec(), DataType.time(), 8675309L),
109+ // set|getByte
110+ new TypeMapping<Byte>(new IntToByteCodec(), DataType.tinyint(), (byte)0xCF),
111+ // set|getShort
112+ new TypeMapping<Short>(new IntToShortCodec(), DataType.smallint(), (short)1003),
113+ // set|getTimestamp
114+ new TypeMapping<Date>(new IntToDateCodec(), DataType.timestamp(), new Date(124677)),
115+ // set|getDate
116+ new TypeMapping<LocalDate>(new IntToLocalDateCodec(), DataType.date(), LocalDate.fromDaysSinceEpoch(1523)),
117+ // set|getUUID
118+ new TypeMapping<UUID>(new IntToUUIDCodec(), DataType.uuid(), new UUID(244242, 0)),
119+ // set|getVarint
120+ new TypeMapping<BigInteger>(new IntToBigIntegerCodec(), DataType.varint(), BigInteger.valueOf(4566432L))
121+ };
122+
123+ /**
124+ * Validates that all {@link GettableData} types will allow their get methods to be invoked on a column that does
125+ * not match data's cql type if a codec is registered that maps the java type of the getter method to the cql type
126+ * of the column.
127+ * <p/>
128+ * Also validates that all {@link SettableData} types will allow their set methods to be invoked on a column that
129+ * does not match data's cql type if a codec is registered that maps the java type of the setter method to the cql
130+ * type of the column.
131+ * <p/>
132+ * Executes the following for each set|get set:
133+ *
134+ * <ol>
135+ * <li>Insert row using a prepared statement and binding by name.</li>
136+ * <li>Insert row using a prepared statement and binding by index.</li>
137+ * <li>Insert row using a prepared statement and binding everything at once.</li>
138+ * <li>Retrieve inserted rows and get values by name.</li>
139+ * <li>Retrieve inserted rows and get values by index.</li>
140+ * </ol>
141+ *
142+ * @jira_ticket JAVA-940
143+ * @test_category queries
144+ */
145+ @Test(groups = "short")
146+ public void should_allow_getting_and_setting_by_type_if_codec_registered() {
147+ String insertStmt = "INSERT INTO codec_mapping (k,v,l,m" + (is21 ? ",t,s" : "") + ") values (?,?,?,?" + (is21 ? ",?,?)" : ")");
148+ PreparedStatement insert = session.prepare(insertStmt);
149+ PreparedStatement select = session.prepare("SELECT v,l,m" + (is21 ? ",t,s" : "") + " from codec_mapping where k=?");
150+
151+ TupleType tupleType = new TupleType(newArrayList(DataType.cint(), DataType.cint()),
152+ cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), registry);
153+
154+ for(TypeMapping<?> mapping : mappings) {
155+ // Keys used to insert data in this iteration.
156+ List<Integer> keys = newArrayList();
157+
158+ // Values to store.
159+ Map<Object,Object> map = ImmutableMap.of(mapping.value, mapping.value);
160+ List<Object> list = newArrayList(mapping.value);
161+ Set<List<Object>> set = ImmutableSet.of(list);
162+ TupleValue tupleValue = new TupleValue(tupleType);
163+ setValue(tupleValue, 0, mapping.outerType, mapping.value);
164+ setValue(tupleValue, 1, mapping.outerType, mapping.value);
165+
166+ // Insert by name.
167+ BoundStatement byName = insert.bind();
168+ int byNameKey = keyCounter.incrementAndGet();
169+ keys.add(byNameKey);
170+ byName.setInt("k", byNameKey);
171+ setValue(byName, "v", mapping.outerType, mapping.value);
172+ byName.setList("l", list, mapping.javaType);
173+ byName.setMap("m", map, mapping.javaType, mapping.javaType);
174+ if(is21) {
175+ byName.setTupleValue("t", tupleValue);
176+ byName.setSet("s", set, listOf(mapping.javaType));
177+ }
178+ session.execute(byName);
179+
180+ // Insert by index.
181+ BoundStatement byIndex = insert.bind();
182+ int byIndexKey = keyCounter.incrementAndGet();
183+ keys.add(byIndexKey);
184+ byIndex.setInt(0, byIndexKey);
185+ setValue(byIndex, 1, mapping.outerType, mapping.value);
186+ byIndex.setList(2, list, mapping.javaType);
187+ byIndex.setMap(3, map, mapping.javaType, mapping.javaType);
188+ if(is21) {
189+ byIndex.setTupleValue(4, tupleValue);
190+ byIndex.setSet(5, set, listOf(mapping.javaType));
191+ }
192+ session.execute(byIndex);
193+
194+ // Insert by binding all at once.
195+ BoundStatement fullBind;
196+ int fullBindKey = keyCounter.incrementAndGet();
197+ keys.add(fullBindKey);
198+ if(is21) {
199+ fullBind = insert.bind(fullBindKey, mapping.value, list, map, tupleValue, set);
200+ } else {
201+ fullBind = insert.bind(fullBindKey, mapping.value, list, map);
202+ }
203+ session.execute(fullBind);
204+
205+ for(int key : keys) {
206+ // Retrieve by name.
207+ Row row = session.execute(select.bind(key)).one();
208+ assertThat(getValue(row, "v", mapping.outerType, registry)).isEqualTo(mapping.value);
209+ assertThat(row.getList("l", mapping.codec.getJavaType())).isEqualTo(list);
210+ assertThat(row.getMap("m", mapping.codec.getJavaType(), mapping.codec.getJavaType())).isEqualTo(map);
211+
212+ if(is21) {
213+ TupleValue returnedTuple = row.getTupleValue("t");
214+ assertThat(getValue(returnedTuple, 0, mapping.outerType, registry)).isEqualTo(mapping.value);
215+ assertThat(getValue(returnedTuple, 1, mapping.outerType, registry)).isEqualTo(mapping.value);
216+
217+ assertThat(row.getSet("s", listOf(mapping.javaType))).isEqualTo(set);
218+ }
219+
220+ // Retrieve by index.
221+ assertThat(getValue(row, 0, mapping.outerType, registry)).isEqualTo(mapping.value);
222+ assertThat(row.getList(1, mapping.codec.getJavaType())).isEqualTo(list);
223+ assertThat(row.getMap(2, mapping.codec.getJavaType(), mapping.codec.getJavaType())).isEqualTo(map);
224+
225+ if(is21) {
226+ TupleValue returnedTuple = row.getTupleValue(3);
227+ assertThat(getValue(returnedTuple, 0, mapping.outerType, registry)).isEqualTo(mapping.value);
228+ assertThat(getValue(returnedTuple, 1, mapping.outerType, registry)).isEqualTo(mapping.value);
229+
230+ assertThat(row.getSet(4, listOf(mapping.javaType))).isEqualTo(set);
231+ }
232+ }
233+ }
234+ }
235+
236+ private static class TypeMapping<T> {
237+ final TypeCodec<T> codec;
238+ final TypeToken<Object> javaType;
239+ final DataType outerType;
240+ final T value;
241+
242+ @SuppressWarnings("unchecked")
243+ TypeMapping(TypeCodec<T> codec, DataType outerType, T value) {
244+ this.codec = codec;
245+ this.javaType = (TypeToken<Object>)codec.getJavaType();
246+ this.outerType = outerType;
247+ this.value = value;
248+ }
249+ }
250+
251+ // Int <-> Type mappings.
252+ private static class IntToLongCodec extends MappingCodec<Long, Integer> {
253+
254+ IntToLongCodec() {
255+ super(TypeCodec.cint(), Long.class);
256+ }
257+
258+ @Override
259+ protected Long deserialize(Integer value) {
260+ return value.longValue();
261+ }
262+
263+ @Override
264+ protected Integer serialize(Long value) {
265+ return value.intValue();
266+ }
267+ }
268+
269+ private static class IntToStringCodec extends MappingCodec<String, Integer> {
270+
271+ IntToStringCodec() {
272+ super(TypeCodec.cint(), String.class);
273+ }
274+
275+ @Override
276+ protected String deserialize(Integer value) {
277+ return value.toString();
278+ }
279+
280+ @Override
281+ protected Integer serialize(String value) {
282+ return Integer.parseInt(value);
283+ }
284+ }
285+
286+ private static class IntToByteBufferCodec extends MappingCodec<ByteBuffer, Integer> {
287+
288+ IntToByteBufferCodec() {
289+ super(TypeCodec.cint(), ByteBuffer.class);
290+ }
291+
292+ @Override
293+ protected ByteBuffer deserialize(Integer value) {
294+ ByteBuffer buf = ByteBuffer.allocate(4);
295+ buf.putInt(value);
296+ buf.flip();
297+ return buf;
298+ }
299+
300+ @Override
301+ protected Integer serialize(ByteBuffer value) {
302+ return value.duplicate().getInt();
303+ }
304+ }
305+
306+ private static class IntToBooleanCodec extends MappingCodec<Boolean, Integer> {
307+
308+ IntToBooleanCodec() {
309+ super(TypeCodec.cint(), Boolean.class);
310+ }
311+
312+ @Override
313+ protected Boolean deserialize(Integer value) {
314+ return value != 0;
315+ }
316+
317+ @Override
318+ protected Integer serialize(Boolean value) {
319+ return value ? 1 : 0;
320+ }
321+ }
322+
323+ private static class IntToBigDecimalCodec extends MappingCodec<BigDecimal, Integer> {
324+
325+ IntToBigDecimalCodec() {
326+ super(TypeCodec.cint(), BigDecimal.class);
327+ }
328+
329+ @Override
330+ protected BigDecimal deserialize(Integer value) {
331+ return new BigDecimal(value);
332+ }
333+
334+ @Override
335+ protected Integer serialize(BigDecimal value) {
336+ return value.intValue();
337+ }
338+ }
339+
340+ private static class IntToDoubleCodec extends MappingCodec<Double, Integer> {
341+
342+ IntToDoubleCodec() {
343+ super(TypeCodec.cint(), Double.class);
344+ }
345+
346+ @Override
347+ protected Double deserialize(Integer value) {
348+ return value.doubleValue();
349+ }
350+
351+ @Override
352+ protected Integer serialize(Double value) {
353+ return value.intValue();
354+ }
355+ }
356+
357+ private static class IntToFloatCodec extends MappingCodec<Float, Integer> {
358+
359+ IntToFloatCodec() {
360+ super(TypeCodec.cint(), Float.class);
361+ }
362+
363+ @Override
364+ protected Float deserialize(Integer value) {
365+ return value.floatValue();
366+ }
367+
368+ @Override
369+ protected Integer serialize(Float value) {
370+ return value.intValue();
371+ }
372+ }
373+
374+ private static class IntToInetAddressCodec extends MappingCodec<InetAddress, Integer> {
375+
376+ IntToInetAddressCodec() {
377+ super(TypeCodec.cint(), InetAddress.class);
378+ }
379+
380+ @Override
381+ protected InetAddress deserialize(Integer value) {
382+ byte[] address = ByteBuffer.allocate(4).putInt(value).array();
383+ try {
384+ return InetAddress.getByAddress(address);
385+ } catch(UnknownHostException e) {
386+ return null;
387+ }
388+ }
389+
390+ @Override
391+ protected Integer serialize(InetAddress value) {
392+ return ByteBuffer.wrap(value.getAddress()).getInt();
393+ }
394+ }
395+
396+ private static class IntToByteCodec extends MappingCodec<Byte, Integer> {
397+
398+ IntToByteCodec() {
399+ super(TypeCodec.cint(), Byte.class);
400+ }
401+
402+ @Override
403+ protected Byte deserialize(Integer value) {
404+ return value.byteValue();
405+ }
406+
407+ @Override
408+ protected Integer serialize(Byte value) {
409+ return value.intValue();
410+ }
411+ }
412+
413+ private static class IntToShortCodec extends MappingCodec<Short, Integer> {
414+
415+ IntToShortCodec() {
416+ super(TypeCodec.cint(), Short.class);
417+ }
418+
419+ @Override
420+ protected Short deserialize(Integer value) {
421+ return value.shortValue();
422+ }
423+
424+ @Override
425+ protected Integer serialize(Short value) {
426+ return value.intValue();
427+ }
428+ }
429+
430+ private static class IntToDateCodec extends MappingCodec<Date, Integer> {
431+
432+ IntToDateCodec() {
433+ super(TypeCodec.cint(), Date.class);
434+ }
435+
436+ @Override
437+ protected Date deserialize(Integer value) {
438+ return new Date(value);
439+ }
440+
441+ @Override
442+ protected Integer serialize(Date value) {
443+ return new Long(value.getTime()).intValue();
444+ }
445+ }
446+
447+ private static class IntToLocalDateCodec extends MappingCodec<LocalDate, Integer> {
448+
449+ IntToLocalDateCodec() {
450+ super(TypeCodec.cint(), LocalDate.class);
451+ }
452+
453+ @Override
454+ protected LocalDate deserialize(Integer value) {
455+ return LocalDate.fromDaysSinceEpoch(value);
456+ }
457+
458+ @Override
459+ protected Integer serialize(LocalDate value) {
460+ return value.getDaysSinceEpoch();
461+ }
462+ }
463+
464+ private static class IntToUUIDCodec extends MappingCodec<UUID, Integer> {
465+
466+ IntToUUIDCodec() {
467+ super(TypeCodec.cint(), UUID.class);
468+ }
469+
470+ @Override
471+ protected UUID deserialize(Integer value) {
472+ return new UUID(value, 0);
473+ }
474+
475+ @Override
476+ protected Integer serialize(UUID value) {
477+ return new Long(value.getMostSignificantBits()).intValue();
478+ }
479+ }
480+
481+ private static class IntToBigIntegerCodec extends MappingCodec<BigInteger, Integer> {
482+
483+ IntToBigIntegerCodec() {
484+ super(TypeCodec.cint(), BigInteger.class);
485+ }
486+
487+ @Override
488+ protected BigInteger deserialize(Integer value) {
489+ return BigInteger.valueOf((long)value);
490+ }
491+
492+ @Override
493+ protected Integer serialize(BigInteger value) {
494+ return value.intValue();
495+ }
496+ }
497+}
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +17 −9
@@ -34,7 +34,7 @@
3434 import static com.datastax.driver.core.TestUtils.getFixedValue;
3535 import static com.datastax.driver.core.TestUtils.getFixedValue2;
3636 import static com.datastax.driver.core.TestUtils.getValue;
37-import static com.datastax.driver.core.TestUtils.setBoundValue;
37+import static com.datastax.driver.core.TestUtils.setValue;
3838
3939 /**
4040 * Prepared statement tests.
@@ -131,7 +131,8 @@
131131 String name = "c_" + type;
132132 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name));
133133 BoundStatement bs = ps.bind();
134- session.execute(setBoundValue(bs, name, type, getFixedValue(type)));
134+ setValue(bs, name, type, getFixedValue(type));
135+ session.execute(bs);
135136
136137 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one();
137138 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), getFixedValue(type), "For type " + type);
@@ -152,7 +153,8 @@
152153 String name = "c_" + type;
153154 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name));
154155 BoundStatement bs = ps.bind();
155- session.execute(setBoundValue(bs, name, type, getFixedValue2(type)));
156+ setValue(bs, name, type, getFixedValue2(type));
157+ session.execute(bs);
156158
157159 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one();
158160 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), getFixedValue2(type), "For type " + type);
@@ -173,7 +175,8 @@
173175 List<Object> value = (List<Object>)getFixedValue(type);
174176 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name));
175177 BoundStatement bs = ps.bind();
176- session.execute(setBoundValue(bs, name, type, value));
178+ setValue(bs, name, type, value);
179+ session.execute(bs);
177180
178181 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one();
179182 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type);
@@ -197,7 +200,8 @@
197200 List<Object> value = (List<Object>)getFixedValue2(type);
198201 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name));
199202 BoundStatement bs = ps.bind();
200- session.execute(setBoundValue(bs, name, type, value));
203+ setValue(bs, name, type, value);
204+ session.execute(bs);
201205
202206 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one();
203207 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type);
@@ -218,7 +222,8 @@
218222 Set<Object> value = (Set<Object>)getFixedValue(type);
219223 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name));
220224 BoundStatement bs = ps.bind();
221- session.execute(setBoundValue(bs, name, type, value));
225+ setValue(bs, name, type, value);
226+ session.execute(bs);
222227
223228 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one();
224229 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type);
@@ -242,7 +247,8 @@
242247 Set<Object> value = (Set<Object>)getFixedValue2(type);
243248 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name));
244249 BoundStatement bs = ps.bind();
245- session.execute(setBoundValue(bs, name, type, value));
250+ setValue(bs, name, type, value);
251+ session.execute(bs);
246252
247253 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one();
248254 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type);
@@ -268,7 +274,8 @@
268274 Map<Object, Object> value = (Map<Object, Object>)getFixedValue(type);
269275 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name));
270276 BoundStatement bs = ps.bind();
271- session.execute(setBoundValue(bs, name, type, value));
277+ setValue(bs, name, type, value);
278+ session.execute(bs);
272279
273280 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one();
274281 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type);
@@ -298,7 +305,8 @@
298305 Map<Object, Object> value = (Map<Object, Object>)getFixedValue2(type);
299306 PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name));
300307 BoundStatement bs = ps.bind();
301- session.execute(setBoundValue(bs, name, type, value));
308+ setValue(bs, name, type, value);
309+ session.execute(bs);
302310
303311 Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one();
304312 assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type);
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +179 −48
@@ -51,136 +51,267 @@
5151
5252 public static final int TEST_BASE_NODE_WAIT = SystemProperties.getInt("com.datastax.driver.TEST_BASE_NODE_WAIT", 60);
5353
54+ public static void setValue(SettableByIndexData<?> data, int i, DataType type, Object value) {
55+ switch (type.getName()) {
56+ case ASCII:
57+ data.setString(i, (String)value);
58+ break;
59+ case BIGINT:
60+ data.setLong(i, (Long)value);
61+ break;
62+ case BLOB:
63+ data.setBytes(i, (ByteBuffer)value);
64+ break;
65+ case BOOLEAN:
66+ data.setBool(i, (Boolean)value);
67+ break;
68+ case COUNTER:
69+ // Just a no-op, we shouldn't handle counters the same way than other types
70+ break;
71+ case DECIMAL:
72+ data.setDecimal(i, (BigDecimal)value);
73+ break;
74+ case DOUBLE:
75+ data.setDouble(i, (Double)value);
76+ break;
77+ case FLOAT:
78+ data.setFloat(i, (Float)value);
79+ break;
80+ case INET:
81+ data.setInet(i, (InetAddress)value);
82+ break;
83+ case TINYINT:
84+ data.setByte(i, (Byte) value);
85+ break;
86+ case SMALLINT:
87+ data.setShort(i, (Short) value);
88+ break;
89+ case INT:
90+ data.setInt(i, (Integer) value);
91+ break;
92+ case TEXT:
93+ data.setString(i, (String) value);
94+ break;
95+ case TIMESTAMP:
96+ data.setTimestamp(i, (Date) value);
97+ break;
98+ case DATE:
99+ data.setDate(i, (LocalDate)value);
100+ break;
101+ case TIME:
102+ data.setTime(i, (Long) value);
103+ break;
104+ case UUID:
105+ data.setUUID(i, (UUID)value);
106+ break;
107+ case VARCHAR:
108+ data.setString(i, (String)value);
109+ break;
110+ case VARINT:
111+ data.setVarint(i, (BigInteger)value);
112+ break;
113+ case TIMEUUID:
114+ data.setUUID(i, (UUID)value);
115+ break;
116+ case LIST:
117+ data.setList(i, (List)value);
118+ break;
119+ case SET:
120+ data.setSet(i, (Set)value);
121+ break;
122+ case MAP:
123+ data.setMap(i, (Map)value);
124+ break;
125+ default:
126+ throw new RuntimeException("Missing handling of " + type);
127+ }
128+ }
129+
54130 @SuppressWarnings({ "unchecked", "rawtypes" })
55- public static BoundStatement setBoundValue(BoundStatement bs, String name, DataType type, Object value) {
131+ public static void setValue(SettableByNameData<?> data, String name, DataType type, Object value) {
56132 switch (type.getName()) {
57133 case ASCII:
58- bs.setString(name, (String)value);
134+ data.setString(name, (String)value);
59135 break;
60136 case BIGINT:
61- bs.setLong(name, (Long)value);
137+ data.setLong(name, (Long)value);
62138 break;
63139 case BLOB:
64- bs.setBytes(name, (ByteBuffer)value);
140+ data.setBytes(name, (ByteBuffer)value);
65141 break;
66142 case BOOLEAN:
67- bs.setBool(name, (Boolean)value);
143+ data.setBool(name, (Boolean)value);
68144 break;
69145 case COUNTER:
70146 // Just a no-op, we shouldn't handle counters the same way than other types
71147 break;
72148 case DECIMAL:
73- bs.setDecimal(name, (BigDecimal)value);
149+ data.setDecimal(name, (BigDecimal)value);
74150 break;
75151 case DOUBLE:
76- bs.setDouble(name, (Double)value);
152+ data.setDouble(name, (Double)value);
77153 break;
78154 case FLOAT:
79- bs.setFloat(name, (Float)value);
155+ data.setFloat(name, (Float)value);
80156 break;
81157 case INET:
82- bs.setInet(name, (InetAddress)value);
158+ data.setInet(name, (InetAddress)value);
83159 break;
84160 case TINYINT:
85- bs.setByte(name, (Byte) value);
161+ data.setByte(name, (Byte) value);
86162 break;
87163 case SMALLINT:
88- bs.setShort(name, (Short) value);
164+ data.setShort(name, (Short) value);
89165 break;
90166 case INT:
91- bs.setInt(name, (Integer) value);
167+ data.setInt(name, (Integer) value);
92168 break;
93169 case TEXT:
94- bs.setString(name, (String) value);
170+ data.setString(name, (String) value);
95171 break;
96172 case TIMESTAMP:
97- bs.setTimestamp(name, (Date) value);
173+ data.setTimestamp(name, (Date) value);
98174 break;
99175 case DATE:
100- bs.setDate(name, (LocalDate)value);
176+ data.setDate(name, (LocalDate)value);
101177 break;
102178 case TIME:
103- bs.setTime(name, (Long) value);
179+ data.setTime(name, (Long) value);
104180 break;
105181 case UUID:
106- bs.setUUID(name, (UUID)value);
182+ data.setUUID(name, (UUID)value);
107183 break;
108184 case VARCHAR:
109- bs.setString(name, (String)value);
185+ data.setString(name, (String)value);
110186 break;
111187 case VARINT:
112- bs.setVarint(name, (BigInteger)value);
188+ data.setVarint(name, (BigInteger)value);
113189 break;
114190 case TIMEUUID:
115- bs.setUUID(name, (UUID)value);
191+ data.setUUID(name, (UUID)value);
116192 break;
117193 case LIST:
118- bs.setList(name, (List)value);
194+ data.setList(name, (List)value);
119195 break;
120196 case SET:
121- bs.setSet(name, (Set)value);
197+ data.setSet(name, (Set)value);
122198 break;
123199 case MAP:
124- bs.setMap(name, (Map)value);
200+ data.setMap(name, (Map)value);
125201 break;
126202 default:
127203 throw new RuntimeException("Missing handling of " + type);
128204 }
129- return bs;
130205 }
131206
132- public static Object getValue(Row row, String name, DataType type, CodecRegistry codecRegistry) {
207+ public static Object getValue(GettableByIndexData data, int i, DataType type, CodecRegistry codecRegistry) {
208+ switch (type.getName()) {
209+ case ASCII:
210+ return data.getString(i);
211+ case BIGINT:
212+ return data.getLong(i);
213+ case BLOB:
214+ return data.getBytes(i);
215+ case BOOLEAN:
216+ return data.getBool(i);
217+ case COUNTER:
218+ return data.getLong(i);
219+ case DECIMAL:
220+ return data.getDecimal(i);
221+ case DOUBLE:
222+ return data.getDouble(i);
223+ case FLOAT:
224+ return data.getFloat(i);
225+ case INET:
226+ return data.getInet(i);
227+ case TINYINT:
228+ return data.getByte(i);
229+ case SMALLINT:
230+ return data.getShort(i);
231+ case INT:
232+ return data.getInt(i);
233+ case TEXT:
234+ return data.getString(i);
235+ case TIMESTAMP:
236+ return data.getTimestamp(i);
237+ case DATE:
238+ return data.getDate(i);
239+ case TIME:
240+ return data.getTime(i);
241+ case UUID:
242+ return data.getUUID(i);
243+ case VARCHAR:
244+ return data.getString(i);
245+ case VARINT:
246+ return data.getVarint(i);
247+ case TIMEUUID:
248+ return data.getUUID(i);
249+ case LIST:
250+ Class<?> listEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType();
251+ return data.getList(i, listEltClass);
252+ case SET:
253+ Class<?> setEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType();
254+ return data.getSet(i, setEltClass);
255+ case MAP:
256+ Class<?> keyClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType();
257+ Class<?> valueClass = codecRegistry.codecFor(type.getTypeArguments().get(1)).getJavaType().getRawType();
258+ return data.getMap(i, keyClass, valueClass);
259+ }
260+ throw new RuntimeException("Missing handling of " + type);
261+ }
262+
263+ public static Object getValue(GettableByNameData data, String name, DataType type, CodecRegistry codecRegistry) {
133264 switch (type.getName()) {
134265 case ASCII:
135- return row.getString(name);
266+ return data.getString(name);
136267 case BIGINT:
137- return row.getLong(name);
268+ return data.getLong(name);
138269 case BLOB:
139- return row.getBytes(name);
270+ return data.getBytes(name);
140271 case BOOLEAN:
141- return row.getBool(name);
272+ return data.getBool(name);
142273 case COUNTER:
143- return row.getLong(name);
274+ return data.getLong(name);
144275 case DECIMAL:
145- return row.getDecimal(name);
276+ return data.getDecimal(name);
146277 case DOUBLE:
147- return row.getDouble(name);
278+ return data.getDouble(name);
148279 case FLOAT:
149- return row.getFloat(name);
280+ return data.getFloat(name);
150281 case INET:
151- return row.getInet(name);
282+ return data.getInet(name);
152283 case TINYINT:
153- return row.getByte(name);
284+ return data.getByte(name);
154285 case SMALLINT:
155- return row.getShort(name);
286+ return data.getShort(name);
156287 case INT:
157- return row.getInt(name);
288+ return data.getInt(name);
158289 case TEXT:
159- return row.getString(name);
290+ return data.getString(name);
160291 case TIMESTAMP:
161- return row.getTimestamp(name);
292+ return data.getTimestamp(name);
162293 case DATE:
163- return row.getDate(name);
294+ return data.getDate(name);
164295 case TIME:
165- return row.getTime(name);
296+ return data.getTime(name);
166297 case UUID:
167- return row.getUUID(name);
298+ return data.getUUID(name);
168299 case VARCHAR:
169- return row.getString(name);
300+ return data.getString(name);
170301 case VARINT:
171- return row.getVarint(name);
302+ return data.getVarint(name);
172303 case TIMEUUID:
173- return row.getUUID(name);
304+ return data.getUUID(name);
174305 case LIST:
175306 Class<?> listEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType();
176- return row.getList(name, listEltClass);
307+ return data.getList(name, listEltClass);
177308 case SET:
178309 Class<?> setEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType();
179- return row.getSet(name, setEltClass);
310+ return data.getSet(name, setEltClass);
180311 case MAP:
181312 Class<?> keyClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType();
182313 Class<?> valueClass = codecRegistry.codecFor(type.getTypeArguments().get(1)).getJavaType().getRawType();
183- return row.getMap(name, keyClass, valueClass);
314+ return data.getMap(name, keyClass, valueClass);
184315 }
185316 throw new RuntimeException("Missing handling of " + type);
186317 }