/*
 * Copyright (C) 2015 The Dagger Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dagger.internal.codegen;

import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;

import com.google.common.collect.ImmutableList;
import com.google.testing.compile.JavaFileObjects;
import javax.tools.JavaFileObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class RepeatedModuleValidationTest {
  private static final JavaFileObject MODULE_FILE =
      JavaFileObjects.forSourceLines(
          "test.TestModule",
          "package test;",
          "",
          "import dagger.Module;",
          "",
          "@Module",
          "final class TestModule {}");

  @Test
  public void moduleRepeatedInSubcomponentFactoryMethod() {
    JavaFileObject subcomponentFile =
        JavaFileObjects.forSourceLines(
            "test.TestSubcomponent",
            "package test;",
            "",
            "import dagger.Subcomponent;",
            "",
            "@Subcomponent(modules = TestModule.class)",
            "interface TestSubcomponent {",
            "}");
    JavaFileObject componentFile =
        JavaFileObjects.forSourceLines(
            "test.TestComponent",
            "package test;",
            "",
            "import dagger.Component;",
            "",
            "@Component(modules = TestModule.class)",
            "interface TestComponent {",
            "  TestSubcomponent newTestSubcomponent(TestModule module);",
            "}");
    assertAbout(javaSources())
        .that(ImmutableList.of(MODULE_FILE, subcomponentFile, componentFile))
        .processedWith(new ComponentProcessor())
        .failsToCompile()
        .withErrorContaining("TestModule is present in test.TestComponent.")
        .in(componentFile)
        .onLine(7)
        .atColumn(51);
  }

  @Test
  public void moduleRepeatedInSubcomponentBuilderMethod() {
    JavaFileObject subcomponentFile =
        JavaFileObjects.forSourceLines(
            "test.TestSubcomponent",
            "package test;",
            "",
            "import dagger.Subcomponent;",
            "",
            "@Subcomponent(modules = TestModule.class)",
            "interface TestSubcomponent {",
            "  @Subcomponent.Builder",
            "  interface Builder {",
            "    Builder testModule(TestModule testModule);",
            "    TestSubcomponent build();",
            "  }",
            "}");
    JavaFileObject componentFile =
        JavaFileObjects.forSourceLines(
            "test.TestComponent",
            "package test;",
            "",
            "import dagger.Component;",
            "",
            "@Component(modules = TestModule.class)",
            "interface TestComponent {",
            "  TestSubcomponent.Builder newTestSubcomponentBuilder();",
            "}");
    assertAbout(javaSources())
        .that(ImmutableList.of(MODULE_FILE, subcomponentFile, componentFile))
        .processedWith(new ComponentProcessor())
        .compilesWithoutError();
    // TODO(gak): assert about the warning when we have that ability
  }

  @Test
  public void moduleRepeatedButNotPassed() {
    JavaFileObject subcomponentFile =
        JavaFileObjects.forSourceLines(
            "test.TestSubcomponent",
            "package test;",
            "",
            "import dagger.Subcomponent;",
            "",
            "@Subcomponent(modules = TestModule.class)",
            "interface TestSubcomponent {",
            "}");
    JavaFileObject componentFile =
        JavaFileObjects.forSourceLines(
            "test.TestComponent",
            "package test;",
            "",
            "import dagger.Component;",
            "",
            "@Component(modules = TestModule.class)",
            "interface TestComponent {",
            "  TestSubcomponent newTestSubcomponent();",
            "}");
    assertAbout(javaSources())
        .that(ImmutableList.of(MODULE_FILE, subcomponentFile, componentFile))
        .processedWith(new ComponentProcessor())
        .compilesWithoutError();
  }
}
