Back to Repositories

Testing JSON Difference Conversion Functionality in Canal

This test suite evaluates the JSON difference conversion functionality in Alibaba Canal’s binary log synchronization module. It focuses on validating the proper handling of JSON data differences with various input parameters and edge cases, ensuring robust data transformation during MySQL binlog processing.

Test Coverage Overview

The test suite provides comprehensive coverage for JsonDiffConversion class functionality.

Key areas tested include:
  • Buffer handling with various position and limit configurations
  • Character set conversion scenarios using GBK encoding
  • Edge cases with zero-length inputs
  • Error handling for illegal arguments

Implementation Analysis

The testing approach employs JUnit’s exception handling and timeout mechanisms to ensure robust validation. Tests utilize detailed byte array configurations and LogBuffer manipulation to verify JSON difference conversion behavior.

Implementation patterns include:
  • Systematic boundary testing with buffer positions
  • Explicit exception verification using @Rule annotations
  • Side-effect validation after exception scenarios

Technical Details

Testing infrastructure includes:
  • JUnit 4 framework with @Test annotations
  • ExpectedException rule for exception handling
  • Timeout rule set to 10000ms for performance boundaries
  • LogBuffer class for binary data manipulation
  • Custom byte array configurations for various test scenarios

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Thorough setup and teardown of test environments
  • Explicit validation of both positive and negative scenarios
  • Comprehensive assertion chains for state verification
  • Clear test method naming conventions
  • Proper exception handling and verification

alibaba/canal

dbsync/src/test/java/com/taobao/tddl/dbsync/binlog/JsonDiffConversionTest.java

            
package com.taobao.tddl.dbsync.binlog;

import java.lang.reflect.InvocationTargetException;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.Timeout;

public class JsonDiffConversionTest {

    @Rule
    public final ExpectedException thrown        = ExpectedException.none();

    @SuppressWarnings("deprecation")
    @Rule
    public final Timeout           globalTimeout = new Timeout(10000);

