Back to Repositories

Validating Annotation Processor Extensions in Glide

This test suite validates the Glide annotation processor’s behavior when processing a single extension type. It ensures proper generation of source files and verifies the correctness of the annotation compilation process in the Glide image loading library.

Test Coverage Overview

The test suite provides comprehensive coverage of the Glide annotation processor’s source file generation capabilities.

Key areas tested include:
  • Verification of the total number of generated source files
  • Individual validation of generated class files including GlideOptions, GlideRequest, GlideRequests, and GlideApp
  • Validation of module implementation and factory classes
  • Edge case handling for file generation and compilation

Implementation Analysis

The testing approach utilizes JUnit4 with custom compilation verification tools. The implementation employs a systematic pattern of compiling test resources and comparing generated sources against expected outputs.

Technical patterns include:
  • Custom compilation provider interface implementation
  • Resource regeneration rule usage
  • Source file equivalence testing
  • Annotation processing verification

Technical Details

Testing infrastructure includes:
  • JUnit4 test runner and framework
  • Custom RegenerateResourcesRule for resource management
  • Google Truth assertion library
  • JavaFileObject compilation and verification tools
  • Custom utility classes for resource handling
  • Annotation processor testing setup

Best Practices Demonstrated

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

Notable practices include:
  • Separation of setup and test execution using @Before
  • Granular test methods for specific functionality
  • Clear test method naming conventions
  • Proper resource management and cleanup
  • Comprehensive assertion coverage
  • Modular test organization

bumptech/glide

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

            
package com.bumptech.glide.annotation.compiler;

import static com.bumptech.glide.annotation.compiler.test.Util.appResource;
import static com.bumptech.glide.annotation.compiler.test.Util.emptyAppModule;
import static com.bumptech.glide.annotation.compiler.test.Util.glide;
import static com.bumptech.glide.annotation.compiler.test.Util.subpackage;
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.ReferencedResource;
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;

/** Verifies the output of the processor with a simple single extension type. */
@RunWith(JUnit4.class)
public class GlideExtensionWithTypeTest implements CompilationProvider {
  @Rule
  public final RegenerateResourcesRule regenerateResourcesRule = new RegenerateResourcesRule(this);

  private Compilation compilation;

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

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

  @Test
  public void compilation_generatesExpectedGlideOptionsClass() throws IOException {
    assertThat(compilation)
        .generatedSourceFile(subpackage("GlideOptions"))
        .hasSourceEquivalentTo(forResource("GlideOptions.java"));
  }

  @Test
  @ReferencedResource
  public void compilation_generatesExpectedGlideRequestClass() throws IOException {
    assertThat(compilation)
        .generatedSourceFile(subpackage("GlideRequest"))
        .hasSourceEquivalentTo(appResource("GlideRequest.java"));
  }

  @Test
  public void compilation_generatesExpectedGlideRequestsClass() throws IOException {
    assertThat(compilation)
        .generatedSourceFile(subpackage("GlideRequests"))
        .hasSourceEquivalentTo(forResource("GlideRequests.java"));
  }

  @Test
  @ReferencedResource
  public void compilationGeneratesExpectedGlideAppClass() throws IOException {
    assertThat(compilation)
        .generatedSourceFile(subpackage("GlideApp"))
        .hasSourceEquivalentTo(appResource("GlideApp.java"));
  }

  @Test
  @ReferencedResource
  public void compilation_generatesExpectedGeneratedAppGlideModuleImpl() throws IOException {
    assertThat(compilation)
        .generatedSourceFile(glide("GeneratedAppGlideModuleImpl"))
        .hasSourceEquivalentTo(appResource("GeneratedAppGlideModuleImpl.java"));
  }

  @Test
  @ReferencedResource
  public void compilation_generatesExpectedGeneratedRequestManagerFactory() throws IOException {
    assertThat(compilation)
        .generatedSourceFile(glide("GeneratedRequestManagerFactory"))
        .hasSourceEquivalentTo(appResource("GeneratedRequestManagerFactory.java"));
  }

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

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