<?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 using ==</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>5,6</expected-linenumbers>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class UseEqualsToCompareStringsSample {
    void bar(String x) {
        if (x == "hello") {}
        if (x == new String("hello")) {}
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>failure case using !=</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>5</expected-linenumbers>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class UseEqualsToCompareStringsSample {
    void bar(String x) {
        if (x != "hello") {}
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>using equals, OK</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class UseEqualsToCompareStringsSample {
    void bar(String x) {
        if ("hello".equals(x)) {}
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>using compareTo, OK</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class UseEqualsToCompareStringsSample {
    void bar(String x) {
        if ( "yes".compareTo( x ) == 0 ) {}
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>using length, OK</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class UseEqualsToCompareStringsSample {
    void bar(String x) {
        if (x.length() == "DOCTYPE".length()) {}
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#2934 false negatives with fields</description>
        <expected-problems>8</expected-problems>
        <expected-linenumbers>8,9,10,11,13,14,15,16</expected-linenumbers>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class ClassWithStringFields {
    private String string1 = "a";
    private String string2 = "a";

    public void bar() {
        if (string1 == string2) { }  // violation
        if (string1 == this.string2) { } // violation
        if (this.string1 == string2) { } // violation
        if (this.string1 == this.string2) { } // violation

        if (string1 != string2) { } // violation
        if (string1 != this.string2) { } // violation
        if (this.string1 != string2) { } // violation
        if (this.string1 != this.string2) { } // violation

        if (string1.equals(string2)) { } // ok
        if (this.string1.equals(string2)) { } // ok
        if (string1.equals(this.string2)) { } // ok
        if (this.string1.equals(this.string2)) { } // ok
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#2934 comparison with null is valid</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.errorprone.useequalstocomparestrings;

public class ClassWithStringFields {
    private String string1 = "a";
    private String string2 = "a";

    public void bar() {
        if (string1 == null) { } // ok
        if (this.string1 == null) { } // ok
    }

    public void bar(String param) {
        if (param != null) { } // ok
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#3004 UseEqualsToCompareStrings false positive with PMD 6.30.0</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class O {
    boolean f(String s) {
        return s.charAt(0) == s.charAt(1);
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#2979 UseEqualsToCompareStrings: FP with "var" variables</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.util.regex.Matcher;

public class O {
    boolean f(String s) {
        final Matcher matcher = null;
        if (matcher.matches()) {

            final var firstString = matcher.group("a");
            final var secondString = matcher.group("b");

            if (firstString.isEmpty() != secondString.isEmpty()) { // <- violation
                // ...
            }
        }
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>#2979 UseEqualsToCompareStrings: FP with "var" variables (control, types are explicit)</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.util.regex.Matcher;

public class O {
    boolean f(String s) {
        final Matcher matcher = null;
        if (matcher.matches()) {

            final String firstString = matcher.group("a");
            final String secondString = matcher.group("b");

            if (firstString.isEmpty() != secondString.isEmpty()) { // <- violation
                // ...
            }
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>False positive with string concatentation</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {{
    String a = "b" + "c";
}}
        ]]></code>
    </test-code>

    <test-code>
        <description>[java] UseEqualsToCompareStrings should consider return type #3843</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    String getLiteral() {
        return "hello";
    }
    void useEqualsToCompareStrings(String x) {
        if (x == getLiteral()) {} // a false negative should report
    }
}
]]></code>
    </test-code>
</test-data>
