View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, Steve Meyfroidt 4 * All rights reserved. 5 * Email: smeyfroi@users.sourceforge.net 6 * 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * Neither the name "Scope" nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 * 36 * 37 * $Id: TestDateStringConvertors.java,v 1.7 2002/08/05 13:16:45 ludovicc Exp $ 38 */ 39 40 41 package test.util.convertor; 42 43 44 import java.text.DateFormat; 45 import java.util.Date; 46 import java.util.GregorianCalendar; 47 import java.util.Locale; 48 import java.util.TimeZone; 49 import junit.framework.TestCase; 50 import org.apache.commons.logging.LogFactory;import org.apache.commons.logging.Log; 51 import org.scopemvc.util.ScopeConfig; 52 import org.scopemvc.util.convertor.DateStringConvertor; 53 54 55 /*** 56 * <P> 57 * </P> 58 * 59 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 60 * @version $Revision: 1.7 $ $Date: 2002/08/05 13:16:45 $ 61 */ 62 public final class TestDateStringConvertors extends TestCase { 63 64 65 private static final Log LOG = LogFactory.getLog(TestDateStringConvertors.class); 66 67 68 /*** 69 * Must set a known default locale for the tests. 70 */ 71 static { 72 Locale.setDefault(Locale.UK); 73 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 74 System.out.println("Default medium format " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date(0))); 75 System.out.println("Default locale " + Locale.getDefault()); 76 } 77 78 private Date jan1_1970; 79 private GregorianCalendar cal = new GregorianCalendar(); 80 81 public TestDateStringConvertors(String inName) { 82 super(inName); 83 } 84 85 86 protected void setUp() { 87 cal.clear(); 88 cal.set(1970, 0, 1, 0, 0, 0); 89 jan1_1970 = cal.getTime(); 90 } 91 92 93 public void testDefaultDateStringConvertor() throws Exception { 94 95 DateStringConvertor c = new DateStringConvertor(); 96 String shortFormat = DateFormat.getDateInstance(DateFormat.SHORT).format(jan1_1970); 97 String mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM).format(jan1_1970); 98 String longFormat = DateFormat.getDateInstance(DateFormat.LONG).format(jan1_1970); 99 String fullFormat = DateFormat.getDateInstance(DateFormat.FULL).format(jan1_1970); 100 101 System.out.println("shortFormat " + shortFormat); 102 System.out.println("mediumFormat " + mediumFormat); 103 System.out.println("longFormat " + longFormat); 104 System.out.println("fullFormat " + fullFormat); 105 106 // don't use string constants as date formats depend on the JVM version. 107 assertEquals(mediumFormat, c.valueAsString(jan1_1970)); 108 109 if (LOG.isDebugEnabled()) LOG.debug("Try " + shortFormat); 110 assertEquals(jan1_1970, c.stringAsValue(shortFormat)); 111 if (LOG.isDebugEnabled()) LOG.debug("Try " + mediumFormat); 112 assertEquals(jan1_1970, c.stringAsValue(mediumFormat)); 113 if (LOG.isDebugEnabled()) LOG.debug("Try " + longFormat); 114 assertEquals(jan1_1970, c.stringAsValue(longFormat)); 115 if (LOG.isDebugEnabled()) LOG.debug("Try " + fullFormat); 116 assertEquals(jan1_1970, c.stringAsValue(fullFormat)); 117 } 118 119 120 public void testConfigDateStringConvertor() throws Exception { 121 122 ScopeConfig.setPropertiesName("test.util.convertor.ConvertorScopeConfig"); 123 124 DateStringConvertor c = new DateStringConvertor(); 125 126 assertEquals("01-01-1970", c.valueAsString(jan1_1970)); 127 128 assertEquals(jan1_1970, c.stringAsValue("01-01-1970")); 129 assertEquals(jan1_1970, c.stringAsValue("01/01/1970")); 130 assertEquals(jan1_1970, c.stringAsValue("01.01.1970")); 131 assertEquals(jan1_1970, c.stringAsValue("01011970")); 132 133 // Is it just me or is the Java calendar API impenetrable? 134 Date d = (Date)c.stringAsValue("2-2-2001"); 135 cal.setTime(d); 136 assertEquals("Date: " + d, 2001, cal.get(cal.YEAR)); 137 assertEquals("Date: " + d, 1, cal.get(cal.MONTH)); 138 assertEquals("Date: " + d, 2, cal.get(cal.DAY_OF_MONTH)); 139 } 140 }

This page was automatically generated by Maven