1 21 22 package org.apache.commons.validator; 23 24 import java.io.IOException ; 25 import java.util.Iterator ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 32 import org.xml.sax.SAXException ; 33 34 37 public class TypeTest extends TestCommon { 38 39 43 protected static String FORM_KEY = "typeForm"; 44 45 48 protected static String ACTION = "byte"; 49 50 public TypeTest(String name) { 51 super(name); 52 } 53 54 59 public static void main(String [] theArgs) { 60 junit.awtui.TestRunner.main(new String [] {TypeTest.class.getName()}); 61 } 62 63 67 public static Test suite() { 68 return new TestSuite(TypeTest.class); 70 } 71 72 76 protected void setUp() throws IOException , SAXException { 77 loadResources("validator-type.xml"); 79 } 80 81 protected void tearDown() { 82 } 83 84 87 public void testType() throws ValidatorException { 88 TypeBean info = new TypeBean(); 90 info.setByte("12"); 91 info.setShort("129"); 92 info.setInteger("-144"); 93 info.setLong("88000"); 94 info.setFloat("12.1555f"); 95 info.setDouble("129.1551511111d"); 96 97 Validator validator = new Validator(resources, FORM_KEY); 100 validator.setParameter(Validator.BEAN_PARAM, info); 103 104 ValidatorResults results = null; 106 107 results = validator.validate(); 112 113 assertNotNull("Results are null.", results); 114 115 Map hResultValues = results.getResultValueMap(); 116 117 assertTrue("Expecting byte result to be an instance of Byte.", (hResultValues.get("byte") instanceof Byte )); 118 assertTrue("Expecting short result to be an instance of Short.", (hResultValues.get("short") instanceof Short )); 119 assertTrue("Expecting integer result to be an instance of Integer.", (hResultValues.get("integer") instanceof Integer )); 120 assertTrue("Expecting long result to be an instance of Long.", (hResultValues.get("long") instanceof Long )); 121 assertTrue("Expecting float result to be an instance of Float.", (hResultValues.get("float") instanceof Float )); 122 assertTrue("Expecting double result to be an instance of Double.", (hResultValues.get("double") instanceof Double )); 123 124 for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) { 125 String key = (String )i.next(); 126 Object value = hResultValues.get(key); 127 128 assertNotNull("value ValidatorResults.getResultValueMap() should not be null.", value); 129 } 130 131 133 137 } 138 139 142 public void testUSLocale() throws ValidatorException { 143 TypeBean info = new TypeBean(); 145 info.setByte("12"); 146 info.setShort("129"); 147 info.setInteger("-144"); 148 info.setLong("88000"); 149 info.setFloat("12.1555"); 150 info.setDouble("129.1551511111"); 151 localeTest(info, Locale.US); 152 } 153 154 157 public void testFRLocale() throws ValidatorException { 158 TypeBean info = new TypeBean(); 160 info.setByte("12"); 161 info.setShort("-129"); 162 info.setInteger("1443"); 163 info.setLong("88000"); 164 info.setFloat("12,1555"); 165 info.setDouble("129,1551511111"); 166 Map map = localeTest(info, Locale.FRENCH); 167 assertTrue("float value not correct", ((Float )map.get("float")).intValue() == 12); 168 assertTrue("double value not correct", ((Double )map.get("double")).intValue() == 129); 169 } 170 171 174 private Map localeTest(TypeBean info, Locale locale) throws ValidatorException { 175 176 Validator validator = new Validator(resources, "typeLocaleForm"); 179 validator.setParameter(Validator.BEAN_PARAM, info); 182 validator.setParameter("java.util.Locale", locale); 183 184 ValidatorResults results = null; 186 187 results = validator.validate(); 192 193 assertNotNull("Results are null.", results); 194 195 Map hResultValues = results.getResultValueMap(); 196 197 assertTrue("Expecting byte result to be an instance of Byte for locale: "+locale, (hResultValues.get("byte") instanceof Byte )); 198 assertTrue("Expecting short result to be an instance of Short for locale: "+locale, (hResultValues.get("short") instanceof Short )); 199 assertTrue("Expecting integer result to be an instance of Integer for locale: "+locale, (hResultValues.get("integer") instanceof Integer )); 200 assertTrue("Expecting long result to be an instance of Long for locale: "+locale, (hResultValues.get("long") instanceof Long )); 201 assertTrue("Expecting float result to be an instance of Float for locale: "+locale, (hResultValues.get("float") instanceof Float )); 202 assertTrue("Expecting double result to be an instance of Double for locale: "+locale, (hResultValues.get("double") instanceof Double )); 203 204 for (Iterator i = hResultValues.keySet().iterator(); i.hasNext(); ) { 205 String key = (String )i.next(); 206 Object value = hResultValues.get(key); 207 208 assertNotNull("value ValidatorResults.getResultValueMap() should not be null for locale: "+locale, value); 209 } 210 return hResultValues; 211 } 212 213 } | Popular Tags |