KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > TestBuilderFactory


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 org.apache.hivemind.service;
16
17 import hivemind.test.services.AutowireTarget;
18 import hivemind.test.services.ConstructorAutowireTarget;
19 import hivemind.test.services.ErrorHandlerHolder;
20 import hivemind.test.services.InitializeFixture;
21 import hivemind.test.services.ServiceAutowireTarget;
22 import hivemind.test.services.SimpleService;
23 import hivemind.test.services.StringHolder;
24 import hivemind.test.services.impl.StringHolderImpl;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.hivemind.ApplicationRuntimeException;
28 import org.apache.hivemind.ClassResolver;
29 import org.apache.hivemind.ErrorHandler;
30 import org.apache.hivemind.ErrorLog;
31 import org.apache.hivemind.HiveMind;
32 import org.apache.hivemind.Messages;
33 import org.apache.hivemind.Registry;
34 import org.apache.hivemind.ServiceImplementationFactoryParameters;
35 import org.apache.hivemind.internal.Module;
36 import org.apache.hivemind.service.impl.BuilderClassResolverFacet;
37 import org.apache.hivemind.service.impl.BuilderErrorHandlerFacet;
38 import org.apache.hivemind.service.impl.BuilderErrorLogFacet;
39 import org.apache.hivemind.service.impl.BuilderFacet;
40 import org.apache.hivemind.service.impl.BuilderFactoryLogic;
41 import org.apache.hivemind.service.impl.BuilderLogFacet;
42 import org.apache.hivemind.service.impl.BuilderMessagesFacet;
43 import org.apache.hivemind.service.impl.BuilderParameter;
44 import org.apache.hivemind.service.impl.BuilderServiceIdFacet;
45 import org.apache.hivemind.xml.XmlTestCase;
46 import org.easymock.MockControl;
47
48 /**
49  * Tests for the standard {@link org.apache.hivemind.service.impl.BuilderFactory} service and
50  * various implementations of {@link org.apache.hivemind.service.impl.BuilderFacet}.
51  *
52  * @author Howard Lewis Ship
53  */

