KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.beans.PropertyEditorManager JavaDoc;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.Registry;
22 import org.apache.hivemind.schema.Translator;
23 import org.apache.hivemind.schema.rules.SmartTranslator;
24 import org.apache.hivemind.test.HiveMindTestCase;
25
26 /**
27  * Tests for {@link org.apache.hivemind.schema.rules.SmartTranslator}.
28  *
29  * @author Howard Lewis Ship
30  */

31 public class TestSmartTranslator extends HiveMindTestCase
32 {
33
34     /**
35      * Test a primitive type (int).
36      */

37     public void testInt()
38     {
39         Translator t = new SmartTranslator();
40
41         Object JavaDoc result = t.translate(null, int.class, "-37", null);
42
43         assertEquals(new Integer JavaDoc(-37), result);
44     }
45
46     public void testNullInput()
47     {
48         Translator t = new SmartTranslator();
49
50         assertNull(t.translate(null, int.class, null, null));
51     }
52
53     public void testBlankInput()
54     {
55         Translator t = new SmartTranslator();
56
57         assertEquals("", t.translate(null, String JavaDoc.class, "", null));
58     }
59
60     public void testDefault()
61     {
62         Translator t = new SmartTranslator("default=100");
63
64         Object JavaDoc result = t.translate(null, int.class, null, null);
65
66         assertEquals(new Integer JavaDoc(100), result);
67     }
68
69     /**
70      * Test a wrapper type (Double).
71      */

72     public void testDouble()
73     {
74         Translator t = new SmartTranslator();
75
76         Object JavaDoc result = t.translate(null, Double JavaDoc.class, "3.14", null);
77
78         assertEquals(new Double JavaDoc("3.14"), result);
79     }
80
81     /**
82      * Test with a String value (apparently, this doesn't always work, see bug
83      * HIVEMIND-15).
84      */

85     public void testString()
86     {
87         Translator t = new SmartTranslator();
88
89         Object JavaDoc result = t.translate(null, String JavaDoc.class, "Fluffy Puppies", null);
90
91         assertEquals("Fluffy Puppies", result);
92     }
93
94     /**
95      * The input value should be returned as is (i.e. as a String) when the
96      * property type is Object (see HIVEMIND-15).
97      */

98     public void testObjectAsString()
99     {
100         Translator t = new SmartTranslator();
101
102         Object JavaDoc result = t.translate(null, Object JavaDoc.class, "Fluffy Puppies", null);
103
104         assertEquals("Fluffy Puppies", result);
105     }
106
107     public void testStringWithNoEditor()
108     {
109         String JavaDoc[] paths = PropertyEditorManager.getEditorSearchPath();
110
111         try
112         {
113             PropertyEditorManager.setEditorSearchPath(new String JavaDoc[] { "bogus.package" });
114             Translator t = new SmartTranslator();
115
116             Object JavaDoc result = t.translate(null, String JavaDoc.class, "Fluffy Puppies", null);
117
118             assertEquals("Fluffy Puppies", result);
119         }
120         finally
121         {
122             PropertyEditorManager.setEditorSearchPath(paths);
123         }
124
125     }
126
127     public void testNoEditor()
128     {
129         Translator t = new SmartTranslator();
130         Location l = newLocation();
131
132         try
133         {
134             t.translate(null, Registry.class, "fred", l);
135
136             unreachable();
137         }
138         catch (ApplicationRuntimeException ex)
139         {
140             assertEquals("Unable to translate 'fred' to type org.apache.hivemind.Registry: "
141                     + "No property editor for org.apache.hivemind.Registry.", ex.getMessage());
142
143             assertSame(l, ex.getLocation());
144         }
145     }
146 }
147
Popular Tags