Back to Repositories

Testing DataCacheKey Equivalence and Hash Generation in Glide

This test suite validates the DataCacheKey functionality in Glide’s caching system, focusing on key equality, hash code generation, and digest calculations. The tests ensure proper cache key handling for image loading and caching operations.

Test Coverage Overview

The test suite provides comprehensive coverage of DataCacheKey functionality, focusing on key equivalence testing and hash code verification.

Key areas covered include:
  • Equality comparison between different DataCacheKey combinations
  • Hash code generation for cache keys
  • Message digest calculation and verification
  • Regression testing with known hash values

Implementation Analysis

The testing approach utilizes JUnit4 framework with Mockito for mocking dependencies. The implementation employs a custom KeyTester utility for streamlined equivalence testing.

Notable patterns include:
  • Mock setup for key and signature components
  • Custom digest writing behavior implementation
  • Equivalence group testing methodology

Technical Details

Testing infrastructure includes:
  • JUnit4 test runner and annotations
  • Mockito framework for mock objects
  • Custom KeyTester rule for equivalence testing
  • MessageDigest for cache key verification
  • WriteDigest utility for consistent digest generation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation using @Before setup
  • Comprehensive equivalence testing
  • Regression test cases with known hash values
  • Clean mock object initialization and configuration
  • Structured test organization using JUnit Rules

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/engine/DataCacheKeyTest.java

            
package com.bumptech.glide.load.engine;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;

import com.bumptech.glide.load.Key;
import com.bumptech.glide.tests.KeyTester;
import com.bumptech.glide.tests.Util.WriteDigest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@RunWith(JUnit4.class)
public class DataCacheKeyTest {
  @Rule public final KeyTester keyTester = new KeyTester();

  @Mock private Key firstKey;
  @Mock private Key firstSignature;
  @Mock private Key secondKey;
  @Mock private Key secondSignature;

  @Before
  public void setUp() throws UnsupportedEncodingException {
    MockitoAnnotations.initMocks(this);
    doAnswer(new WriteDigest("firstKey"))
        .when(firstKey)
        .updateDiskCacheKey(any(MessageDigest.class));
    doAnswer(new WriteDigest("firstSignature"))
        .when(firstSignature)
        .updateDiskCacheKey(any(MessageDigest.class));
    doAnswer(new WriteDigest("secondKey"))
        .when(secondKey)
        .updateDiskCacheKey(any(MessageDigest.class));
    doAnswer(new WriteDigest("secondSignature"))
        .when(secondSignature)
        .updateDiskCacheKey(any(MessageDigest.class));
  }

  @Test
  public void testEqualsHashCodeDigest() throws NoSuchAlgorithmException {
    keyTester
        .addEquivalenceGroup(
            new DataCacheKey(firstKey, firstSignature), new DataCacheKey(firstKey, firstSignature))
        .addEquivalenceGroup(new DataCacheKey(firstKey, secondSignature))
        .addEquivalenceGroup(new DataCacheKey(secondKey, firstSignature))
        .addEquivalenceGroup(new DataCacheKey(secondKey, secondSignature))
        .addRegressionTest(
            new DataCacheKey(firstKey, firstSignature),
            "801d7440d65a0e7c9ad0097d417f346dac4d4c4d5630724110fa3f3fe66236d9")
        .test();
  }
}