Source Code Cross Referenced for TestBuilderFactory.java in  » Inversion-of-Control » hivemind » hivemind » test » services » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Inversion of Control » hivemind » hivemind.test.services 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.services;
016:
017:        import hivemind.test.services.impl.StringHolderImpl;
018:
019:        import org.apache.commons.logging.Log;
020:        import org.apache.hivemind.ApplicationRuntimeException;
021:        import org.apache.hivemind.ClassResolver;
022:        import org.apache.hivemind.ErrorHandler;
023:        import org.apache.hivemind.ErrorLog;
024:        import org.apache.hivemind.Messages;
025:        import org.apache.hivemind.Registry;
026:        import org.apache.hivemind.ServiceImplementationFactoryParameters;
027:        import org.apache.hivemind.internal.Module;
028:        import org.apache.hivemind.service.impl.BuilderClassResolverFacet;
029:        import org.apache.hivemind.service.impl.BuilderErrorHandlerFacet;
030:        import org.apache.hivemind.service.impl.BuilderErrorLogFacet;
031:        import org.apache.hivemind.service.impl.BuilderFacet;
032:        import org.apache.hivemind.service.impl.BuilderFactoryLogic;
033:        import org.apache.hivemind.service.impl.BuilderLogFacet;
034:        import org.apache.hivemind.service.impl.BuilderMessagesFacet;
035:        import org.apache.hivemind.service.impl.BuilderParameter;
036:        import org.apache.hivemind.service.impl.BuilderServiceIdFacet;
037:        import org.apache.hivemind.test.HiveMindTestCase;
038:        import org.easymock.MockControl;
039:
040:        /**
041:         * Tests for the standard {@link org.apache.hivemind.service.impl.BuilderFactory} service and
042:         * various implementations of {@link org.apache.hivemind.service.impl.BuilderFacet}.
043:         * 
044:         * @author Howard Lewis Ship
045:         */
046:        public class TestBuilderFactory extends HiveMindTestCase {
047:            private Object execute(ServiceImplementationFactoryParameters fp,
048:                    BuilderParameter p) {
049:                return new BuilderFactoryLogic(fp, p).createService();
050:            }
051:
052:            public void testSmartFacet() throws Exception {
053:                Registry r = buildFrameworkRegistry("SmartFacet.xml");
054:
055:                SimpleService s = (SimpleService) r.getService(
056:                        "hivemind.test.services.Simple", SimpleService.class);
057:
058:                assertEquals(99, s.add(1, 1));
059:            }
060:
061:            public void testInitializeMethodFailure() throws Exception {
062:                Registry r = buildFrameworkRegistry("InitializeMethodFailure.xml");
063:
064:                Runnable s = (Runnable) r.getService(
065:                        "hivemind.test.services.Runnable", Runnable.class);
066:
067:                interceptLogging("hivemind.test.services.Runnable");
068:
069:                s.run();
070:
071:                assertLoggedMessagePattern("Error at .*?: Unable to initialize service hivemind\\.test\\.services\\.Runnable "
072:                        + "\\(by invoking method doesNotExist on "
073:                        + "hivemind\\.test\\.services\\.impl\\.MockRunnable\\):");
074:            }
075:
076:            public void testBuilderErrorHandlerFacet() {
077:                MockControl c = newControl(Module.class);
078:                Module m = (Module) c.getMock();
079:
080:                ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
081:
082:                MockControl pc = newControl(ServiceImplementationFactoryParameters.class);
083:                ServiceImplementationFactoryParameters p = (ServiceImplementationFactoryParameters) pc
084:                        .getMock();
085:
086:                p.getInvokingModule();
087:                pc.setReturnValue(m);
088:
089:                m.getErrorHandler();
090:                c.setReturnValue(eh);
091:
092:                replayControls();
093:
094:                BuilderFacet f = new BuilderErrorHandlerFacet();
095:
096:                Object actual = f.getFacetValue(p, null);
097:
098:                assertSame(eh, actual);
099:
100:                verifyControls();
101:            }
102:
103:            public void testSetErrorHandler() throws Exception {
104:                Registry r = buildFrameworkRegistry("SetErrorHandler.xml");
105:
106:                ErrorHandlerHolder h = (ErrorHandlerHolder) r.getService(
107:                        "hivemind.test.services.SetErrorHandler",
108:                        ErrorHandlerHolder.class);
109:
110:                assertNotNull(h.getErrorHandler());
111:            }
112:
113:            public void testConstructErrorHandler() throws Exception {
114:                Registry r = buildFrameworkRegistry("ConstructErrorHandler.xml");
115:
116:                ErrorHandlerHolder h = (ErrorHandlerHolder) r.getService(
117:                        "hivemind.test.services.ConstructErrorHandler",
118:                        ErrorHandlerHolder.class);
119:
120:                assertNotNull(h.getErrorHandler());
121:            }
122:
123:            public void testBuilderClassResolverFacet() {
124:                ClassResolver cr = (ClassResolver) newMock(ClassResolver.class);
125:
126:                MockControl pc = newControl(ServiceImplementationFactoryParameters.class);
127:                ServiceImplementationFactoryParameters p = (ServiceImplementationFactoryParameters) pc
128:                        .getMock();
129:
130:                MockControl control = newControl(Module.class);
131:                Module module = (Module) control.getMock();
132:
133:                p.getInvokingModule();
134:                pc.setReturnValue(module);
135:
136:                module.getClassResolver();
137:                control.setReturnValue(cr);
138:
139:                replayControls();
140:
141:                BuilderClassResolverFacet fc = new BuilderClassResolverFacet();
142:
143:                Object result = fc.getFacetValue(p, null);
144:
145:                assertSame(cr, result);
146:
147:                verifyControls();
148:            }
149:
150:            public void testSetClassResolver() throws Exception {
151:                Registry r = buildFrameworkRegistry("SetClassResolver.xml");
152:
153:                ClassResolverHolder h = (ClassResolverHolder) r.getService(
154:                        "hivemind.test.services.SetClassResolver",
155:                        ClassResolverHolder.class);
156:
157:                assertNotNull(h.getClassResolver());
158:            }
159:
160:            public void testConstructClassResolver() throws Exception {
161:                Registry r = buildFrameworkRegistry("ConstructClassResolver.xml");
162:
163:                ClassResolverHolder h = (ClassResolverHolder) r.getService(
164:                        "hivemind.test.services.ConstructClassResolver",
165:                        ClassResolverHolder.class);
166:
167:                assertNotNull(h.getClassResolver());
168:            }
169:
170:            protected ServiceImplementationFactoryParameters newParameters() {
171:                return (ServiceImplementationFactoryParameters) newMock(ServiceImplementationFactoryParameters.class);
172:            }
173:
174:            protected Module newModule() {
175:                return (Module) newMock(Module.class);
176:            }
177:
178:            protected ErrorHandler newErrorHandler() {
179:                return (ErrorHandler) newMock(ErrorHandler.class);
180:            }
181:
182:            protected Log newLog() {
183:                return (Log) newMock(Log.class);
184:            }
185:
186:            protected Messages newMessages() {
187:                return (Messages) newMock(Messages.class);
188:            }
189:
190:            protected ErrorLog newErrorLog() {
191:                return (ErrorLog) newMock(ErrorLog.class);
192:            }
193:
194:            public void testAutowire() {
195:                ServiceImplementationFactoryParameters fp = newParameters();
196:                Module module = newModule();
197:                ErrorHandler eh = newErrorHandler();
198:                Log log = newLog();
199:                Messages messages = newMessages();
200:                ErrorLog errorLog = newErrorLog();
201:
202:                // Normally I try and get all the invocations into chronological
203:                // order ... but with this refactoring, that's painful; these
204:                // are in an order that appeases junit.
205:
206:                trainGetLog(fp, log);
207:
208:                trainGetServiceId(fp, "foo.bar.Baz");
209:
210:                trainGetInvokingModule(fp, module);
211:
212:                trainResolveType(module,
213:                        "hivemind.test.services.AutowireTarget",
214:                        AutowireTarget.class);
215:
216:                trainGetLog(fp, log);
217:
218:                trainDebug(fp, log, "Autowired property log to " + log);
219:
220:                trainGetInvokingModule(fp, module);
221:
222:                trainGetClassResolver(module, getClassResolver());
223:
224:                trainDebug(fp, log, "Autowired property classResolver to "
225:                        + getClassResolver());
226:
227:                trainGetInvokingModule(fp, module);
228:
229:                trainGetMessages(module, messages);
230:
231:                trainDebug(fp, log, "Autowired property messages to "
232:                        + messages);
233:
234:                trainGetInvokingModule(fp, module);
235:
236:                trainGetErrorHandler(module, eh);
237:
238:                trainDebug(fp, log, "Autowired property errorHandler to " + eh);
239:
240:                trainGetServiceId(fp);
241:
242:                trainDebug(fp, log,
243:                        "Autowired property serviceId to foo.bar.Baz");
244:
245:                trainGetErrorLog(fp, errorLog);
246:
247:                trainDebug(fp, log, "Autowired property errorLog to "
248:                        + errorLog);
249:
250:                replayControls();
251:
252:                BuilderParameter p = new BuilderParameter();
253:
254:                p.setClassName(AutowireTarget.class.getName());
255:                p.addProperty(new BuilderLogFacet());
256:                p.addProperty(new BuilderClassResolverFacet());
257:                p.addProperty(new BuilderMessagesFacet());
258:                p.addProperty(new BuilderErrorHandlerFacet());
259:                p.addProperty(new BuilderServiceIdFacet());
260:                p.addProperty(new BuilderErrorLogFacet());
261:
262:                AutowireTarget t = (AutowireTarget) execute(fp, p);
263:
264:                assertSame(eh, t.getErrorHandler());
265:                assertSame(getClassResolver(), t.getClassResolver());
266:                assertSame(messages, t.getMessages());
267:                assertSame(log, t.getLog());
268:                assertEquals("foo.bar.Baz", t.getServiceId());
269:                assertSame(errorLog, t.getErrorLog());
270:
271:                verifyControls();
272:            }
273:
274:            private void trainGetErrorLog(
275:                    ServiceImplementationFactoryParameters fp, ErrorLog errorLog) {
276:                fp.getErrorLog();
277:                setReturnValue(fp, errorLog);
278:            }
279:
280:            private void trainGetServiceId(
281:                    ServiceImplementationFactoryParameters fp) {
282:                fp.getServiceId();
283:                setReturnValue(fp, "foo.bar.Baz");
284:            }
285:
286:            private void trainGetErrorHandler(Module module, ErrorHandler eh) {
287:                module.getErrorHandler();
288:                setReturnValue(module, eh);
289:            }
290:
291:            private void trainGetMessages(Module module, Messages messages) {
292:                module.getMessages();
293:                setReturnValue(module, messages);
294:            }
295:
296:            private void trainGetClassResolver(Module module,
297:                    ClassResolver resolver) {
298:                module.getClassResolver();
299:                setReturnValue(module, resolver);
300:            }
301:
302:            private void trainResolveType(Module module, String typeName,
303:                    Class type) {
304:                module.resolveType(typeName);
305:                setReturnValue(module, type);
306:            }
307:
308:            private void trainGetInvokingModule(
309:                    ServiceImplementationFactoryParameters fp, Module module) {
310:                fp.getInvokingModule();
311:                setReturnValue(fp, module);
312:            }
313:
314:            protected void trainGetServiceId(
315:                    ServiceImplementationFactoryParameters fp, String serviceId) {
316:                fp.getServiceId();
317:                setReturnValue(fp, serviceId);
318:            }
319:
320:            protected void trainGetLog(
321:                    ServiceImplementationFactoryParameters fp, Log log) {
322:                fp.getLog();
323:                setReturnValue(fp, log);
324:            }
325:
326:            private void trainDebug(ServiceImplementationFactoryParameters fp,
327:                    Log log, String string) {
328:                fp.getLog();
329:                setReturnValue(fp, log);
330:
331:                log.isDebugEnabled();
332:                setReturnValue(log, true);
333:
334:                log.debug(string);
335:            }
336:
337:            /**
338:             * Test that BuilderFactory will invoke the "initializeService" method by default.
339:             */
340:            public void testAutowireInitializer() {
341:                Module module = newModule();
342:                ServiceImplementationFactoryParameters fp = newParameters();
343:                Log log = newLog();
344:
345:                trainGetLog(fp, log);
346:                trainGetServiceId(fp, "foo");
347:                trainGetInvokingModule(fp, module);
348:                trainResolveType(module,
349:                        "hivemind.test.services.InitializeFixture",
350:                        InitializeFixture.class);
351:
352:                replayControls();
353:
354:                BuilderParameter p = new BuilderParameter();
355:
356:                p.setClassName(InitializeFixture.class.getName());
357:
358:                InitializeFixture f = (InitializeFixture) execute(fp, p);
359:
360:                // Check which method was actually invoked (if any)
361:
362:                assertEquals("initializeService", f.getMethod());
363:
364:                verifyControls();
365:            }
366:
367:            /**
368:             * Test that BuilderFactory will invoke the named initializer.
369:             */
370:            public void testInitializer() {
371:                ServiceImplementationFactoryParameters fp = newParameters();
372:                Module module = newModule();
373:                Log log = newLog();
374:
375:                trainGetLog(fp, log);
376:                trainGetServiceId(fp, "foo");
377:                trainGetInvokingModule(fp, module);
378:                trainResolveType(module,
379:                        "hivemind.test.services.InitializeFixture",
380:                        InitializeFixture.class);
381:
382:                replayControls();
383:
384:                BuilderParameter p = new BuilderParameter();
385:
386:                p.setClassName(InitializeFixture.class.getName());
387:                p.setInitializeMethod("initializeCustom");
388:
389:                InitializeFixture f = (InitializeFixture) execute(fp, p);
390:
391:                assertEquals("initializeCustom", f.getMethod());
392:
393:                verifyControls();
394:            }
395:
396:            public void testAutowireServices() {
397:                ServiceImplementationFactoryParameters fp = newParameters();
398:                Module module = newModule();
399:                Log log = newLog();
400:
401:                trainGetLog(fp, log);
402:                trainGetServiceId(fp, "foo");
403:                trainGetInvokingModule(fp, module);
404:                trainResolveType(module,
405:                        "hivemind.test.services.ServiceAutowireTarget",
406:                        ServiceAutowireTarget.class);
407:
408:                StringHolder h = new StringHolderImpl();
409:
410:                trainContainsService(module, String.class, false);
411:                trainContainsService(module, StringHolder.class, true);
412:                trainGetService(module, StringHolder.class, h);
413:                trainIsDebugEnabled(log);
414:
415:                replayControls();
416:
417:                BuilderParameter parameter = new BuilderParameter();
418:
419:                parameter.setClassName(ServiceAutowireTarget.class.getName());
420:                parameter.setAutowireServices(true);
421:
422:                ServiceAutowireTarget service = (ServiceAutowireTarget) execute(
423:                        fp, parameter);
424:
425:                assertSame(h, service.getStringHolder());
426:
427:                verifyControls();
428:            }
429:
430:            private void trainIsDebugEnabled(Log log) {
431:                log.isDebugEnabled();
432:                setReturnValue(log, false);
433:            }
434:
435:            private void trainGetService(Module module, Class serviceInterface,
436:                    Object service) {
437:                module.getService(serviceInterface);
438:                setReturnValue(module, service);
439:            }
440:
441:            private void trainContainsService(Module module,
442:                    Class serviceInterface, boolean containsService) {
443:                module.containsService(serviceInterface);
444:                setReturnValue(module, containsService);
445:            }
446:
447:            public void testAutowireServicesFailure() {
448:                ServiceImplementationFactoryParameters fp = newParameters();
449:                Module module = newModule();
450:                Log log = newLog();
451:
452:                trainGetLog(fp, log);
453:                trainGetServiceId(fp, "foo.bar");
454:                trainGetInvokingModule(fp, module);
455:                trainResolveType(module,
456:                        "hivemind.test.services.ServiceAutowireTarget",
457:                        ServiceAutowireTarget.class);
458:
459:                trainContainsService(module, String.class, false);
460:                trainContainsService(module, StringHolder.class, false);
461:
462:                replayControls();
463:
464:                BuilderParameter parameter = new BuilderParameter();
465:
466:                parameter.setClassName(ServiceAutowireTarget.class.getName());
467:                parameter.setAutowireServices(true);
468:
469:                ServiceAutowireTarget service = (ServiceAutowireTarget) execute(
470:                        fp, parameter);
471:
472:                assertNull(service.getStringHolder());
473:
474:                verifyControls();
475:            }
476:
477:            public void testAutowireConstructor() throws Exception {
478:                ServiceImplementationFactoryParameters fp = newParameters();
479:                Module module = newModule();
480:                Log log = newLog();
481:
482:                trainGetLog(fp, log);
483:                trainGetServiceId(fp, "foo");
484:
485:                fp.getInvokingModule();
486:                getControl(fp).setReturnValue(module, MockControl.ONE_OR_MORE);
487:
488:                trainResolveType(module,
489:                        "hivemind.test.services.ConstructorAutowireTarget",
490:                        ConstructorAutowireTarget.class);
491:
492:                trainContainsService(module, Comparable.class, false);
493:                trainContainsService(module, StringHolder.class, true);
494:
495:                StringHolder h = new StringHolderImpl();
496:
497:                trainGetService(module, StringHolder.class, h);
498:
499:                trainGetClassResolver(module, getClassResolver());
500:
501:                replayControls();
502:
503:                BuilderParameter parameter = new BuilderParameter();
504:
505:                parameter.setClassName(ConstructorAutowireTarget.class
506:                        .getName());
507:                parameter.setAutowireServices(true);
508:                parameter.addProperty(new BuilderClassResolverFacet());
509:
510:                ConstructorAutowireTarget service = (ConstructorAutowireTarget) execute(
511:                        fp, parameter);
512:
513:                assertSame(h, service.getStringHolder());
514:                assertSame(getClassResolver(), service.getClassResolver());
515:
516:                verifyControls();
517:            }
518:
519:            public void testAutowireConstructorFailure() throws Exception {
520:                ServiceImplementationFactoryParameters fp = newParameters();
521:                Module module = newModule();
522:                Log log = newLog();
523:
524:                trainGetLog(fp, log);
525:                trainGetServiceId(fp, "foo");
526:
527:                fp.getInvokingModule();
528:                getControl(fp).setReturnValue(module, MockControl.ONE_OR_MORE);
529:
530:                trainResolveType(module,
531:                        "hivemind.test.services.ConstructorAutowireTarget",
532:                        ConstructorAutowireTarget.class);
533:
534:                trainContainsService(module, Comparable.class, false);
535:                trainContainsService(module, StringHolder.class, false);
536:                trainContainsService(module, StringHolder.class, false);
537:
538:                replayControls();
539:
540:                BuilderParameter parameter = new BuilderParameter();
541:
542:                parameter.setClassName(ConstructorAutowireTarget.class
543:                        .getName());
544:                parameter.setAutowireServices(true);
545:
546:                try {
547:                    execute(fp, parameter);
548:                    unreachable();
549:                } catch (ApplicationRuntimeException ex) {
550:                    assertEquals(
551:                            "Error building service foo: Unable to find constructor applicable for autowiring. Use explicit constructor parameters.",
552:                            ex.getMessage());
553:                }
554:
555:                verifyControls();
556:            }
557:
558:            public void testSetObject() throws Exception {
559:                Registry r = buildFrameworkRegistry("SetObject.xml");
560:
561:                SetObjectFixture f = (SetObjectFixture) r
562:                        .getService(SetObjectFixture.class);
563:
564:                assertNotNull(f.getClassFactory1());
565:                assertSame(f.getClassFactory1(), f.getClassFactory2());
566:            }
567:
568:            public void testAutowireService() throws Exception {
569:                Registry r = buildFrameworkRegistry("AutowireService.xml");
570:
571:                SetObjectFixture f = (SetObjectFixture) r
572:                        .getService(SetObjectFixture.class);
573:
574:                assertNotNull(f.getClassFactory1());
575:                assertSame(f.getClassFactory1(), f.getClassFactory2());
576:            }
577:
578:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.