<?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>appending single character string, should fail</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("a");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>appending single char, should be ok</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append('a');
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>appending single char escaped</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("\n");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>this is probably wrong, but shouldn't fail</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("a" + "foo");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates a three character int</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append(123);
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates a string explicitly set to 1 character, not explicitly checking right now</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        String str = "a";
        sb.append(str);
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>for statement</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        for(int ix = 0; ix < 100; ix++)
            sb.append("a");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates an escaped character</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("\t");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates all escaped characters</description>
        <expected-problems>8</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("\n");
        sb.append("\t");
        sb.append("\b");
        sb.append("\r");
        sb.append("\f");
        sb.append("\\");
        sb.append("\'");
        sb.append("\"");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates a single upper case</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("A");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates a single number</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("3");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates a single character &amp;</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("&");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>concatenates two characters</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("/t");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>a single octal character</description>
        <expected-problems>2</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("\12");
        sb.append("\123");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>octal character in longer string</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb) {
        sb.append("\1234");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>append a single character as part of an expression (#2275)</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    public void bar(StringBuffer sb, int length) {
        sb.append("a".repeat(length));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>append single character string in constructor chain</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>3,4</expected-linenumbers>
        <code><![CDATA[
public class Foo {
    public void bar() {
        StringBuilder sb = new StringBuilder().append("a");
        sb = new StringBuilder().append("c");
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>false positive with string as constructor arg in StringBuilder method chain</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    private String name = "bar";
    public String toString() {
        StringBuilder sb = new StringBuilder("#").append("Foo").append('#').append(this.name);
        return sb.toString();
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description>[java] StringBuilder/Buffer false negatives with typeres #2881</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
            package net.sourceforge.pmd.lang.java.types.testdata;

            public class DummyCompiledClass {
                public String toString() {
                    StringBuilder sb = new StringBuilder();
                    int start = sb.indexOf(" ");
                    if (start == -1) {
                        sb.append("Bar");
                        sb.append(" "); // warning expected: Avoid appending characters as strings in StringBuffer.append.
                    }
                    return sb.toString();
                }
            }
        ]]></code>
    </test-code>
    <test-code>
        <description>[java] StringBuilder/Buffer false negatives with typeres #2881 (countertest, no classpath)</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
            public class NoCompiledClass {
                public String toString() {
                    StringBuilder sb = new StringBuilder();
                    int start = sb.indexOf(" ");
                    if (start == -1) {
                        sb.append("Bar");
                        sb.append(" "); // warning expected: Avoid appending characters as strings in StringBuffer.append.
                    }
                    return sb.toString();
                }
            }
            ]]></code>
    </test-code>
</test-data>
