Back to Repositories

Validating Multiple LibraryGlideModule Compilation in Glide

This test suite verifies the functionality of multiple empty LibraryGlideModule implementations in the Glide image loading library. It ensures proper compilation and indexing of multiple library modules, validating the annotation processing workflow for empty Glide modules.

Test Coverage Overview

The test suite covers the compilation and generation of source files for multiple empty LibraryGlideModule implementations.

Key areas tested include:
  • Successful compilation without warnings
  • Verification of generated source file count
  • Validation of generated indexer class names
  • Integration with Glide’s annotation processing system

Implementation Analysis

The testing approach utilizes JUnit4 framework with custom compilation testing utilities. The tests implement a systematic verification of the annotation processor’s output, using the javac compiler API programmatically.

Technical patterns include:
  • Compilation result validation
  • Source file generation verification
  • Resource-based test file management
  • Custom test rule implementation for resource regeneration

Technical Details

Testing infrastructure includes:
  • JUnit4 test runner
  • Google Truth assertion library
  • Custom RegenerateResourcesRule
  • JavaFileObject utilities
  • GlideAnnotationProcessor
  • CompilationProvider interface implementation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clean test setup using @Before annotation
  • Isolated test cases for different aspects
  • Resource management through custom test rules
  • Clear separation of compilation and verification steps
  • Reusable test utilities and helper methods

bumptech/glide

annotation/compiler/test/src/test/java/com/bumptech/glide/annotation/compiler/MultipleEmptyLibraryGlideModuleTest.java

            
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.test.Util.annotation;
import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;

import com.bumptech.glide.annotation.compiler.test.CompilationProvider;
import com.bumptech.glide.annotation.compiler.test.RegenerateResourcesRule;
import com.bumptech.glide.annotation.compiler.test.Util;
import com.google.common.truth.Truth;
import com.google.testing.compile.Compilation;
import java.io.IOException;
import javax.tools.JavaFileObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests adding multiple {@link com.bumptech.glide.module.LibraryGlideModule}s in a project. */
@RunWith(JUnit4.class)
public class MultipleEmptyLibraryGlideModuleTest implements CompilationProvider {
  @Rule
  public final RegenerateResourcesRule regenerateResourcesRule = new RegenerateResourcesRule(this);

  private Compilation compilation;

  @Before
  public void setUp() {
    compilation =
        javac()
            .withProcessors(new GlideAnnotationProcessor())
            .compile(
                forResource("EmptyLibraryModule1.java"), forResource("EmptyLibraryModule2.java"));
    assertThat(compilation).succeededWithoutWarnings();
  }

  @Test
  public void compilation_generatesAllExpectedFiles() {
    Truth.assertThat(compilation.generatedSourceFiles()).hasSize(1);
  }

  @Test
  public void compilation_generatesExpectedIndexerForModules() throws IOException {
    String expectedClassName =
        "GlideIndexer_GlideModule_com_bumptech_glide_test_EmptyLibraryModule1_com_bumptech_glide"
            + "_test_EmptyLibraryModule2";
    assertThat(compilation)
        .generatedSourceFile(annotation(expectedClassName))
        .hasSourceEquivalentTo(forResource(expectedClassName + ".java"));
  }

  private JavaFileObject forResource(String name) {
    return Util.forResource(getClass().getSimpleName(), name);
  }

  @Override
  public Compilation getCompilation() {
    return compilation;
  }
}