Back to Repositories

Validating Oracle Database Synchronization Workflows in Canal

This test suite validates Oracle database synchronization functionality in Canal’s RDB adapter, focusing on insert and update operations. It ensures reliable data replication between Oracle databases while maintaining data integrity and proper transformation.

Test Coverage Overview

The test suite provides comprehensive coverage of Oracle database synchronization operations.

Key areas tested include:
  • Insert operations with multiple data types and character sets
  • Update operations with before/after state validation
  • Timestamp handling and data transformation
  • Destination mapping and database connectivity

Implementation Analysis

The testing approach uses JUnit to validate the RdbAdapter’s synchronization capabilities. Tests implement a structured pattern of creating Dml objects with specific data payloads, executing sync operations, and verifying results.

Key implementation features:
  • Mock data generation for database operations
  • Lifecycle management with @Before annotations
  • Isolated test cases for different sync scenarios

Technical Details

Testing infrastructure includes:
  • JUnit test framework
  • RdbAdapter for database operations
  • Dml class for data manipulation
  • LinkedHashMap for ordered data storage
  • Custom initialization through Common.init()
  • Support for multiple character sets including Unicode

Best Practices Demonstrated

The test suite exemplifies several testing best practices in database synchronization validation.

Notable practices include:
  • Proper test isolation and setup
  • Comprehensive data type coverage
  • Clear test case organization
  • Explicit state management
  • Proper exception handling and resource cleanup

alibaba/canal

client-adapter/rdb/src/test/java/com/alibaba/otter/canal/client/adapter/rdb/test/sync/OracleSyncTest.java

            
package com.alibaba.otter.canal.client.adapter.rdb.test.sync;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import com.alibaba.otter.canal.client.adapter.rdb.RdbAdapter;
import com.alibaba.otter.canal.client.adapter.support.Dml;
@Ignore
public class OracleSyncTest {

    private RdbAdapter rdbAdapter;

    @Before
    public void init() {
        rdbAdapter = Common.init();
    }

    @Test
    public void test01() {
        Dml dml = new Dml();
        dml.setDestination("example");
        dml.setTs(new Date().getTime());
        dml.setType("INSERT");
        dml.setDatabase("mytest");
        dml.setTable("user");
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> data = new LinkedHashMap<>();
        dataList.add(data);
        data.put("id", 1L);
        data.put("name", "Eric");
        data.put("role_id", 1L);
        data.put("c_time", new Date());
        data.put("test1", "sdfasdfawe中国asfwef");
        dml.setData(dataList);

        rdbAdapter.sync(Collections.singletonList(dml));
    }

    @Test
    public void test02() {
        Dml dml = new Dml();
        dml.setDestination("example");
        dml.setTs(new Date().getTime());
        dml.setType("UPDATE");
        dml.setDatabase("mytest");
        dml.setTable("user");
        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> data = new LinkedHashMap<>();
        dataList.add(data);
        data.put("id", 1L);
        data.put("name", "Eric2");
        dml.setData(dataList);
        List<Map<String, Object>> oldList = new ArrayList<>();
        Map<String, Object> old = new LinkedHashMap<>();
        oldList.add(old);
        old.put("name", "Eric");
        dml.setOld(oldList);

        rdbAdapter.sync(Collections.singletonList(dml));
    }

}