package com.alibaba.json.bvt.parser.deser.date;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import junit.framework.TestCase;

import org.junit.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.serializer.CalendarCodec;


public class DateParseTest9 extends TestCase {
    public void test_date() throws Exception {
        String text = "\"/Date(1242357713797+0800)/\"";
        Date date = JSON.parseObject(text, Date.class);
        Assert.assertEquals(date.getTime(), 1242357713797L);
        
        Assert.assertEquals(JSONToken.LITERAL_INT, CalendarCodec.instance.getFastMatchToken());

        text = "\"/Date(1242357713797+0545)/\"";
        date = JSON.parseObject(text, Date.class);
        Assert.assertEquals(date.getTime(), 1242357713797L);
        Assert.assertEquals(JSONToken.LITERAL_INT, CalendarCodec.instance.getFastMatchToken());
    }
    
    public void test_error() throws Exception {
        Exception error = null;
        try {
            JSON.parseObject("{\"date\":\"/Date(1242357713797A0800)/\"}", VO.class);
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }
    
    public void test_error_1() throws Exception {
        Exception error = null;
        try {
            JSON.parseObject("{\"date\":\"/Date(1242357713797#0800)/\"}", VO.class);
        } catch (Exception ex) {
            error = ex;
        }
        Assert.assertNotNull(error);
    }

    public void test_dates_different_timeZones() {
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));
        Date now = cal.getTime();

        VO vo = new VO();
        vo.date = now;

        String json = JSON.toJSONString(vo);
        VO result = JSON.parseObject(json, VO.class);
        assertEquals(vo.date, result.date);
    }

    public static class VO {
        public Date date;
    }
}

@@ -92,16 +92,41 @@
 l.Date;%0A
+import java.util.Random;%0A
 import j
@@ -183,34 +183,8 @@
 e;%0A%0A
-import org.junit.Assert;%0A%0A
 impo
@@ -317,56 +317,535 @@
 ec;%0A
-%0A%0Apublic class DateParseTest9 extends TestCase %7B
+import com.alibaba.fastjson.serializer.SerializerFeature;%0A%0A%0Apublic class DateParseTest9 extends TestCase %7B%0A%0A    private static Random random = new Random();%0A    private TimeZone original = TimeZone.getDefault();%0A    private String%5B%5D zoneIds = TimeZone.getAvailableIDs();%0A%0A    @Override%0A    public void setUp() %7B%0A        int index = random.nextInt(zoneIds.length);%0A        TimeZone.setDefault(TimeZone.getTimeZone(zoneIds%5Bindex%5D));%0A    %7D%0A%0A    @Override%0A    public void tearDown () %7B%0A        TimeZone.setDefault(original);%0A    %7D%0A
 %0A   
@@ -1000,39 +1000,32 @@
 class);%0A        
-Assert.
 assertEquals(dat
@@ -1063,39 +1063,32 @@
        %0A        
-Assert.
 assertEquals(JSO
@@ -1253,39 +1253,32 @@
 class);%0A        
-Assert.
 assertEquals(dat
@@ -1311,31 +1311,24 @@
 L);%0A        
-Assert.
 assertEquals
@@ -1653,39 +1653,32 @@
       %7D%0A        
-Assert.
 assertNotNull(er
@@ -1954,15 +1954,8 @@
     
-Assert.
 asse
@@ -2078,35 +2078,8 @@
 nce(
-TimeZone.getTimeZone(%22IST%22)
 );%0A 
@@ -2304,16 +2304,217 @@
 t.date);
+%0A%0A        // with iso-format%0A        json = JSON.toJSONString(vo, SerializerFeature.UseISO8601DateFormat);%0A        result = JSON.parseObject(json, VO.class);%0A        assertEquals(vo.date, result.date);
 %0A    %7D%0A%0A

add support to run tests with different timeZones