Back to Repositories

Validating AppGlideModule Annotation Processing in Glide

This test suite validates the proper usage of @Excludes annotation in AppGlideModules within the Glide image loading library. It ensures that invalid configurations are properly caught and handled during compilation, preventing potential runtime issues.

Test Coverage Overview

The test suite provides comprehensive coverage of invalid @Excludes annotation scenarios in AppGlideModule implementations.

Key areas tested include:
  • Missing excluded module class handling
  • Empty @Excludes annotation validation
  • Non-GlideModule class exclusion attempts
Integration points focus on the annotation processor’s interaction with the Java compiler.

Implementation Analysis

The testing approach utilizes JUnit4 framework with Google’s compile-testing library to validate annotation processing during compilation.

Key patterns include:
  • Compilation validation using javac() processor
  • Source code generation via JavaFileObjects
  • Exception handling verification with assertThrows

Technical Details

Testing tools and configuration:
  • JUnit4 test runner
  • Google compile-testing library
  • GlideAnnotationProcessor for compilation verification
  • JavaFileObjects for test source generation
  • Custom RuntimeException validation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through systematic validation of annotation processing edge cases.

Notable practices include:
  • Isolated test cases for each failure scenario
  • Clear test method naming conventions
  • Proper exception handling verification
  • Comprehensive invalid use case coverage

bumptech/glide

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

            
package com.bumptech.glide.annotation.compiler;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
import static org.junit.Assert.assertThrows;

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Test;
import org.junit.function.ThrowingRunnable;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests AppGlideModules with invalid usages of the @Excludes annotation. */
// Ignore warnings since most methods use assertThrows
@SuppressWarnings("ResultOfMethodCallIgnored")
@RunWith(JUnit4.class)
public class InvalidAppGlideModuleWithExcludesTest {
  @Test
  public void compilation_withMissingExcludedModuleClass_throws() {
    assertThrows(
        RuntimeException.class,
        new ThrowingRunnable() {
          @Override
          public void run() throws Throwable {
            javac()
                .withProcessors(new GlideAnnotationProcessor())
                .compile(
                    JavaFileObjects.forSourceLines(
                        "AppModuleWithExcludes",
                        "package com.bumptech.glide.test;",
                        "import com.bumptech.glide.annotation.Excludes;",
                        "import com.bumptech.glide.annotation.GlideModule;",
                        "import com.bumptech.glide.module.AppGlideModule;",
                        "import com.bumptech.glide.test.EmptyLibraryModule;",
                        "@GlideModule",
                        "@Excludes(EmptyLibraryModule.class)",
                        "public final class AppModuleWithExcludes extends AppGlideModule {}"));
          }
        });
  }

  @Test
  public void compilation_withEmptyExcludes_fails() {
    Compilation compilation =
        javac()
            .withProcessors(new GlideAnnotationProcessor())
            .compile(
                JavaFileObjects.forSourceLines(
                    "AppModuleWithExcludes",
                    "package com.bumptech.glide.test;",
                    "import com.bumptech.glide.annotation.Excludes;",
                    "import com.bumptech.glide.annotation.GlideModule;",
                    "import com.bumptech.glide.module.AppGlideModule;",
                    "import com.bumptech.glide.test.EmptyLibraryModule;",
                    "@GlideModule",
                    "@Excludes",
                    "public final class AppModuleWithExcludes extends AppGlideModule {}"));
    assertThat(compilation).failed();
  }

  @Test
  public void compilation_withNonGlideModule_throws() {
    Compilation compilation =
        javac()
            .withProcessors(new GlideAnnotationProcessor())
            .compile(
                JavaFileObjects.forSourceLines(
                    "AppModuleWithExcludes",
                    "package com.bumptech.glide.test;",
                    "import com.bumptech.glide.annotation.Excludes;",
                    "import com.bumptech.glide.annotation.GlideModule;",
                    "import com.bumptech.glide.module.AppGlideModule;",
                    "import com.bumptech.glide.test.EmptyLibraryModule;",
                    "@GlideModule",
                    "@Excludes(Object.class)",
                    "public final class AppModuleWithExcludes extends AppGlideModule {}"));
    assertThat(compilation).failed();
  }
}