1 15 package org.apache.tapestry.form.translator; 16 17 import java.util.Locale ; 18 19 import org.apache.tapestry.form.FormEventType; 20 import org.apache.tapestry.valid.ValidationConstraint; 21 import org.apache.tapestry.valid.ValidatorException; 22 23 public class TestNumberTranslator extends TranslatorTestCase 24 { 25 private NumberTranslator _translator = new NumberTranslator(); 26 27 public void testDefaultFormat() 28 { 29 testFormat(new Integer (10), "10"); 30 } 31 32 public void testCustomFormat() 33 { 34 _translator.setPattern("$#0.00"); 35 36 testFormat(new Integer (10), "$10.00"); 37 } 38 39 public void testFormat(Number number, String expected) 40 { 41 _component.getPage(); 42 _componentControl.setReturnValue(_page); 43 44 _page.getLocale(); 45 _pageControl.setReturnValue(Locale.US); 46 47 replay(); 48 49 String result = _translator.format(_component, number); 50 51 assertEquals(expected, result); 52 53 verify(); 54 } 55 56 public void testNullFormat() 57 { 58 replay(); 59 60 String result = _translator.format(_component, null); 61 62 assertEquals("", result); 63 64 verify(); 65 } 66 67 public void testDefaultParse() 68 { 69 testParse("0.1", new Double (0.1)); 70 } 71 72 public void testCustomParse() 73 { 74 _translator.setPattern("#%"); 75 76 testParse("10%", new Double (0.1)); 77 } 78 79 public void testTrimmedParse() 80 { 81 _translator.setTrim(true); 82 83 testParse(" 100 ", new Long (100)); 84 } 85 86 private void testParse(String number, Number expected) 87 { 88 _component.getPage(); 89 _componentControl.setReturnValue(_page); 90 91 _page.getLocale(); 92 _pageControl.setReturnValue(Locale.US); 93 94 replay(); 95 96 try 97 { 98 Number result = (Number ) _translator.parse(_component, number); 99 100 assertEquals(expected, result); 101 } 102 catch (ValidatorException e) 103 { 104 unreachable(); 105 } 106 finally 107 { 108 verify(); 109 } 110 } 111 112 public void testFailedParseDefaultMessage() 113 { 114 testFailedParse("Field Name must be a numeric value."); 115 } 116 117 public void testFailedParseCustomMessage() 118 { 119 String message = "Field Name is an invalid number."; 120 121 _translator.setMessage(message); 122 123 testFailedParse(message); 124 } 125 126 private void testFailedParse(String message) 127 { 128 _component.getPage(); 129 _componentControl.setReturnValue(_page); 130 131 _page.getLocale(); 132 _pageControl.setReturnValue(Locale.US); 133 134 _component.getPage(); 135 _componentControl.setReturnValue(_page); 136 137 _page.getLocale(); 138 _pageControl.setReturnValue(Locale.US); 139 140 _component.getDisplayName(); 141 _componentControl.setReturnValue("Field Name"); 142 143 replay(); 144 145 try 146 { 147 System.out.println(_translator.parse(_component, "Bad-Number")); 148 149 unreachable(); 150 } 151 catch (ValidatorException e) 152 { 153 assertEquals(message, e.getMessage()); 154 assertEquals(ValidationConstraint.NUMBER_FORMAT, e.getConstraint()); 155 } 156 finally 157 { 158 verify(); 159 } 160 } 161 162 public void testRenderContribution() 163 { 164 addScript("/org/apache/tapestry/form/translator/NumberTranslator.js"); 165 166 _component.getPage(); 167 _componentControl.setReturnValue(_page); 168 169 _page.getLocale(); 170 _pageControl.setReturnValue(Locale.US); 171 172 _component.getDisplayName(); 173 _componentControl.setReturnValue("Field Label"); 174 175 _component.getForm(); 176 _componentControl.setReturnValue(_form); 177 178 _form.getName(); 179 _formControl.setReturnValue("formName"); 180 181 _component.getName(); 182 _componentControl.setReturnValue("fieldName"); 183 184 _form.addEventHandler(FormEventType.SUBMIT, "validate_number(event, document.formName.fieldName,'Field Label must be a numeric value.')"); 185 _formControl.setVoidCallable(); 186 187 replay(); 188 189 _translator.renderContribution(null, _cycle, null, _component); 190 191 verify(); 192 } 193 194 public void testMessageRenderContribution() 195 { 196 _translator.setMessage("You entered a bunk value for {0}. I should look like {1}."); 197 198 addScript("/org/apache/tapestry/form/translator/NumberTranslator.js"); 199 200 _component.getPage(); 201 _componentControl.setReturnValue(_page); 202 203 _page.getLocale(); 204 _pageControl.setReturnValue(Locale.US); 205 206 _component.getDisplayName(); 207 _componentControl.setReturnValue("Field Label"); 208 209 _component.getForm(); 210 _componentControl.setReturnValue(_form); 211 212 _form.getName(); 213 _formControl.setReturnValue("formName"); 214 215 _component.getName(); 216 _componentControl.setReturnValue("fieldName"); 217 218 _form.addEventHandler(FormEventType.SUBMIT, "validate_number(event, document.formName.fieldName,'You entered a bunk value for Field Label. I should look like #.')"); 219 _formControl.setVoidCallable(); 220 221 replay(); 222 223 _translator.renderContribution(null, _cycle, null, _component); 224 225 verify(); 226 } 227 228 public void testTrimRenderContribution() 229 { 230 _translator.setTrim(true); 231 trim(); 232 233 addScript("/org/apache/tapestry/form/translator/NumberTranslator.js"); 234 235 _component.getPage(); 236 _componentControl.setReturnValue(_page); 237 238 _page.getLocale(); 239 _pageControl.setReturnValue(Locale.US); 240 241 _component.getDisplayName(); 242 _componentControl.setReturnValue("Field Label"); 243 244 _component.getForm(); 245 _componentControl.setReturnValue(_form); 246 247 _form.getName(); 248 _formControl.setReturnValue("formName"); 249 250 _component.getName(); 251 _componentControl.setReturnValue("fieldName"); 252 253 _form.addEventHandler(FormEventType.SUBMIT, "validate_number(event, document.formName.fieldName,'Field Label must be a numeric value.')"); 254 _formControl.setVoidCallable(); 255 256 replay(); 257 258 _translator.renderContribution(null, _cycle, null, _component); 259 260 verify(); 261 } 262 } 263 | Popular Tags |