Back to Repositories

Testing Long Filename Handling in Annotation Processor for Glide

This test suite validates Glide’s annotation processor handling of extremely long file and package names during compilation. It ensures the system can properly process and generate files when dealing with names exceeding 255 characters, addressing a specific edge case documented in issue #4106.

Test Coverage Overview

The test suite focuses on validating the GlideAnnotationProcessor’s ability to handle edge cases involving extremely long file names.

Key areas covered include:
  • Processing of class names exceeding 255 characters
  • Successful compilation without warnings
  • File system operations with generated files
  • Verification of temporary file creation

Implementation Analysis

The testing approach utilizes JUnit4 framework with a systematic setup and verification process. The implementation employs Google’s compile-testing library to simulate the annotation processing environment.

Key patterns include:
  • Use of TemporaryFolder rule for file system testing
  • Compilation verification through CompilationSubject assertions
  • Dynamic file generation and validation

Technical Details

Testing tools and configuration:
  • JUnit4 test runner and framework
  • Google’s compile-testing library
  • GlideAnnotationProcessor for compilation
  • TemporaryFolder for file system operations
  • JavaFileObjects for source code generation

Best Practices Demonstrated

The test implementation showcases several testing best practices in annotation processor validation.

Notable practices include:
  • Proper test isolation using @Before setup
  • Clean separation of concerns with CompilationProvider interface
  • Robust error handling for file operations
  • Clear test method naming conventions
  • Effective use of JUnit rules for resource management

bumptech/glide

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

            
package com.bumptech.glide.annotation.compiler;

import static com.google.testing.compile.Compiler.javac;

import com.bumptech.glide.annotation.compiler.test.CompilationProvider;
import com.google.testing.compile.Compilation;
import com.google.testing.compile.CompilationSubject;
import com.google.testing.compile.JavaFileObjects;
import java.io.File;
import java.io.IOException;
import javax.tools.JavaFileObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Makes sure that we can handle indexers based on long package or file names, or many modules.
 *
 * <p>See #4106.
 */
@RunWith(JUnit4.class)
public class OverlyLongFileNameTest implements CompilationProvider {
  @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
  private Compilation compilation;
  private static final String FILE_NAME_LONGER_THAN_255_CHARS =
      "SomeReallyReallyRidiculouslyLongFileNameOrPackageNameIGuessThatExceedsTwoHundredAndFiftyFive"
          + "CharactersThoughThatsOnlyAroundOneHundredCharactersWhichMeansINeedToKeepTypingToGetTo"
          + "TwoHundredAndFiftyFiveSomehowThankfullyOnlyLikeFiftyToGoNowMaybeButNotQuiteYet"
          + "SomewhereAroundNowIsProbablyGood";

  @Before
  public void setUp() {
    compilation =
        javac()
            .withProcessors(new GlideAnnotationProcessor())
            .compile(
                JavaFileObjects.forSourceLines(
                    FILE_NAME_LONGER_THAN_255_CHARS,
                    "package com.bumptech.glide.test;",
                    "import com.bumptech.glide.annotation.GlideModule;",
                    "import com.bumptech.glide.module.LibraryGlideModule;",
                    "@GlideModule",
                    "public final class "
                        + FILE_NAME_LONGER_THAN_255_CHARS
                        + " extends LibraryGlideModule {}"));
  }

  @Test
  public void compilingLongClassAndOrPackageNameShouldSucceed() throws IOException {
    CompilationSubject.assertThat(compilation).succeededWithoutWarnings();
    for (JavaFileObject file : compilation.generatedFiles()) {
      temporaryFolder.create();
      String actualFileName = new File(file.getName()).getName();
      if (!actualFileName.startsWith(FILE_NAME_LONGER_THAN_255_CHARS)) {
        try {
          temporaryFolder.newFile(actualFileName).createNewFile();
        } catch (IOException e) {
          throw new RuntimeException("Failed to create: " + actualFileName, e);
        }
      }
    }
  }

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