Refine search
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());
Configuration configuration = cluster.getConfiguration(); CodecRegistry codecRegistry = configuration.getCodecRegistry(); codecRegistry.register( InstantCodec.instance, LocalDateCodec.instance, Session session = cluster.connect(cstr.keyspace()); hierarchy(session.getClass(), type -> bind.apply(type, cstr.keyspace(), session)); .onFailure(x -> log.error("session.close() resulted in exception", x)); cluster.close();
@SuppressWarnings("unchecked") private <T> TypeCodec<T> findCodec(DataType cqlType, TypeToken<T> javaType) { checkNotNull(cqlType, "Parameter cqlType cannot be null"); if (logger.isTraceEnabled()) logger.trace("Looking for codec [{} <-> {}]", toString(cqlType), toString(javaType)); // Look at the built-in codecs first for (TypeCodec<?> codec : BUILT_IN_CODECS) { if (codec.accepts(cqlType) && (javaType == null || codec.accepts(javaType))) { logger.trace("Built-in codec found: {}", codec); return (TypeCodec<T>) codec; } } // Look at the user-registered codecs next for (TypeCodec<?> codec : codecs) { if (codec.accepts(cqlType) && (javaType == null || codec.accepts(javaType))) { logger.trace("Already registered codec found: {}", codec); return (TypeCodec<T>) codec; } } return createCodec(cqlType, javaType); }
@Test(groups = "unit", dataProvider = "cql") public void should_find_codec_by_cql_type(DataType cqlType, TypeCodec<?> expected) { // given CodecRegistry registry = new CodecRegistry(); // when TypeCodec<?> actual = registry.codecFor(cqlType); // then assertThat(actual).isNotNull().accepts(cqlType).isSameAs(expected); }
@Test(groups = "unit") public void should_find_newly_registered_codec_by_cql_type() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> expected = mockCodec(list(text()), listOf(String.class)); registry.register(expected); // when TypeCodec<?> actual = registry.codecFor(list(text())); // then assertThat(actual).isNotNull().isSameAs(expected); }
@Test(groups = "short") public void should_handle_tuples_with_custom_codecs() { CodecRegistry codecRegistry = new CodecRegistry(); Cluster cluster = register( Cluster.builder() .addContactPoints(getContactPoints()) .withPort(ccm().getBinaryPort()) .withCodecRegistry(codecRegistry) .build()); Session session = cluster.connect(keyspace); setUpTupleTypes(cluster); codecRegistry.register(new LocationCodec(TypeCodec.tuple(locationType))); session.execute(insertQuery, uuid, "John Doe", locationValue); ResultSet rows = session.execute(selectQuery, uuid);
@Test(groups = "unit") public void should_ignore_codec_colliding_with_already_registered_codec() { MemoryAppender logs = startCapturingLogs(); CodecRegistry registry = new CodecRegistry(); TypeCodec<?> newCodec = mockCodec(cint(), of(Integer.class)); registry.register(newCodec); assertThat(logs.getNext()).contains("Ignoring codec MockCodec"); assertThat(registry.codecFor(cint(), Integer.class)).isNotSameAs(newCodec); stopCapturingLogs(logs); }
@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); }
@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") 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", 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 = "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); }
@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); }
@SuppressWarnings("serial") @Test(groups = "unit") public void parseFormatSetTest() { String toParse = "{'Foo','Bar','Foo''bar'}"; Set<String> toFormat = new LinkedHashSet<String>() { { add("Foo"); add("Bar"); add("Foo'bar"); } }; DataType dt = DataType.set(DataType.text()); assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat); assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse); }
@Test(groups = "unit") public void should_deserialize_empty_buffer_as_udt_with_null_values() { CodecRegistry codecRegistry = new CodecRegistry(); UserType udt = new UserType( "ks", "t", false, Arrays.asList( new UserType.Field("t", DataType.text()), new UserType.Field("i", DataType.cint()), new UserType.Field("l", DataType.list(DataType.text()))), ProtocolVersion.NEWEST_SUPPORTED, codecRegistry); UDTValue expected = udt.newValue(); expected.setString("t", null); expected.setToNull("i"); expected.setList("l", null); UDTValue actual = codecRegistry .codecFor(udt, UDTValue.class) .deserialize(ByteBuffer.allocate(0), ProtocolVersion.NEWEST_SUPPORTED); assertThat(actual).isNotNull(); assertThat(actual).isEqualTo(expected); }
@Test(groups = "unit") public void should_create_derived_codecs_for_java_type_handled_by_custom_codec() { TypeCodec<?> newCodec = mockCodec(varchar(), of(StringBuilder.class)); CodecRegistry registry = new CodecRegistry().register(newCodec); // lookup by CQL type only returns default codec assertThat(registry.codecFor(list(varchar()))).doesNotAccept(listOf(StringBuilder.class)); assertThat(registry.codecFor(list(varchar()), listOf(StringBuilder.class))).isNotNull(); }
@Test(groups = "unit") public void formatNativeTest() { for (DataType dt : DataType.allPrimitiveTypes()) { if (exclude(dt)) continue; for (TestValue value : primitiveTestValues(dt)) assertThat(codecRegistry.codecFor(dt).format(value.javaObject)) .as("Formatting a %s expecting %s", dt, value.cqlOutputString) .isEqualTo(value.cqlOutputString); } }