mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
Add unit tests (#2235)
* Add Unit Tests for the following classes: com.alibaba.otter.canal.client.adapter.support.JdbcTypeUtilTest com.alibaba.otter.canal.client.adapter.support.UtilTest com.taobao.tddl.dbsync.binlog.JsonConversion_Json_ValueTest com.taobao.tddl.dbsync.binlog.JsonDiffConversionTest com.taobao.tddl.dbsync.binlog.LogBufferTest com.taobao.tddl.dbsync.binlog.LogEventTest com.taobao.tddl.dbsync.binlog.event.LogHeaderTest com.taobao.tddl.dbsync.binlog.event.RowsLogBufferTest com.alibaba.otter.canal.parse.driver.mysql.packets.client.RegisterSlaveCommandPacketTest com.alibaba.otter.canal.parse.driver.mysql.utils.ByteHelperTest com.alibaba.otter.canal.parse.inbound.mysql.ddl.DdlResultTest com.alibaba.otter.canal.parse.inbound.mysql.ddl.DruidDdlParserTest These tests were written using Diffblue Cover * Add dependencies and refactor
This commit is contained in:
@@ -36,6 +36,36 @@
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>1.19</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<version>1.6.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>1.6.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-all</artifactId>
|
||||
<version>1.10.19</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.diffblue</groupId>
|
||||
<artifactId>deeptestutils</artifactId>
|
||||
<version>1.9.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.alibaba.otter.canal.client.adapter.support;
|
||||
|
||||
import static org.mockito.AdditionalMatchers.or;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Matchers.isNull;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
import com.alibaba.otter.canal.client.adapter.support.JdbcTypeUtil;
|
||||
import com.alibaba.otter.canal.client.adapter.support.Util;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.Timeout;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
public class JdbcTypeUtilTest {
|
||||
|
||||
@Rule public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Rule public final Timeout globalTimeout = new Timeout(10000);
|
||||
|
||||
/* testedClasses: JdbcTypeUtil */
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullNegativeNotNullOutput3() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "foo";
|
||||
final String columnName = "foo";
|
||||
final String value = "foo";
|
||||
final int sqlType = -4;
|
||||
final String mysqlType = "foo";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertArrayEquals(new byte[] {(byte)102, (byte)111, (byte)111}, ((byte[])actual));
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullNegativeNotNullOutputPositive() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "foo";
|
||||
final String columnName = "foo";
|
||||
final String value = "1234";
|
||||
final int sqlType = -5;
|
||||
final String mysqlType = "foo";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals(1234L, actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullNegativeNotNullOutputPositive2() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "?????????";
|
||||
final String columnName = "?";
|
||||
final String value = "2";
|
||||
final int sqlType = -5;
|
||||
final String mysqlType = "bigint";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals(2L, actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullPositiveNotNullOutputFalse() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "?????????";
|
||||
final String columnName = "?";
|
||||
final String value = "0";
|
||||
final int sqlType = 16;
|
||||
final String mysqlType = "bigint\u046bunsigned";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertFalse((boolean)actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullPositiveNotNullOutputNotNull() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "1";
|
||||
final String columnName = "Bar";
|
||||
final String value = "1";
|
||||
final int sqlType = 12;
|
||||
final String mysqlType = "2";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals("1", actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullPositiveNotNullOutputPositive() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "foo";
|
||||
final String columnName = "foo";
|
||||
final String value = "3";
|
||||
final int sqlType = 4;
|
||||
final String mysqlType = "foo";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals(3, actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullPositiveNotNullOutputTrue() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "?????????";
|
||||
final String columnName = "?";
|
||||
final String value = "5";
|
||||
final int sqlType = 16;
|
||||
final String mysqlType = "bigintsunsigned";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertTrue((boolean)actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullPositiveNotNullOutputZero2() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "?????????";
|
||||
final String columnName = "?";
|
||||
final String value = "0";
|
||||
final int sqlType = 7;
|
||||
final String mysqlType = "bigint\u046bunsigned";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals(0.0f, (float)actual, 0.0f);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void typeConvertInputNotNullNotNullNotNullPositiveNotNullOutputZero3() {
|
||||
|
||||
// Arrange
|
||||
final String tableName = "?????????";
|
||||
final String columnName = "?";
|
||||
final String value = "0";
|
||||
final int sqlType = 5;
|
||||
final String mysqlType = "bigintiunsigned";
|
||||
|
||||
// Act
|
||||
final Object actual =
|
||||
JdbcTypeUtil.typeConvert(tableName, columnName, value, sqlType, mysqlType);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals((short)0, actual);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package com.alibaba.otter.canal.client.adapter.support;
|
||||
|
||||
import static org.mockito.AdditionalMatchers.or;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Matchers.isNull;
|
||||
import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
|
||||
import com.alibaba.otter.canal.client.adapter.support.Util;
|
||||
import com.diffblue.deeptestutils.mock.DTUMemberMatcher;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.Timeout;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockIgnore({"javax.management.*"})
|
||||
public class UtilTest {
|
||||
|
||||
@Rule public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
@Rule public final Timeout globalTimeout = new Timeout(10000);
|
||||
|
||||
/* testedClasses: Util */
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void cleanColumnInputNotNullOutputNotNull() {
|
||||
|
||||
// Arrange
|
||||
final String column = "1234";
|
||||
|
||||
// Act
|
||||
final String actual = Util.cleanColumn(column);
|
||||
|
||||
// Assert result
|
||||
Assert.assertEquals("1234", actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@Test
|
||||
public void cleanColumnInputNullOutputNull() {
|
||||
|
||||
// Arrange
|
||||
final String column = null;
|
||||
|
||||
// Act
|
||||
final String actual = Util.cleanColumn(column);
|
||||
|
||||
// Assert result
|
||||
Assert.assertNull(actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@PrepareForTest(StringUtils.class)
|
||||
@Test
|
||||
public void parseDate2InputNotNullOutputNull() throws Exception {
|
||||
|
||||
// Setup mocks
|
||||
PowerMockito.mockStatic(StringUtils.class);
|
||||
|
||||
// Arrange
|
||||
final String datetimeStr = "1a 2b 3c";
|
||||
final Method isEmptyMethod =
|
||||
DTUMemberMatcher.method(StringUtils.class, "isEmpty", String.class);
|
||||
PowerMockito.doReturn(true)
|
||||
.when(StringUtils.class, isEmptyMethod)
|
||||
.withArguments(or(isA(String.class), isNull(String.class)));
|
||||
|
||||
// Act
|
||||
final Date actual = Util.parseDate2(datetimeStr);
|
||||
|
||||
// Assert result
|
||||
Assert.assertNull(actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@PrepareForTest(StringUtils.class)
|
||||
@Test
|
||||
public void parseDate2InputNotNullOutputNull2() throws Exception {
|
||||
|
||||
// Setup mocks
|
||||
PowerMockito.mockStatic(StringUtils.class);
|
||||
|
||||
// Arrange
|
||||
final String datetimeStr = "1a 2b 3c";
|
||||
final Method isEmptyMethod =
|
||||
DTUMemberMatcher.method(StringUtils.class, "isEmpty", String.class);
|
||||
PowerMockito.doReturn(false)
|
||||
.when(StringUtils.class, isEmptyMethod)
|
||||
.withArguments(or(isA(String.class), isNull(String.class)));
|
||||
|
||||
// Act
|
||||
final Date actual = Util.parseDate2(datetimeStr);
|
||||
|
||||
// Assert result
|
||||
Assert.assertNull(actual);
|
||||
}
|
||||
|
||||
// Test written by Diffblue Cover.
|
||||
@PrepareForTest(StringUtils.class)
|
||||
@Test
|
||||
public void parseDateInputNotNullOutputNull() throws Exception {
|
||||
|
||||
// Setup mocks
|
||||
PowerMockito.mockStatic(StringUtils.class);
|
||||
|
||||
// Arrange
|
||||
final String datetimeStr = "a/b/c";
|
||||
final Method isEmptyMethod =
|
||||
DTUMemberMatcher.method(StringUtils.class, "isEmpty", String.class);
|
||||
PowerMockito.doReturn(true)
|
||||
.when(StringUtils.class, isEmptyMethod)
|
||||
.withArguments(or(isA(String.class), isNull(String.class)));
|
||||
|
||||
// Act
|
||||
final Date actual = Util.parseDate(datetimeStr);
|
||||
|
||||
// Assert result
|
||||
Assert.assertNull(actual);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user