/*
 * Copyright 2014 Google Inc. All Rights Reserved.
 *
 * 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.matchers;

import static org.junit.Assert.assertTrue;

import com.google.errorprone.Scanner;
import com.google.errorprone.VisitorState;

import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.MemberSelectTree;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * @author deminguyen@google.com (Demi Nguyen)
 */
@RunWith(JUnit4.class)
public class NonNullLiteralTest extends CompilerBasedAbstractTest {
  
  @Test
  public void shouldMatchPrimitiveLiterals() {
    writeFile("A.java",
        "public class A {",
        "  public int getInt() {",
        "    return 2;",
        "  }",
        "  public long getLong() {",
        "    return 3L;",
        "  }",
        "  public float getFloat() {",
        "    return 4.0f;",
        "  }",
        "  public double getDouble() {",
        "    return 5.0d;",
        "  }",
        "  public boolean getBool() {",
        "    return true;",
        "  }",
        "  public char getChar() {",
        "    return 'c';",
        "  }",
        "  public String getString() {",
        "    return \"test string\";",
        "  }",
        "}"
      );
      assertCompiles(nonNullLiteralMatches(true, Matchers.nonNullLiteral()));
  }
  
  @Test
  public void shouldMatchClassLiteral() {
    writeFile("A.java",
        "import java.lang.reflect.Type;",
        "public class A {",
        "  public void testClassLiteral() {",
        "    Type klass = String.class;",
        "  }",
        "}"
      );
      assertCompiles(nonNullLiteralMatches(true, Matchers.nonNullLiteral()));
  }
  
  @Test
  public void shouldNotMatchClassDeclaration() {
    writeFile("A.java",
        "public class A {",
        "  protected class B {",
        "    private class C {",
        "    }",
        "  }",
        "}"
    );
    assertCompiles(nonNullLiteralMatches(false, Matchers.nonNullLiteral()));
  }
  
  @Test
  public void shouldNotMatchMemberAccess() {
    writeFile("A.java",
        "package com.google;",
        "public class A {",
        "  public String stringVar;",
        "  public void testMemberAccess() {",
        "    this.stringVar = new String();",
        "  }",
        "}"
    );
    
    writeFile("B.java",
        "import com.google.A;",
        "public class B {",
        "  public void testInstanceMemberAccess() {",
        "    A foo = new A();",
        "    foo.stringVar = new String();",
        "  }",
        "}"
    );
    assertCompiles(nonNullLiteralMatches(false, Matchers.nonNullLiteral()));
  }
  
  private Scanner nonNullLiteralMatches(
      final boolean shouldMatch, final Matcher<ExpressionTree> toMatch) {
    return new Scanner() {
      @Override
      public Void visitLiteral(LiteralTree node, VisitorState visitorState) {
        assertTrue(node.toString(), !shouldMatch ^ toMatch.matches(node, visitorState));
        return super.visitLiteral(node, visitorState);
      }
      
      @Override
      public Void visitMemberSelect(MemberSelectTree node, VisitorState visitorState) {
        assertTrue(node.toString(), !shouldMatch ^ toMatch.matches(node, visitorState));
        return super.visitMemberSelect(node, visitorState);
      }
      
    };
  }
}
@@ -773,24 +773,72 @@
 itorState;%0A%0A
+import com.sun.source.tree.CompilationUnitTree;%0A
 import com.s
@@ -3280,19 +3280,17 @@
 ));%0A  %7D%0A
-  
 %0A
+
   privat
@@ -3665,31 +3665,25 @@
 e);%0A      %7D%0A
-      
 %0A
+
       @Overr
@@ -3934,24 +3934,163 @@
    %7D%0A      %0A
+      @Override%0A      public Void visitCompilationUnit(CompilationUnitTree node, VisitorState visitorState) %7B%0A        return null;%0A      %7D%0A
     %7D;%0A  %7D%0A%7D
@@ -4088,9 +4088,10 @@
  %7D;%0A  %7D%0A
-
 %7D
+%0A

Prepare NonNullLiteralTest for javac9.