Home | History | Annotate | Download | only in codegen
      1 package dagger.internal.codegen;
      2 
      3 import com.google.common.collect.ImmutableList;
      4 import com.google.testing.compile.JavaFileObjects;
      5 import javax.tools.JavaFileObject;
      6 import org.junit.Test;
      7 import org.junit.runner.RunWith;
      8 import org.junit.runners.JUnit4;
      9 
     10 import static com.google.common.truth.Truth.assertAbout;
     11 import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
     12 
     13 @RunWith(JUnit4.class)
     14 public class RepeatedModuleValidationTest {
     15   private static final JavaFileObject MODULE_FILE =
     16       JavaFileObjects.forSourceLines(
     17           "test.TestModule",
     18           "package test;",
     19           "",
     20           "import dagger.Module;",
     21           "",
     22           "@Module",
     23           "final class TestModule {}");
     24 
     25   @Test
     26   public void moduleRepeatedInSubcomponentFactoryMethod() {
     27     JavaFileObject subcomponentFile =
     28         JavaFileObjects.forSourceLines(
     29             "test.TestSubcomponent",
     30             "package test;",
     31             "",
     32             "import dagger.Subcomponent;",
     33             "",
     34             "@Subcomponent(modules = TestModule.class)",
     35             "interface TestSubcomponent {",
     36             "}");
     37     JavaFileObject componentFile =
     38         JavaFileObjects.forSourceLines(
     39             "test.TestComponent",
     40             "package test;",
     41             "",
     42             "import dagger.Component;",
     43             "",
     44             "@Component(modules = TestModule.class)",
     45             "interface TestComponent {",
     46             "  TestSubcomponent newTestSubcomponent(TestModule module);",
     47             "}");
     48     assertAbout(javaSources())
     49         .that(ImmutableList.of(MODULE_FILE, subcomponentFile, componentFile))
     50         .processedWith(new ComponentProcessor())
     51         .failsToCompile()
     52         .withErrorContaining("This module is present in test.TestComponent.")
     53         .in(componentFile)
     54         .onLine(7)
     55         .atColumn(51);
     56   }
     57 
     58   @Test
     59   public void moduleRepeatedInSubcomponentBuilderMethod() {
     60     JavaFileObject subcomponentFile =
     61         JavaFileObjects.forSourceLines(
     62             "test.TestSubcomponent",
     63             "package test;",
     64             "",
     65             "import dagger.Subcomponent;",
     66             "",
     67             "@Subcomponent(modules = TestModule.class)",
     68             "interface TestSubcomponent {",
     69             "  @Subcomponent.Builder",
     70             "  interface Builder {",
     71             "    Builder testModule(TestModule testModule);",
     72             "    TestSubcomponent build();",
     73             "  }",
     74             "}");
     75     JavaFileObject componentFile =
     76         JavaFileObjects.forSourceLines(
     77             "test.TestComponent",
     78             "package test;",
     79             "",
     80             "import dagger.Component;",
     81             "",
     82             "@Component(modules = TestModule.class)",
     83             "interface TestComponent {",
     84             "  TestSubcomponent.Builder newTestSubcomponentBuilder();",
     85             "}");
     86     assertAbout(javaSources())
     87         .that(ImmutableList.of(MODULE_FILE, subcomponentFile, componentFile))
     88         .processedWith(new ComponentProcessor())
     89         .compilesWithoutError();
     90     // TODO(gak): assert about the warning when we have that ability
     91   }
     92 
     93   @Test
     94   public void moduleRepeatedButNotPassed() {
     95     JavaFileObject subcomponentFile =
     96         JavaFileObjects.forSourceLines(
     97             "test.TestSubcomponent",
     98             "package test;",
     99             "",
    100             "import dagger.Subcomponent;",
    101             "",
    102             "@Subcomponent(modules = TestModule.class)",
    103             "interface TestSubcomponent {",
    104             "}");
    105     JavaFileObject componentFile =
    106         JavaFileObjects.forSourceLines(
    107             "test.TestComponent",
    108             "package test;",
    109             "",
    110             "import dagger.Component;",
    111             "",
    112             "@Component(modules = TestModule.class)",
    113             "interface TestComponent {",
    114             "  TestSubcomponent newTestSubcomponent();",
    115             "}");
    116     assertAbout(javaSources())
    117         .that(ImmutableList.of(MODULE_FILE, subcomponentFile, componentFile))
    118         .processedWith(new ComponentProcessor())
    119         .compilesWithoutError();
    120   }
    121 }
    122