KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List 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.impl.ElementImpl;
23 import org.apache.hivemind.impl.LocationImpl;
24 import org.apache.hivemind.impl.ModuleImpl;
25 import org.apache.hivemind.internal.Module;
26 import org.apache.hivemind.schema.rules.ClassTranslator;
27 import org.apache.hivemind.schema.rules.InstanceTranslator;
28 import org.apache.hivemind.xml.XmlTestCase;
29
30 /**
31  * Fill in some gaps in {@link org.apache.hivemind.schema.rules.InstanceTranslator} and
32  * {@link org.apache.hivemind.schema.rules.ClassTranslator}.
33  *
34  * @author Howard Lewis Ship
35  */

36 public class TestInstanceTranslator extends XmlTestCase
37 {
38
39     public void testNull()
40     {
41         InstanceTranslator t = new InstanceTranslator();
42
43         assertNull(t.translate(null, null, null, null));
44     }
45
46     protected Module newModule()
47     {
48         return (Module) newMock(Module.class);
49     }
50
51     public void testBadClass() throws Exception JavaDoc
52     {
53         InstanceTranslator t = new InstanceTranslator();
54         ElementImpl e = new ElementImpl();
55         Location l = new LocationImpl(getResource("TestInstanceTranslator.class"), 50);
56         e.setLocation(l);
57
58         Module m = newModule();
59
60         m.resolveType("bad.class.Name");
61         ApplicationRuntimeException cause = new ApplicationRuntimeException("failure");
62         setThrowable(m, cause);
63
64         replayControls();
65
66         try
67         {
68             t.translate(m, null, "bad.class.Name", null);
69         }
70         catch (ApplicationRuntimeException ex)
71         {
72             assertSame(cause, ex);
73         }
74
75         verifyControls();
76     }
77
78     public void testInitializer() throws Exception JavaDoc
79     {
80         InstanceTranslator t = new InstanceTranslator();
81
82         Module m = newModule();
83
84         m.resolveType("Bean");
85         setReturnValue(m, IntHolder.class);
86
87         replayControls();
88
89         IntHolder ih = (IntHolder) t.translate(m, Object JavaDoc.class, "Bean,value=37", null);
90
91         assertEquals(37, ih.getValue());
92
93         verifyControls();
94     }
95
96     public void testPrivateObject() throws Exception JavaDoc
97     {
98         InstanceTranslator t = new InstanceTranslator();
99         ElementImpl e = new ElementImpl();
100         Location l = new LocationImpl(getResource("TestInstanceTranslator.class"), 50);
101         e.setLocation(l);
102
103         ModuleImpl m = new ModuleImpl();
104         m.setClassResolver(getClassResolver());
105
106         replayControls();
107
108         try
109         {
110             t.translate(m, null, PrivateObject.class.getName(), null);
111             unreachable();
112         }
113         catch (ApplicationRuntimeException ex)
114         {
115             assertExceptionSubstring(
116                     ex,
117                     "Unable to instantiate instance of class hivemind.test.rules.PrivateObject");
118         }
119
120         verifyControls();
121     }
122
123     public void testWrongType() throws Exception JavaDoc
124     {
125         Registry r = buildFrameworkRegistry("WrongType.xml");
126
127         interceptLogging();
128
129         List JavaDoc l = (List JavaDoc) r.getConfiguration("hivemind.test.rules.WrongType");
130
131         // Convert the proxy into a real list; this will trigger the
132
// expected errors.
133

134         l.size();
135
136         assertLoggedMessagePattern("Unable to update property value of object hivemind\\.test\\.config\\.impl\\.Datum@");
137     }
138
139     public void testClassTranslator() throws Exception JavaDoc
140     {
141         ModuleImpl m = new ModuleImpl();
142         m.setClassResolver(getClassResolver());
143
144         replayControls();
145
146         ClassTranslator t = new ClassTranslator();
147
148         Class JavaDoc c = (Class JavaDoc) t.translate(m, null, getClass().getName(), null);
149
150         assertEquals(getClass(), c);
151
152         verifyControls();
153     }
154 }
Popular Tags