Back to Repositories

Testing ByteBuffer Stream Conversion Utilities in Glide

This test suite validates the ByteBufferUtil class functionality in Glide, focusing on stream-to-ByteBuffer conversion operations. The tests ensure reliable data transformation across various buffer sizes and edge cases while maintaining data integrity.

Test Coverage Overview

The test suite provides comprehensive coverage of ByteBufferUtil’s stream handling capabilities.

Key areas tested include:
  • Empty stream conversion
  • Small data streams (4 bytes)
  • Buffer-size aligned data
  • Large data streams (over 12 buffer sizes)
The suite verifies byte-by-byte accuracy of the conversion process across all test cases.

Implementation Analysis

The testing approach utilizes JUnit with Robolectric for Android environment simulation. The implementation employs a parametric testing pattern where a single test method (testFromStream) is reused with different data sizes.

Technical patterns include:
  • Buffer size constants definition
  • Systematic byte pattern generation
  • Byte-by-byte verification
  • Resource cleanup handling

Technical Details

Testing infrastructure includes:
  • JUnit 4 test framework
  • Robolectric test runner
  • Custom SDK configuration
  • ByteArrayInputStream for test data
  • 16384 byte buffer size constant
The setup leverages Robolectric’s Android environment simulation capabilities for accurate testing.

Best Practices Demonstrated

The test suite exemplifies several testing best practices.

Notable implementations include:
  • Systematic edge case testing
  • Resource cleanup in stream handling
  • Modular test helper methods
  • Clear test method naming
  • Efficient test data generation
  • Comprehensive assertion checks

bumptech/glide

library/test/src/test/java/com/bumptech/glide/util/ByteBufferUtilTest.java

            
package com.bumptech.glide.util;

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

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
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 ByteBufferUtilTest {
  private static final int BUFFER_SIZE = 16384;

  @Test
  public void testFromStream_small() throws IOException {
    testFromStream(4);
  }

  @Test
  public void testFromStream_empty() throws IOException {
    testFromStream(0);
  }

  @Test
  public void testFromStream_bufferAndAHalf() throws IOException {
    testFromStream(BUFFER_SIZE + BUFFER_SIZE / 2);
  }

  @Test
  public void testFromStream_massive() throws IOException {
    testFromStream(12 * BUFFER_SIZE + 12345);
  }

  /** All tests are basically the same thing but with different amounts of data. */
  private void testFromStream(int dataLength) throws IOException {
    byte[] bytes = createByteData(dataLength);
    InputStream byteStream = new ByteArrayInputStream(bytes);
    ByteBuffer byteBuffer = ByteBufferUtil.fromStream(byteStream);
    assertByteBufferContents(byteBuffer, bytes);
    byteStream.close();
  }

  private byte[] createByteData(int size) {
    byte[] bytes = new byte[size];

    // Put some arbitrary bytes in there.
    for (int i = 0; i < size; i++) {
      bytes[i] = (byte) (i % 4);
    }

    return bytes;
  }

  private void assertByteBufferContents(ByteBuffer buffer, byte[] expectedBytes) {
    assertEquals(expectedBytes.length, buffer.limit());
    for (int i = 0; i < expectedBytes.length; i++) {
      assertEquals(expectedBytes[i], buffer.get(i));
    }
  }
}