Back to Repositories

Testing ModelCache Dimension Handling Implementation in Glide

This test suite validates the ModelCache class functionality in Glide, focusing on caching behavior for model objects with different dimensions. The tests ensure proper key equivalence and cache operations for storing and retrieving model objects.

Test Coverage Overview

The test suite provides comprehensive coverage of ModelCache functionality, including:
  • Model key equivalence testing for different dimension combinations
  • Basic cache put/get operations
  • Multiple cache entries for the same model with different dimensions
Edge cases are covered through varying dimensions and object combinations, ensuring cache integrity across different scenarios.

Implementation Analysis

The testing approach utilizes JUnit 4 framework with a systematic verification of cache operations. The implementation employs Google’s EqualsTester for thorough equivalence testing and follows a clear arrange-act-assert pattern. Each test case isolates specific cache behaviors with distinct test methods for different functional aspects.

Technical Details

Testing tools and configuration include:
  • JUnit 4 test runner and annotations
  • Google Common Testing library for equality testing
  • ModelCache initialization with size limit of 10
  • Before setup method for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Clear test method naming reflecting purpose
  • Proper test isolation and setup
  • Comprehensive equality testing
  • Separate test cases for distinct functionality
  • Consistent assertion patterns

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/model/ModelCacheTest.java

            
package com.bumptech.glide.load.model;

import static org.junit.Assert.assertEquals;

import com.google.common.testing.EqualsTester;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ModelCacheTest {

  private ModelCache<Object, Object> cache;

  @Before
  public void setUp() {
    cache = new ModelCache<>(10);
  }

  @Test
  public void testModelKeyEquivalence() {
    new EqualsTester()
        .addEqualityGroup(
            ModelCache.ModelKey.get(14f, 100, 200), ModelCache.ModelKey.get(14f, 100, 200))
        .addEqualityGroup(ModelCache.ModelKey.get(13f, 100, 200))
        .addEqualityGroup(ModelCache.ModelKey.get(14f, 200, 200))
        .addEqualityGroup(ModelCache.ModelKey.get(14f, 100, 300))
        .testEquals();
  }

  @Test
  public void testCanSetAndGetModel() {
    Object model = new Object();
    int width = 10;
    int height = 20;
    Object result = new Object();
    cache.put(model, width, height, result);
    assertEquals(result, cache.get(model, width, height));
  }

  @Test
  public void testCanSetAndGetMultipleResultsWithDifferentDimensionsForSameObject() {
    Object model = new Object();
    int firstWidth = 10;
    int firstHeight = 20;
    Object firstResult = new Object();
    int secondWidth = 30;
    int secondHeight = 40;
    Object secondResult = new Object();

    cache.put(model, firstWidth, firstHeight, firstResult);
    cache.put(model, secondWidth, secondHeight, secondResult);

    assertEquals(firstResult, cache.get(model, firstWidth, firstHeight));
    assertEquals(secondResult, cache.get(model, secondWidth, secondHeight));
  }
}