Back to Repositories

Testing Binary Log Event Type Resolution in alibaba/canal

This test suite comprehensively validates the LogEvent class’s type name resolution functionality in Alibaba Canal’s binary log processing system. It systematically tests the mapping between numeric event types and their corresponding string representations, ensuring reliable event type identification in MySQL binary log parsing.

Test Coverage Overview

The test suite provides extensive coverage of the LogEvent.getTypeName() method, validating all possible binary log event types.

Key areas tested include:
  • Standard MySQL binary log events (Query, Rotate, Stop etc.)
  • Row-based logging events (Write_rows, Update_rows, Delete_rows)
  • Special events like GTID and Format_desc
  • Legacy event types (v1 variants)
  • Edge case handling for unknown event types

Implementation Analysis

The testing approach employs systematic verification of event type mappings using JUnit 4 framework. Each test follows a consistent Arrange-Act-Assert pattern with positive test cases for different numeric type values.

Notable patterns include:
  • Consistent test method naming convention
  • Explicit timeout rules for performance boundaries
  • Exception handling verification
  • Automated test generation via Diffblue Cover

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • ExpectedException Rule for error case handling
  • Global timeout configuration (10000ms)
  • Assert class for result verification
  • Diffblue Cover for test generation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Java unit testing.

Notable practices include:
  • Comprehensive coverage of all event types
  • Consistent test structure and naming
  • Clear separation of arrange, act, and assert phases
  • Proper test isolation
  • Effective use of JUnit rules
  • Performance boundary enforcement

alibaba/canal

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

            
package com.taobao.tddl.dbsync.binlog;

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

public class LogEventTest {

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

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

    /* testedClasses: LogEvent */
    // Test written by Diffblue Cover.
    @Test
    public void getTypeNameInputPositiveOutputNotNull() {

        // Arrange
        final int type = 8;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 12;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 39;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 80;

        // Act
        final String actual = LogEvent.getTypeName(type);

        // Assert result
        Assert.assertTrue(actual.startsWith("Unknown"));
    }

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

        // Arrange
        final int type = 7;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 6;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 10;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 5;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 11;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 4;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 13;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 34;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 15;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 32;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 16;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 30;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 18;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 29;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 19;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 28;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 33;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 27;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 26;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 31;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 25;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 24;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 23;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 35;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 22;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 21;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 20;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 17;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 14;

        // Act
        final String actual = LogEvent.getTypeName(type);

        // Assert result
        Assert.assertEquals("User var", actual);
    }

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

        // Arrange
        final int type = 9;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 2;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 3;

        // Act
        final String actual = LogEvent.getTypeName(type);

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

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

        // Arrange
        final int type = 1;

        // Act
        final String actual = LogEvent.getTypeName(type);

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