KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > rules > TestNumericTranslators


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 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 /**
26  * Tests the numeric translators.
27  *
28  * @author Howard Lewis Ship
29  */

30 public class TestNumericTranslators extends FrameworkTestCase
31 {
32     /**
33     * Tests {@link org.apache.hivemind.schema.rules.IntTranslator} with
34     * the default constructor.
35     */

36     public void testIntTranslator()
37     {
38         IntTranslator t = new IntTranslator();
39
40         assertEquals(new Integer JavaDoc(10), t.translate(null, null, "10", null));
41     }
42
43     public void testIntDefault()
44     {
45         IntTranslator t = new IntTranslator();
46
47         assertEquals(new Integer JavaDoc(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 JavaDoc(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 JavaDoc(10), t.translate(null, null, "10", null));
109     }
110
111     public void testLongDefault()
112     {
113         LongTranslator t = new LongTranslator();
114
115         assertEquals(new Long JavaDoc(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 JavaDoc(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     /**
177      * Tests {@link org.apache.hivemind.schema.rules.IntTranslator} with
178      * the default constructor.
179      */

180     public void testDoubleTranslator()
181     {
182         DoubleTranslator t = new DoubleTranslator();
183
184         assertEquals(new Double JavaDoc(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 JavaDoc(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 JavaDoc(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