1 7 package com.inversoft.util.typevalidator.test; 8 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import junit.framework.TestCase; 14 15 import com.inversoft.error.BasicError; 16 import com.inversoft.util.typevalidator.NumberTypeValidator; 17 18 19 26 public class NumberTypeValidatorTest extends TestCase { 27 28 33 public NumberTypeValidatorTest(String name) { 34 super(name); 35 } 36 37 38 41 public void testNumber() { 42 Double value = new Double (2.3d); 43 NumberTypeValidator validator = new NumberTypeValidator(); 44 45 BasicError error = validator.validate(value, null, "fail", null); 46 assertNull(error); 47 } 48 49 52 public void testNull() { 53 String value = null; 54 NumberTypeValidator validator = new NumberTypeValidator(); 55 56 BasicError error = validator.validate(value, null, "fail", null); 57 assertNotNull(error); 58 } 59 60 63 public void testStringNumber() { 64 String value = "12"; 65 NumberTypeValidator validator = new NumberTypeValidator(); 66 67 BasicError error = validator.validate(value, null, "fail", null); 68 assertNull(error); 69 } 70 71 74 public void testNumberMinMax() { 75 Double value = new Double (2.3d); 76 NumberTypeValidator validator = new NumberTypeValidator(); 77 78 Map params = new HashMap (); 79 params.put("min", new Double (1)); 80 params.put("max", new Double (4)); 81 82 BasicError error = validator.validate(value, params, "fail", null); 83 assertNull(error); 84 } 85 86 89 public void testStringNumberMinMax() { 90 String value = "2.3"; 91 NumberTypeValidator validator = new NumberTypeValidator(); 92 93 Map params = new HashMap (); 94 params.put("min", new Double (1)); 95 params.put("max", new Double (4)); 96 97 BasicError error = validator.validate(value, params, "fail", null); 98 assertNull(error); 99 } 100 101 104 public void testStringFailure() { 105 String value = "foo"; 106 NumberTypeValidator validator = new NumberTypeValidator(); 107 108 BasicError error = validator.validate(value, null, "fail", null); 109 assertNotNull(error); 110 assertEquals("fail", error.getMessage()); 111 } 112 113 116 public void testNumberMinFailure() { 117 Double value = new Double (1); 118 NumberTypeValidator validator = new NumberTypeValidator(); 119 120 Map params = new HashMap (); 121 params.put("min", new Double (3)); 122 params.put("max", new Double (4)); 123 124 BasicError error = validator.validate(value, params, "fail", null); 125 assertNotNull(error); 126 assertEquals("fail", error.getMessage()); 127 } 128 129 132 public void testNumberMaxFailure() { 133 Double value = new Double (23); 134 NumberTypeValidator validator = new NumberTypeValidator(); 135 136 Map params = new HashMap (); 137 params.put("min", new Double (1)); 138 params.put("max", new Double (4)); 139 140 BasicError error = validator.validate(value, params, "fail", null); 141 assertNotNull(error); 142 assertEquals("fail", error.getMessage()); 143 } 144 145 148 public void testStringNumberMinFailure() { 149 String value = "3"; 150 NumberTypeValidator validator = new NumberTypeValidator(); 151 152 Map params = new HashMap (); 153 params.put("min", new Double (13)); 154 params.put("max", new Double (42)); 155 156 BasicError error = validator.validate(value, params, "fail", null); 157 assertNotNull(error); 158 assertEquals("fail", error.getMessage()); 159 } 160 161 164 public void testStringNumberMaxFailure() { 165 String value = "300"; 166 NumberTypeValidator validator = new NumberTypeValidator(); 167 168 Map params = new HashMap (); 169 params.put("min", new Double (13)); 170 params.put("max", new Double (42)); 171 172 BasicError error = validator.validate(value, params, "fail", null); 173 assertNotNull(error); 174 assertEquals("fail", error.getMessage()); 175 } 176 177 180 public void testEmptyParams() { 181 String value = "30"; 182 NumberTypeValidator validator = new NumberTypeValidator(); 183 184 Map params = new HashMap (); 185 BasicError error = validator.validate(value, params, "fail", null); 186 assertNull(error); 187 } 188 189 192 public void testStringMinMax() { 193 String value = "30"; 194 NumberTypeValidator validator = new NumberTypeValidator(); 195 196 Map params = new HashMap (); 197 params.put("min", "13"); 198 params.put("max", "42"); 199 200 BasicError error = validator.validate(value, params, "fail", null); 201 assertNull(error); 202 } 203 204 207 public void testBadParams() { 208 String value = "30"; 209 NumberTypeValidator validator = new NumberTypeValidator(); 210 211 Map params = new HashMap (); 212 params.put("min", "foo"); 213 214 try { 215 validator.validate(value, params, "fail", null); 216 fail("Should have failed"); 217 } catch (NumberFormatException nfe) { 218 } 220 221 params = new HashMap (); 222 params.put("max", "bar"); 223 224 try { 225 validator.validate(value, params, "fail", null); 226 fail("Should have failed"); 227 } catch (NumberFormatException nfe) { 228 } 230 } 231 } | Popular Tags |