<?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>Format called from non-synchronized block</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    void bar() {
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>2, No call to format</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private final SimpleDateFormat sdf = new SimpleDateFormat();
    void bar() {
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>3, Inside synchronized, Not OK</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    synchronized void bar() {
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>4, Inside synchronized, OK</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    public void bar() {
        synchronized (sdf) {
            sdf.format();
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>5, Use DateFormat, not ok</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.DateFormat;

public class Foo {
    private static final DateFormat sdf = new DateFormat();
    synchronized void bar() {
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>5a, Use DateFormat, ok</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.DateFormat;

public class Foo {
    private static final DateFormat sdf = new DateFormat();
    void bar() {
        synchronized(sdf) {
            sdf.format();
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>6, Use DateFormat, fail</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.DateFormat;

public class Foo {
    private static final DateFormat sdf = new DateFormat();
    void bar() {
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#940 False positive on UnsynchronizedStaticDateFormatter</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Test {
    private static final DateFormat enFormat = new SimpleDateFormat("EEEE MMMM d, yyyy 'at' hh':'mma", Locale.US);
    private static final DateFormat frFormat = new SimpleDateFormat("EEEE 'le' d MMMM yyyy 'à' HH'h'mm", Locale.CANADA_FRENCH);

    protected DateFormat getDateFormat() {
        return "fr".equals(getLang()) ? frFormat : enFormat;
    }

    private String getLang() {
        return "fr";
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>NumberFormatters should be detected</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.DecimalFormat;

public class Foo {
    private static final DecimalFormat decimalFormat = new DecimalFormat();
    void bar() {
        decimalFormat.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>MessageFormatters should be detected</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.MessageFormat;

public class Foo {
    private static final MessageFormat messageFormat = new MessageFormat();
    void bar() {
        messageFormat.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1633 [java] UnsynchronizedStaticFormatter reports commons lang FastDateFormat</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import org.apache.commons.lang3.time.FastDateFormat;
import java.util.Date;

public class Foo {
    private static final FastDateFormat FDF = FastDateFormat.getInstance("dd.MM.yyyy HH:mm:ss");
    public void format() {
        FDF.format(new Date());
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1815 static synchronized, Not OK</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    synchronized void bar() { // not static
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1815 static synchronized, also Not OK</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    static synchronized void bar() { // not a synchronized blocks
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1815 static synchronized, OK if allowMethodLevelSynchronization</description>
        <rule-property name="allowMethodLevelSynchronization">true</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    static synchronized void bar() {
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1815 static synchronized, Not OK if allowMethodLevelSynchronization</description>
        <rule-property name="allowMethodLevelSynchronization">true</rule-property>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>6</expected-linenumbers>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    synchronized void bar() { // should be static
        sdf.format();
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1815 static synchronized, OK</description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    static void bar() {
        synchronized(sdf) {
            sdf.format();
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1815 static synchronized, Not OK</description>
        <expected-problems>1</expected-problems>
        <expected-linenumbers>7</expected-linenumbers>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    static void bar() {
        synchronized(Foo.class) { // not synchronized on sdf
            sdf.format();
        }
    }
}
        ]]></code>
    </test-code>

    <test-code>
        <description>#1903 - Block synch is valid even with allowMethodLevelSynchronization</description>
        <rule-property name="allowMethodLevelSynchronization">true</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
import java.text.SimpleDateFormat;

public class Foo {
    private static final SimpleDateFormat sdf = new SimpleDateFormat();
    private void bar() {
        synchronized(sdf) {
            sdf.format();
        }
    }
}
        ]]></code>
    </test-code>
    <test-code>
        <description> [java] False positive UnsynchronizedStaticFormatter in static initializer #2368 </description>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
            import java.text.NumberFormat;

            class WithFormatter {
                private static final NumberFormat formatter;

                static {
                    formatter = NumberFormat.getInstance();
                    formatter.setMaximumFractionDigits(2); // FALSE POSITIVE
                }
                // …
            }
        ]]></code>
    </test-code>
</test-data>
