package org.sfm.map.column.joda;

import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Test;
import org.sfm.csv.CsvColumnDefinition;
import java.util.TimeZone;

import static org.junit.Assert.*;

public class JodaHelperTest {

    @Test
    public void testFormatterFailWhenEmpty() {
        try {
            JodaHelper.getDateTimeFormatter(CsvColumnDefinition.IDENTITY);
            fail();
        } catch(IllegalArgumentException e) {

        }
    }

    @Test
    public void testFormatterFromString() {
        final DateTimeFormatter yyyyMMdd = JodaHelper.getDateTimeFormatter(CsvColumnDefinition.IDENTITY.addDateFormat("yyyyMMdd"));
        final long instant = System.currentTimeMillis();
        assertEquals(DateTimeFormat.forPattern("yyyyMMdd").print(instant), yyyyMMdd.print(instant));
        assertEquals(DateTimeZone.getDefault(), yyyyMMdd.getZone());
    }

    @Test
    public void testFormatterFromFormatter() {
        final DateTimeFormatter yyyyMMdd = JodaHelper.getDateTimeFormatter(CsvColumnDefinition.IDENTITY.add(new JodaDateTimeFormatterProperty(DateTimeFormat.forPattern("MMddyyyy"))));
        final long instant = System.currentTimeMillis();
        assertEquals(DateTimeFormat.forPattern("MMddyyyy").print(instant), yyyyMMdd.print(instant));
        assertEquals(DateTimeZone.getDefault(), yyyyMMdd.getZone());

    }

    @Test
    public void testFormatterFromFormatterWithOwnTZ() {
        final DateTimeFormatter yyyyMMdd = JodaHelper.getDateTimeFormatter(CsvColumnDefinition.IDENTITY.add(new JodaDateTimeFormatterProperty(DateTimeFormat.forPattern("ddMMyyyy").withZone(DateTimeZone.forID("America/Chicago")))));
        final long instant = System.currentTimeMillis();
        assertEquals(DateTimeFormat.forPattern("ddMMyyyy").print(instant), yyyyMMdd.print(instant));
        assertEquals(DateTimeZone.forID("America/Chicago"), yyyyMMdd.getZone());
    }


    @Test
    public void testFormatterFromFormatterWithSpecifiedTZ() {
        final DateTimeFormatter yyyyMMdd = JodaHelper.getDateTimeFormatter(CsvColumnDefinition.IDENTITY.add(new JodaDateTimeFormatterProperty(DateTimeFormat.forPattern("ddMMyyyy").withZone(DateTimeZone.forID("America/Chicago")))).addTimeZone(TimeZone.getTimeZone("America/New_York")));
        final long instant = System.currentTimeMillis();
        assertEquals(DateTimeFormat.forPattern("ddMMyyyy").print(instant), yyyyMMdd.print(instant));
        assertEquals(DateTimeZone.forID("America/New_York"), yyyyMMdd.getZone());
    }
    @Test
    public void testGetDateTimeZoneWithNone() {
        assertEquals(null, JodaHelper.getDateTimeZone(CsvColumnDefinition.IDENTITY));
    }

    @Test
    public void testGetDateTimeZoneFromTimeZone() {
        assertEquals(DateTimeZone.forID("America/Chicago"), JodaHelper.getDateTimeZone(CsvColumnDefinition.IDENTITY.addTimeZone(TimeZone.getTimeZone("America/Chicago"))));
    }

    @Test
    public void testGetDateTimeZoneFromDateTimeZone() {
        assertEquals(DateTimeZone.forID("America/Chicago"), JodaHelper.getDateTimeZone(CsvColumnDefinition.IDENTITY.add(new JodaDateTimeZoneProperty(DateTimeZone.forID("America/Chicago")))));
    }

}
@@ -306,24 +306,201 @@
 lperTest %7B%0A%0A
+    private static final DateTimeZone CHICAGO_TZ = DateTimeZone.forID(%22America/Chicago%22);%0A    private static final DateTimeZone NY_TZ = DateTimeZone.forID(%22America/New_York%22);%0A%0A
     @Test%0A  
@@ -1872,45 +1872,18 @@
 one(
-DateTimeZone.forID(%22America/Chicago%22)
+CHICAGO_TZ
 ))))
@@ -1992,32 +1992,53 @@
 ern(%22ddMMyyyy%22).
+withZone(CHICAGO_TZ).
 print(instant), 
@@ -2088,45 +2088,18 @@
 als(
-DateTimeZone.forID(%22America/Chicago%22)
+CHICAGO_TZ
 , yy
@@ -2390,45 +2390,18 @@
 one(
-DateTimeZone.forID(%22America/Chicago%22)
+CHICAGO_TZ
 ))).
@@ -2568,24 +2568,40 @@
 %22ddMMyyyy%22).
+withZone(NY_TZ).
 print(instan
@@ -2655,46 +2655,13 @@
 als(
-DateTimeZone.forID(%22America/New_York%22)
+NY_TZ
 , yy
@@ -2923,45 +2923,18 @@
 als(
-DateTimeZone.forID(%22America/Chicago%22)
+CHICAGO_TZ
 , Jo
@@ -3141,45 +3141,18 @@
 als(
-DateTimeZone.forID(%22America/Chicago%22)
+CHICAGO_TZ
 , Jo
@@ -3242,45 +3242,18 @@
 rty(
-DateTimeZone.forID(%22America/Chicago%22)
+CHICAGO_TZ
 ))))

fix test tz issue