#!/bin/sh
# Functional check of the installed jar: the usage examples of upstream's
# README.md (strict and safe patterns) and a few conversions.
set -e

WORK="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORK"
CP=/usr/share/java/strftime4j.jar

cat > Format.java <<'X'
import java.time.YearMonth;
import java.time.ZonedDateTime;
import ua.co.k.strftime.StrftimeFormatter;

public class Format {
    static void check(String expected, String got) {
        System.out.println(got);
        if (!expected.equals(got))
            throw new AssertionError("expected [" + expected + "], got [" + got + "]");
    }
    public static void main(String[] args) {
        ZonedDateTime t = ZonedDateTime.parse("2021-02-16T23:43:00-02:00");
        check("Printed on 02/16/2021",
              StrftimeFormatter.ofStrictPattern("Printed on %m/%d/%Y").format(t));
        check("Tue Feb 16 23:43:00 2021", StrftimeFormatter.ofStrictPattern("%c").format(t));
        check("TUESDAY -0200 047 23:43",
              StrftimeFormatter.ofStrictPattern("%^A %z %j %R").format(t));
        YearMonth ym = YearMonth.of(2020, 1);
        boolean failed = false;
        try {
            StrftimeFormatter.ofStrictPattern("Printed on %m/%d/%Y").format(ym);
        } catch (Exception e) {
            failed = true;
        }
        if (!failed) throw new AssertionError("strict pattern accepted a YearMonth");
        check("Printed on 01//2020",
              StrftimeFormatter.ofSafePattern("Printed on %m/%d/%Y").format(ym));
    }
}
X
javac -cp "$CP" -d . Format.java
java -cp ".:$CP" Format
echo "format: PASS"
