@Override public String format(Map<K, V> value) { if (value == null) return "NULL"; StringBuilder sb = new StringBuilder(); sb.append("{"); int i = 0; for (Map.Entry<K, V> e : value.entrySet()) { if (i++ != 0) sb.append(","); sb.append(keyCodec.format(e.getKey())); sb.append(":"); sb.append(valueCodec.format(e.getValue())); } sb.append("}"); return sb.toString(); }
private String formatInitCond() { if (stateTypeCodec.accepts(initCond)) { try { return stateTypeCodec.format(initCond); } catch (RuntimeException e) { LOGGER.info("Failed to format INITCOND literal: {}", initCond); } } return initCond.toString(); }
@Override public String format(C value) { if (value == null) return "NULL"; StringBuilder sb = new StringBuilder(); sb.append(getOpeningChar()); int i = 0; for (E v : value) { if (i++ != 0) sb.append(","); sb.append(eltCodec.format(v)); } sb.append(getClosingChar()); return sb.toString(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); TypeCodec<Object> codec = getCodecRegistry().codecFor(definition); sb.append(codec.format(this)); return sb.toString(); } }
@Override public String toString() { StringBuilder sb = new StringBuilder(); TypeCodec<Object> codec = getCodecRegistry().codecFor(type); sb.append(codec.format(this)); return sb.toString(); } }
@SuppressWarnings("unchecked") public TypeCodecAssert<T> cannotFormat(Object value) { try { actual.format((T) value); fail("Should not have been able to format " + value + " with " + actual); } catch (Exception e) { // ok } return this; } }
@Override public String format(NumberBox<T> value) throws InvalidTypeException { return numberCodec.format(value.getNumber()); } }
@Override public String format(O value) throws InvalidTypeException { return value == null ? null : innerCodec.format(serialize(value)); }
@Override protected String formatField(TupleValue value, int index) { DataType elementType = definition.getComponentTypes().get(index); TypeCodec<Object> codec = definition.getCodecRegistry().codecFor(elementType); return codec.format(value.get(index, codec.getJavaType())); }
@Override protected String formatField(UDTValue source, String fieldName) { DataType elementType = definition.getFieldType(fieldName); TypeCodec<Object> codec = definition.getCodecRegistry().codecFor(elementType); return codec.format(source.get(fieldName, codec.getJavaType())); }
@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); }
@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); } }
@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); }
private void assertCodec(String input, Duration expected) { // serialize + deserialize ByteBuffer bytes = TypeCodec.duration().serialize(Duration.from(input), V4); Duration actual = TypeCodec.duration().deserialize(bytes, V4); assertThat(actual).isEqualTo(expected); // format + parse String format = TypeCodec.duration().format(Duration.from(input)); actual = TypeCodec.duration().parse(format); assertThat(actual).isEqualTo(expected); // parse alone actual = TypeCodec.duration().parse(input); assertThat(actual).isEqualTo(expected); } }
@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 = "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))); } }
@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 = "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))); } }