<?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>test is ok, ok</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        if (foo.length() == 0) {
            // this is bad
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.length is called, should have failed</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        if (foo.trim().length() == 0) {
            // this is bad
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.length not is called, ok</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        if (foo.trim().equals("")) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.length is called, should have failed</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        while (foo.trim().length() == 0) {
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.length is called, assigned to int, ok</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        int i = foo.trim().length();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.length is called, assigned to boolean, should have failed</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b = foo.trim().length() == 0;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Using trim.length to find the length and compare to 1, OK</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b = foo.trim().length() == 1;
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Passes trim().length() and 0 to another method</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b = foo(foo.trim().length(), 0);
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Compares the length against a mathematical function</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b = foo(foo.trim().length() == (2 - 1));
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim().isEmpty() is called, should have failed</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b = foo.trim().isEmpty();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim().isEmpty() is called after a chain call, should have failed</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>5,7</expected-linenumbers>
        <code><![CDATA[
import java.util.Arrays;
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b =  Arrays.toString(foo.toCharArray()).trim().isEmpty();
        int i = 2;
        b =  String.valueOf(i).trim().isEmpty();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim().isEmpty() is called after a chain call, should have failed twice</description>
        <expected-problems>2</expected-problems>
        <expected-linenumbers>5,6</expected-linenumbers>
        <code><![CDATA[
import java.util.Arrays;
public class Foo {
    void bar() {
        String foo = "foo";
        boolean b =  Arrays.toString(foo.toCharArray()).trim().isEmpty();
        b =  String.valueOf(2).trim().isEmpty();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Verify no NPE is thrown</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Foo extends SomeUnresolvedClass {
    void bar(String[] logLevels, String[] guardMethods) {
        if (super.guardStmtByLogLevel.isEmpty() && logLevels.length > 0 && guardMethods.length > 0) {
            configureGuards(logLevels, guardMethods);
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Avoid NPE for pattern matching instanceof</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class PatternMatchingInstanceof {
    private String s = "other string";

    public void test() {
        Object obj = "abc";
        if (obj instanceof String s) {
            System.out.println("a) obj == s: " + (obj == s)); // true
        }
    }
}
        ]]></code>
        <source-type>java 16</source-type>
    </test-code>

    <test-code>
        <description>[java] InefficientEmptyStringCheck false negative in anonymous class #1224</description>
        <expected-problems>1</expected-problems>
        <code><![CDATA[
            import javax.swing.*;
            import java.awt.event.KeyEvent;
            import java.awt.event.KeyListener;

            public class Main {
                public static void main(String[] args) {
                    final JPanel jPanel = new JPanel();
                    jPanel.addKeyListener(new KeyListener() {
                        @Override
                        public void keyTyped(KeyEvent e) {
                            final String val = "";
                            if (val.trim().length() > 0) {
                                System.out.println("Non-empty string.");
                            }
                        }

                        @Override
                        public void keyPressed(KeyEvent e) {
                        }

                        @Override
                        public void keyReleased(KeyEvent e) {
                        }
                    });
                }
            }
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.isEmpty is called on method result, should have failed</description>
        <expected-problems>4</expected-problems>
        <expected-linenumbers>7,10,15,18</expected-linenumbers>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.performance.inefficientemptystringcheck;
public class StringTrimIsEmpty {
    String get() {
        return "foo";
    }
    void bar() {
        if (get().trim().isEmpty()) {
            // violation
        }
        if (this.get().trim().isEmpty()) {
            // violation
        }

        String bar = get();
        if (bar.trim().isEmpty()) {
            // violation
        }
        if (bar.toString().trim().isEmpty()) {
            // violation
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.length is called on method result, should have failed</description>
        <expected-problems>4</expected-problems>
        <expected-linenumbers>7,10,15,18</expected-linenumbers>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.performance.inefficientemptystringcheck;
public class StringTrimLength {
    String get() {
        return "foo";
    }
    void bar() {
        if (get().trim().length() == 0) {
            // violation missing
        }
        if (this.get().trim().length() == 0) {
            // violation missing
        }

        String bar = get();
        if (bar.trim().length() == 0) {
            // violation already detected
        }
        if (bar.toString().trim().length() == 0) {
            // violation missing
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>String.trim.isEmpty and length called in method argument</description>
        <expected-problems>6</expected-problems>
        <expected-linenumbers>8,9,10,11,12,13</expected-linenumbers>
        <code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.performance.inefficientemptystringcheck;
public class StringTrimMethodArgument {
    public String get() {
        return "foo";
    }
    public void bar() {
        String bar = "foo";
        System.out.println(bar.trim().isEmpty()); // violation missing
        System.out.println(bar.trim().length() == 0);
        System.out.println(get().trim().isEmpty()); // violation missing
        System.out.println(get().trim().length() == 0); // violation missing
        System.out.println(this.get().trim().isEmpty()); // violation missing
        System.out.println(this.get().trim().length() == 0); // violating missing
    }
}
        ]]></code>
    </test-code>
</test-data>
