<?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>basic test</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>3</expected-linenumbers>
        <code><![CDATA[
public class PrimitiveType {
    public void downCastPrimitiveType() {
        if(i==1) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>basic test, part2</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class PrimitiveType {
    public static final int PRIMITIVE_TYPE = 1;
    public void downCastPrimitiveType() {
        if(i==PRIMITIVE_TYPE) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>allow null literal</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class MyClass {
    public void myMethod(Object foo) {
        if(foo != null) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1213 AvoidLiteralsInIfCondition -- switch for integer comparison with 0</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(String aString) {
        if( ( flags & Flag.IMPORTANT ) != 0 ) {}
        if (aString.indexOf(DOT) != -1) {}     // magic number -1, by default ignored
    }
    private static final String DOT = ".";
    public static class Flag {
        public static final int IMPORTANT = 0x1;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1213 AvoidLiteralsInIfCondition -- switch for integer comparison with 0.0</description>
        <rule-property name="ignoreMagicNumbers">-1,0,0.0</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(double aDouble) {
        if (aDouble > 0.0) {}                  // magic number 0.0
        if (aDouble >= Double.MIN_VALUE) {}    // preferred approach
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>XPath error: An empty sequence is not allowed as the second argument of index-of()</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(double aDouble) {
        if (true && aDouble > 0) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#388 False positive due to space in property list</description>
        <rule-property name="ignoreMagicNumbers"><![CDATA[-1,0,1, 0.0]]></rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    void bar(double num) {
        if (num == 0.0) {
            return Math.signum(num) == 1 ? IEEEclass.PositiveZero : IEEEclass.NegativeZero;
        }
    }
    public static enum IEEEclass { PositiveZero, NegativeZero; }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#2140 [java] AvoidLiteralsInIfCondition: false negative for expressions</description>
        <rule-property name="ignoreExpressions">false</rule-property>
        <expected-problems>4</expected-problems>
        <expected-linenumbers>3,3,4,5</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void bar(int a) {
        if (a > 3 + 5) {}
        if (b == -5) {}
        if (true && b == -5) {}
        if (c == -1) {}
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>More tests with expressions (see #2150)</description>
        <rule-property name="ignoreExpressions">false</rule-property>
        <rule-property name="ignoreMagicNumbers">-1,0,1</rule-property>
        <expected-problems>8</expected-problems>
        <expected-linenumbers>4,7,7,8,9,10,11,11</expected-linenumbers>
        <code><![CDATA[
public class AvoidLiteralsInIfConditionWithExpressions {
    public void test(String currentToken, int bodyStart, String s) {
        if (1) {}    // ok, "1" is in ignoreMagicNumbers
        if (1+1) {}  // not ok! multiple literals in expression
        if (a+1) {}  // ok, single literal, whitelisted
        if (bodyStart >= 0 && bodyStart != (currentToken.length() - 1)) {} // ok, single literal per expression, both whitelisted
        if (1 * 5) {} // not ok - literal 5 and also a expression with two literals
        if (a + 5) {} // not ok
        if (i == a + 5) {} // not ok - literal 5
        if (i == 1 + 5) {} // not ok - expression with two literals
        if (s.equals("Prefix" + "Suffix")) {}
    }
}
        ]]></code>
    </test-code>

    <code-fragment id="code-for-4514"><![CDATA[
public class AvoidLiteralsInIfCondition {
    private static final int MY_CONSTANT = 1;
    public void test(int i, String s) {
        if ("test".equals(s)) {}      // expected violation is missing (false negative)
        if (i == 1) {}                // expected violation

        // the following literals should be ignored because ignoreExpression=true
        if (i == MY_CONSTANT && "test".equals(s)) {} // violation for "test" only  (false positive)
        if (i == 1 && "test".equals(s)) {}           // violation for "test" (false positive)
    }
}
]]></code-fragment>

    <test-code>
        <description>[java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=true #4514</description>
        <rule-property name="ignoreExpressions">true</rule-property>
        <rule-property name="ignoreMagicNumbers">-1,0</rule-property>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>4,5</expected-linenumbers>
        <code-ref id="code-for-4514"/>
    </test-code>

    <test-code>
        <description>[java] AvoidLiteralsInIfCondition false positive and negative for String literals when ignoreExpressions=false #4514</description>
        <rule-property name="ignoreExpressions">false</rule-property>
        <rule-property name="ignoreMagicNumbers">-1,0</rule-property>
        <expected-problems>5</expected-problems>
        <expected-linenumbers>4,5,8,9,9</expected-linenumbers>
        <code-ref id="code-for-4514"/>
    </test-code>
</test-data>
