Back to Repositories

Validating Cache Key Generation Workflow in Bumptech Glide

This test suite validates the SafeKeyGenerator functionality in Glide’s caching system, ensuring generated keys are properly formatted for disk cache operations. It focuses on verifying the consistent generation of valid cache keys across multiple iterations.

Test Coverage Overview

The test suite provides comprehensive coverage of the SafeKeyGenerator class functionality, focusing on key generation validity.

  • Tests key format compliance with disk cache requirements
  • Validates against a specific regex pattern
  • Verifies consistency across 1000 iterations
  • Includes edge case handling for different input IDs

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation.

Key implementation patterns include:
  • Mock key implementation for controlled testing
  • Regex pattern matching for validation
  • Iterative testing for consistency verification
  • Custom test fixture setup with @Before annotation

Technical Details

Testing infrastructure includes:
  • Robolectric test runner configuration
  • JUnit 4 testing framework
  • Custom SDK version specification
  • MessageDigest implementation for key generation
  • Pattern matching utilities for validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation through fixture setup
  • Clear test method naming conventions
  • Comprehensive validation using regex patterns
  • Efficient test data generation
  • Proper use of assertion methods

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/engine/cache/SafeKeyGeneratorTest.java

            
package com.bumptech.glide.load.engine.cache;

import static com.bumptech.glide.RobolectricConstants.ROBOLECTRIC_SDK;
import static org.junit.Assert.assertTrue;

import androidx.annotation.NonNull;
import com.bumptech.glide.load.Key;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = ROBOLECTRIC_SDK)
public class SafeKeyGeneratorTest {
  private SafeKeyGenerator keyGenerator;
  private int nextId;

  @Before
  public void setUp() {
    nextId = 0;
    keyGenerator = new SafeKeyGenerator();
  }

  @Test
  public void testKeysAreValidForDiskCache() {
    final Pattern diskCacheRegex = Pattern.compile("[a-z0-9_-]{64}");
    for (int i = 0; i < 1000; i++) {
      String key = getRandomKeyFromGenerator();
      Matcher matcher = diskCacheRegex.matcher(key);
      assertTrue(key, matcher.matches());
    }
  }

  private String getRandomKeyFromGenerator() {
    return keyGenerator.getSafeKey(new MockKey(getNextId()));
  }

  private String getNextId() {
    return String.valueOf(nextId++);
  }

  private static final class MockKey implements Key {
    private final String id;

    MockKey(String id) {
      this.id = id;
    }

    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
      messageDigest.update(id.getBytes(CHARSET));
    }
  }
}