You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/08/29 14:34:31 UTC

[GitHub] [incubator-seatunnel] laglangyue commented on a diff in pull request #2545: [Bug][ConsoleSinkV2]fix fieldToString StackOverflow and add Unit-Test

laglangyue commented on code in PR #2545:
URL: https://github.com/apache/incubator-seatunnel/pull/2545#discussion_r957421805


##########
seatunnel-connectors-v2/connector-console/src/test/java/org/apache/seatunnel/connectors/seatunnel/console/sink/ConsoleSinkWriterIT.java:
##########
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.connectors.seatunnel.console.sink;
+
+import org.apache.seatunnel.api.table.type.ArrayType;
+import org.apache.seatunnel.api.table.type.BasicType;
+import org.apache.seatunnel.api.table.type.MapType;
+import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.utils.ReflectionUtils;
+
+import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.commons.lang3.RandomUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Optional;
+
+public class ConsoleSinkWriterIT {
+
+    private ConsoleSinkWriter consoleSinkWriter;
+
+    @BeforeEach
+    void setUp() {
+        String[] fieldNames = {};
+        SeaTunnelDataType<?>[] fieldTypes = {};
+        SeaTunnelRowType seaTunnelRowType = new SeaTunnelRowType(fieldNames, fieldTypes);
+        consoleSinkWriter = new ConsoleSinkWriter(seaTunnelRowType);
+    }
+
+    private Object fieldToStringTest(SeaTunnelDataType<?> dataType, Object value) {
+        Optional<Method> fieldToString = ReflectionUtils.getDeclaredMethod(ConsoleSinkWriter.class, "fieldToString", SeaTunnelDataType.class, Object.class);
+        Method method = fieldToString.orElseThrow(() -> new RuntimeException("method fieldToString not found"));
+        try {
+            return method.invoke(consoleSinkWriter, dataType, value);
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test
+    void arrayIntTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            Integer[] integerArr = {1};
+            Object integerArrString = fieldToStringTest(ArrayType.INT_ARRAY_TYPE, integerArr);
+            Assertions.assertEquals(integerArrString, "[1]");
+            int[] intArr = {1, 2};
+            Object intArrString = fieldToStringTest(ArrayType.INT_ARRAY_TYPE, intArr);
+            Assertions.assertEquals(intArrString, "[1, 2]");
+        });
+    }
+
+    @Test
+    void stringTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            String str = RandomStringUtils.randomAlphanumeric(10);
+            Object obj = fieldToStringTest(BasicType.STRING_TYPE, str);
+            Assertions.assertTrue(obj instanceof String);
+            Assertions.assertEquals(10, ((String) obj).length());
+        });
+    }
+
+    @Test
+    void hashMapTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            HashMap<Object, Object> map = new HashMap<>();
+            map.put("key", "value");
+            MapType<String, String> mapType = new MapType<>(BasicType.STRING_TYPE, BasicType.STRING_TYPE);
+            Object mapString = fieldToStringTest(mapType, map);
+            Assertions.assertNotNull(mapString);
+        });
+    }
+
+    @Test
+    void rowTypeTest() {
+        Assertions.assertDoesNotThrow(() -> {
+            String[] fieldNames = {"c_byte", "c_array", "bytes"};
+            SeaTunnelDataType<?>[] fieldTypes = {BasicType.BYTE_TYPE, ArrayType.BYTE_ARRAY_TYPE, PrimitiveByteArrayType.INSTANCE};
+            SeaTunnelRowType seaTunnelRowType = new SeaTunnelRowType(fieldNames, fieldTypes);
+            byte[] bytes = RandomUtils.nextBytes(10);
+            Object[] rowData = {(byte) 1, bytes, bytes};
+            Object rowString = fieldToStringTest(seaTunnelRowType, rowData);
+            Assertions.assertNotNull(rowString);

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org