/*
 * Copyright 2018 The Error Prone 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 com.google.errorprone.bugpatterns;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Unit tests for {@link AutoValueFinalMethods} bug pattern.
 *
 * @author bhagwani@google.com (Sumit Bhagwani)
 */
@RunWith(JUnit4.class)
public class AutoValueFinalMethodsTest {
  private final BugCheckerRefactoringTestHelper testHelper =
      BugCheckerRefactoringTestHelper.newInstance(new AutoValueFinalMethods(), getClass());
  private final CompilationTestHelper compilationHelper =
      CompilationTestHelper.newInstance(AutoValueFinalMethods.class, getClass());

  @Test
  public void testFinalAdditionToEqHcTs() {
    testHelper
        .addInputLines(
            "in/Test.java",
            "import com.google.auto.value.AutoValue;",
            "@AutoValue",
            "abstract class Test {",
            "  abstract String valueOne();",
            "  abstract String valueTwo();",
            "  static Test create(String valueOne, String valueTwo) {",
            "    return null;",
            "  }",
            "  @Override",
            "  public int hashCode() {",
            "    return 1;",
            "  }",
            "  @Override",
            "  public String toString() {",
            "    return \"Hakuna Matata\";",
            "  }",
            "  @Override",
            "  public boolean equals(Object obj) {",
            "    return true;",
            "  }",
            "}")
        .addOutputLines(
            "out/Test.java",
            "import com.google.auto.value.AutoValue;",
            "@AutoValue",
            "abstract class Test {",
            "  abstract String valueOne();",
            "  abstract String valueTwo();",
            "  static Test create(String valueOne, String valueTwo) {",
            "    return null;",
            "  }",
            "  @Override",
            "  public final int hashCode() {",
            "    return 1;",
            "  }",
            "  @Override",
            "  public final String toString() {",
            "    return \"Hakuna Matata\";",
            "  }",
            "  @Override",
            "  public final boolean equals(Object obj) {",
            "    return true;",
            "  }",
            "}")
        .doTest();
  }

  @Test
  public void testNegativeCases() {
    compilationHelper
        .addSourceLines(
            "out/Test.java",
            "import com.google.auto.value.AutoValue;",
            "import com.google.auto.value.extension.memoized.Memoized;",
            "@AutoValue",
            "abstract class Test {",
            "  abstract String valueOne();",
            "  abstract String valueTwo();",
            "  static Test create(String valueOne, String valueTwo) {",
            "    return null;",
            "  }",
            "  @Override",
            "  public abstract int hashCode(); ",
            "  @Override",
            "  @Memoized",
            "  public String toString() {",
            "    return \"Hakuna Matata\";",
            "  }",
            "  @Override",
            "  public final boolean equals(Object obj) {",
            "    return true;",
            "  }",
            "  private int privateNonEqTsHcMethod() {",
            "    return 2;",
            "  }",
            "  public final String publicFinalNonEqTsHcMethod() {",
            "    return \"Hakuna Matata\";",
            "  }",
            "  public boolean publicNonEqTsHcMethod(Object obj) {",
            "    return true;",
            "  }",
            "}")
        .doTest();
  }

  @Test
  public void testAbstractMemoizedNegativeCase() {
    compilationHelper
        .addSourceLines(
            "out/Test.java",
            "import com.google.auto.value.AutoValue;",
            "import com.google.auto.value.extension.memoized.Memoized;",
            "@AutoValue",
            "abstract class Test {",
            "  static Test create() {",
            "    return null;",
            "  }",
            "  @Override",
            "  @Memoized",
            "  public abstract int hashCode(); ",
            "}")
        .doTest();
  }

  @Test
  public void testDiagnosticString() {
    compilationHelper
        .addSourceLines(
            "out/Test.java",
            "import com.google.auto.value.AutoValue;",
            "import com.google.auto.value.extension.memoized.Memoized;",
            "@AutoValue",
            "abstract class Test {",
            "  static Test create() {",
            "    return null;",
            "  }",
            "  @Override",
            "  // BUG: Diagnostic contains: Make equals final in AutoValue classes",
            "  public boolean equals(Object obj) {",
            "    return true;",
            "  }",
            "}")
        .doTest();
  }
}

@@ -5521,11 +5521,824 @@
 t();%0A  %7D
+%0A%0A  @Test%0A  public void testDiagnosticStringWithMultipleMethodMatches() %7B%0A    compilationHelper%0A        .addSourceLines(%0A            %22out/Test.java%22,%0A            %22import com.google.auto.value.AutoValue;%22,%0A            %22import com.google.auto.value.extension.memoized.Memoized;%22,%0A            %22@AutoValue%22,%0A            %22abstract class Test %7B%22,%0A            %22  static Test create() %7B%22,%0A            %22    return null;%22,%0A            %22  %7D%22,%0A            %22  @Override%22,%0A            %22  // BUG: Diagnostic contains: Make equals, hashCode final in AutoValue classes%22,%0A            %22  public boolean equals(Object obj) %7B%22,%0A            %22    return true;%22,%0A            %22  %7D%22,%0A            %22  @Override%22,%0A            %22  public int hashCode() %7B%22,%0A            %22    return 1;%22,%0A            %22  %7D%22,%0A            %22%7D%22)%0A        .doTest();%0A  %7D
 %0A%7D%0A

Add test for Diagnostic String with multiple method matches for AutoValueFinalMethods check