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 examples.jodatime;
17  
18  import java.util.Date;
19  
20  import junit.framework.TestCase;
21  
22  import org.joda.time.DateMidnight;
23  import org.joda.time.DateTime;
24  import org.joda.time.DateTimeZone;
25  import org.yaml.snakeyaml.Yaml;
26  import org.yaml.snakeyaml.constructor.AbstractConstruct;
27  import org.yaml.snakeyaml.constructor.Construct;
28  import org.yaml.snakeyaml.constructor.Constructor;
29  import org.yaml.snakeyaml.nodes.Node;
30  import org.yaml.snakeyaml.nodes.NodeId;
31  import org.yaml.snakeyaml.nodes.Tag;
32  
33  public class JodaTimeExampleTest extends TestCase {
34      private static final long timestamp = 1000000000000L;
35  
36      public void testDump() {
37          DateTime time = new DateTime(timestamp, DateTimeZone.UTC);
38          Yaml yaml = new Yaml(new JodaTimeRepresenter());
39          String joda = yaml.dump(time);
40          String date = new Yaml().dump(new Date(timestamp));
41          assertEquals(date, joda);
42          assertEquals("2001-09-09T01:46:40Z\n", joda);
43      }
44  
45      public void testLoad() {
46          Yaml yaml = new Yaml(new JodaTimeImplicitContructor());
47          DateTime time = (DateTime) yaml.load("2001-09-09T01:46:40Z");
48          assertEquals(new DateTime(timestamp, DateTimeZone.UTC), time);
49      }
50  
51      /**
52       * test issue 109
53       */
54      public void test109() {
55          Date someDate = new DateMidnight(9, 2, 21, DateTimeZone.forID("Europe/Amsterdam")).toDate();
56          Yaml yaml = new Yaml();
57          String timestamp = yaml.dump(someDate);
58          assertEquals("0009-02-22T23:40:28Z\n", timestamp);
59          // System.out.println(timestamp);
60          Object o = yaml.load(timestamp);
61          assertEquals(someDate, o);
62      }
63  
64      class JodaPropertyConstructor extends Constructor {
65          public JodaPropertyConstructor() {
66              yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
67          }
68  
69          class TimeStampConstruct extends Constructor.ConstructScalar {
70              @Override
71              public Object construct(Node nnode) {
72                  if (nnode.getTag().equals("tag:yaml.org,2002:timestamp")) {
73                      Construct dateConstructor = yamlConstructors.get(Tag.TIMESTAMP);
74                      Date date = (Date) dateConstructor.construct(nnode);
75                      return new DateTime(date, DateTimeZone.UTC);
76                  } else {
77                      return super.construct(nnode);
78                  }
79              }
80          }
81      }
82  
83      /**
84       * This class should be used if JodaTime may appear with a tag or as a
85       * JavaBean property
86       */
87      public class JodaTimeConstructor extends Constructor {
88          private final Construct javaDateConstruct;
89          private final Construct jodaDateConstruct;
90  
91          public JodaTimeConstructor() {
92              javaDateConstruct = new ConstructYamlTimestamp();
93              jodaDateConstruct = new ConstructJodaTimestamp();
94              // Whenever we see an explicit timestamp tag, make a Joda Date
95              // instead
96              yamlConstructors.put(Tag.TIMESTAMP, jodaDateConstruct);
97              // See
98              // We need this to work around implicit construction.
99              yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
100         }
101 
102         public class ConstructJodaTimestamp extends AbstractConstruct {
103             public Object construct(Node node) {
104                 Date date = (Date) javaDateConstruct.construct(node);
105                 return new DateTime(date, DateTimeZone.UTC);
106             }
107         }
108 
109         class TimeStampConstruct extends Constructor.ConstructScalar {
110             @Override
111             public Object construct(Node nnode) {
112                 if (nnode.getTag().equals(Tag.TIMESTAMP)) {
113                     return jodaDateConstruct.construct(nnode);
114                 } else {
115                     return super.construct(nnode);
116                 }
117             }
118         }
119     }
120 }