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;
17  
18  import java.sql.Date;
19  import java.sql.Timestamp;
20  
21  import junit.framework.TestCase;
22  
23  import org.yaml.snakeyaml.DumperOptions.FlowStyle;
24  
25  public class JavaBeanTimeStampTest extends TestCase {
26      public void testLoadDefaultJavaSqlTimestamp() {
27          JavaBeanWithSqlTimestamp javaBeanToDump = new JavaBeanWithSqlTimestamp();
28          Timestamp stamp = new Timestamp(1000000000000L);
29          javaBeanToDump.setTimestamp(stamp);
30          Date date = new Date(1001376000000L);
31          javaBeanToDump.setDate(date);
32          DumperOptions options = new DumperOptions();
33          options.setDefaultFlowStyle(FlowStyle.BLOCK);
34          Yaml yaml = new Yaml(options);
35          String dumpStr = yaml.dump(javaBeanToDump);
36          assertEquals(
37                  "!!org.yaml.snakeyaml.JavaBeanWithSqlTimestamp\ndate: 2001-09-25T00:00:00Z\ntimestamp: 2001-09-09T01:46:40Z\n",
38                  dumpStr);
39          Yaml loader = new Yaml();
40          JavaBeanWithSqlTimestamp javaBeanToLoad = loader.loadAs(dumpStr,
41                  JavaBeanWithSqlTimestamp.class);
42          assertEquals(stamp, javaBeanToLoad.getTimestamp());
43          assertEquals(date, javaBeanToLoad.getDate());
44      }
45  
46      public void testLoadDefaultJavaSqlTimestampNoGlobalTag() {
47          JavaBeanWithSqlTimestamp javaBeanToDump = new JavaBeanWithSqlTimestamp();
48          Timestamp stamp = new Timestamp(1000000000000L);
49          javaBeanToDump.setTimestamp(stamp);
50          Date date = new Date(1001376000000L);
51          javaBeanToDump.setDate(date);
52          Yaml yaml = new Yaml();
53          String dumpStr = yaml.dumpAsMap(javaBeanToDump);
54          assertEquals("date: 2001-09-25T00:00:00Z\ntimestamp: 2001-09-09T01:46:40Z\n", dumpStr);
55          Yaml loader = new Yaml();
56          JavaBeanWithSqlTimestamp javaBeanToLoad = loader.loadAs(dumpStr,
57                  JavaBeanWithSqlTimestamp.class);
58          assertEquals(stamp, javaBeanToLoad.getTimestamp());
59          assertEquals(date, javaBeanToLoad.getDate());
60      }
61  }