001    package org.apache.turbine.services.pull.util;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.util.Calendar;
023    import java.util.Date;
024    import java.util.GregorianCalendar;
025    
026    import junit.framework.TestCase;
027    
028    /**
029     * Test class for DateFormatter.
030     *
031     * @author <a href="mailto:seade@backstagetech.com.au">Scott Eade</a>
032     * @version $Id: DateFormatterTest.java 534527 2007-05-02 16:10:59Z tv $
033     */
034    public class DateFormatterTest extends TestCase
035    {
036    
037    //    /*
038    //     * Class under test for String format(Date)
039    //     */
040    //    public void testFormatDate()
041    //    {
042    //        // Need configuration to test.
043    //    }
044    
045        /*
046         * Class under test for String format(Date, String)
047         */
048        public void testFormatDateString()
049        {
050            Calendar cal = new GregorianCalendar();
051            DateFormatter df = new DateFormatter();
052            int day = cal.get(Calendar.DAY_OF_MONTH);
053            int month = cal.get(Calendar.MONTH) + 1; // zero based
054            int year = cal.get(Calendar.YEAR);
055            String dayString = (day < 10 ? "0" : "") + day;
056            String monthString = (month < 10 ? "0" : "") + month;
057            String ddmmyyyy = dayString + "/" + monthString + "/" + year;
058            assertEquals(ddmmyyyy, df.format(cal.getTime(), "dd/MM/yyyy"));
059    
060            String mmddyyyy = "" + monthString + "/" + dayString + "/" + year;
061            assertEquals(mmddyyyy, df.format(cal.getTime(), "MM/dd/yyyy"));
062        }
063    
064        /*
065         * Class under test for String format(null, String)
066         */
067        public void testFormatDateStringNullString()
068        {
069            DateFormatter df = new DateFormatter();
070            assertEquals("null argument should produce an empty String",
071                    "", df.format(null, "MM/dd/yyyy"));
072        }
073    
074        /*
075         * Class under test for String format(Date, "")
076         */
077        public void testFormatDateStringEmptyString()
078        {
079            Date today = new Date();
080            DateFormatter df = new DateFormatter();
081            assertEquals("Empty pattern should produce empty String",
082                    "", df.format(today, ""));
083        }
084    
085        /*
086         * Class under test for String format(Date, "")
087         */
088        public void testFormatDateStringNullFormat()
089        {
090            Date today = new Date();
091            DateFormatter df = new DateFormatter();
092            assertEquals("null pattern should produce empty String",
093                    "", df.format(today, null));
094        }
095    
096    }