Source code: org/apache/hivemind/schema/rules/TestNumericTranslators.java
1 // Copyright 2004, 2005 The Apache Software Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package org.apache.hivemind.schema.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
24 /**
25 * Tests the numeric translators.
26 *
27 * @author Howard Lewis Ship
28 */
29 public class TestNumericTranslators extends FrameworkTestCase
30 {
31 /**
32 * Tests {@link org.apache.hivemind.schema.rules.IntTranslator} with
33 * the default constructor.
34 */
35 public void testIntTranslator()
36 {
37 IntTranslator t = new IntTranslator();
38
39 assertEquals(new Integer(10), t.translate(null, null, "10", null));
40 }
41
42 public void testIntDefault()
43 {
44 IntTranslator t = new IntTranslator();
45
46 assertEquals(new Integer(0), t.translate(null, null, null, null));
47 }
48
49 public void testIntLow()
50 {
51 IntTranslator t = new IntTranslator("min=5,max=200");
52
53 try
54 {
55 t.translate(null, null, "3", null);
56 unreachable();
57 }
58 catch (ApplicationRuntimeException ex)
59 {
60 assertExceptionSubstring(ex, RulesMessages.minIntValue("3", 5));
61 }
62 }
63
64 public void testIntHigh()
65 {
66 IntTranslator t = new IntTranslator("min=5,max=200");
67
68 try
69 {
70 t.translate(null, null, "50900", null);
71 unreachable();
72 }
73 catch (ApplicationRuntimeException ex)
74 {
75 assertExceptionSubstring(ex, RulesMessages.maxIntValue("50900", 200));
76 }
77
78 }
79
80 public void testIntDefaultValue()
81 {
82 IntTranslator t = new IntTranslator("default=7");
83
84 assertEquals(new Integer(7), t.translate(null, null, null, null));
85 }
86
87 public void testIntInvalid()
88 {
89 IntTranslator t = new IntTranslator("default=13");
90
91 try
92 {
93 t.translate(null, null, "qbert", null);
94 unreachable();
95 }
96 catch (ApplicationRuntimeException ex)
97 {
98
99 assertExceptionSubstring(ex, "'qbert' is not an integer value.");
100 }
101 }
102
103 public void testLongTranslator()
104 {
105 LongTranslator t = new LongTranslator();
106
107 assertEquals(new Long(10), t.translate(null, null, "10", null));
108 }
109
110 public void testLongDefault()
111 {
112 LongTranslator t = new LongTranslator();
113
114 assertEquals(new Long(0), t.translate(null, null, null, null));
115 }
116
117 public void testLongLow()
118 {
119 LongTranslator t = new LongTranslator("min=5,max=200");
120
121 try
122 {
123
124 t.translate(null, null, "3", null);
125 unreachable();
126 }
127 catch (ApplicationRuntimeException ex)
128 {
129
130 assertExceptionSubstring(ex, RulesMessages.minLongValue("3", 5));
131 }
132 }
133
134 public void testLongHigh()
135 {
136
137 LongTranslator t = new LongTranslator("min=5,max=200");
138
139 try
140 {
141
142 t.translate(null, null, "50900", null);
143 unreachable();
144 }
145 catch (ApplicationRuntimeException ex)
146 {
147
148 assertExceptionSubstring(ex, RulesMessages.maxLongValue("50900", 200));
149 }
150 }
151
152 public void testLongDefaultValue()
153 {
154 LongTranslator t = new LongTranslator("default=7");
155
156 assertEquals(new Long(7), t.translate(null, null, null, null));
157 }
158
159 public void testLongInvalid()
160 {
161 LongTranslator t = new LongTranslator("default=13");
162
163 try
164 {
165
166 t.translate(null, null, "qbert", null);
167 unreachable();
168 }
169 catch (ApplicationRuntimeException ex)
170 {
171 assertExceptionSubstring(ex, "'qbert' is not a long value.");
172 }
173 }
174
175 /**
176 * Tests {@link org.apache.hivemind.schema.rules.IntTranslator} with
177 * the default constructor.
178 */
179 public void testDoubleTranslator()
180 {
181 DoubleTranslator t = new DoubleTranslator();
182
183 assertEquals(new Double(10.7), t.translate(null, null, "10.7", null));
184 }
185
186 public void testDoubleDefault()
187 {
188 DoubleTranslator t = new DoubleTranslator();
189
190 assertEquals(new Double(0), t.translate(null, null, null, null));
191 }
192
193 public void testDoubleLow()
194 {
195 DoubleTranslator t = new DoubleTranslator("min=5.25,max=200");
196
197 try
198 {
199 t.translate(null, null, "3", null);
200 unreachable();
201 }
202 catch (ApplicationRuntimeException ex)
203 {
204 assertExceptionSubstring(ex, RulesMessages.minDoubleValue("3", 5.25));
205 }
206 }
207
208 public void testDoubleHigh()
209 {
210 DoubleTranslator t = new DoubleTranslator("min=07,max=207.5");
211
212 try
213 {
214 t.translate(null, null, "208.3", null);
215 unreachable();
216 }
217 catch (ApplicationRuntimeException ex)
218 {
219 assertExceptionSubstring(ex, RulesMessages.maxDoubleValue("208.3", 207.5));
220 }
221 }
222
223 public void testDoubleDefaultValue()
224 {
225 DoubleTranslator t = new DoubleTranslator("default=7.77");
226
227 assertEquals(new Double(7.77), t.translate(null, null, null, null));
228 }
229
230 public void testDoubleInvalid()
231 {
232 DoubleTranslator t = new DoubleTranslator("default=13");
233
234 try
235 {
236 t.translate(null, null, "qbert", null);
237 unreachable();
238 }
239 catch (ApplicationRuntimeException ex)
240 {
241 assertExceptionSubstring(ex, "'qbert' is not a double value.");
242 }
243 }
244
245 }