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