@BeforeMethod(groups = "unit") public void setUpQueryBuilder() throws Exception { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); }
/** * Returns the cluster builder to use for this test. * * <p>The default implementation returns a vanilla builder. * * <p>It's not required to call {@link * com.datastax.driver.core.Cluster.Builder#addContactPointsWithPorts}, it will be done * automatically. * * @return The cluster builder to use for the tests. */ public Cluster.Builder createClusterBuilder() { return Cluster.builder() // use a different codec registry for each cluster instance .withCodecRegistry(new CodecRegistry()); }
@BeforeMethod(groups = "unit") public void initMocks() { CodecRegistry codecRegistry = new CodecRegistry(); cluster = mock(Cluster.class); Configuration configuration = mock(Configuration.class); ProtocolOptions protocolOptions = mock(ProtocolOptions.class); Metadata metadata = mock(Metadata.class); childPolicy = mock(LoadBalancingPolicy.class); when(cluster.getConfiguration()).thenReturn(configuration); when(configuration.getCodecRegistry()).thenReturn(codecRegistry); when(configuration.getProtocolOptions()).thenReturn(protocolOptions); when(protocolOptions.getProtocolVersion()).thenReturn(ProtocolVersion.NEWEST_SUPPORTED); when(cluster.getMetadata()).thenReturn(metadata); when(metadata.getReplicas(Metadata.quote("keyspace"), routingKey)) .thenReturn(Sets.newLinkedHashSet(host1, host2)); when(childPolicy.newQueryPlan("keyspace", statement)) .thenReturn(Sets.newLinkedHashSet(host4, host3, host2, host1).iterator()); when(childPolicy.distance(any(Host.class))).thenReturn(HostDistance.LOCAL); when(host1.isUp()).thenReturn(true); when(host2.isUp()).thenReturn(true); when(host3.isUp()).thenReturn(true); when(host4.isUp()).thenReturn(true); }
@Test(groups = "unit", dataProvider = "value") public void should_find_codec_by_value(Object value, TypeCodec<?> expected) { // given CodecRegistry registry = new CodecRegistry(); // when TypeCodec<?> actual = registry.codecFor(value); // then assertThat(actual).isNotNull().accepts(value).isSameAs(expected); }
@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_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); }
public Cluster.Builder createClusterBuilder() { return Cluster.builder() .withCodecRegistry(new CodecRegistry().register(new IntToStringCodec())); }
@Test(groups = "unit", dataProvider = "cqlAndValue") public void should_find_codec_by_cql_type_and_value( DataType cqlType, Object value, TypeCodec<?> expected) { // given CodecRegistry registry = new CodecRegistry(); // when TypeCodec<?> actual = registry.codecFor(cqlType, value); // then assertThat(actual).isNotNull().accepts(cqlType).accepts(value).isSameAs(expected); }
public Cluster.Builder createClusterBuilder() { return Cluster.builder() .withCodecRegistry( new CodecRegistry().register(new MyIntCodec()) // global User <-> varchar codec ); }
@Test(groups = "unit", dataProvider = "cqlAndJava") public void should_find_codec_by_cql_type_java_type( DataType cqlType, Class<?> javaType, TypeCodec<?> expected) { // given CodecRegistry registry = new CodecRegistry(); // when TypeCodec<?> actual = registry.codecFor(cqlType, javaType); // then assertThat(actual).isNotNull().accepts(cqlType).accepts(javaType).isSameAs(expected); }
@Test(groups = "unit") public void should_create_codec_for_custom_cql_type() { DataType custom = DataType.custom("foo"); assertThat(new CodecRegistry().codecFor(custom)) .isNotNull() .accepts(custom) .accepts(ByteBuffer.class); assertThat(new CodecRegistry().codecFor(custom, ByteBuffer.class)) .isNotNull() .accepts(custom) .accepts(ByteBuffer.class); assertThat(new CodecRegistry().codecFor(custom, ByteBuffer.allocate(0))) .isNotNull() .accepts(custom) .accepts(ByteBuffer.class); }
@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 = "unit") public void should_find_default_codec_if_cql_type_already_registered() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> newCodec = mockCodec(text(), of(StringBuilder.class)); registry.register(newCodec); // when TypeCodec<?> actual = registry.codecFor(text()); // then assertThat(actual) .isNotNull() .isNotSameAs(newCodec) .accepts(text()) .accepts(String.class) .doesNotAccept(StringBuilder.class); }
@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 = "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 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_find_newly_registered_codec_by_cql_type_and_java_type() { // given CodecRegistry registry = new CodecRegistry(); TypeCodec<?> expected = mockCodec(list(text()), listOf(String.class)); registry.register(expected); // when TypeCodec<?> actual = registry.codecFor(list(text()), listOf(String.class)); // then assertThat(actual).isNotNull().isSameAs(expected); }
@Test(groups = "unit") public void should_ignore_codec_colliding_with_already_generated_codec() { MemoryAppender logs = startCapturingLogs(); CodecRegistry registry = new CodecRegistry(); // Force generation of a list token from the default token registry.codecFor(list(cint()), listOf(Integer.class)); TypeCodec<?> newCodec = mockCodec(list(cint()), listOf(Integer.class)); registry.register(newCodec); assertThat(logs.getNext()).contains("Ignoring codec MockCodec"); assertThat(registry.codecFor(list(cint()), listOf(Integer.class))).isNotSameAs(newCodec); stopCapturingLogs(logs); }
@Test(groups = "unit") public void should_create_tuple_codec() { CodecRegistry registry = new CodecRegistry(); TupleType tupleType = TupleType.of(V4, registry, cint(), varchar()); assertThat(registry.codecFor(tupleType)) .isNotNull() .accepts(tupleType) .accepts(TupleValue.class); registry = new CodecRegistry(); tupleType = TupleType.of(V4, registry, cint(), varchar()); assertThat(registry.codecFor(tupleType, TupleValue.class)) .isNotNull() .accepts(tupleType) .accepts(TupleValue.class); registry = new CodecRegistry(); tupleType = TupleType.of(V4, registry, cint(), varchar()); assertThat(registry.codecFor(new TupleValue(tupleType))) .isNotNull() .accepts(tupleType) .accepts(TupleValue.class); assertThat(registry.codecFor(tupleType, new TupleValue(tupleType))) .isNotNull() .accepts(tupleType) .accepts(TupleValue.class); }
@Override public Cluster.Builder createClusterBuilder() { return Cluster.builder() .withCodecRegistry( new CodecRegistry() .register( new NumberBoxCodec<Integer>(TypeCodec.cint()), new NumberBoxCodec<Long>(TypeCodec.bigint()), new NumberBoxCodec<Float>(TypeCodec.cfloat()), new NumberBoxCodec<Double>(TypeCodec.cdouble()), new NumberBoxCodec<BigInteger>(TypeCodec.varint()), new NumberBoxCodec<BigDecimal>(TypeCodec.decimal()))); }