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.StringTypeValidator; 17 18 19 26 public class StringTypeValidatorTest extends TestCase { 27 28 33 public StringTypeValidatorTest(String name) { 34 super(name); 35 } 36 37 38 41 public void testString() { 42 String str = "testString"; 43 StringTypeValidator validator = new StringTypeValidator(); 44 45 BasicError error = validator.validate(str, null, "fail", null); 46 assertNull(error); 47 } 48 49 52 public void testStringObject() { 53 Map map = new HashMap (); 54 StringTypeValidator validator = new StringTypeValidator(); 55 56 BasicError error = validator.validate(map, null, "fail", null); 57 assertNull(error); 58 } 59 60 63 public void testStringMinMax() { 64 String str = "1"; 65 StringTypeValidator validator = new StringTypeValidator(); 66 67 Map params = new HashMap (); 68 params.put("min", new Double (1)); 69 params.put("max", new Double (1)); 70 71 BasicError error = validator.validate(str, params, "fail", null); 72 assertNull(error); 73 } 74 75 78 public void testMinMax() { 79 String str = "12"; 80 StringTypeValidator validator = new StringTypeValidator(); 81 82 Map params = new HashMap (); 83 params.put("min", new Double (2)); 84 params.put("max", new Double (3)); 85 86 BasicError error = validator.validate(str, params, "fail", null); 87 assertNull(error); 88 89 str = "123"; 90 params = new HashMap (); 91 params.put("min", new Double (2)); 92 params.put("max", new Double (3)); 93 94 error = validator.validate(str, params, "fail", null); 95 assertNull(error); 96 } 97 98 101 public void testStringMinMaxFailures() { 102 String str = "1"; 103 StringTypeValidator validator = new StringTypeValidator(); 104 105 Map params = new HashMap (); 106 params.put("min", new Double (2)); 107 params.put("max", new Double (3)); 108 109 BasicError error = validator.validate(str, params, "fail", null); 110 assertNotNull(error); 111 assertEquals("fail", error.getMessage()); 112 113 str = "1234"; 114 params = new HashMap (); 115 params.put("min", new Double (2)); 116 params.put("max", new Double (3)); 117 118 error = validator.validate(str, params, "fail", null); 119 assertNotNull(error); 120 assertEquals("fail", error.getMessage()); 121 } 122 123 126 public void testNull() { 127 String str = null; 128 StringTypeValidator validator = new StringTypeValidator(); 129 130 BasicError error = validator.validate(str, null, "fail", null); 131 assertNull(error); 132 } 133 134 137 public void testToStringNull() { 138 Object value = new Object () { 139 public String toString() {return null;} 140 }; 141 142 StringTypeValidator validator = new StringTypeValidator(); 143 BasicError error = validator.validate(value, null, "fail", null); 144 assertNull(error); 145 } 146 147 150 public void testEmpty() { 151 String str = ""; 152 StringTypeValidator validator = new StringTypeValidator(); 153 154 BasicError error = validator.validate(str, null, "fail", null); 155 assertNull(error); 156 } 157 158 161 public void testStringFailure() { 162 Object value = new Object () { 163 public String toString() { 164 throw new NullPointerException (); 165 } 166 }; 167 168 StringTypeValidator validator = new StringTypeValidator(); 169 BasicError error = validator.validate(value, null, "fail", null); 170 assertNotNull(error); 171 assertEquals("fail", error.getMessage()); 172 } 173 174 177 public void testMinMaxAsStrings() { 178 String str = "1"; 179 StringTypeValidator validator = new StringTypeValidator(); 180 181 Map params = new HashMap (); 182 params.put("min", "2"); 183 params.put("max", "3"); 184 185 BasicError error = validator.validate(str, params, "fail", null); 186 assertNotNull(error); 187 assertEquals("fail", error.getMessage()); 188 189 str = "1234"; 190 params = new HashMap (); 191 params.put("min", "2"); 192 params.put("max", "3"); 193 194 error = validator.validate(str, params, "fail", null); 195 assertNotNull(error); 196 assertEquals("fail", error.getMessage()); 197 } 198 199 202 public void testMinMaxFailure() { 203 String str = "1"; 204 StringTypeValidator validator = new StringTypeValidator(); 205 206 Map params = new HashMap (); 207 params.put("min", "foo"); 208 params.put("max", "3"); 209 210 try { 211 validator.validate(str, params, "fail", null); 212 fail("Should have failed"); 213 } catch (NumberFormatException nfe) { 214 } 216 217 params = new HashMap (); 218 params.put("min", "2"); 219 params.put("max", "bar"); 220 221 try { 222 validator.validate(str, params, "fail", null); 223 fail("Should have failed"); 224 } catch (NumberFormatException nfe) { 225 } 227 } 228 } | Popular Tags |