<?xml version="1.0" encoding="UTF-8"?>
<test-data
    xmlns="http://pmd.sourceforge.net/rule-tests"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">

    <test-code>
        <description>failure case</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
class Foo {
    private void bar(String x) {}
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Explicit receiver parameters are ignored</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    private void test(@AnnotatedUsage Foo this) {
    }
}
@interface AnnotatedUsage {}
        ]]></code>
    </test-code>

    <test-code>
        <description>method called on param</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    private void foo (String s) {
        String str = s.toString();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>assignment to param</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
class Foo {
    private void bar(int s) {
        s = 2;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>interface</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public interface Foo {
    void foo(int x);
}
        ]]></code>
    </test-code>

    <code-fragment id="public-methods"><![CDATA[
class Foo {
    public void bar(int s) {}
}
    ]]></code-fragment>

    <test-code>
        <description>don't flag public methods by default</description>
        <expected-problems>0</expected-problems>
        <code-ref id="public-methods"/>
    </test-code>

    <test-code>
        <description>flag public methods if checkall property is set</description>
        <rule-property name="checkAll">true</rule-property>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>2</expected-linenumbers>
        <code-ref id="public-methods"/>
    </test-code>

    <test-code>
        <description>skip native/abstract methods</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    public native void bar(int s);
    protected abstract void foo(Long s);
}
        ]]></code>
    </test-code>

    <test-code>
        <description>anonymous inner class npe</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    void bar() {
        Runnable a  = new Runnable() {
            public void run() {}
        };
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>unused constructor param</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
class Foo {
    public Foo(int s) {}
}
        ]]></code>
    </test-code>

    <test-code>
        <description>assigned but not used</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
class Foo {
    private void foo (String s) {
        s = "hello";
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>array element is set</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    private void foo (String[] s) {
        s[2] = "hello";
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>unused array in constructor</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
class Foo {
    int type;
    public Foo (int type, String[] s) {
        this.type = type;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>unused array in method</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
class Foo {
    private final void setLang(final String lang[]){
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>False - ! if "checkAll" property is not set</description>
        <rule-property name="checkAll">true</rule-property>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar2(Object paramA, Object paramB)
    {
        paramA.hashCode();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>violation suppression regex works</description>
        <rule-property name="violationSuppressRegex">.*'paramB'.*</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    private void bar2(Object paramA, Object paramB)
    {
        paramA.hashCode();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>violation suppression xpath works, by name</description>
        <rule-property name="violationSuppressXPath">.[@Name = 'paramB']</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    private void bar2(Object paramA, Object paramB)
    {
        paramA.hashCode();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>violation suppression xpath works, by type</description>
        <rule-property name="violationSuppressXPath">.[pmd-java:typeIs('java.lang.String')]</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    private void bar2(Object paramA, String paramB)
    {
        paramA.hashCode();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#878 don't flag abstract methods even if checkall property is set when checking an abstract class</description>
        <rule-property name="checkAll">true</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
abstract class Foo {
    protected abstract void foo(Long s);
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1159 false positive UnusedFormalParameter readObject(ObjectInputStream) if not used</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.io.*;
public class Foo {
    private void readObject(ObjectInputStream stream) throws InvalidObjectException{
       throw new InvalidObjectException("Proxy required");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1159 correct UnusedFormalParameter for readObject(foo) if not used</description>
        <rule-property name="checkAll">true</rule-property>
        <expected-problems>5</expected-problems>
        <code><![CDATA[import java.io.*;

public class Foo {
    // throws declaration is missing - so not the serialization readObject method
    private void readObject(ObjectInputStream stream) {
       throw new InvalidObjectException("Proxy required");
    }
    // argument is not a ObjectInputStream
    private void readObject(Foo a) throws InvalidObjectException {
       throw new InvalidObjectException("Proxy required");
    }
    // two arguments - two violations
    private void readObject(ObjectInputStream stream, Object o) throws InvalidObjectException{
       throw new InvalidObjectException("Proxy required");
    }
}
class Other {
    // it's public
    public void readObject(ObjectInputStream stream) throws InvalidObjectException {
        throw new InvalidObjectException("Proxy required");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1456 UnusedFormalParameter should ignore overriding methods</description>
        <rule-property name="checkAll">true</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
abstract class Base{
  abstract public int badMethod(int arg1, String arg2);
}

class Imp1 extends Base {
  @Override
  public int badMethod(int arg1, String arg2) {
    return arg2.length();
  }
}

class Imp2 extends Base {
  @Override
  public int badMethod(int arg1, String arg2) {
    return arg2.length() + arg1;
  }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#2957 Unused rules should ignore some names</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>2,3</expected-linenumbers>
        <code><![CDATA[
class Imp1 {
  private void m(int arg1,
                String arg2,
                int unusedArg1,
                int ignoredArg2,
                int unused,
                String ignored) {}
}
        ]]></code>
    </test-code>

    <test-code>
        <description>anonymous classes defined in methods should be considered</description>
        <rule-property name="checkAll">false</rule-property>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>4,12</expected-linenumbers>
        <code><![CDATA[
class Impl {
    public void m1() {
        class Local {
            private void m2(int arg1) { }
            private void m3() { m2(0); }
        }
        Local l = new Local();
        l.m3();
    }
    public void m4() {
        Object o = new Object() {
            private void m5(int arg1) { }
            public boolean equals(Object other) { m5(0); return false; }
        };
        o.equals(o);
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>[java] UnusedFormalParameter false-positive with qualified super ctor call #2937</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public final class SuperClassTest {

  private SuperClassTest() {
    throw new IllegalStateException("Private constructor.");
  }

  public static void main(String[] args) {
    Outer x = new Outer();
    ChildOfInner a = new ChildOfInner(x);
    ChildOfInner b = new ChildOfInner(x);
    System.out.println(b.getSecret());
    a.setSecret(6);
    System.out.println(b.getSecret());
  }
}

class Outer {
  int secret = 5;

  class Inner {
    int getSecret() {
      return secret;
    }

    void setSecret(int s) {
      secret = s;
    }
  }
}

class ChildOfInner extends Outer.Inner {
  ChildOfInner(Outer x) {
    x.super();  // FP here
  }
}
        ]]></code>
    </test-code>
</test-data>
