Repository: alibaba/fescar
Pull request #441 (2019-02-22 05:30:20)

add common module test
add test for these class
1.XID
2.StringUtils





Patch:
diff --git a/common/pom.xml b/common/pom.xml
index e6e7472f7..441306a61 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -47,5 +47,15 @@
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
         </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.testng</groupId>
+            <artifactId>testng</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>


Test patch:
diff --git a/common/src/test/java/com/alibaba/fescar/common/StringUtilsTest.java b/common/src/test/java/com/alibaba/fescar/common/StringUtilsTest.java
new file mode 100644
index 000000000..819095e43
--- /dev/null
+++ b/common/src/test/java/com/alibaba/fescar/common/StringUtilsTest.java
@@ -0,0 +1,72 @@
+/*
+ *  Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ *  Licensed 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 com.alibaba.fescar.common;
+
+import com.alibaba.fescar.common.util.StringUtils;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.SQLException;
+import javax.sql.rowset.serial.SerialBlob;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * @author melon.zhao
+ * @since 2019/2/20
+ */
+public class StringUtilsTest {
+
+    @Test
+    public void testIsEmpty() {
+        Assert.assertEquals(StringUtils.isEmpty(null), true);
+        Assert.assertEquals(StringUtils.isEmpty("abc"), false);
+        Assert.assertEquals(StringUtils.isEmpty(""), true);
+        Assert.assertEquals(StringUtils.isEmpty(" "), false);
+    }
+
+    @Test
+    public void testString2blob() throws SQLException {
+        Assert.assertEquals(StringUtils.string2blob(null), null);
+        String[] strs = new String[]{"abc", "", " "};
+        for (String str : strs) {
+            Assert.assertEquals(StringUtils.string2blob(str), new SerialBlob(str.getBytes()));
+        }
+    }
+
+    @Test
+    public void testBlob2string() throws SQLException {
+        String[] strs = new String[]{"abc", " "};
+        for (String str : strs) {
+            Assert.assertEquals(StringUtils.blob2string(new SerialBlob(str.getBytes())), str);
+        }
+    }
+
+    @Test
+    public void testInputStream2String() {
+        try {
+            InputStream inputStream = StringUtilsTest.class.getClassLoader().getResourceAsStream("test.txt");
+            Assert.assertEquals(StringUtils.inputStream2String(inputStream), "abc\n"
+                + ":\"klsdf\n"
+                + "2ks,x:\".,-3sd˚ø≤ø¬≥");
+        } catch (FileNotFoundException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+}
diff --git a/common/src/test/java/com/alibaba/fescar/common/XIDTest.java b/common/src/test/java/com/alibaba/fescar/common/XIDTest.java
new file mode 100644
index 000000000..da522ee5c
--- /dev/null
+++ b/common/src/test/java/com/alibaba/fescar/common/XIDTest.java
@@ -0,0 +1,61 @@
+/*
+ *  Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ *  Licensed 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 com.alibaba.fescar.common;
+
+import java.util.Random;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * @author melon.zhao
+ * @since 2019/2/22
+ */
+public class XIDTest {
+
+    @Test
+    public void testSetIpAddress() {
+        XID.setIpAddress("127.0.0.1");
+        Assert.assertEquals(XID.getIpAddress(), "127.0.0.1");
+    }
+
+    @Test
+    void testSetPort() {
+        XID.setPort(8080);
+        Assert.assertEquals(XID.getPort(), 8080);
+    }
+
+
+    @Test
+    void testGenerateXID() {
+        long tranId = new Random().nextLong();
+        XID.setPort(8080);
+        XID.setIpAddress("127.0.0.1");
+        Assert.assertEquals(XID.generateXID(tranId), XID.getIpAddress() + ":" + XID.getPort() + ":" + tranId);
+    }
+
+    @Test
+    void testGetServerAddress() {
+        Assert.assertNull(XID.getServerAddress(null));
+        Assert.assertEquals(XID.getServerAddress("127.0.0.1:8080:8577662204289747564"), "127.0.0.1:8080");
+    }
+
+    @Test
+    void testGetTransactionId() {
+        Assert.assertEquals(XID.getTransactionId(null), -1);
+        Assert.assertEquals(XID.getTransactionId("127.0.0.1:8080:8577662204289747564"), 8577662204289747564L);
+    }
+}
diff --git a/common/src/test/resources/test.txt b/common/src/test/resources/test.txt
new file mode 100644
index 000000000..9df2d6bbb
--- /dev/null
+++ b/common/src/test/resources/test.txt
@@ -0,0 +1,3 @@
+abc
+:"klsdf
+2ks,x:".,-3sd˚ø≤ø¬≥
\ No newline at end of file