<?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">

    <code-fragment id="full-example"><![CDATA[
import java.util.*;
public class Foo {
    public static void bar() {
        boolean a, b = true;
        try { // total 8
            if (true) { // 2
                List buz = new ArrayList();
            }

            for(int i = 0; i < 19; i++) { // 3
                List buz = a ? new ArrayList() : null;
            }
        } catch(Exception e) {
            if (true) { // 2
                e.printStackTrace();
            }
        }

        int j = 0;
        if (true || a && b) { // 4
            j = 10;
            return;
        } else {
            j = 12;
        }

        j = a || b ? j + 1 : j; // 3

        while (j++ < 20) { // 2
            List buz = new ArrayList();
        }

        switch(j) { // 7
        case 1:
        case 2: break;
        case 3: j = 5; break;
        case 4: if (b && a) { bar(); } break;
        default: break;
        }

        do { // 3
            List buz = new ArrayList();
        } while (a && j++ < 30);

        if (b) { // 2
            return;
        }
    }
}
]]></code-fragment>

    <test-code>
        <description>Full example</description>
        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>The method 'bar()' has an NPath complexity of 8064, current threshold is 200</message>
        </expected-messages>
        <code-ref id="full-example"/>
    </test-code>

    <test-code>
        <description>Test default report level - report 200</description>
        <rule-property name="reportLevel">1</rule-property>
        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>The method 'bar()' has an NPath complexity of 200, current threshold is 1</message>
        </expected-messages>
        <code><![CDATA[
public class Foo {
    public static void bar() {
        boolean a, b = true;
        int j = 0;

        switch (j) { // 5
        case 0: case 1: case 2: case 3: case 4: break;
        }

        switch (j) { // 5
        case 0: case 1: case 2: case 3: case 4: break;
        }

        if (true || a && b); // * 4
        while (j++ < 20);    // * 2
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Test default report level - ignore 199</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
class Foo {
    void bar() {
        boolean a, b;
        try {
            switch(j) { // 7
            case 1:
            case 2: break;
            case 3: j = 5; break;
            case 4: if (b && a) { bar(); } break;
            default: break;
            }

            switch(j) { // * 7
            case 1:
            case 2: break;
            case 3: j = 5; break;
            case 4: if (b && a) { bar(); } break;
            default: break;
            }

            if (true || a || b); // * 4

        } catch (Exception e) {
            if (true || a); // + 3
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Boolean operators</description>
        <rule-property name="reportLevel">2</rule-property>
        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>The method 'bar()' has an NPath complexity of 4, current threshold is 2</message>
        </expected-messages>
        <code><![CDATA[
class Foo {
    void bar() {
        if (a && b || c);
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Switch stmt</description>
        <rule-property name="reportLevel">2</rule-property>
        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>The method 'bar()' has an NPath complexity of 9, current threshold is 2</message>
        </expected-messages>
        <code><![CDATA[
class Foo {
    void bar() {
        switch (j) { // 5
        case 0:
        case 1:
        case 3: if (a || b) {} break;
        }

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

    <code-fragment id="bug3484404"><![CDATA[
class Bar {
    public void x(boolean x, boolean y) {
        z((x ? 1 : 2), (y ? 3 : 4));
    }

    public int y(boolean x, boolean y) {
        return z((x ? 1 : 2), (y ? 3 : 4));
    }

    public int z(int x, int y) {
        return x + y;
    }
}
    ]]></code-fragment>

    <test-code>
        <description>test case for bug 3484404 (Invalid NPath calculation in return statement)</description>
        <rule-property name="reportLevel">4</rule-property>
        <expected-problems>2</expected-problems>
        <expected-messages>
            <message>The method 'x(boolean, boolean)' has an NPath complexity of 4, current threshold is 4</message>
            <message>The method 'y(boolean, boolean)' has an NPath complexity of 4, current threshold is 4</message>
        </expected-messages>
        <code-ref id="bug3484404"/>
    </test-code>

    <test-code>
        <description>NPath supports constructors</description>
        <rule-property name="reportLevel">6</rule-property>
        <expected-problems>1</expected-problems>
        <expected-messages>
            <message>The constructor 'Foo()' has an NPath complexity of 7, current threshold is 6</message>
        </expected-messages>
        <code><![CDATA[
class Foo {
    Foo() {
        boolean a, b;
        int j = 23;
        switch(j) {
            case 1:
            case 2: break;
            case 3: j = 5; break;
            case 4: if (b && a) { bar(); } break;
            default: break;
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>Switch arrow branch (#2625)</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Product {
    Product buildProduct(String title, Language language) {
        if (title.length() > 100 || title.length() == 0) {
            throw new IllegalArgumentException("Invalid title length (1-100 chars): " + title);
        }
        var product = new Product();
        var description = new MultiLangString();
        switch (language) {
            case FR -> description.set1(title);
            case IT -> description.set1(title);
            case EN -> description.set2(title);
            default -> description.set2(title);
        }
        product.setDescription(description);
        product.setMagicNumber(3);
        return product;
    }
    void setDescription(MultiLangString s) {}
    void setMagicNumber(int s) {}
    enum Language {FR,IT,EN}
    class MultiLangString { void set1(String s) {} void set2(String s) {}}
}
        ]]></code>
    </test-code>
    <test-code>
        <description>Switch expressions (#2625)</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
public class Product {
    Product buildProduct(String title, Language language) {
        if (title.length() > 100 || title.length() == 0) {
            throw new IllegalArgumentException("Invalid title length (1-100 chars): " + title);
        }
        var product = new Product();
        var description = switch (language) {
            case FR -> new Description.Fr(title);
            case IT -> new Description.Fr(title);
            case EN -> new Description.En(title);
            default -> new Description.En(title);
        };
        product.setDescription(description);
        product.setMagicNumber(3);
        return product;
    }
    void setDescription(Description s) {}
    void setMagicNumber(int s) {}
    enum Language {FR,IT,EN}
    static class Description { static class Fr{ Fr(String s){} } static class En{ En(String s){} }}
}
        ]]></code>
    </test-code>
</test-data>
