1 15 package hivemind.test.rules; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.schema.rules.DoubleTranslator; 21 import org.apache.hivemind.schema.rules.IntTranslator; 22 import org.apache.hivemind.schema.rules.LongTranslator; 23 import org.apache.hivemind.schema.rules.RulesMessages; 24 25 30 public class TestNumericTranslators extends FrameworkTestCase 31 { 32 36 public void testIntTranslator() 37 { 38 IntTranslator t = new IntTranslator(); 39 40 assertEquals(new Integer (10), t.translate(null, null, "10", null)); 41 } 42 43 public void testIntDefault() 44 { 45 IntTranslator t = new IntTranslator(); 46 47 assertEquals(new Integer (0), t.translate(null, null, null, null)); 48 } 49 50 public void testIntLow() 51 { 52 IntTranslator t = new IntTranslator("min=5,max=200"); 53 54 try 55 { 56 t.translate(null, null, "3", null); 57 unreachable(); 58 } 59 catch (ApplicationRuntimeException ex) 60 { 61 assertExceptionSubstring(ex, RulesMessages.minIntValue("3", 5)); 62 } 63 } 64 65 public void testIntHigh() 66 { 67 IntTranslator t = new IntTranslator("min=5,max=200"); 68 69 try 70 { 71 t.translate(null, null, "50900", null); 72 unreachable(); 73 } 74 catch (ApplicationRuntimeException ex) 75 { 76 assertExceptionSubstring(ex, RulesMessages.maxIntValue("50900", 200)); 77 } 78 79 } 80 81 public void testIntDefaultValue() 82 { 83 IntTranslator t = new IntTranslator("default=7"); 84 85 assertEquals(new Integer (7), t.translate(null, null, null, null)); 86 } 87 88 public void testIntInvalid() 89 { 90 IntTranslator t = new IntTranslator("default=13"); 91 92 try 93 { 94 t.translate(null, null, "qbert", null); 95 unreachable(); 96 } 97 catch (ApplicationRuntimeException ex) 98 { 99 100 assertExceptionSubstring(ex, "'qbert' is not an integer value."); 101 } 102 } 103 104 public void testLongTranslator() 105 { 106 LongTranslator t = new LongTranslator(); 107 108 assertEquals(new Long (10), t.translate(null, null, "10", null)); 109 } 110 111 public void testLongDefault() 112 { 113 LongTranslator t = new LongTranslator(); 114 115 assertEquals(new Long (0), t.translate(null, null, null, null)); 116 } 117 118 public void testLongLow() 119 { 120 LongTranslator t = new LongTranslator("min=5,max=200"); 121 122 try 123 { 124 125 t.translate(null, null, "3", null); 126 unreachable(); 127 } 128 catch (ApplicationRuntimeException ex) 129 { 130 131 assertExceptionSubstring(ex, RulesMessages.minLongValue("3", 5)); 132 } 133 } 134 135 public void testLongHigh() 136 { 137 138 LongTranslator t = new LongTranslator("min=5,max=200"); 139 140 try 141 { 142 143 t.translate(null, null, "50900", null); 144 unreachable(); 145 } 146 catch (ApplicationRuntimeException ex) 147 { 148 149 assertExceptionSubstring(ex, RulesMessages.maxLongValue("50900", 200)); 150 } 151 } 152 153 public void testLongDefaultValue() 154 { 155 LongTranslator t = new LongTranslator("default=7"); 156 157 assertEquals(new Long (7), t.translate(null, null, null, null)); 158 } 159 160 public void testLongInvalid() 161 { 162 LongTranslator t = new LongTranslator("default=13"); 163 164 try 165 { 166 167 t.translate(null, null, "qbert", null); 168 unreachable(); 169 } 170 catch (ApplicationRuntimeException ex) 171 { 172 assertExceptionSubstring(ex, "'qbert' is not a long value."); 173 } 174 } 175 176 180 public void testDoubleTranslator() 181 { 182 DoubleTranslator t = new DoubleTranslator(); 183 184 assertEquals(new Double (10.7), t.translate(null, null, "10.7", null)); 185 } 186 187 public void testDoubleDefault() 188 { 189 DoubleTranslator t = new DoubleTranslator(); 190 191 assertEquals(new Double (0), t.translate(null, null, null, null)); 192 } 193 194 public void testDoubleLow() 195 { 196 DoubleTranslator t = new DoubleTranslator("min=5.25,max=200"); 197 198 try 199 { 200 t.translate(null, null, "3", null); 201 unreachable(); 202 } 203 catch (ApplicationRuntimeException ex) 204 { 205 assertExceptionSubstring(ex, RulesMessages.minDoubleValue("3", 5.25)); 206 } 207 } 208 209 public void testDoubleHigh() 210 { 211 DoubleTranslator t = new DoubleTranslator("min=07,max=207.5"); 212 213 try 214 { 215 t.translate(null, null, "208.3", null); 216 unreachable(); 217 } 218 catch (ApplicationRuntimeException ex) 219 { 220 assertExceptionSubstring(ex, RulesMessages.maxDoubleValue("208.3", 207.5)); 221 } 222 } 223 224 public void testDoubleDefaultValue() 225 { 226 DoubleTranslator t = new DoubleTranslator("default=7.77"); 227 228 assertEquals(new Double (7.77), t.translate(null, null, null, null)); 229 } 230 231 public void testDoubleInvalid() 232 { 233 DoubleTranslator t = new DoubleTranslator("default=13"); 234 235 try 236 { 237 t.translate(null, null, "qbert", null); 238 unreachable(); 239 } 240 catch (ApplicationRuntimeException ex) 241 { 242 assertExceptionSubstring(ex, "'qbert' is not a double value."); 243 } 244 } 245 246 } 247 | Popular Tags |