View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.issues.issue49;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  import java.util.GregorianCalendar;
21  import java.util.TimeZone;
22  
23  import junit.framework.TestCase;
24  
25  import org.yaml.snakeyaml.Yaml;
26  
27  public class CalendarTest extends TestCase {
28      /**
29       * Daylight Saving Time is not taken into account
30       */
31      public void testDumpDstIgnored() {
32          CalendarBean bean = new CalendarBean();
33          bean.setName("lunch");
34          Calendar cal = Calendar.getInstance();
35          cal.setTime(new Date(1000000000000L));
36          cal.setTimeZone(TimeZone.getTimeZone("GMT-8:00"));
37          bean.setCalendar(cal);
38          Yaml yaml = new Yaml();
39          String output = yaml.dumpAsMap(bean);
40          // System.out.println(output);
41          assertEquals("calendar: 2001-09-08T17:46:40-8:00\nname: lunch\n", output);
42          //
43          Yaml loader = new Yaml();
44          CalendarBean parsed = loader.loadAs(output, CalendarBean.class);
45          assertEquals(bean.getCalendar(), parsed.getCalendar());
46      }
47  
48      /**
49       * Daylight Saving Time is in effect on this date/time in
50       * America/Los_Angeles Daylight<br/>
51       * Saving Time is not in effect on this date/time in GMT
52       */
53      public void testDumpDstIsNotTheSame() {
54          check(1000000000000L, "America/Los_Angeles", "Must be 7 hours difference.",
55                  "2001-09-08T18:46:40-7:00");
56      }
57  
58      /**
59       * Daylight Saving Time is not in effect on this date/time in
60       * America/Los_Angeles Daylight<br/>
61       * Saving Time is not in effect on this date/time in GMT
62       */
63      public void testDumpDstIsTheSame() {
64          check(1266833741374L, "America/Los_Angeles", "Must be 8 hours difference.",
65                  "2010-02-22T02:15:41.374-8:00");
66      }
67  
68      /**
69       * Test odd time zone
70       */
71      public void testNepal() {
72          check(1266833741374L, "Asia/Katmandu", "Must be 5:45 hours difference.",
73                  "2010-02-22T16:00:41.374+5:45");
74      }
75  
76      public void testMoreThen10hours() {
77          check(1266833741374L, "Asia/Kamchatka", "Must be 12 hours difference.",
78                  "2010-02-22T22:15:41.374+12:00");
79      }
80  
81      private void check(long time, String timeZone, String warning, String etalon) {
82          CalendarBean bean = new CalendarBean();
83          bean.setName("lunch");
84          Calendar cal = Calendar.getInstance();
85          cal.setTime(new Date(time));
86          cal.setTimeZone(TimeZone.getTimeZone(timeZone));
87          bean.setCalendar(cal);
88          Yaml yaml = new Yaml();
89          String output = yaml.dumpAsMap(bean);
90          // System.out.println(output);
91          assertEquals(warning, "calendar: " + etalon + "\nname: lunch\n", output);
92          //
93          Yaml loader = new Yaml();
94          CalendarBean parsed = loader.loadAs(output, CalendarBean.class);
95          assertFalse("TimeZone must deviate.", bean.getCalendar().equals(parsed.getCalendar()));
96          assertEquals(bean.getCalendar().getTimeInMillis(), parsed.getCalendar().getTimeInMillis());
97      }
98  
99      public void testLoadBean() {
100         Yaml beanLoader = new Yaml();
101         CalendarBean bean = beanLoader.loadAs(
102                 "calendar:  2001-12-14t21:59:43.10-05:00\nname: dinner", CalendarBean.class);
103         assertEquals("dinner", bean.getName());
104         Calendar calendar = bean.getCalendar();
105         assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()),
106                 calendar.getTimeZone().getOffset(calendar.getTime().getTime()));
107         //
108         Yaml yaml = new Yaml();
109         Date date = (Date) yaml.load("2001-12-14t21:59:43.10-05:00");
110         assertEquals(date, calendar.getTime());
111     }
112 
113     public void testLoadWithTag() {
114         Yaml yaml = new Yaml();
115         GregorianCalendar calendar = (GregorianCalendar) yaml
116                 .load("!!java.util.GregorianCalendar 2001-12-14t21:59:43.10-05:00");
117         assertEquals(TimeZone.getTimeZone("GMT-5:00").getOffset(calendar.getTime().getTime()),
118                 calendar.getTimeZone().getOffset(calendar.getTime().getTime()));
119         //
120         Date date = (Date) yaml.load("2001-12-14t21:59:43.10-05:00");
121         assertEquals(date, calendar.getTime());
122     }
123 }