1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.action.impl;
17  
18  import static java.util.Calendar.SEPTEMBER;
19  
20  import java.sql.Time;
21  import java.sql.Timestamp;
22  import java.text.DateFormat;
23  import java.util.Calendar;
24  import java.util.Date;
25  
26  import org.seasar.cubby.action.FormatPattern;
27  import org.seasar.extension.unit.S2TestCase;
28  
29  public class FormatPatternImplTest extends S2TestCase {
30  
31  	public FormatPattern formatPattern;
32  
33  	@Override
34  	protected void setUp() throws Exception {
35  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
36  	}
37  
38  	public void testDefaultPattern() {
39  		FormatPattern formatPattern = new FormatPatternImpl();
40  		assertEquals("yyyy-MM-dd", formatPattern.getDatePattern());
41  		assertEquals("HH:mm:ss", formatPattern.getTimePattern());
42  		assertEquals("yyyy-MM-dd HH:mm:ss", formatPattern.getTimestampPattern());
43  	}
44  
45  	public void testDate() {
46  		Calendar calendar = Calendar.getInstance();
47  		calendar.set(2007, Calendar.SEPTEMBER, 2);
48  		Date date = new Date(calendar.getTimeInMillis());
49  
50  		String pattern = formatPattern.getDatePattern();
51  		System.out.println(pattern);
52  		assertEquals("yyyy-MM-dd", pattern);
53  
54  		DateFormat dateFormat = formatPattern.getDateFormat();
55  		String actual = dateFormat.format(date);
56  		System.out.println(actual);
57  		assertEquals("2007-09-02", actual);
58  	}
59  
60  	public void testSqlDate() {
61  		Calendar calendar = Calendar.getInstance();
62  		calendar.set(2007, SEPTEMBER, 2);
63  		java.sql.Date date = new java.sql.Date(calendar.getTimeInMillis());
64  
65  		String pattern = formatPattern.getDatePattern();
66  		System.out.println(pattern);
67  		assertEquals("yyyy-MM-dd", pattern);
68  
69  		DateFormat dateFormat = formatPattern.getDateFormat();
70  		String actual = dateFormat.format(date);
71  		System.out.println(actual);
72  		assertEquals("2007-09-02", actual);
73  	}
74  
75  	public void testTime() {
76  		Calendar calendar = Calendar.getInstance();
77  		calendar.set(2007, SEPTEMBER, 2, 8, 5, 6);
78  		Time time = new Time(calendar.getTimeInMillis());
79  
80  		String pattern = formatPattern.getTimePattern();
81  		System.out.println(pattern);
82  		assertEquals("HH:mm:ss", pattern);
83  
84  		DateFormat dateFormat = formatPattern.getTimeFormat();
85  		String actual = dateFormat.format(time);
86  		System.out.println(actual);
87  		assertEquals("08:05:06", actual);
88  	}
89  
90  	public void testTime2() {
91  		Calendar calendar = Calendar.getInstance();
92  		calendar.set(2007, SEPTEMBER, 2, 18, 5, 6);
93  		Time time = new Time(calendar.getTimeInMillis());
94  
95  		String pattern = formatPattern.getTimePattern();
96  		System.out.println(pattern);
97  		assertEquals("HH:mm:ss", pattern);
98  
99  		DateFormat dateFormat = formatPattern.getTimeFormat();
100 		String actual = dateFormat.format(time);
101 		System.out.println(actual);
102 		assertEquals("18:05:06", actual);
103 	}
104 
105 	public void testTimestamp() {
106 		Calendar calendar = Calendar.getInstance();
107 		calendar.set(2007, SEPTEMBER, 2, 8, 5, 6);
108 		Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
109 
110 		String pattern = formatPattern.getTimestampPattern();
111 		System.out.println(pattern);
112 		assertEquals("yyyy-MM-dd HH:mm:ss", pattern);
113 
114 		DateFormat dateFormat = formatPattern.getTimestampFormat();
115 		String actual = dateFormat.format(timestamp);
116 		System.out.println(actual);
117 		assertEquals("2007-09-02 08:05:06", actual);
118 	}
119 
120 }