54 public class TestBuilderFactory extends XmlTestCase
55 {
56     private Object JavaDoc execute(ServiceImplementationFactoryParameters fp, BuilderParameter p)
57     {
58         return new BuilderFactoryLogic(fp, p).createService();
59     }
60
61     public void testSmartFacet() throws Exception JavaDoc
62     {
63         Registry r = buildFrameworkRegistry("SmartFacet.xml");
64
65         SimpleService s = (SimpleService) r.getService(
66                 "hivemind.test.services.Simple",
67                 SimpleService.class);
68
69         assertEquals(99, s.add(1, 1));
70     }
71
72     public void testInitializeMethodFailure() throws Exception JavaDoc
73     {
74         Registry r = buildFrameworkRegistry("InitializeMethodFailure.xml");
75
76         Runnable JavaDoc s = (Runnable JavaDoc) r.getService("hivemind.test.services.Runnable", Runnable JavaDoc.class);
77
78         interceptLogging("hivemind.test.services.Runnable");
79
80         s.run();
81
82         assertLoggedMessagePattern("Error at .*?: Unable to initialize service hivemind\\.test\\.services\\.Runnable "
83                 + "\\(by invoking method doesNotExist on "
84                 + "org\\.apache\\.hivemind\\.service\\.impl\\.MockRunnable\\):");
85     }
86
87     public void testBuilderErrorHandlerFacet()
88     {
89         MockControl c = newControl(Module.class);
90         Module m = (Module) c.getMock();
91
92         ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
93
94         MockControl pc = newControl(ServiceImplementationFactoryParameters.class);
95         ServiceImplementationFactoryParameters p = (ServiceImplementationFactoryParameters) pc
96                 .getMock();
97
98         p.getInvokingModule();
99         pc.setReturnValue(m);
100
101         m.getErrorHandler();
102         c.setReturnValue(eh);
103
104         replayControls();
105
106         BuilderFacet f = new BuilderErrorHandlerFacet();
107
108         Object JavaDoc actual = f.getFacetValue(p, null);
109
110         assertSame(eh, actual);
111
112         verifyControls();
113     }
114
115     public void testSetErrorHandler() throws Exception JavaDoc
116     {
117         Registry r = buildFrameworkRegistry("SetErrorHandler.xml");
118
119         ErrorHandlerHolder h = (ErrorHandlerHolder) r.getService(
120                 "hivemind.test.services.SetErrorHandler",
121                 ErrorHandlerHolder.class);
122
123         assertNotNull(h.getErrorHandler());
124     }
125
126     public void testConstructErrorHandler() throws Exception JavaDoc
127     {
128         Registry r = buildFrameworkRegistry("ConstructErrorHandler.xml");
129
130         ErrorHandlerHolder h = (ErrorHandlerHolder) r.getService(
131                 "hivemind.test.services.ConstructErrorHandler",
132                 ErrorHandlerHolder.class);
133
134         assertNotNull(h.getErrorHandler());
135     }
136
137     public void testBuilderClassResolverFacet()
138     {
139         ClassResolver cr = (ClassResolver) newMock(ClassResolver.class);
140
141         MockControl pc = newControl(ServiceImplementationFactoryParameters.class);
142         ServiceImplementationFactoryParameters p = (ServiceImplementationFactoryParameters) pc
143                 .getMock();
144
145         MockControl control = newControl(Module.class);
146         Module module = (Module) control.getMock();
147
148         p.getInvokingModule();
149         pc.setReturnValue(module);
150
151         module.getClassResolver();
152         control.setReturnValue(cr);
153
154         replayControls();
155
156         BuilderClassResolverFacet fc = new BuilderClassResolverFacet();
157
158         Object JavaDoc result = fc.getFacetValue(p, null);
159
160         assertSame(cr, result);
161
162         verifyControls();
163     }
164
165     public void testSetClassResolver() throws Exception JavaDoc
166     {
167         Registry r = buildFrameworkRegistry("SetClassResolver.xml");
168
169         ClassResolverHolder h = (ClassResolverHolder) r.getService(
170                 "hivemind.test.services.SetClassResolver",
171                 ClassResolverHolder.class);
172
173         assertNotNull(h.getClassResolver());
174     }
175
176     public void testConstructClassResolver() throws Exception JavaDoc
177     {
178         Registry r = buildFrameworkRegistry("ConstructClassResolver.xml");
179
180         ClassResolverHolder h = (ClassResolverHolder) r.getService(
181                 "hivemind.test.services.ConstructClassResolver",
182                 ClassResolverHolder.class);
183
184         assertNotNull(h.getClassResolver());
185     }
186
187     protected ServiceImplementationFactoryParameters newParameters()
188     {
189         return (ServiceImplementationFactoryParameters) newMock(ServiceImplementationFactoryParameters.class);
190     }
191
192     protected Module newModule()
193     {
194         return (Module) newMock(Module.class);
195     }
196
197     protected ErrorHandler newErrorHandler()
198     {
199         return (ErrorHandler) newMock(ErrorHandler.class);
200     }
201
202     protected Log newLog()
203     {
204         return (Log) newMock(Log.class);
205     }
206
207     protected Messages newMessages()
208     {
209         return (Messages) newMock(Messages.class);
210     }
211
212     protected ErrorLog newErrorLog()
213     {
214         return (ErrorLog) newMock(ErrorLog.class);
215     }
216
217     public void testAutowire()
218     {
219         ServiceImplementationFactoryParameters fp = newParameters();
220         Module module = newModule();
221         ErrorHandler eh = newErrorHandler();
222         Log log = newLog();
223         Messages messages = newMessages();
224         ErrorLog errorLog = newErrorLog();
225
226         // Normally I try and get all the invocations into chronological
227
// order ... but with this refactoring, that's painful; these
228
// are in an order that appeases junit.
229

230         trainGetLog(fp, log);
231
232         trainGetServiceId(fp, "foo.bar.Baz");
233
234         trainGetInvokingModule(fp, module);
235
236         trainResolveType(module, "hivemind.test.services.AutowireTarget", AutowireTarget.class);
237
238         trainGetLog(fp, log);
239
240         trainDebug(fp, log, "Autowired property log to " + log);
241
242         trainGetInvokingModule(fp, module);
243
244         trainGetClassResolver(module, getClassResolver());
245
246         trainDebug(fp, log, "Autowired property classResolver to " + getClassResolver());
247
248         trainGetInvokingModule(fp, module);
249
250         trainGetMessages(module, messages);
251
252         trainDebug(fp, log, "Autowired property messages to " + messages);
253
254         trainGetInvokingModule(fp, module);
255
256         trainGetErrorHandler(module, eh);
257
258         trainDebug(fp, log, "Autowired property errorHandler to " + eh);
259
260         trainGetServiceId(fp);
261
262         trainDebug(fp, log, "Autowired property serviceId to foo.bar.Baz");
263
264         trainGetErrorLog(fp, errorLog);
265
266         trainDebug(fp, log, "Autowired property errorLog to " + errorLog);
267
268         replayControls();
269
270         BuilderParameter p = new BuilderParameter();
271
272         p.setClassName(AutowireTarget.class.getName());
273         p.addProperty(new BuilderLogFacet());
274         p.addProperty(new BuilderClassResolverFacet());
275         p.addProperty(new BuilderMessagesFacet());
276         p.addProperty(new BuilderErrorHandlerFacet());
277         p.addProperty(new BuilderServiceIdFacet());
278         p.addProperty(new BuilderErrorLogFacet());
279
280         AutowireTarget t = (AutowireTarget) execute(fp, p);
281
282         assertSame(eh, t.getErrorHandler());
283         assertSame(getClassResolver(), t.getClassResolver());
284         assertSame(messages, t.getMessages());
285         assertSame(log, t.getLog());
286         assertEquals("foo.bar.Baz", t.getServiceId());
287         assertSame(errorLog, t.getErrorLog());
288
289         verifyControls();
290     }
291
292     private void trainGetErrorLog(ServiceImplementationFactoryParameters fp, ErrorLog errorLog)
293     {
294         fp.getErrorLog();
295         setReturnValue(fp, errorLog);
296     }
297
298     private void trainGetServiceId(ServiceImplementationFactoryParameters fp)
299     {
300         fp.getServiceId();
301         setReturnValue(fp, "foo.bar.Baz");
302     }
303
304     private void trainGetErrorHandler(Module module, ErrorHandler eh)
305     {
306         module.getErrorHandler();
307         setReturnValue(module, eh);
308     }
309
310     private void trainGetMessages(Module module, Messages messages)
311     {
312         module.getMessages();
313         setReturnValue(module, messages);
314     }
315
316     private void trainGetClassResolver(Module module, ClassResolver resolver)
317     {
318         module.getClassResolver();
319         setReturnValue(module, resolver);
320     }
321
322     private void trainResolveType(Module module, String JavaDoc typeName, Class JavaDoc type)
323     {
324         module.resolveType(typeName);
325         setReturnValue(module, type);
326     }
327
328     private void trainGetInvokingModule(ServiceImplementationFactoryParameters fp, Module module)
329     {
330         fp.getInvokingModule();
331         setReturnValue(fp, module);
332     }
333
334     protected void trainGetServiceId(ServiceImplementationFactoryParameters fp, String JavaDoc serviceId)
335     {
336         fp.getServiceId();
337         setReturnValue(fp, serviceId);
338     }
339
340     protected void trainGetLog(ServiceImplementationFactoryParameters fp, Log log)
341     {
342         fp.getLog();
343         setReturnValue(fp, log);
344     }
345
346     private void trainDebug(ServiceImplementationFactoryParameters fp, Log log, String JavaDoc string)
347     {
348         fp.getLog();
349         setReturnValue(fp, log);
350
351         log.isDebugEnabled();
352         setReturnValue(log, true);
353
354         log.debug(string);
355     }
356
357     /**
358      * Test that BuilderFactory will invoke the "initializeService" method by default.
359      */

360     public void testAutowireInitializer()
361     {
362         Module module = newModule();
363         ServiceImplementationFactoryParameters fp = newParameters();
364         Log log = newLog();
365
366         trainGetLog(fp, log);
367         trainGetServiceId(fp, "foo");
368         trainGetInvokingModule(fp, module);
369         trainResolveType(
370                 module,
371                 "hivemind.test.services.InitializeFixture",
372                 InitializeFixture.class);
373
374         replayControls();
375
376         BuilderParameter p = new BuilderParameter();
377
378         p.setClassName(InitializeFixture.class.getName());
379
380         InitializeFixture f = (InitializeFixture) execute(fp, p);
381
382         // Check which method was actually invoked (if any)
383

384         assertEquals("initializeService", f.getMethod());
385
386         verifyControls();
387     }
388
389     /**
390      * Test that BuilderFactory will invoke the named initializer.
391      */

392     public void testInitializer()
393     {
394         ServiceImplementationFactoryParameters fp = newParameters();
395         Module module = newModule();
396         Log log = newLog();
397
398         trainGetLog(fp, log);
399         trainGetServiceId(fp, "foo");
400         trainGetInvokingModule(fp, module);
401         trainResolveType(
402                 module,
403                 "hivemind.test.services.InitializeFixture",
404                 InitializeFixture.class);
405
406         replayControls();
407
408         BuilderParameter p = new BuilderParameter();
409
410         p.setClassName(InitializeFixture.class.getName());
411         p.setInitializeMethod("initializeCustom");
412
413         InitializeFixture f = (InitializeFixture) execute(fp, p);
414
415         assertEquals("initializeCustom", f.getMethod());
416
417         verifyControls();
418     }
419
420     public void testAutowireServices()
421     {
422         ServiceImplementationFactoryParameters fp = newParameters();
423         Module module = newModule();
424         Log log = newLog();
425
426         trainGetLog(fp, log);
427         trainGetServiceId(fp, "foo");
428         trainGetInvokingModule(fp, module);
429         trainResolveType(
430                 module,
431                 "hivemind.test.services.ServiceAutowireTarget",
432                 ServiceAutowireTarget.class);
433
434         final StringHolder h = new StringHolderImpl();
435         
436         Autowiring autowiring = new Autowiring() {
437
438             public Object JavaDoc autowireProperties(Object JavaDoc target, String JavaDoc[] propertyNames)
439             {
440                 return null;
441             }
442
443             public Object JavaDoc autowireProperties(Object JavaDoc target)
444             {
445                 return null;
446             }
447
448             public Object JavaDoc autowireProperties(String JavaDoc strategy, Object JavaDoc target, String JavaDoc[] propertyNames)
449             {
450                 ((ServiceAutowireTarget) target).setStringHolder(h);
451                 return target;
452             }
453
454             public Object JavaDoc autowireProperties(String JavaDoc strategy, Object JavaDoc target)
455             {
456                 return null;
457             }};
458
459         module.getService(HiveMind.AUTOWIRING_SERVICE, Autowiring.class);
460         setReturnValue(module, autowiring);
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(fp, parameter);
470
471         assertSame(h, service.getStringHolder());
472
473         verifyControls();
474     }
475
476     private void trainGetService(Module module, Class JavaDoc serviceInterface, Object JavaDoc service)
477     {
478         module.getService(serviceInterface);
479         setReturnValue(module, service);
480     }
481
482     private void trainContainsService(Module module, Class JavaDoc serviceInterface, boolean containsService)
483     {
484         module.containsService(serviceInterface);
485         setReturnValue(module, containsService);
486     }
487
488     public void testAutowireConstructor() throws Exception JavaDoc
489     {
490         ServiceImplementationFactoryParameters fp = newParameters();
491         Module module = newModule();
492         Log log = newLog();
493
494         trainGetLog(fp, log);
495         trainGetServiceId(fp, "foo");
496
497         fp.getInvokingModule();
498         getControl(fp).setReturnValue(module, MockControl.ONE_OR_MORE);
499
500         trainResolveType(
501                 module,
502                 "hivemind.test.services.ConstructorAutowireTarget",
503                 ConstructorAutowireTarget.class);
504
505         trainContainsService(module, Comparable JavaDoc.class, false);
506         trainContainsService(module, StringHolder.class, true);
507
508         StringHolder h = new StringHolderImpl();
509
510         trainGetService(module, StringHolder.class, h);
511
512         trainGetClassResolver(module, getClassResolver());
513         
514         replayControls();
515
516         BuilderParameter parameter = new BuilderParameter();
517
518         parameter.setClassName(ConstructorAutowireTarget.class.getName());
519         parameter.setAutowireServices(true);
520         parameter.addProperty(new BuilderClassResolverFacet());
521
522         ConstructorAutowireTarget service = (ConstructorAutowireTarget) execute(fp, parameter);
523
524         assertSame(h, service.getStringHolder());
525         assertSame(getClassResolver(), service.getClassResolver());
526
527         verifyControls();
528     }
529
530     public void testAutowireConstructorFailure() throws Exception JavaDoc
531     {
532         ServiceImplementationFactoryParameters fp = newParameters();
533         Module module = newModule();
534         Log log = newLog();
535
536         trainGetLog(fp, log);
537         trainGetServiceId(fp, "foo");
538
539         fp.getInvokingModule();
540         getControl(fp).setReturnValue(module, MockControl.ONE_OR_MORE);
541
542         trainResolveType(
543                 module,
544                 "hivemind.test.services.ConstructorAutowireTarget",
545                 ConstructorAutowireTarget.class);
546
547         trainContainsService(module, Comparable JavaDoc.class, false);
548         trainContainsService(module, StringHolder.class, false);
549         trainContainsService(module, StringHolder.class, false);
550
551         replayControls();
552
553         BuilderParameter parameter = new BuilderParameter();
554
555         parameter.setClassName(ConstructorAutowireTarget.class.getName());
556         parameter.setAutowireServices(true);
557
558         try
559         {
560             execute(fp, parameter);
561             unreachable();
562         }
563         catch (ApplicationRuntimeException ex)
564         {
565             assertEquals(
566                     "Error building service foo: Unable to find constructor applicable for autowiring. Use explicit constructor parameters.",
567                     ex.getMessage());
568         }
569
570         verifyControls();
571     }
572
573     public void testSetObject() throws Exception JavaDoc
574     {
575         Registry r = buildFrameworkRegistry("SetObject.xml");
576
577         SetObjectFixture f = (SetObjectFixture) r.getService(SetObjectFixture.class);
578
579         assertNotNull(f.getClassFactory1());
580         assertSame(f.getClassFactory1(), f.getClassFactory2());
581     }
582
583     public void testAutowireService() throws Exception JavaDoc
584     {
585         Registry r = buildFrameworkRegistry("AutowireService.xml");
586
587         SetObjectFixture f = (SetObjectFixture) r.getService(SetObjectFixture.class);
588
589         assertNotNull(f.getClassFactory1());
590         assertSame(f.getClassFactory1(), f.getClassFactory2());
591     }
592
593     public void testBuilderAccess() throws Exception JavaDoc
594     {
595         Registry r = buildFrameworkRegistry("BuilderAccess.xml");
596
597         BuilderAccess s =
598             (BuilderAccess) r.getService(
599                 "hivemind.test.services.BuilderAccess",
600                 BuilderAccess.class);
601
602         assertEquals("A successful test of BuilderFactory.", s.getLocalizedMessage("success"));
603
604         assertEquals("hivemind.test.services.BuilderAccess", s.getExtensionPointId());
605
606         interceptLogging("hivemind.test.services.BuilderAccess");
607
608         s.logMessage("This is a test.");
609
610         assertLoggedMessage("This is a test.");
611     }
612
613     public void testBuilderAccessFailure() throws Exception JavaDoc
614     {
615         Registry r = buildFrameworkRegistry("BuilderAccessFailure.xml");
616
617         // interceptLogging("hivemind.test.services.BuilderAccessFailure");
618

619         BuilderAccess s =
620             (BuilderAccess) r.getService(
621                 "hivemind.test.services.BuilderAccessFailure",
622                 BuilderAccess.class);
623
624         assertNotNull(s);
625
626         // s is a proxy, invoke a service method to force the creation of the
627
// service (and the error).
628

629         interceptLogging("hivemind.test.services");
630
631         String JavaDoc result = s.getLocalizedMessage("success");
632
633         assertLoggedMessagePattern("Class org.apache.hivemind.service.impl.BuilderAccessImpl does not contain a property named 'EVIL'.");
634
635         assertEquals("Stumbles, logs error, and continues.", result);
636     }
637
638     public void testConstructorFactory() throws Exception JavaDoc
639     {
640         Registry r = buildFrameworkRegistry("ConstructorFactory.xml");
641
642         String JavaDoc[] servicesToTest =
643             {
644                 "DefaultConstructor",
645                 "LongConstructor",
646                 "StringConstructor",
647                 "ServiceConstructor",
648                 "MultiConstructor",
649                 "ConfigurationConstructor",
650                 "MappedConfigurationConstructor",
651                 "LogAndMessagesConstructor",
652                 "NullConstructor"};
653
654         for (int i = 0; i < servicesToTest.length; i++)
655         {
656             ConstructorAccess s =
657                 (ConstructorAccess) r.getService(
658                     "hivemind.test.services." + servicesToTest[i],
659                     ConstructorAccess.class);
660             s.verify();
661         }
662     }
663 }
Popular Tags