View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.yaml.snakeyaml.issues.issue49;
18  
19  import java.util.Calendar;
20  import java.util.Date;
21  import java.util.GregorianCalendar;
22  import java.util.TimeZone;
23  
24  import junit.framework.TestCase;
25  
26  import org.yaml.snakeyaml.Yaml;
27  
28  public class CalendarTest extends TestCase {
29      /**
30       * Daylight Saving Time is not taken into account
31       */
32      public void testDumpDstIgnored() {
33          CalendarBean bean = new CalendarBean();
34          bean.setName("lunch");
35          Calendar cal = Calendar.getInstance();
36          cal.setTime(new Date(1000000000000L));
37          cal.setTimeZone(TimeZone.getTimeZone("GMT-8:00"));
38          bean.setCalendar(cal);
39          Yaml yaml = new Yaml();
40          String output = yaml.dumpAsMap(bean);
41          // System.out.println(output);
42          assertEquals("calendar: 2001-09-08T17:46:40-8:00\nname: lunch\n", output);
43          //
44          Yaml loader = new Yaml();
45          CalendarBean parsed = loader.loadAs(output, CalendarBean.class);
46          assertEquals(bean.getCalendar(), parsed.getCalendar());
47      }
48  
49      /**
50       * Daylight Saving Time is in effect on this date/time in
51       * America/Los_Angeles Daylight<br/>
52       * Saving Time is not in effect on this date/time in GMT
53       */
54      public void testDumpDstIsNotTheSame() {
55          check(1000000000000L, "America/Los_Angeles", "Must be 7 hours difference.",
56                  "2001-09-08T18:46:40-7:00");
57      }
58  
59      /**
60       * Daylight Saving Time is not in effect on this date/time in
61       * America/Los_Angeles Daylight<br/>
62       * Saving Time is not in effect on this date/time in GMT
63       */
64      public void testDumpDstIsTheSame() {
65          check(1266833741374L, "America/Los_Angeles", "Must be 8 hours difference.",
66                  "2010-02-22T02:15:41.374-8:00");
67      }
68  
69      /**
70       * Test odd time zone
71       */
72      public void testNepal() {
73          check(1266833741374L, "Asia/Katmandu", "Must be 5:45 hours difference.",
74                  "2010-02-22T16:00:41.374+5:45");
75      }
76  
77      public void testMoreThen10hours() {
78          check(1266833741374L, "Asia/Kamchatka", "Must be 12 hours difference.",
79                  "2010-02-22T22:15:41.374+12:00");
80      }
81  
82      private void check(long time, String timeZone, String warning, String etalon) {
83          CalendarBean bean = new CalendarBean();
84          bean.setName("lunch");
85          Calendar cal = Calendar.getInstance();
86          cal.setTime(new Date(time));
87          cal.setTimeZone(TimeZone.getTimeZone(timeZone));
88          bean.setCalendar(cal);
89          Yaml yaml = new Yaml();
90          String output = yaml.dumpAsMap(bean);
91          // System.out.println(output);
92          assertEquals(warning, "calendar: " + etalon + "\nname: lunch\n", output);
93          //
94          Yaml loader = new Yaml();
95          CalendarBean parsed = loader.loadAs(output, CalendarBean.class);
96          assertFalse("TimeZone must deviate.", bean.getCalendar().equals(parsed.getCalendar()));
97          assertEquals(bean.getCalendar().getTimeInMillis(), parsed.getCalendar().getTimeInMillis());
98      }
99  
100     public void testLoadBean() {
101         Yaml beanLoader = new Yaml();
102         CalendarBean bean = beanLoader.loadAs(
103                 "calendar:  2001-12-14t21:59:43.10-05:00\nname: dinner", CalendarBean.class);
104         assertEquals("dinner", bean.getName());
105         Calendar calendar = bean.getCalendar();
106         assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()),
107                 calendar.getTimeZone().getOffset(calendar.getTime().getTime()));
108         //
109         Yaml yaml = new Yaml();
110         Date date = (Date) yaml.load("2001-12-14t21:59:43.10-05:00");
111         assertEquals(date, calendar.getTime());
112     }
113 
114     public void testLoadWithTag() {
115         Yaml yaml = new Yaml();
116         GregorianCalendar calendar = (GregorianCalendar) yaml
117                 .load("!!java.util.GregorianCalendar 2001-12-14t21:59:43.10-05:00");
118         assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()),
119                 calendar.getTimeZone().getOffset(calendar.getTime().getTime()));
120         //
121         Date date = (Date) yaml.load("2001-12-14t21:59:43.10-05:00");
122         assertEquals(date, calendar.getTime());
123     }
124 }