Back to Repositories

Validating Resource Transcoder Registry Operations in Glide

This test suite validates the functionality of the TranscoderRegistry class in Glide’s resource transcoding system. It ensures proper registration and retrieval of resource transcoders while verifying unit transcoding behavior and type compatibility.

Test Coverage Overview

The test suite provides comprehensive coverage of TranscoderRegistry’s core functionality.

Key areas tested include:
  • Unit transcoder handling for identical classes
  • Registration and retrieval of custom resource transcoders
  • Type compatibility verification
  • Error handling for unregistered transcoders

Implementation Analysis

The testing approach utilizes JUnit4 framework with Mockito for mocking dependencies. The suite implements a clear arrange-act-assert pattern, with each test focusing on a specific transcoder registry behavior.

Tests verify both positive scenarios (successful transcoding) and negative cases (missing transcoders).

Technical Details

Testing tools and configuration:
  • JUnit4 test runner
  • Mockito mocking framework
  • setUp method for test initialization
  • Assert methods for verification
  • Expected exception testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming reflecting purpose
  • Proper test isolation through setUp
  • Effective use of mocking
  • Explicit exception testing
  • Comprehensive type compatibility verification

bumptech/glide

library/test/src/test/java/com/bumptech/glide/load/resource/transcode/TranscoderRegistryTest.java

            
package com.bumptech.glide.load.resource.transcode;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;

import java.io.File;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class TranscoderRegistryTest {
  private TranscoderRegistry factories;

  @Before
  public void setUp() {
    factories = new TranscoderRegistry();
  }

  @Test
  public void testReturnsUnitDecoderIfClassesAreIdentical() {
    assertEquals(UnitTranscoder.get(), factories.get(Object.class, Object.class));
  }

  @Test
  public void testCanRegisterAndRetrieveResourceTranscoder() {
    @SuppressWarnings("unchecked")
    ResourceTranscoder<File, String> transcoder = mock(ResourceTranscoder.class);
    factories.register(File.class, String.class, transcoder);

    assertEquals(transcoder, factories.get(File.class, String.class));
  }

  @Test
  public void testDoesNotThrowIfRequestCanBeSatisfiedByUnitTranscoder() {
    // Assignable from.
    assertNotNull(factories.get(Integer.class, Number.class));
    // Equal to.
    assertNotNull(factories.get(Integer.class, Integer.class));
  }

  @Test(expected = IllegalArgumentException.class)
  public void testThrowsIfNoTranscoderRegistered() {
    factories.get(File.class, Integer.class);
  }
}