Back to Repositories

Validating LibraryGlideModule Compilation and Indexer Generation in Glide

This test suite validates the compilation and generation of source files for an empty LibraryGlideModule in the Glide image loading library. It ensures proper annotation processing and indexer generation for library modules integration.

Test Coverage Overview

The test suite covers the compilation and source file generation for empty LibraryGlideModule implementations.

Key areas tested include:
  • Successful compilation without warnings
  • Generation of expected number of source files
  • Correct indexer class generation with expected naming patterns
  • Resource file handling and compilation verification

Implementation Analysis

The testing approach utilizes JUnit4 framework with custom compilation verification tools. The tests employ a systematic pattern of compilation setup and verification, using specialized utilities for resource handling and annotation processing verification.

Key implementation aspects:
  • Custom CompilationProvider interface implementation
  • RegenerateResourcesRule for resource management
  • Annotation processor testing with javac compiler

Technical Details

Testing tools and configuration:
  • JUnit4 test runner and annotations
  • Google Truth assertion library
  • Custom GlideAnnotationProcessor
  • JavaFileObject utilities for resource handling
  • Compilation verification through CompilationSubject assertions

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through organized and systematic verification approaches.

Notable practices include:
  • Clear test setup separation using @Before
  • Specific test cases for different aspects of compilation
  • Resource management through custom rules
  • Comprehensive compilation result verification
  • Clean separation of concerns in test structure

bumptech/glide

annotation/compiler/test/src/test/java/com/bumptech/glide/annotation/compiler/EmptyLibraryGlideModuleTest.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 a single {@link com.bumptech.glide.module.LibraryGlideModule} in a project. */
@RunWith(JUnit4.class)
public class EmptyLibraryGlideModuleTest implements CompilationProvider {
  @Rule
  public final RegenerateResourcesRule regenerateResourcesRule = new RegenerateResourcesRule(this);

  private static final String MODULE_NAME = "EmptyLibraryModule.java";
  private Compilation compilation;

  @Before
  public void setUp() {
    compilation =
        javac().withProcessors(new GlideAnnotationProcessor()).compile(forResource(MODULE_NAME));
    assertThat(compilation).succeededWithoutWarnings();
  }

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

  @Test
  public void compilation_generatesExpectedIndexer() throws IOException {
    String expectedClassName =
        "GlideIndexer_GlideModule_com_bumptech_glide_test_EmptyLibraryModule";
    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;
  }
}