Back to Repositories

Testing ByteArrayLoader Data Conversion in Glide

This test suite validates the ByteArrayLoader functionality in Glide, focusing on byte array handling and data conversion operations. The tests ensure proper data loading, conversion, and callback mechanisms for byte array processing in the Glide image loading library.

Test Coverage Overview

The test suite provides comprehensive coverage of ByteArrayLoader functionality, including byte array handling, data conversion, and callback mechanisms.

Key areas tested include:
  • Byte array data handling validation
  • Object conversion verification
  • Data class compatibility checks
  • Callback mechanism verification

Implementation Analysis

The testing approach utilizes JUnit4 with Mockito for mocking dependencies. The implementation follows a structured pattern with @Before setup and isolated @Test methods.

Technical implementation features:
  • Mockito annotation-based mock initialization
  • Converter interface mocking
  • DataCallback verification
  • UTF-8 byte array conversion testing

Technical Details

Testing infrastructure includes:
  • JUnit4 test runner
  • Mockito mocking framework
  • ByteArrayLoader class under test
  • Custom Options configuration
  • Mock DataCallback and Converter interfaces

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, mock usage, and thorough edge case coverage.

Notable practices include:
  • Clear test method naming
  • Proper test setup isolation
  • Mock verification for callback behavior
  • Null safety checks using Preconditions
  • Exception handling validation

bumptech/glide

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

            
package com.bumptech.glide.load.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.util.Preconditions;
import java.io.IOException;
import org.junit.Before;
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 ByteArrayLoaderTest {

  @Mock private ByteArrayLoader.Converter<Object> converter;
  @Mock private DataFetcher.DataCallback<Object> callback;
  private ByteArrayLoader<Object> loader;
  private Options options;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    loader = new ByteArrayLoader<>(converter);
    options = new Options();
  }

  @Test
  public void testCanHandleByteArray() {
    byte[] data = new byte[10];
    DataFetcher<Object> fetcher =
        Preconditions.checkNotNull(loader.buildLoadData(data, -1, -1, options)).fetcher;
    assertNotNull(fetcher);
  }

  @Test
  public void testFetcherReturnsObjectReceivedFromConverter() throws IOException {
    byte[] data = "fake".getBytes("UTF-8");
    Object expected = new Object();
    when(converter.convert(eq(data))).thenReturn(expected);

    Preconditions.checkNotNull(loader.buildLoadData(data, 10, 10, options))
        .fetcher
        .loadData(Priority.HIGH, callback);
    verify(callback).onDataReady(eq(expected));
  }

  @Test
  public void testFetcherReturnsDataClassFromConverter() {
    when(converter.getDataClass()).thenReturn(Object.class);
    assertEquals(
        Object.class,
        Preconditions.checkNotNull(loader.buildLoadData(new byte[10], 10, 10, options))
            .fetcher
            .getDataClass());
  }
}