Refine search
@Override public <V> T set(int i, V v, TypeCodec<V> codec) { checkType(i, codec.getCqlType().getName()); return setValue(i, codec.serialize(v, protocolVersion)); }
@Override public <T> T get(int i, TypeCodec<T> codec) { checkType(i, codec.getCqlType().getName()); ByteBuffer value = getValue(i); return codec.deserialize(value, protocolVersion); } }
protected static Object getCassandraObject(Row row, int i, DataType dataType) { if (dataType.equals(DataType.blob())) { return row.getBytes(i); } else if (dataType.equals(DataType.varint()) || dataType.equals(DataType.decimal())) { TypeCodec firstCodec = codecRegistry.codecFor(firstArg); if (dataType.equals(DataType.set(firstArg))) { return row.getSet(i, firstCodec.getJavaType()); } else if (dataType.equals(DataType.list(firstArg))) { return row.getList(i, firstCodec.getJavaType()); } else { TypeCodec secondCodec = codecRegistry.codecFor(secondArg); if (dataType.equals(DataType.map(firstArg, secondArg))) { return row.getMap(i, firstCodec.getJavaType(), secondCodec.getJavaType());
protected AbstractMapCodec(TypeCodec<K> keyCodec, TypeCodec<V> valueCodec) { super( DataType.map(keyCodec.getCqlType(), valueCodec.getCqlType()), TypeTokens.mapOf(keyCodec.getJavaType(), valueCodec.getJavaType())); this.keyCodec = keyCodec; this.valueCodec = valueCodec; }
@Test(groups = "short") public void should_choose_proper_host_based_on_routing_key() { ScassandraCluster.builder().withNodes(3).withSimpleKeyspace("keyspace", 1).build(); Cluster cluster = Cluster.builder() .addContactPoints(sCluster.address(1).getAddress()) .withPort(sCluster.getBinaryPort()) sCluster.init(); Session session = cluster.connect(); TypeCodec.varchar() .serialize( "should_choose_proper_host_based_on_routing_key", ProtocolVersion.NEWEST_SUPPORTED); queryTracker.assertQueried(sCluster, 1, 3, 10); } finally { cluster.close(); sCluster.stop();
@Test(groups = "short") public void should_create_token_from_partition_key() { Metadata metadata = cluster().getMetadata(); Row row = session().execute("SELECT token(i) FROM foo WHERE i = 1").one(); Token expected = row.getToken(0); ProtocolVersion protocolVersion = cluster().getConfiguration().getProtocolOptions().getProtocolVersion(); assertThat(metadata.newToken(TypeCodec.cint().serialize(1, protocolVersion))) .isEqualTo(expected); }
/** Ensures that primitive types are correctly handled and wrapped when necessary. */ @Test(groups = "unit") public void should_wrap_primitive_types() { assertThat(TypeCodec.cboolean()).accepts(Boolean.class).accepts(Boolean.TYPE).accepts(true); assertThat(TypeCodec.cint()).accepts(Integer.class).accepts(Integer.TYPE).accepts(42); assertThat(TypeCodec.bigint()).accepts(Long.class).accepts(Long.TYPE).accepts(42L); assertThat(TypeCodec.cfloat()).accepts(Float.class).accepts(Float.TYPE).accepts(42.0F); assertThat(TypeCodec.cdouble()).accepts(Double.class).accepts(Double.TYPE).accepts(42.0D); }
@Test(groups = "short") public void simpleValueTest() throws Exception { TupleType t = cluster().getMetadata().newTupleType(DataType.cint(), DataType.text(), DataType.cfloat()); TupleValue v = t.newValue(); v.setInt(0, 1); v.setString(1, "a"); v.setFloat(2, 1.0f); assertEquals(v.getType().getComponentTypes().size(), 3); assertEquals(v.getType().getComponentTypes().get(0), DataType.cint()); assertEquals(v.getType().getComponentTypes().get(1), DataType.text()); assertEquals(v.getType().getComponentTypes().get(2), DataType.cfloat()); assertEquals(v.getInt(0), 1); assertEquals(v.getString(1), "a"); assertEquals(v.getFloat(2), 1.0f); assertEquals(TypeCodec.tuple(t).format(v), "(1,'a',1.0)"); }
@Test(groups = "short") @CassandraVersion("2.0.0") public void should_log_all_parameter_types_simple_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().withMaxParameterValueLength(Integer.MAX_VALUE).build(); cluster().register(queryLogger); // when String query = "UPDATE test SET " + assignments + " WHERE pk = 42"; SimpleStatement ss = new SimpleStatement(query, values.toArray()); session().execute(ss); // then String line = normalAppender.waitAndGet(10000); assertThat(line).contains("Query completed normally").contains(ipOfNode(1)).contains(query); CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry(); for (DataType type : dataTypes) { TypeCodec<Object> codec; if (type.equals(DataType.time())) { codec = codecRegistry.codecFor(DataType.bigint()); } else { codec = codecRegistry.codecFor(type); } assertThat(line).contains(codec.format(getFixedValue(type))); } }
@SuppressWarnings("deprecation") @Test(groups = "unit") public void parseFormatTupleTest() { String toParse = "(1,'foo',1.0)"; TupleType t = new TupleType( newArrayList(DataType.cint(), DataType.text(), DataType.cfloat()), protocolVersion, codecRegistry); TupleValue toFormat = t.newValue(1, "foo", 1.0f); assertEquals(codecRegistry.codecFor(t).parse(toParse), toFormat); assertEquals(codecRegistry.codecFor(t).format(toFormat), toParse); }
@SuppressWarnings("serial") @Test(groups = "unit") public void parseFormatUDTTest() { String toParse = "{t:'fo''o',i:3,\"L\":['a','b'],s:{3:{a:0x01}}}"; "t", false, Arrays.asList(new UserType.Field("a", DataType.blob())), protocolVersion, codecRegistry); false, Arrays.asList( new UserType.Field("t", DataType.text()), new UserType.Field("i", DataType.cint()), new UserType.Field("L", DataType.list(DataType.text())), new UserType.Field("s", DataType.map(DataType.cint(), udt1))), assertEquals(codecRegistry.codecFor(udt2).parse(toParse), toFormat); assertEquals(codecRegistry.codecFor(udt2).format(toFormat), toParse);
@Test(groups = "unit") public void parseFormatListTest() { String toParse = "['Foo','Bar','Foo''bar']"; List<String> toFormat = Arrays.asList("Foo", "Bar", "Foo'bar"); DataType dt = DataType.list(DataType.text()); assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat); assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse); }
@SuppressWarnings("serial") @Test(groups = "unit") public void parseFormatMapTest() { String toParse = "{'Foo':3,'Bar':42,'Foo''bar':-24}"; Map<String, Integer> toFormat = new LinkedHashMap<String, Integer>() { { put("Foo", 3); put("Bar", 42); put("Foo'bar", -24); } }; DataType dt = DataType.map(DataType.text(), DataType.cint()); assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat); assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse); }
@Test(groups = "unit") public void test_inheritance() { CodecRegistry codecRegistry = new CodecRegistry(); ACodec aCodec = new ACodec(); codecRegistry.register(aCodec); assertThat(codecRegistry.codecFor(cint(), A.class)).isNotNull().isSameAs(aCodec); try { codecRegistry.codecFor(cint(), B.class); fail(); } catch (CodecNotFoundException e) { TypeCodec<List<A>> expected = TypeCodec.list(aCodec); TypeCodec<List<A>> actual = codecRegistry.codecFor(list(cint()), LIST_OF_A_TOKEN); assertThat(actual.getCqlType()).isEqualTo(expected.getCqlType()); assertThat(actual.getJavaType()).isEqualTo(expected.getJavaType()); TypeCodec<List<B>> expectedB = TypeCodec.list(bCodec); TypeCodec<List<B>> actualB = codecRegistry.codecFor(list(cint()), LIST_OF_B_TOKEN); assertThat(actualB.getCqlType()).isEqualTo(expectedB.getCqlType()); assertThat(actualB.getJavaType()).isEqualTo(expectedB.getJavaType());
@Test( groups = "unit", expectedExceptions = {IllegalArgumentException.class}) public void collectionElementTooLargeTest() throws Exception { DataType cqlType = DataType.list(DataType.text()); List<String> list = newArrayList(Strings.repeat("a", 65536)); TypeCodec<List<?>> codec = codecRegistry.codecFor(cqlType); codec.serialize(list, ProtocolVersion.V2); }
@Test(groups = "short") public void should_log_all_parameter_types_bound_statements() throws Exception { // given normal.setLevel(TRACE); queryLogger = QueryLogger.builder().withMaxParameterValueLength(Integer.MAX_VALUE).build(); cluster().register(queryLogger); // when String query = "UPDATE test SET " + assignments + " WHERE pk = 42"; PreparedStatement ps = session().prepare(query); BoundStatement bs = ps.bind(values.toArray()); session().execute(bs); // then String line = normalAppender.waitAndGet(10000); assertThat(line).contains("Query completed normally").contains(ipOfNode(1)).contains(query); CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry(); for (DataType type : dataTypes) { TypeCodec<Object> codec = codecRegistry.codecFor(type); assertThat(line).contains(codec.format(getFixedValue(type))); } }
@Test( groups = "unit", expectedExceptions = {IllegalArgumentException.class}) public void collectionTooLargeTest() throws Exception { DataType cqlType = DataType.list(DataType.cint()); List<Integer> list = Collections.nCopies(65536, 1); TypeCodec<List<?>> codec = codecRegistry.codecFor(cqlType); codec.serialize(list, ProtocolVersion.V2); }
@Test(groups = "unit") public void should_deserialize_empty_buffer_as_tuple_with_null_values() { CodecRegistry codecRegistry = new CodecRegistry(); TupleType tupleType = new TupleType( newArrayList(DataType.cint(), DataType.varchar(), DataType.cfloat()), ProtocolVersion.NEWEST_SUPPORTED, codecRegistry); TupleValue expected = tupleType.newValue(null, null, null); TupleValue actual = codecRegistry .codecFor(tupleType, TupleValue.class) .deserialize(ByteBuffer.allocate(0), ProtocolVersion.NEWEST_SUPPORTED); assertThat(actual).isNotNull(); assertThat(actual).isEqualTo(expected); }