001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package hivemind.test.lib;
016:
017: import org.apache.hivemind.ApplicationRuntimeException;
018: import org.apache.hivemind.Registry;
019: import org.apache.hivemind.ServiceImplementationFactoryParameters;
020: import org.apache.hivemind.lib.DefaultImplementationBuilder;
021: import org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl;
022: import org.apache.hivemind.lib.impl.PlaceholderFactory;
023: import org.apache.hivemind.service.impl.ClassFactoryImpl;
024: import org.apache.hivemind.test.HiveMindTestCase;
025: import org.easymock.MockControl;
026:
027: /**
028: * Tests for {@link org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl} and
029: * {@link org.apache.hivemind.lib.impl.PlaceholderFactory}.
030: *
031: * @author Howard Lewis Ship
032: */
033: public class TestDefaultImplementationBuilder extends HiveMindTestCase {
034: private DefaultImplementationBuilder _builder;
035:
036: protected void setUp() throws Exception {
037: super .setUp();
038:
039: DefaultImplementationBuilderImpl bi = new DefaultImplementationBuilderImpl();
040:
041: bi.setClassFactory(new ClassFactoryImpl());
042:
043: _builder = bi;
044: }
045:
046: private Object create(Class interfaceType) {
047: replayControls();
048:
049: return _builder.buildDefaultImplementation(interfaceType);
050: }
051:
052: public void testSimple() {
053: Runnable r = (Runnable) create(Runnable.class);
054:
055: r.run();
056:
057: assertEquals(
058: "<Default implementation of interface java.lang.Runnable>",
059: r.toString());
060:
061: verifyControls();
062: }
063:
064: public void testComplex() {
065: ValueHolder vh = (ValueHolder) create(ValueHolder.class);
066:
067: assertNull(vh.getStringValue());
068: assertEquals(false, vh.getBooleanValue());
069: assertEquals(0, vh.getIntValue());
070:
071: verifyControls();
072: }
073:
074: public void testToStringInInterface() {
075: ToString ts = (ToString) create(ToString.class);
076:
077: assertNull(ts.toString());
078:
079: verifyControls();
080: }
081:
082: public void testCache() {
083: Runnable r1 = (Runnable) create(Runnable.class);
084: Runnable r2 = (Runnable) _builder
085: .buildDefaultImplementation(Runnable.class);
086:
087: assertSame(r1, r2);
088:
089: verifyControls();
090: }
091:
092: public void testNotInterface() {
093: try {
094: create(String.class);
095: unreachable();
096: } catch (ApplicationRuntimeException ex) {
097: assertExceptionSubstring(ex,
098: "Class java.lang.String is not an interface.");
099: }
100: }
101:
102: public void testModuleDescriptor() throws Exception {
103: Registry r = buildFrameworkRegistry("DefaultImplementationBuilder.xml");
104:
105: DefaultImplementationBuilder dib = (DefaultImplementationBuilder) r
106: .getService(
107: "hivemind.lib.DefaultImplementationBuilder",
108: DefaultImplementationBuilder.class);
109:
110: replayControls();
111:
112: Runnable o = (Runnable) dib
113: .buildDefaultImplementation(Runnable.class);
114:
115: o.run();
116:
117: verifyControls();
118: }
119:
120: public void testPlaceholderBuilderSimulated() throws Exception {
121: MockControl fpc = newControl(ServiceImplementationFactoryParameters.class);
122: ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc
123: .getMock();
124:
125: PlaceholderFactory db = new PlaceholderFactory();
126:
127: db.setBuilder(_builder);
128:
129: fp.getServiceInterface();
130: fpc.setReturnValue(Runnable.class);
131:
132: replayControls();
133:
134: Runnable r = (Runnable) db.createCoreServiceImplementation(fp);
135:
136: r.run();
137:
138: verifyControls();
139: }
140:
141: public void testPlaceholderFactory() throws Exception {
142: Registry r = buildFrameworkRegistry("DefaultImplementationBuilder.xml");
143:
144: Runnable o = (Runnable) r.getService(
145: "hivemind.test.lib.Runnable", Runnable.class);
146:
147: o.run();
148: }
149: }
|