• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2008, SnakeYAML
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package examples.jodatime;
15 
16 import java.util.Date;
17 import junit.framework.TestCase;
18 import org.joda.time.DateMidnight;
19 import org.joda.time.DateTime;
20 import org.joda.time.DateTimeZone;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.constructor.AbstractConstruct;
23 import org.yaml.snakeyaml.constructor.Construct;
24 import org.yaml.snakeyaml.constructor.Constructor;
25 import org.yaml.snakeyaml.nodes.Node;
26 import org.yaml.snakeyaml.nodes.NodeId;
27 import org.yaml.snakeyaml.nodes.Tag;
28 
29 public class JodaTimeExampleTest extends TestCase {
30 
31   private static final long timestamp = 1000000000000L;
32 
testDump()33   public void testDump() {
34     DateTime time = new DateTime(timestamp, DateTimeZone.UTC);
35     Yaml yaml = new Yaml(new JodaTimeRepresenter());
36     String joda = yaml.dump(time);
37     String date = new Yaml().dump(new Date(timestamp));
38     assertEquals(date, joda);
39     assertEquals("2001-09-09T01:46:40Z\n", joda);
40   }
41 
testLoad()42   public void testLoad() {
43     Yaml yaml = new Yaml(new JodaTimeImplicitContructor());
44     DateTime time = yaml.load("2001-09-09T01:46:40Z");
45     assertEquals(new DateTime(timestamp, DateTimeZone.UTC), time);
46   }
47 
48   /**
49    * test issue 109
50    */
test109()51   public void test109() {
52     Date someDate = new DateMidnight(9, 2, 21, DateTimeZone.forID("Europe/Amsterdam")).toDate();
53     Yaml yaml = new Yaml();
54     String timestamp = yaml.dump(someDate);
55     assertEquals("0009-02-22T23:40:28Z\n", timestamp);
56     // System.out.println(timestamp);
57     Object o = yaml.load(timestamp);
58     assertEquals(someDate, o);
59   }
60 
61   class JodaPropertyConstructor extends Constructor {
62 
JodaPropertyConstructor()63     public JodaPropertyConstructor() {
64       yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
65     }
66 
67     class TimeStampConstruct extends Constructor.ConstructScalar {
68 
69       @Override
construct(Node nnode)70       public Object construct(Node nnode) {
71         if (nnode.getTag().equals("tag:yaml.org,2002:timestamp")) {
72           Construct dateConstructor = yamlConstructors.get(Tag.TIMESTAMP);
73           Date date = (Date) dateConstructor.construct(nnode);
74           return new DateTime(date, DateTimeZone.UTC);
75         } else {
76           return super.construct(nnode);
77         }
78       }
79     }
80   }
81 
82   /**
83    * This class should be used if JodaTime may appear with a tag or as a JavaBean property
84    */
85   public class JodaTimeConstructor extends Constructor {
86 
87     private final Construct javaDateConstruct;
88     private final Construct jodaDateConstruct;
89 
JodaTimeConstructor()90     public JodaTimeConstructor() {
91       javaDateConstruct = new ConstructYamlTimestamp();
92       jodaDateConstruct = new ConstructJodaTimestamp();
93       // Whenever we see an explicit timestamp tag, make a Joda Date
94       // instead
95       yamlConstructors.put(Tag.TIMESTAMP, jodaDateConstruct);
96       // See
97       // We need this to work around implicit construction.
98       yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
99     }
100 
101     public class ConstructJodaTimestamp extends AbstractConstruct {
102 
construct(Node node)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 
111       @Override
construct(Node nnode)112       public Object construct(Node nnode) {
113         if (nnode.getTag().equals(Tag.TIMESTAMP)) {
114           return jodaDateConstruct.construct(nnode);
115         } else {
116           return super.construct(nnode);
117         }
118       }
119     }
120   }
121 }
122