Back to Repositories

Testing Memory-Based Table Metadata Management in Canal

This test suite validates the memory-based table metadata management functionality in Canal’s MySQL parsing module. It focuses on verifying the correct handling of table schema definitions and metadata storage in memory.

Test Coverage Overview

The test suite covers the core functionality of MemoryTableMeta class, focusing on schema parsing and metadata storage.

  • Tests DDL parsing from SQL files
  • Validates primary key identification
  • Verifies metadata retrieval operations
  • Ensures correct schema application to test databases

Implementation Analysis

The implementation uses Spring-based test configuration with JUnit4 runner for dependency injection and test execution.

Key patterns include:
  • File-based DDL script loading
  • In-memory metadata storage verification
  • Spring context configuration for test setup
  • Assertion-based validation of metadata attributes

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • Spring Test Context framework
  • Apache Commons IO for file operations
  • Custom XML configuration for test context
  • Resource file-based test data

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Isolated test environment using Spring test context
  • Clear separation of test data and test logic
  • Proper resource management for file operations
  • Explicit assertions for metadata validation
  • Well-structured test method organization

alibaba/canal

parse/src/test/java/com/alibaba/otter/canal/parse/inbound/mysql/tsdb/MemoryTableMetaTest.java

            
package com.alibaba.otter.canal.parse.inbound.mysql.tsdb;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.otter.canal.parse.inbound.TableMeta;

/**
 * @author agapple 2017年8月1日 下午7:15:54
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/tsdb/mysql-tsdb.xml" })
@Ignore
public class MemoryTableMetaTest {

    @Test
    public void testSimple() throws Throwable {
        MemoryTableMeta memoryTableMeta = new MemoryTableMeta();
        URL url = Thread.currentThread().getContextClassLoader().getResource("dummy.txt");
        File dummyFile = new File(url.getFile());
        File create = new File(dummyFile.getParent() + "/ddl", "create.sql");
        String sql = StringUtils.join(IOUtils.readLines(new FileInputStream(create)), "\n");
        memoryTableMeta.apply(null, "test", sql, null);

        TableMeta meta = memoryTableMeta.find("test", "test");
        System.out.println(meta);
        Assert.assertTrue(meta.getFieldMetaByName("ID").isKey());
    }
}