KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hivemind > test > lib > TestDefaultImplementationBuilder


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.lib;
16
17 import org.apache.hivemind.ApplicationRuntimeException;
18 import org.apache.hivemind.Registry;
19 import org.apache.hivemind.ServiceImplementationFactoryParameters;
20 import org.apache.hivemind.lib.DefaultImplementationBuilder;
21 import org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl;
22 import org.apache.hivemind.lib.impl.PlaceholderFactory;
23 import org.apache.hivemind.service.impl.ClassFactoryImpl;
24 import org.apache.hivemind.xml.XmlTestCase;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl} and
29  * {@link org.apache.hivemind.lib.impl.PlaceholderFactory}.
30  *
31  * @author Howard Lewis Ship
32  */

33 public class TestDefaultImplementationBuilder extends XmlTestCase
34 {
35     private DefaultImplementationBuilder _builder;
36
37     protected void setUp() throws Exception JavaDoc
38     {
39         super.setUp();
40
41         DefaultImplementationBuilderImpl bi = new DefaultImplementationBuilderImpl();
42
43         bi.setClassFactory(new ClassFactoryImpl());
44
45         _builder = bi;
46     }
47
48     private Object JavaDoc create(Class JavaDoc interfaceType)
49     {
50         replayControls();
51
52         return _builder.buildDefaultImplementation(interfaceType);
53     }
54
55     public void testSimple()
56     {
57         Runnable JavaDoc r = (Runnable JavaDoc) create(Runnable JavaDoc.class);
58
59         r.run();
60
61         assertEquals("<Default implementation of interface java.lang.Runnable>", r.toString());
62
63         verifyControls();
64     }
65
66     public void testComplex()
67     {
68         ValueHolder vh = (ValueHolder) create(ValueHolder.class);
69
70         assertNull(vh.getStringValue());
71         assertEquals(false, vh.getBooleanValue());
72         assertEquals(0, vh.getIntValue());
73
74         verifyControls();
75     }
76
77     public void testToStringInInterface()
78     {
79         ToString ts = (ToString) create(ToString.class);
80
81         assertNull(ts.toString());
82
83         verifyControls();
84     }
85
86     public void testCache()
87     {
88         Runnable JavaDoc r1 = (Runnable JavaDoc) create(Runnable JavaDoc.class);
89         Runnable JavaDoc r2 = (Runnable JavaDoc) _builder.buildDefaultImplementation(Runnable JavaDoc.class);
90
91         assertSame(r1, r2);
92
93         verifyControls();
94     }
95
96     public void testNotInterface()
97     {
98         try
99         {
100             create(String JavaDoc.class);
101             unreachable();
102         }
103         catch (ApplicationRuntimeException ex)
104         {
105             assertExceptionSubstring(ex, "Class java.lang.String is not an interface.");
106         }
107     }
108
109     public void testModuleDescriptor() throws Exception JavaDoc
110     {
111         Registry r = buildFrameworkRegistry("DefaultImplementationBuilder.xml");
112
113         DefaultImplementationBuilder dib = (DefaultImplementationBuilder) r.getService(
114                 "hivemind.lib.DefaultImplementationBuilder",
115                 DefaultImplementationBuilder.class);
116
117         replayControls();
118
119         Runnable JavaDoc o = (Runnable JavaDoc) dib.buildDefaultImplementation(Runnable JavaDoc.class);
120
121         o.run();
122
123         verifyControls();
124     }
125
126     public void testPlaceholderBuilderSimulated() throws Exception JavaDoc
127     {
128         MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
129         ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
130                 .getMock();
131
132         PlaceholderFactory db = new PlaceholderFactory();
133
134         db.setBuilder(_builder);
135
136         fp.getServiceInterface();
137         fpc.setReturnValue(Runnable JavaDoc.class);
138
139         replayControls();
140
141         Runnable JavaDoc r = (Runnable JavaDoc) db.createCoreServiceImplementation(fp);
142
143         r.run();
144
145         verifyControls();
146     }
147
148     public void testPlaceholderFactory() throws Exception JavaDoc
149     {
150         Registry r = buildFrameworkRegistry("DefaultImplementationBuilder.xml");
151
152         Runnable JavaDoc o = (Runnable JavaDoc) r.getService("hivemind.test.lib.Runnable", Runnable JavaDoc.class);
153
154         o.run();
155     }
156 }
Popular Tags