KickJava   Java API By Example, From Geeks To Geeks.

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


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.impl.ModuleImpl;
21 import org.apache.hivemind.internal.Module;
22 import org.apache.hivemind.schema.rules.EnumerationTranslator;
23
24 /**
25  * Tests for {@link org.apache.hivemind.schema.rules.EnumerationTranslator}.
26  *
27  * @author Howard Lewis Ship
28  */

29 public class TestEnumerationTranslator extends FrameworkTestCase
30 {
31
32     private Module newModule()
33     {
34         ModuleImpl result = new ModuleImpl();
35         result.setClassResolver(getClassResolver());
36
37         return result;
38     }
39
40     public void testNull()
41     {
42         Module m = (Module) newMock(Module.class);
43
44         EnumerationTranslator t = new EnumerationTranslator(
45                 "java.lang.Boolean,true=TRUE,false=FALSE");
46
47         assertEquals(null, t.translate(m, null, null, null));
48         ;
49     }
50
51     public void testMatch()
52     {
53         Module m = newModule();
54
55         EnumerationTranslator t = new EnumerationTranslator(
56                 "java.lang.Boolean,true=TRUE,false=FALSE");
57
58         assertEquals(Boolean.TRUE, t.translate(m, null, "true", null));
59         assertEquals(Boolean.FALSE, t.translate(m, null, "false", null));
60     }
61
62     public void testBadClass()
63     {
64         Module m = newModule();
65
66         EnumerationTranslator t = new EnumerationTranslator(
67                 "lava.jang.Boolean,true=TRUE,false=FALSE");
68
69         try
70         {
71             t.translate(m, null, "true", null);
72
73             unreachable();
74         }
75         catch (ApplicationRuntimeException ex)
76         {
77             assertExceptionSubstring(ex, "Unable to convert type 'lava.jang.Boolean'");
78         }
79     }
80
81     public void testUnrecognizedValue() throws Exception JavaDoc
82     {
83         Module m = newModule();
84
85         EnumerationTranslator t = new EnumerationTranslator(
86                 "java.lang.Boolean,true=TRUE,false=FALSE");
87
88         try
89         {
90
91             t.translate(m, null, "fred", null);
92             unreachable();
93         }
94         catch (ApplicationRuntimeException ex)
95         {
96             assertExceptionSubstring(ex, "'fred' is not a recognized enumerated value.");
97         }
98     }
99
100     public void testBadField() throws Exception JavaDoc
101     {
102         Module m = newModule();
103
104         EnumerationTranslator t = new EnumerationTranslator(
105                 "java.lang.Boolean,true=HONEST_TO_GOD_TRUE,false=FALSE");
106
107         try
108         {
109             t.translate(m, null, "true", null);
110             unreachable();
111         }
112         catch (ApplicationRuntimeException ex)
113         {
114             assertExceptionSubstring(
115                     ex,
116                     "Unable to obtain value for static field java.lang.Boolean.HONEST_TO_GOD_TRUE");
117         }
118     }
119
120 }
Popular Tags