1 16 17 package org.apache.commons.beanutils.locale.converters; 18 19 import junit.framework.TestSuite; 20 import junit.framework.TestCase; 21 22 import java.text.SimpleDateFormat ; 23 import java.text.ParseException ; 24 25 import java.util.Locale ; 26 27 import org.apache.commons.beanutils.Converter; 28 import org.apache.commons.beanutils.ConversionException; 29 30 36 37 public class DateLocaleConverterTestCase extends TestCase { 38 39 41 public DateLocaleConverterTestCase(String name) { 42 super(name); 43 } 44 45 47 public static TestSuite suite() { 48 return new TestSuite(DateLocaleConverterTestCase.class); 49 } 50 51 52 54 public void testSetLenient() { 55 SimpleDateFormat dateFormat = new SimpleDateFormat ("MMM dd, yyyy", Locale.UK); 57 58 dateFormat.setLenient(false); 60 61 try { 62 63 dateFormat.parse("Feb 10, 2001"); 64 65 } catch (ParseException e) { 66 fail("Could not parse date (1) - " + e.getMessage()); 67 } 68 69 try { 70 71 dateFormat.parse("Feb 31, 2001"); 72 fail("Parsed illegal date (1)"); 73 74 } catch (ParseException e) { 75 } 77 78 dateFormat.setLenient(true); 80 81 try { 82 83 dateFormat.parse("Feb 10, 2001"); 84 85 } catch (ParseException e) { 86 fail("Could not parse date (2) - " + e.getMessage()); 87 } 88 89 try { 90 91 dateFormat.parse("Feb 31, 2001"); 92 93 } catch (ParseException e) { 94 fail("Could not parse date (3) - " + e.getMessage()); 95 } 96 97 DateLocaleConverter converter = new DateLocaleConverter(Locale.UK, "MMM dd, yyyy"); 99 100 converter.setLenient(false); 102 assertEquals("Set lenient failed", converter.isLenient(), false); 103 104 try { 105 106 converter.convert("Feb 10, 2001"); 107 108 } catch (ConversionException e) { 109 fail("Could not parse date (4) - " + e.getMessage()); 110 } 111 112 try { 113 114 converter.convert("Feb 31, 2001"); 115 assertEquals("Set lenient failed", converter.isLenient(), false); 116 fail("Parsed illegal date (2)"); 117 118 } catch (ConversionException e) { 119 } 121 122 converter.setLenient(true); 124 assertEquals("Set lenient failed", converter.isLenient(), true); 125 126 try { 127 128 converter.convert("Feb 10, 2001"); 129 130 } catch (ConversionException e) { 131 fail("Could not parse date (5) - " + e.getMessage()); 132 } 133 134 try { 135 136 converter.convert("Feb 31, 2001"); 137 138 } catch (ConversionException e) { 139 fail("Could not parse date (6) - " + e.getMessage()); 140 } 141 } 142 } 143 144 | Popular Tags |