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