KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > TestInstanceCreationUtils


1 // Copyright 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 org.apache.hivemind.util;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Location;
19 import org.apache.hivemind.impl.BaseLocatable;
20 import org.apache.hivemind.internal.Module;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.easymock.MockControl;
23
24 /**
25  * Tests for {@link org.apache.hivemind.util.InstanceCreationUtils}.
26  *
27  * @author Howard M. Lewis Ship
28  * @since 1.1
29  */

30 public class TestInstanceCreationUtils extends HiveMindTestCase
31 {
32     public static class Bean
33     {
34         private int _value;
35
36         public int getValue()
37         {
38             return _value;
39         }
40
41         public void setValue(int value)
42         {
43             _value = value;
44         }
45     }
46
47     private Module newModule(String JavaDoc name, Class JavaDoc returnValue)
48     {
49         MockControl control = newControl(Module.class);
50         Module module = (Module) control.getMock();
51
52         module.resolveType(name);
53         control.setReturnValue(returnValue);
54
55         return module;
56     }
57
58     public void testSimple()
59     {
60         Module module = newModule("Bean", Bean.class);
61
62         replayControls();
63
64         Bean bean = (Bean) InstanceCreationUtils.createInstance(module, "Bean", null);
65
66         assertNotNull(bean);
67
68         verifyControls();
69     }
70
71     public void testComplex()
72     {
73         Module module = newModule("Bean", Bean.class);
74
75         replayControls();
76
77         Bean bean = (Bean) InstanceCreationUtils.createInstance(module, "Bean,value=42", null);
78
79         assertEquals(42, bean.getValue());
80
81         verifyControls();
82     }
83
84     public void testSetLocation()
85     {
86         Location l = newLocation();
87         Module module = newModule("Holder", BaseLocatable.class);
88
89         replayControls();
90
91         BaseLocatable holder = (BaseLocatable) InstanceCreationUtils.createInstance(
92                 module,
93                 "Holder",
94                 l);
95
96         assertSame(l, holder.getLocation());
97
98         verifyControls();
99     }
100
101     public void testFailure()
102     {
103         Location l = newLocation();
104         Module module = newModule("Bean", Bean.class);
105
106         replayControls();
107
108         try
109         {
110             InstanceCreationUtils.createInstance(module, "Bean,value=fred", l);
111             unreachable();
112         }
113         catch (ApplicationRuntimeException ex)
114         {
115             assertExceptionSubstring(
116                     ex,
117                     "Unable to instantiate instance of class org.apache.hivemind.util.TestInstanceCreationUtils$Bean");
118             assertSame(l, ex.getLocation());
119         }
120
121         verifyControls();
122     }
123 }
Popular Tags