Back to Repositories

Testing Deep Clone Operations with Generic Types in AndroidUtilCode

This test suite validates the deep cloning functionality in the CloneUtils class of AndroidUtilCode, focusing on complex object structures with generic types. The tests ensure proper object duplication while maintaining data integrity and reference independence.

Test Coverage Overview

The test coverage focuses on deep cloning of nested objects with generics, specifically testing the Result class structure.

Key areas covered include:
  • Deep cloning of generic container classes
  • Validation of reference independence
  • Complex object structure handling
  • Data integrity verification after cloning

Implementation Analysis

The testing approach utilizes JUnit 4 framework with a focused single test method implementation. The test leverages GsonUtils for type handling and implements custom toString() methods for object comparison.

Notable patterns include:
  • Generic type resolution using GsonUtils.getType()
  • Custom assertion logic for object equality
  • Structured test data preparation

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • GsonUtils for type handling
  • Custom Result and Person classes for test data
  • BaseTest extension for common functionality
  • System.out verification for visual inspection

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test methods with clear purpose
  • Proper test data encapsulation
  • Custom toString() implementations for debugging
  • Strong type safety through generics
  • Clear separation of test and production code

blankj/androidutilcode

lib/utilcode/src/test/java/com/blankj/utilcode/util/CloneUtilsTest.java

            
package com.blankj.utilcode.util;

import org.junit.Assert;
import org.junit.Test;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/04/08
 *     desc  : test CloneUtils
 * </pre>
 */
public class CloneUtilsTest extends BaseTest {

    @Test
    public void deepClone() {
        Result<Person> result = new Result<>(new Person("Blankj"));
        Result<Person> cloneResult = CloneUtils.deepClone(result, GsonUtils.getType(Result.class, Person.class));
        System.out.println(result);
        System.out.println(cloneResult);
        Assert.assertNotEquals(result, cloneResult);
    }

    static class Result<T> {
        int    code;
        String message;
        T      data;

        Result(T data) {
            this.code = 200;
            this.message = "success";
            this.data = data;
        }

        @Override
        public String toString() {
            return "{\"code\":" + primitive2String(code) +
                    ",\"message\":" + primitive2String(message) +
                    ",\"data\":" + primitive2String(data) + "}";
        }
    }

    static class Person {

        String name;
        int    gender;
        String address;

        Person(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "{\"name\":" + primitive2String(name) +
                    ",\"gender\":" + primitive2String(gender) +
                    ",\"address\":" + primitive2String(address) + "}";
        }
    }

    private static String primitive2String(final Object obj) {
        if (obj == null) return "null";
        if (obj instanceof CharSequence) return "\"" + obj.toString() + "\"";
        return obj.toString();
    }
}