From b27421ddd0e71efe9eaa119f2781419f9790dce5 Mon Sep 17 00:00:00 2001 From: Gunnar Morling Date: Tue, 26 Jun 2018 12:03:53 +0200 Subject: [PATCH] DBZ-759 Testing via SchemaUtil; * Making RecordWriter private * Removing quotes around string representation of byte arrays --- .../java/io/debezium/data/SchemaUtil.java | 27 +++++----- .../io/debezium/data/RecordWriterTest.java | 42 --------------- .../java/io/debezium/data/SchemaUtilTest.java | 51 +++++++++++++++++++ 3 files changed, 64 insertions(+), 56 deletions(-) delete mode 100644 debezium-core/src/test/java/io/debezium/data/RecordWriterTest.java create mode 100644 debezium-core/src/test/java/io/debezium/data/SchemaUtilTest.java diff --git a/debezium-core/src/main/java/io/debezium/data/SchemaUtil.java b/debezium-core/src/main/java/io/debezium/data/SchemaUtil.java index 3829eda26..3952063b3 100644 --- a/debezium-core/src/main/java/io/debezium/data/SchemaUtil.java +++ b/debezium-core/src/main/java/io/debezium/data/SchemaUtil.java @@ -21,17 +21,17 @@ /** * Utilities for obtaining JSON string representations of {@link Schema}, {@link Struct}, and {@link Field} objects. - * + * * @author Randall Hauch */ public class SchemaUtil { - + private SchemaUtil() { } /** * Obtain a JSON string representation of the specified field. - * + * * @param field the field; may not be null * @return the JSON string representation */ @@ -41,7 +41,7 @@ public static String asString(Object field) { /** * Obtain a JSON string representation of the specified field. - * + * * @param field the field; may not be null * @return the JSON string representation */ @@ -51,7 +51,7 @@ public static String asString(Field field) { /** * Obtain a JSON string representation of the specified {@link Struct}. - * + * * @param struct the {@link Struct}; may not be null * @return the JSON string representation */ @@ -61,7 +61,7 @@ public static String asString(Struct struct) { /** * Obtain a JSON string representation of the specified {@link Schema}. - * + * * @param schema the {@link Schema}; may not be null * @return the JSON string representation */ @@ -71,7 +71,7 @@ public static String asString(Schema schema) { /** * Obtain a JSON string representation of the specified {@link SourceRecord}. - * + * * @param record the {@link SourceRecord}; may not be null * @return the JSON string representation */ @@ -81,7 +81,7 @@ public static String asString(SourceRecord record) { /** * Obtain a JSON string representation of the specified field. - * + * * @param field the field; may not be null * @return the JSON string representation */ @@ -91,7 +91,7 @@ public static String asDetailedString(Field field) { /** * Obtain a JSON string representation of the specified {@link Struct}. - * + * * @param struct the {@link Struct}; may not be null * @return the JSON string representation */ @@ -101,7 +101,7 @@ public static String asDetailedString(Struct struct) { /** * Obtain a JSON string representation of the specified {@link Schema}. - * + * * @param schema the {@link Schema}; may not be null * @return the JSON string representation */ @@ -111,7 +111,7 @@ public static String asDetailedString(Schema schema) { /** * Obtain a JSON string representation of the specified {@link SourceRecord}. - * + * * @param record the {@link SourceRecord}; may not be null * @return the JSON string representation */ @@ -119,7 +119,7 @@ public static String asDetailedString(SourceRecord record) { return new RecordWriter().detailed(true).append(record).toString(); } - public static class RecordWriter { + private static class RecordWriter { private final StringBuilder sb = new StringBuilder(); private boolean detailed = false; @@ -257,8 +257,7 @@ protected void append(ByteBuffer b) { } protected void append(byte[] b) { - String arrayString = Arrays.toString(b); - sb.append('"').append(arrayString).append('"'); + sb.append(Arrays.toString(b)); } protected void appendFirst(String name, Object value) { diff --git a/debezium-core/src/test/java/io/debezium/data/RecordWriterTest.java b/debezium-core/src/test/java/io/debezium/data/RecordWriterTest.java deleted file mode 100644 index 9a2843af3..000000000 --- a/debezium-core/src/test/java/io/debezium/data/RecordWriterTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright Debezium Authors. - * - * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 - */ -package io.debezium.data; - -import io.debezium.data.SchemaUtil.RecordWriter; -import io.debezium.doc.FixFor; - -import java.nio.ByteBuffer; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -/** - * Test for {@link SchemaUtil.RecordWriter}. - * - * @author Andreas Bergmeier - * - */ -public class RecordWriterTest { - - @Test - @FixFor("DBZ-759") - public void correctlySerializesByteArray() { - final RecordWriter recordWriter = new RecordWriter(); - recordWriter.append(new byte[]{1, 3, 5, 7}); - assertThat(recordWriter.toString()).isEqualTo("\"[1, 3, 5, 7]\""); - } - - @Test - @FixFor("DBZ-759") - public void correctlySerializesByteBuffer() { - final RecordWriter recordWriter = new RecordWriter(); - final ByteBuffer buffer = ByteBuffer.allocate(3); - buffer.put(new byte[]{11, 13, 17}); - recordWriter.append(buffer); - assertThat(recordWriter.toString()).isEqualTo("\"[11, 13, 17]\""); - } -} diff --git a/debezium-core/src/test/java/io/debezium/data/SchemaUtilTest.java b/debezium-core/src/test/java/io/debezium/data/SchemaUtilTest.java new file mode 100644 index 000000000..a937cef37 --- /dev/null +++ b/debezium-core/src/test/java/io/debezium/data/SchemaUtilTest.java @@ -0,0 +1,51 @@ +/* + * Copyright Debezium Authors. + * + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.debezium.data; + +import static org.fest.assertions.Assertions.assertThat; + +import java.nio.ByteBuffer; + +import org.apache.kafka.connect.data.Schema; +import org.apache.kafka.connect.data.SchemaBuilder; +import org.apache.kafka.connect.data.Struct; +import org.junit.Test; + +import io.debezium.doc.FixFor; + +/** + * Test for {@link SchemaUtil}. + * + * @author Andreas Bergmeier + * + */ +public class SchemaUtilTest { + + @Test + @FixFor("DBZ-759") + public void correctlySerializesByteArray() { + assertThat(SchemaUtil.asString(new byte[]{1, 3, 5, 7})).isEqualTo("[1, 3, 5, 7]"); + } + + @Test + @FixFor("DBZ-759") + public void correctlySerializesByteBuffer() { + final ByteBuffer buffer = ByteBuffer.allocate(3); + buffer.put(new byte[]{11, 13, 17}); + assertThat(SchemaUtil.asString(buffer)).isEqualTo("[11, 13, 17]"); + } + + @Test + @FixFor("DBZ-759") + public void correctlySerializesStructWithByteArray() { + Schema schema = SchemaBuilder.struct() + .field("some_field", SchemaBuilder.bytes().build()) + .build(); + + Struct struct = new Struct(schema).put("some_field", new byte[]{1, 3, 5, 7}); + assertThat(SchemaUtil.asString(struct)).isEqualTo("{\"some_field\" : [1, 3, 5, 7]}"); + } +}