1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.controller.impl;
18  
19  import static java.util.Calendar.SEPTEMBER;
20  import static org.junit.Assert.assertEquals;
21  
22  import java.sql.Time;
23  import java.sql.Timestamp;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  import java.util.Date;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  import org.seasar.cubby.controller.FormatPattern;
32  import org.seasar.cubby.controller.impl.DefaultFormatPattern;
33  
34  public class FormatPatternImplTest {
35  
36  	private FormatPattern formatPattern;
37  
38  	@Before
39  	public void setupFormatPattern() {
40  		DefaultFormatPattern formatPattern = new DefaultFormatPattern();
41  		formatPattern.setDatePattern("yyyy-MM-dd");
42  		formatPattern.setTimePattern("HH:mm:ss");
43  		formatPattern.setTimestampPattern("yyyy-MM-dd HH:mm:ss");
44  		this.formatPattern = formatPattern;
45  	}
46  
47  	@Test
48  	public void defaultPattern() {
49  		FormatPattern formatPattern = new DefaultFormatPattern();
50  		assertEquals("yyyy-MM-dd", formatPattern.getDatePattern());
51  		assertEquals("HH:mm:ss", formatPattern.getTimePattern());
52  		assertEquals("yyyy-MM-dd HH:mm:ss", formatPattern.getTimestampPattern());
53  	}
54  
55  	@Test
56  	public void date() {
57  		Calendar calendar = Calendar.getInstance();
58  		calendar.set(2007, Calendar.SEPTEMBER, 2);
59  		Date date = new Date(calendar.getTimeInMillis());
60  
61  		String pattern = formatPattern.getDatePattern();
62  		System.out.println(pattern);
63  		assertEquals("yyyy-MM-dd", pattern);
64  
65  		DateFormat dateFormat = new SimpleDateFormat(formatPattern
66  				.getDatePattern());
67  		String actual = dateFormat.format(date);
68  		System.out.println(actual);
69  		assertEquals("2007-09-02", actual);
70  	}
71  
72  	@Test
73  	public void sqlDate() {
74  		Calendar calendar = Calendar.getInstance();
75  		calendar.set(2007, SEPTEMBER, 2);
76  		java.sql.Date date = new java.sql.Date(calendar.getTimeInMillis());
77  
78  		String pattern = formatPattern.getDatePattern();
79  		System.out.println(pattern);
80  		assertEquals("yyyy-MM-dd", pattern);
81  
82  		DateFormat dateFormat = new SimpleDateFormat(formatPattern
83  				.getDatePattern());
84  		String actual = dateFormat.format(date);
85  		System.out.println(actual);
86  		assertEquals("2007-09-02", actual);
87  	}
88  
89  	@Test
90  	public void time() {
91  		Calendar calendar = Calendar.getInstance();
92  		calendar.set(2007, SEPTEMBER, 2, 8, 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 = new SimpleDateFormat(formatPattern
100 				.getTimePattern());
101 		String actual = dateFormat.format(time);
102 		System.out.println(actual);
103 		assertEquals("08:05:06", actual);
104 	}
105 
106 	@Test
107 	public void time2() {
108 		Calendar calendar = Calendar.getInstance();
109 		calendar.set(2007, SEPTEMBER, 2, 18, 5, 6);
110 		Time time = new Time(calendar.getTimeInMillis());
111 
112 		String pattern = formatPattern.getTimePattern();
113 		System.out.println(pattern);
114 		assertEquals("HH:mm:ss", pattern);
115 
116 		DateFormat dateFormat = new SimpleDateFormat(formatPattern
117 				.getTimePattern());
118 		String actual = dateFormat.format(time);
119 		System.out.println(actual);
120 		assertEquals("18:05:06", actual);
121 	}
122 
123 	@Test
124 	public void timestamp() {
125 		Calendar calendar = Calendar.getInstance();
126 		calendar.set(2007, SEPTEMBER, 2, 8, 5, 6);
127 		Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
128 
129 		String pattern = formatPattern.getTimestampPattern();
130 		System.out.println(pattern);
131 		assertEquals("yyyy-MM-dd HH:mm:ss", pattern);
132 
133 		DateFormat dateFormat = new SimpleDateFormat(formatPattern
134 				.getTimestampPattern());
135 		String actual = dateFormat.format(timestamp);
136 		System.out.println(actual);
137 		assertEquals("2007-09-02 08:05:06", actual);
138 	}
139 
140 }