    /* testedClasses: JsonDiffConversion */
    // Test written by Diffblue Cover.
    @Test
    public void print_json_diffInputNotNullPositiveNotNullZeroNotNullOutputIllegalArgumentException()
                                                                                                     throws InvocationTargetException {

        // Arrange
        final LogBuffer buffer = new LogBuffer();
        buffer.position = 28;
        buffer.semival = 0;
        final byte[] myByteArray = { (byte) 3, (byte) 3, (byte) 67, (byte) 67, (byte) 67, (byte) 67, (byte) 67,
                (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 67, (byte) 67, (byte) 66,
                (byte) 67, (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 67, (byte) 67, (byte) 66, (byte) 67,
                (byte) 67, (byte) 67, (byte) 66, (byte) 2, (byte) 66 };
        buffer.buffer = myByteArray;
        buffer.limit = -1_000_000_065;
        buffer.origin = 1_000_000_096;
        final long len = 71L;
        final String columnName = "foo";
        final int columnIndex = 0;
        final String charsetName = "gbk";
        try {

            // Act
            thrown.expect(IllegalArgumentException.class);
            JsonDiffConversion.print_json_diff(buffer, len, columnName, columnIndex, charsetName);
        } catch (IllegalArgumentException ex) {

            // Assert side effects
            Assert.assertNotNull(buffer);
            Assert.assertEquals(30, buffer.position);
            Assert.assertEquals(0, buffer.semival);
            Assert.assertArrayEquals(new byte[] { (byte) 3, (byte) 3, (byte) 67, (byte) 67, (byte) 67, (byte) 67,
                    (byte) 67, (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 67, (byte) 67,
                    (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 66, (byte) 67, (byte) 67, (byte) 67, (byte) 66,
                    (byte) 67, (byte) 67, (byte) 67, (byte) 66, (byte) 2, (byte) 66 }, buffer.buffer);
            Assert.assertEquals(-1_000_000_065, buffer.limit);
            Assert.assertEquals(1_000_000_096, buffer.origin);
            throw ex;
        }
    }

    // Test written by Diffblue Cover.
    @Test
    public void print_json_diffInputNotNullZeroNotNullZeroNotNullOutputIllegalArgumentException()
                                                                                                 throws InvocationTargetException {

        // Arrange
        final LogBuffer buffer = new LogBuffer();
        buffer.position = 28;
        buffer.semival = 0;
        final byte[] myByteArray = { (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2,
                (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2,
                (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2,
                (byte) 3, (byte) 2 };
        buffer.buffer = myByteArray;
        buffer.limit = 31;
        buffer.origin = 0;
        final long len = 0L;
        final String columnName = "foo";
        final int columnIndex = 0;
        final String charsetName = "gbk";
        try {

            // Act
            thrown.expect(IllegalArgumentException.class);
            JsonDiffConversion.print_json_diff(buffer, len, columnName, columnIndex, charsetName);
        } catch (IllegalArgumentException ex) {

            // Assert side effects
            Assert.assertNotNull(buffer);
            Assert.assertEquals(29, buffer.position);
            Assert.assertEquals(0, buffer.semival);
            Assert.assertArrayEquals(new byte[] { (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2,
                    (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2,
                    (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2, (byte) 2,
                    (byte) 2, (byte) 3, (byte) 2 }, buffer.buffer);
            Assert.assertEquals(31, buffer.limit);
            Assert.assertEquals(0, buffer.origin);
            throw ex;
        }
    }

    // Test written by Diffblue Cover.
    @Test
    public void print_json_diffInputNotNullZeroNotNullZeroNotNullOutputIllegalArgumentException2()
                                                                                                  throws InvocationTargetException {

        // Arrange
        final LogBuffer buffer = new LogBuffer();
        buffer.position = 15;
        buffer.semival = 0;
        final byte[] myByteArray = { (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1,
                (byte) 1, (byte) 1, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 0, (byte) 1, (byte) 1,
                (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 0,
                (byte) 1, (byte) 1 };
        buffer.buffer = myByteArray;
        buffer.limit = -1_215_751_986;
        buffer.origin = 1_215_752_002;
        final long len = 0L;
        final String columnName = "foo";
        final int columnIndex = 0;
        final String charsetName = "gbk";
        try {

            // Act
            thrown.expect(IllegalArgumentException.class);
            JsonDiffConversion.print_json_diff(buffer, len, columnName, columnIndex, charsetName);
        } catch (IllegalArgumentException ex) {

            // Assert side effects
            Assert.assertNotNull(buffer);
            Assert.assertEquals(16, buffer.position);
            Assert.assertEquals(0, buffer.semival);
            Assert.assertArrayEquals(new byte[] { (byte) 1, (byte) 1, (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1,
                    (byte) 1, (byte) 1, (byte) 1, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 0, (byte) 1,
                    (byte) 1, (byte) 0, (byte) 0, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1, (byte) 1,
                    (byte) 0, (byte) 1, (byte) 1 }, buffer.buffer);
            Assert.assertEquals(-1_215_751_986, buffer.limit);
            Assert.assertEquals(1_215_752_002, buffer.origin);
            throw ex;
        }
    }

    // Test written by Diffblue Cover.
    @Test
    public void print_json_diffInputNotNullZeroNotNullZeroNotNullOutputIllegalArgumentException3()
                                                                                                  throws InvocationTargetException {

        // Arrange
        final LogBuffer buffer = new LogBuffer();
        buffer.position = 27;
        buffer.semival = 0;
        final byte[] myByteArray = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                (byte) 1, (byte) 0 };
        buffer.buffer = myByteArray;
        buffer.limit = 31;
        buffer.origin = -1;
        final long len = 0L;
        final String columnName = "foo";
        final int columnIndex = 0;
        final String charsetName = "gbk";
        try {

            // Act
            thrown.expect(IllegalArgumentException.class);
            JsonDiffConversion.print_json_diff(buffer, len, columnName, columnIndex, charsetName);
        } catch (IllegalArgumentException ex) {

            // Assert side effects
            Assert.assertNotNull(buffer);
            Assert.assertEquals(29, buffer.position);
            Assert.assertEquals(0, buffer.semival);
            Assert.assertArrayEquals(new byte[] { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                    (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                    (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                    (byte) 0, (byte) 1, (byte) 0 }, buffer.buffer);
            Assert.assertEquals(31, buffer.limit);
            Assert.assertEquals(-1, buffer.origin);
            throw ex;
        }
    }

    // Test written by Diffblue Cover.
    @Test
    public void print_json_diffInputNotNullZeroNotNullZeroNotNullOutputNotNull() {

        // Arrange
        final LogBuffer buffer = new LogBuffer();
        final long len = 0L;
        final String columnName = ",";
        final int columnIndex = 0;
        final String charsetName = "gbk";

        // Act
        final StringBuilder actual = JsonDiffConversion.print_json_diff(buffer,
            len,
            columnName,
            columnIndex,
            charsetName);

        // Assert result
        Assert.assertNotNull(actual);
        Assert.assertEquals(",", actual.toString());
    }
}