<?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>May be final</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>3</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        System.out.println(a); // a is used here
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Final local var</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        final int a = 0;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#4511 Unused, no violation</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Assigned</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        a = 100;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Compound assignment</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        a += 100;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Initialized local vars</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>3,4</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        int b = 0;
        System.out.println("a=" + a + " b=" + b);
    }
}
        ]]></code>
    </test-code>

    <test-code disabled="true">
        <description>#3122 Blank uninitialized local var</description>
        <expected-problems>3</expected-problems>
        <expected-linenumbers>3,4,5</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        int b = 0;
        int c ;
        c = a + b;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Prefix increment</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        ++a;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>TEST7</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        a+=1;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Postfix increment</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        a++;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>TEST9</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void test1() {
        int a = 0;
        int b = 0;
        a++;
        a+=b;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Bug 2614040 : false + if a += assignment operator is used inside a method call.</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
/**
 * See: https://sourceforge.net/tracker/?func=detail&aid=2614040&group_id=56262&atid=479921
 */
public class Foo {
    public String bar() {
        int count = 1;
        final String wrong = String.valueOf( count += 2); // 'count' is used so it's a false +
        return wrong;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Bug #1047 False Positive in 'for' loops for LocalVariableCouldBeFinal in 5.0.1</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar() {
        for ( final String c : strings ) {
            System.out.println(c); // use c
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
       <description>Verify another case for Bug #1047 False Positive in 'for' loops for LocalVariableCouldBeFinal in 5.0.1</description>
       <expected-problems>0</expected-problems>
       <code><![CDATA[
import org.junit.Assert;
public class Test {
    public void bar(java.util.List<java.util.List<Integer>> result) {
        for (final java.util.List<Integer> list : result) {
            Assert.assertEquals( list.size() - 1, list.size() );
        }
    }
}
       ]]></code>
    </test-code>

    <test-code>
        <description>Parameter to ignore non-final variables in for each loops (see #1513).</description>
        <rule-property name="ignoreForEachDecl">true</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar() {
        for ( String c : strings ) {
            System.out.println(c); // use c
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>By default, for-each loops should be flagged (see #1513).</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>3</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void bar() {
        for ( String c : strings ) {
            System.out.println(c); // use c
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Normal for-loops should not be flagged</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar() {
        for (int i = 0; i < 10; i++ ) {
            System.out.println(i);
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#3266 False negative with default methods in interfaces</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>3</expected-linenumbers>
        <code><![CDATA[
public interface InterfaceWithDefaultMethod {
    default void bar() {
        String a = "a";
        System.out.println(a);
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#3266 False negative with class inside interface</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>4</expected-linenumbers>
        <code><![CDATA[
public interface InterfaceWithClass {
    class Inner {
        void bar() {
            String a = "a";
            System.out.println(a);
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#3266 False negative with anonymous classes</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>5</expected-linenumbers>
        <code><![CDATA[
public class ClassWithAnon {
    void method() {
        final ClassWithAnon anon = new ClassWithAnon() {
            void method() {
                String a = "a";
                System.out.println(a);
            }
        };
        final Runnable runnable = new Runnable() {
            public void run() {
                int a = 0; //unused so no warning
            }
        };
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#3266 False negative with lambdas</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>4</expected-linenumbers>
        <code><![CDATA[
public class ClassWithLambda {
    void method() {
        final Runnable runnable = () -> {
            int a = 0;
            System.out.println(a);
        };
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#1619 FP with loop variable</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class ClassWithLambda {
    void method() {
        for (int i = 0, size = loaders.size(); i < size; ++i);
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#3122 should consider blank variables</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>12,24</expected-linenumbers>
        <code><![CDATA[
public class ClassWithLambda {
    void method() {
        int a = 2; // nowarn
        a = 4;
        System.out.println(a);

        int b; // nowarn
        b = 1 + 2;
        b = 4;
        System.out.println(b);

        int c; // warn
        c = 1 + 2;
        System.out.println(c);

        int x;
        if (getClass().isAnonymousClass()) {
            x = 2;
        } else {
            x = 4;
        }
        // x is unused

        int y; // warn
        if (getClass().isAnonymousClass()) {
            y = 2;
        } else {
            y = 4;
        }
        System.out.println(y);
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#3122 should consider blank variables</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>5</expected-linenumbers>
        <code><![CDATA[
public class ClassWithLambda {

    void expandKey(byte key[]) {
        int octet;
        byte ek[] = new byte[128];

        octet = key[0];
        if ((octet & 0x80) != 0) {
            ek[104] |=  4; ek[122] |= 32;
        }

        octet = key[1];
        if ((octet & 0x80) != 0) {
            ek[  1] |=  8; ek[  8] |= 32; ek[ 17] |=  1;
            ek[ 24] |= 16; ek[ 35] |=  4; ek[ 50] |=  1;
            ek[ 57] |= 16; ek[ 67] |=  8; ek[ 83] |=  1;
            ek[ 88] |=  1; ek[ 98] |=  4; ek[105] |= 32;
            ek[114] |= 32; ek[123] |=  2;
        }
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description> [java] LocalVariableCouldBeFinal false positive with try/catch #5046 </description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.util.Optional;
import foo.FileWatcher;
public class ClassWithLambda {
    private Optional<FileWatcher> createFileWatcher() {
        Optional<FileWatcher> optionalFileWatcher;  // false positive in PMD 7.2.0, cannot be final
        try {
            optionalFileWatcher = Optional.of(new FileWatcher());
        } catch (final IOException e) {
            optionalFileWatcher = Optional.empty();
        }
        return optionalFileWatcher;
    }

}
        ]]></code>
    </test-code>
</test-data>
