1 15 package org.apache.tapestry.form.translator; 16 17 import java.util.Calendar ; 18 import java.util.Date ; 19 import java.util.Locale ; 20 21 import org.apache.tapestry.valid.ValidationConstraint; 22 import org.apache.tapestry.valid.ValidatorException; 23 24 30 public class TestDateTranslator extends TranslatorTestCase 31 { 32 private DateTranslator _translator = new DateTranslator(); 33 private Calendar _calendar = Calendar.getInstance(); 34 35 38 protected void setUp() throws Exception 39 { 40 _calendar.clear(); 41 } 42 43 private Date buildDate(int year, int month, int day) 44 { 45 _calendar.set(Calendar.YEAR, year); 46 _calendar.set(Calendar.MONTH, month); 47 _calendar.set(Calendar.DATE, day); 48 49 return _calendar.getTime(); 50 } 51 52 public void testDefaultFormat() 53 { 54 testFormat(buildDate(1976, Calendar.OCTOBER, 29), "10/29/1976"); 55 } 56 57 public void testCustomFormat() 58 { 59 _translator.setPattern("yyyy-MM-dd"); 60 61 testFormat(buildDate(1976, Calendar.OCTOBER, 29), "1976-10-29"); 62 } 63 64 public void testFormat(Date date, String expected) 65 { 66 _component.getPage(); 67 _componentControl.setReturnValue(_page); 68 69 _page.getLocale(); 70 _pageControl.setReturnValue(Locale.US); 71 72 replay(); 73 74 String result = _translator.format(_component, date); 75 76 assertEquals(expected, result); 77 78 verify(); 79 } 80 81 public void testNullFormat() 82 { 83 replay(); 84 85 String result = _translator.format(_component, null); 86 87 assertEquals("", result); 88 89 verify(); 90 } 91 92 public void testDefaultParse() 93 { 94 testParse("10/29/1976", buildDate(1976, Calendar.OCTOBER, 29)); 95 } 96 97 public void testCustomParse() 98 { 99 _translator.setPattern("yyyy-MM-dd"); 100 101 testParse("1976-10-29", buildDate(1976, Calendar.OCTOBER, 29)); 102 } 103 104 public void testTrimmedParse() 105 { 106 _translator.setTrim(true); 107 108 testParse(" 10/29/1976 ", buildDate(1976, Calendar.OCTOBER, 29)); 109 } 110 111 public void testEmptyParse() 112 { 113 replay(); 114 115 try 116 { 117 Date result = (Date ) _translator.parse(_component, ""); 118 119 assertEquals(null, result); 120 } 121 catch (ValidatorException e) 122 { 123 unreachable(); 124 } 125 finally 126 { 127 verify(); 128 } 129 } 130 131 private void testParse(String date, Date expected) 132 { 133 _component.getPage(); 134 _componentControl.setReturnValue(_page); 135 136 _page.getLocale(); 137 _pageControl.setReturnValue(Locale.US); 138 139 replay(); 140 141 try 142 { 143 Date result = (Date ) _translator.parse(_component, date); 144 145 assertEquals(expected, result); 146 } 147 catch (ValidatorException e) 148 { 149 unreachable(); 150 } 151 finally 152 { 153 verify(); 154 } 155 } 156 157 public void testFailedParseDefaultMessage() 158 { 159 testFailedParse("Invalid date format for Field Name. Format is MM/DD/YYYY."); 160 } 161 162 public void testFailedParseCustomMessage() 163 { 164 String message = "Field Name is an invalid date."; 165 166 _translator.setMessage(message); 167 168 testFailedParse(message); 169 } 170 171 private void testFailedParse(String message) 172 { 173 _component.getPage(); 174 _componentControl.setReturnValue(_page); 175 176 _page.getLocale(); 177 _pageControl.setReturnValue(Locale.US); 178 179 _component.getPage(); 180 _componentControl.setReturnValue(_page); 181 182 _page.getLocale(); 183 _pageControl.setReturnValue(Locale.US); 184 185 _component.getDisplayName(); 186 _componentControl.setReturnValue("Field Name"); 187 188 replay(); 189 190 try 191 { 192 System.out.println(_translator.parse(_component, "Bad-Date")); 193 194 unreachable(); 195 } 196 catch (ValidatorException e) 197 { 198 assertEquals(message, e.getMessage()); 199 assertEquals(ValidationConstraint.DATE_FORMAT, e.getConstraint()); 200 } 201 finally 202 { 203 verify(); 204 } 205 } 206 207 public void testRenderContribution() 208 { 209 replay(); 210 211 _translator.renderContribution(null, _cycle, null, _component); 212 213 verify(); 214 } 215 216 public void testTrimRenderContribution() 217 { 218 _translator.setTrim(true); 219 trim(); 220 221 replay(); 222 223 _translator.renderContribution(null, _cycle, null, _component); 224 225 verify(); 226 } 227 } 228 | Popular Tags |