Back to Repositories

Testing ObjectUtils Collection Validation in AndroidUtilCode

This test suite validates the ObjectUtils class functionality in the AndroidUtilCode library, focusing on object emptiness checks and equality comparisons. The tests thoroughly examine various Android data structures and common object types.

Test Coverage Overview

The test suite provides comprehensive coverage of ObjectUtils methods, specifically isEmpty() and equals() functionality.

Key areas tested include:
  • Empty state validation for Android collections (SparseArray, SparseBooleanArray)
  • Standard Java collections emptiness (HashMap, LinkedList)
  • String and StringBuilder emptiness checks
  • Object equality comparisons for primitives and strings
  • Null handling scenarios

Implementation Analysis

The testing approach utilizes JUnit’s assertion framework to validate object states and comparisons.

Testing patterns include:
  • Positive and negative test cases for isEmpty/isNotEmpty methods
  • Comprehensive data structure coverage across Android and Java collections
  • Boolean assertion pairs to verify logical complementary behavior
  • Null safety validation in equality comparisons

Technical Details

Testing infrastructure includes:
  • JUnit 4 testing framework
  • Android support libraries for specialized collections
  • Base test class extension for common functionality
  • Static imports for assertion methods
  • Various Android utility classes (SparseArray, SparseBooleanArray, etc.)

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Comprehensive edge case coverage including null scenarios
  • Clear test method naming reflecting functionality under test
  • Systematic validation of complementary operations
  • Efficient test organization with focused test methods
  • Proper separation of test cases for different functionalities

blankj/androidutilcode

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

            
package com.blankj.utilcode.util;

import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
import android.util.SparseLongArray;

import org.junit.Test;

import java.util.HashMap;
import java.util.LinkedList;

import androidx.collection.LongSparseArray;
import androidx.collection.SimpleArrayMap;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2017/12/24
 *     desc  : test ObjectUtils
 * </pre>
 */
public class ObjectUtilsTest extends BaseTest {

    @Test
    public void isEmpty() {
        StringBuilder sb = new StringBuilder("");
        StringBuilder sb1 = new StringBuilder(" ");
        String string = "";
        String string1 = " ";
        int[][] arr = new int[][]{};
        LinkedList<Integer> list = new LinkedList<>();
        HashMap<String, Integer> map = new HashMap<>();
        SimpleArrayMap<String, Integer> sam = new SimpleArrayMap<>();
        SparseArray<String> sa = new SparseArray<>();
        SparseBooleanArray sba = new SparseBooleanArray();
        SparseIntArray sia = new SparseIntArray();
        SparseLongArray sla = new SparseLongArray();
        LongSparseArray<String> lsa = new LongSparseArray<>();
        android.util.LongSparseArray<String> lsaV4 = new android.util.LongSparseArray<>();

        assertTrue(ObjectUtils.isEmpty(sb));
        assertFalse(ObjectUtils.isEmpty(sb1));
        assertTrue(ObjectUtils.isEmpty(string));
        assertFalse(ObjectUtils.isEmpty(string1));
        assertTrue(ObjectUtils.isEmpty(arr));
        assertTrue(ObjectUtils.isEmpty(list));
        assertTrue(ObjectUtils.isEmpty(map));
        assertTrue(ObjectUtils.isEmpty(sam));
        assertTrue(ObjectUtils.isEmpty(sa));
        assertTrue(ObjectUtils.isEmpty(sba));
        assertTrue(ObjectUtils.isEmpty(sia));
        assertTrue(ObjectUtils.isEmpty(sla));
        assertTrue(ObjectUtils.isEmpty(lsa));
        assertTrue(ObjectUtils.isEmpty(lsaV4));

        assertTrue(!ObjectUtils.isNotEmpty(sb));
        assertFalse(!ObjectUtils.isNotEmpty(sb1));
        assertTrue(!ObjectUtils.isNotEmpty(string));
        assertFalse(!ObjectUtils.isNotEmpty(string1));
        assertTrue(!ObjectUtils.isNotEmpty(arr));
        assertTrue(!ObjectUtils.isNotEmpty(list));
        assertTrue(!ObjectUtils.isNotEmpty(map));
        assertTrue(!ObjectUtils.isNotEmpty(sam));
        assertTrue(!ObjectUtils.isNotEmpty(sa));
        assertTrue(!ObjectUtils.isNotEmpty(sba));
        assertTrue(!ObjectUtils.isNotEmpty(sia));
        assertTrue(!ObjectUtils.isNotEmpty(sla));
        assertTrue(!ObjectUtils.isNotEmpty(lsa));
        assertTrue(!ObjectUtils.isNotEmpty(lsaV4));
    }

    @Test
    public void equals() {
        assertTrue(ObjectUtils.equals(1, 1));
        assertTrue(ObjectUtils.equals("str", "str"));
        assertTrue(ObjectUtils.equals(null, null));

        assertFalse(ObjectUtils.equals(null, 1));
        assertFalse(ObjectUtils.equals(null, ""));
    }

}