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