Source code: hivemind/test/rules/TestInstanceTranslator.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 hivemind.test.rules;
16
17 import hivemind.test.FrameworkTestCase;
18
19 import java.util.List;
20
21 import org.apache.hivemind.ApplicationRuntimeException;
22 import org.apache.hivemind.Location;
23 import org.apache.hivemind.Registry;
24 import org.apache.hivemind.impl.ElementImpl;
25 import org.apache.hivemind.impl.LocationImpl;
26 import org.apache.hivemind.impl.ModuleImpl;
27 import org.apache.hivemind.internal.Module;
28 import org.apache.hivemind.schema.rules.ClassTranslator;
29 import org.apache.hivemind.schema.rules.InstanceTranslator;
30
31 /**
32 * Fill in some gaps in {@link org.apache.hivemind.schema.rules.InstanceTranslator} and
33 * {@link org.apache.hivemind.schema.rules.ClassTranslator}.
34 *
35 * @author Howard Lewis Ship
36 */
37 public class TestInstanceTranslator extends FrameworkTestCase
38 {
39
40 public void testNull()
41 {
42 InstanceTranslator t = new InstanceTranslator();
43
44 assertNull(t.translate(null, null, null, null));
45 }
46
47 protected Module newModule()
48 {
49 return (Module) newMock(Module.class);
50 }
51
52 public void testBadClass() throws Exception
53 {
54 InstanceTranslator t = new InstanceTranslator();
55 ElementImpl e = new ElementImpl();
56 Location l = new LocationImpl(getResource("TestInstanceTranslator.class"), 50);
57 e.setLocation(l);
58
59 Module m = newModule();
60
61 m.resolveType("bad.class.Name");
62 ApplicationRuntimeException cause = new ApplicationRuntimeException("failure");
63 setThrowable(m, cause);
64
65 replayControls();
66
67 try
68 {
69 t.translate(m, null, "bad.class.Name", null);
70 }
71 catch (ApplicationRuntimeException ex)
72 {
73 assertSame(cause, ex);
74 }
75
76 verifyControls();
77 }
78
79 public void testInitializer() throws Exception
80 {
81 InstanceTranslator t = new InstanceTranslator();
82
83 Module m = newModule();
84
85 m.resolveType("Bean");
86 setReturnValue(m, IntHolder.class);
87
88 replayControls();
89
90 IntHolder ih = (IntHolder) t.translate(m, Object.class, "Bean,value=37", null);
91
92 assertEquals(37, ih.getValue());
93
94 verifyControls();
95 }
96
97 public void testPrivateObject() throws Exception
98 {
99 InstanceTranslator t = new InstanceTranslator();
100 ElementImpl e = new ElementImpl();
101 Location l = new LocationImpl(getResource("TestInstanceTranslator.class"), 50);
102 e.setLocation(l);
103
104 ModuleImpl m = new ModuleImpl();
105 m.setClassResolver(getClassResolver());
106
107 replayControls();
108
109 try
110 {
111 t.translate(m, null, PrivateObject.class.getName(), null);
112 unreachable();
113 }
114 catch (ApplicationRuntimeException ex)
115 {
116 assertExceptionSubstring(
117 ex,
118 "Unable to instantiate instance of class hivemind.test.rules.PrivateObject");
119 }
120
121 verifyControls();
122 }
123
124 public void testWrongType() throws Exception
125 {
126 Registry r = buildFrameworkRegistry("WrongType.xml");
127
128 interceptLogging();
129
130 List l = r.getConfiguration("hivemind.test.rules.WrongType");
131
132 // Convert the proxy into a real list; this will trigger the
133 // expected errors.
134
135 l.size();
136
137 assertLoggedMessagePattern("Unable to update property value of object hivemind\\.test\\.config\\.impl\\.Datum@");
138 }
139
140 public void testClassTranslator() throws Exception
141 {
142 ModuleImpl m = new ModuleImpl();
143 m.setClassResolver(getClassResolver());
144
145 replayControls();
146
147 ClassTranslator t = new ClassTranslator();
148
149 Class c = (Class) t.translate(m, null, getClass().getName(), null);
150
151 assertEquals(getClass(), c);
152
153 verifyControls();
154 }
155 }