package com.bumptech.glide.annotation.compiler;

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

import com.google.testing.compile.Compilation;
import com.google.testing.compile.JavaFileObjects;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
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 ExpectedException
@SuppressWarnings("ResultOfMethodCallIgnored")
@RunWith(JUnit4.class)
public class InvalidAppGlideModuleWithExcludesTest {
  @Rule public final ExpectedException expectedException = ExpectedException.none();

  @Test
  public void compilation_withMissingExcludedModuleClass_throws() {
    expectedException.expect(RuntimeException.class);
    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();
  }
}
