KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > tests > jfun > yan > SetterInjectionComponentAdapterTestCase


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by *
9  *****************************************************************************/

10 package tests.jfun.yan;
11
12 import jfun.yan.Component;
13 import jfun.yan.Container;
14 import jfun.yan.containers.*;
15 import tests.jfun.yan.tck.BaseComponentTestCase;
16 import tests.jfun.yan.testmodel.PersonBean;
17 import tests.jfun.yan.testmodel.PurseBean;
18
19 import java.beans.IntrospectionException JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import jfun.yan.*;
24 import jfun.yan.containers.DefaultContainer;
25 import jfun.yan.factory.GlobalScope;
26 import jfun.yan.factory.ThreadLocalScope;
27 import junit.framework.TestSuite;
28
29 public class SetterInjectionComponentAdapterTestCase
30         extends tests.jfun.yan.tck.BaseComponentTestCase {
31   public static void main(String JavaDoc[] args){
32     tests.jfun.yan.Utils.runTest(suite());
33   }
34   private static TestSuite suite(){
35     return new TestSuite(SetterInjectionComponentAdapterTestCase.class);
36   }
37
38 /*
39     protected ComponentAdapterFactory createDefaultComponentAdapterFactory() {
40         return new CachingComponentAdapterFactory(new SetterInjectionComponentAdapterFactory());
41     }
42 */

43     protected Component prepDEF_verifyWithoutDependencyWorks(Container yan){
44       try{
45         return Components.ctor(PersonBean.class).bean()
46         .withProperty("name", Components.value("Pico Container"));
47       }
48       catch(java.beans.IntrospectionException JavaDoc e){
49         throw new IllegalStateException JavaDoc(e.getMessage());
50       }
51       /*
52         return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{new ConstantParameter(
53                 "Pico Container")});*/

54     }
55
56     protected Component prepDEF_verifyDoesNotInstantiate(Container yan) {
57       yan.registerValue("Pico Container");
58       try{
59         return Components.ctor(DeadBody.class).bean();
60       }
61       catch(java.beans.IntrospectionException JavaDoc e){
62         throw new IllegalStateException JavaDoc(e.getMessage());
63       }
64       /*
65         yan.registerComponentInstance("Pico Container");
66         return new SetterInjectionComponentAdapter(DeadBody.class, DeadBody.class, new Parameter[]{ComponentParameter.DEFAULT});
67         */

68     }
69
70     protected Component prepDEF_visitable() {
71       try{
72         return Components.bean(Components.ctor(PersonBean.class))
73           .withProperty("name", Components.value("Pico Container"));
74       }
75       catch(java.beans.IntrospectionException JavaDoc e){
76         throw new IllegalStateException JavaDoc(e.getMessage());
77       }
78       /*
79         return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{new ConstantParameter(
80                 "Pico Container")});*/

81     }
82
83     protected Component prepSER_isSerializable(Container yan) {
84       try{
85         yan.registerValue("Pico Container");
86         return Components.bean(Components.ctor(PersonBean.class));
87       }
88       catch(java.beans.IntrospectionException JavaDoc e){
89         throw new IllegalStateException JavaDoc(e.getMessage());
90       }
91       /*
92         yan.registerComponentInstance("Pico Container");
93         return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{ComponentParameter.DEFAULT});
94         */

95     }
96
97     protected Component prepSER_isXStreamSerializable(Container yan) {
98         return prepSER_isSerializable(yan);
99     }
100
101     protected void prepDEF_isAbleToTakeParameters(Container yan) {
102       yan.registerValue("Pico Container");
103       yan.registerConstructor(PersonBean.class);
104       try{
105         yan.registerComponent(PersonBean.class,
106             Components.ctor(MoneyPurse.class).bean()
107             .withProperty("money", Components.value(new Double JavaDoc(100)))
108         );
109       }
110       catch(java.beans.IntrospectionException JavaDoc e){
111         throw new IllegalStateException JavaDoc(e.getMessage());
112       }
113       /*
114         yan.registerComponentInstance("Pico Container");
115         yan.registerComponentImplementation(PersonBean.class);
116         return yan.registerComponent(new SetterInjectionComponentAdapter(
117                 PurseBean.class, MoneyPurse.class, new Parameter[]{
118                         ComponentParameter.DEFAULT, new ConstantParameter(new Double(100.0))}));
119                         */

120     }
121
122     public static class MoneyPurse
123             extends PurseBean {
124         double money;
125
126         public double getMoney() {
127             return money;
128         }
129
130         public void setMoney(double money) {
131             this.money = money;
132         }
133     }
134
135     protected Component prepVER_verificationFails(Container yan) {
136       yan.registerValue("Pico Container");
137       yan.registerConstructor(PersonBean.class);
138       try{
139         yan.registerComponent(PersonBean.class,
140             Components.bean(Components.ctor(MoneyPurse.class))
141         );
142         return yan.getComponent(PersonBean.class);
143       }
144       catch(java.beans.IntrospectionException JavaDoc e){
145         throw new IllegalStateException JavaDoc(e.getMessage());
146       }
147       /*
148         yan.registerComponentInstance("Pico Container");
149         yan.registerComponentImplementation(PersonBean.class);
150         return yan.registerComponent(new SetterInjectionComponentAdapter(
151                 PurseBean.class, MoneyPurse.class, new Parameter[]{ComponentParameter.DEFAULT}));
152                 */

153     }
154
155     protected Component prepINS_createsNewInstances(Container yan) {
156       yan.registerValue("Pico Container");
157       try{
158         return Components.bean(Components.ctor(PersonBean.class));
159       }
160       catch(java.beans.IntrospectionException JavaDoc e){
161         throw new IllegalStateException JavaDoc(e.getMessage());
162       }
163         /*yan.registerComponentInstance("Pico Container");
164         return new SetterInjectionComponentAdapter(PersonBean.class, PersonBean.class, new Parameter[]{ComponentParameter.DEFAULT});
165         */

166     }
167
168     public static class Ghost
169             extends PersonBean {
170         public Ghost() {
171             throw new VerifyError JavaDoc("test");
172         }
173     }
174
175     protected Component prepINS_errorIsRethrown(Container yan) {
176       yan.registerValue("Pico Container");
177       try{
178         return Components.bean(Ghost.class);
179       }
180       catch(java.beans.IntrospectionException JavaDoc e){
181         throw new IllegalStateException JavaDoc(e.getMessage());
182       }
183       /*
184         yan.registerComponentInstance("Pico Container");
185         return new SetterInjectionComponentAdapter(Ghost.class, Ghost.class, new Parameter[]{ComponentParameter.DEFAULT});
186         */

187     }
188
189     public static class DeadBody
190             extends PersonBean {
191         public DeadBody() throws Exception JavaDoc {
192             throw new RuntimeException JavaDoc("test");
193         }
194     }
195
196     protected Component prepINS_runtimeExceptionIsRethrown(Container yan) {
197       yan.registerValue("Pico Container");
198       try{
199         return Components.bean(Components.ctor(DeadBody.class));
200       }
201       catch(java.beans.IntrospectionException JavaDoc e){
202         throw new IllegalStateException JavaDoc(e.getMessage());
203       }
204       /*
205         yan.registerComponentInstance("Pico Container");
206         return new SetterInjectionComponentAdapter(DeadBody.class, DeadBody.class, new Parameter[]{ComponentParameter.DEFAULT});
207         */

208     }
209
210     public static class HidingPersion
211             extends PersonBean {
212         public HidingPersion() throws Exception JavaDoc {
213             throw new Exception JavaDoc("test");
214         }
215     }
216
217     protected Component prepINS_normalExceptionIsRethrownInsidePicoInvocationTargetInitializationException(
218             Container yan) {
219       yan.registerValue("Pico Container");
220       try{
221         return Components.bean(Components.ctor(HidingPersion.class));
222       }
223       catch(java.beans.IntrospectionException JavaDoc e){
224         throw new IllegalStateException JavaDoc(e.getMessage());
225       }/*
226         yan.registerComponentInstance("Pico Container");
227         return new SetterInjectionComponentAdapter(
228                 HidingPersion.class, HidingPersion.class, new Parameter[]{ComponentParameter.DEFAULT});
229                 */

230     }
231
232     protected Component prepRES_dependenciesAreResolved(Container yan) {
233       yan.registerValue("Pico Container");
234       yan.registerConstructor(PersonBean.class);
235       try{
236         return Components.bean(PurseBean.class);
237       }
238       catch(java.beans.IntrospectionException JavaDoc e){
239         throw new IllegalStateException JavaDoc(e.getMessage());
240       }
241       /*
242         yan.registerComponentInstance("Pico Container");
243         yan.registerComponentImplementation(PersonBean.class);
244         return new SetterInjectionComponentAdapter(PurseBean.class, PurseBean.class, new Parameter[]{ComponentParameter.DEFAULT});
245         */

246     }
247
248     public static class WealthyPerson
249             extends PersonBean {
250         PurseBean purse;
251
252         public PurseBean getPurse() {
253             return purse;
254         }
255
256         public void setPurse(PurseBean purse) {
257             this.purse = purse;
258         }
259     }
260
261     protected Component prepRES_failingVerificationWithCyclicDependencyException(Container yan) {
262       yan.registerValue("Pico Container");
263       yan.registerConstructor(PersonBean.class, WealthyPerson.class);
264       try{
265         yan.registerComponent(PurseBean.class,
266             Components.bean(Components.ctor(PurseBean.class)));
267         return yan.getComponent(PurseBean.class);
268       }
269       catch(java.beans.IntrospectionException JavaDoc e){
270         throw new IllegalStateException JavaDoc(e.getMessage());
271       }/*
272         yan.registerComponentInstance("Pico Container");
273         yan.registerComponentImplementation(PersonBean.class, WealthyPerson.class);
274         return yan.registerComponent(new SetterInjectionComponentAdapter(
275                 PurseBean.class, PurseBean.class, new Parameter[]{ComponentParameter.DEFAULT}));
276                 */

277     }
278
279     protected void prepRES_failingInstantiationWithCyclicDependencyException(Container yan) {
280       yan.registerValue("Pico Container");
281       yan.registerConstructor(PersonBean.class, WealthyPerson.class);
282       try{
283         yan.registerComponent(PurseBean.class,
284             Components.bean(Components.ctor(PurseBean.class)));
285       }
286       catch(java.beans.IntrospectionException JavaDoc e){
287         throw new IllegalStateException JavaDoc(e.getMessage());
288       }
289       /*
290         yan.registerComponentInstance("Pico Container");
291         yan.registerComponentImplementation(PersonBean.class, WealthyPerson.class);
292         return yan.registerComponent(new SetterInjectionComponentAdapter(
293                 PurseBean.class, PurseBean.class, new Parameter[]{ComponentParameter.DEFAULT}));
294                 */

295     }
296
297     public static class A {
298         private B b;
299         private String JavaDoc string = "default";
300         private List JavaDoc list = new java.util.ArrayList JavaDoc();
301
302         public void setB(B b) {
303             this.b = b;
304         }
305
306         public B getB() {
307             return b;
308         }
309
310         public String JavaDoc getString() {
311             return string;
312         }
313
314         public void setString(String JavaDoc string) {
315             this.string = string;
316         }
317
318         public List JavaDoc getList() {
319             return list;
320         }
321
322         public void setList(List JavaDoc list) {
323             this.list = list;
324         }
325     }
326
327     public static class B {
328     }
329
330     public void testAllUnsatisfiableDependenciesAreSignalled()
331     throws IntrospectionException JavaDoc{
332         Component aAdapter =
333           Components.bean(A.class);
334         //new SetterInjectionComponentAdapter("a", A.class, null);
335
Component bAdapter =
336           Components.bean(B.class).singleton();
337           //new SetterInjectionComponentAdapter("b", B.class, null);
338

339         Container yan = new DefaultContainer();
340         yan.verify();
341         yan.registerComponent("b", bAdapter);
342         yan.registerComponent("a", aAdapter);
343         try {
344             //aAdapter.getComponentInstance(yan);
345
yan.getInstance("a");
346         } catch (UnsatisfiedComponentException e) {
347           //System.out.println(e.getMessage());
348
assertEquals("The property list of type java.util.List for component <a> is not resolveable",
349               e.getMessage());
350             //assertTrue(e.getUnsatisfiableDependencies().contains(List.class));
351
//assertTrue(e.getUnsatisfiableDependencies().contains(String.class));
352
}
353         yan.registerComponent("a", aAdapter.optionalProperty("string")
354             .ignoreProperty("list"));
355         final B b = (B) yan.getInstance("b");
356         A a = (A) yan.getInstance("a");
357         assertEquals("default", a.getString());
358         assertEquals(new java.util.ArrayList JavaDoc(), a.getList());
359         assertSame(b, a.getB());
360         yan.registerValue("hello");
361         a = (A) yan.getInstance("a");
362         assertEquals("hello", a.getString());
363         assertEquals(new java.util.ArrayList JavaDoc(), a.getList());
364         assertSame(b, a.getB());
365         final java.util.List JavaDoc list1 = new java.util.LinkedList JavaDoc();
366         list1.add("abc");
367         yan.registerValue(list1);
368         a = (A) yan.getInstance("a");
369         assertEquals("hello", a.getString());
370         assertEquals(new java.util.ArrayList JavaDoc(), a.getList());
371         assertSame(b, a.getB());
372     }
373     public void testVerifyAllUnsatisfiableDependenciesAreSignalled()
374     throws IntrospectionException JavaDoc{
375         Component aAdapter =
376           Components.bean(A.class);
377         //new SetterInjectionComponentAdapter("a", A.class, null);
378
Component bAdapter =
379           Components.bean(B.class);
380           //new SetterInjectionComponentAdapter("b", B.class, null);
381

382         Container yan = new DefaultContainer();
383         yan.verify();
384         yan.registerComponent("b", bAdapter);
385         yan.registerComponent("a", aAdapter);
386         try {
387             //aAdapter.getComponentInstance(yan);
388
yan.verify();
389         } catch (UnsatisfiedComponentException e) {
390           //System.out.println(e.getMessage());
391
//assertTrue(e.getUnsatisfiableDependencies().contains(List.class));
392
//assertTrue(e.getUnsatisfiableDependencies().contains(String.class));
393
}
394     }
395     public static class C {
396         private B b;
397         private List JavaDoc l;
398         private final boolean asBean;
399
400         public C() {
401             asBean = true;
402         }
403
404         public C(B b) {
405             this.l = new ArrayList JavaDoc();
406             this.b = b;
407             asBean = false;
408         }
409
410         public void setB(B b) {
411             this.b = b;
412         }
413
414         public B getB() {
415             return b;
416         }
417
418         public void setList(List JavaDoc l) {
419             this.l = l;
420         }
421
422         public List JavaDoc getList() {
423             return l;
424         }
425
426         public boolean instantiatedAsBean() {
427             return asBean;
428         }
429     }
430
431     public void testHybridBeans()
432     throws IntrospectionException JavaDoc{
433         Component bAdapter =Components.bean(B.class);
434           //new Component("b", B.class, null);
435
Component cAdapter =Components.bean(C.class);
436           //new Component("c", C.class, null);
437
//Component cNullAdapter = Components.bean(C.class);
438
//new Component("c0", C.class, null);
439

440         Container yan = new DefaultContainer();
441         yan.registerComponent("b", bAdapter);
442         yan.registerComponent("c", cAdapter);
443         yan.registerComponent("c0", cAdapter);
444         yan.registerConstructor(ArrayList JavaDoc.class, new Class JavaDoc[0]);
445
446         C c = (C) yan.getInstance("c");
447           //(C) cAdapter.getComponentInstance(yan);
448
assertTrue(c.instantiatedAsBean());
449         C c0 = (C) yan.getInstance("c0");
450           //(C) cNullAdapter.getComponentInstance(yan);
451
assertTrue(c0.instantiatedAsBean());
452     }
453     public interface IYin{
454       public void setYang(IYang yang);
455
456       public IYang getYang();
457     }
458     public interface IYang{
459       public void setYin(IYin yang);
460
461       public IYin getYin();
462     }
463     public static class Yin implements IYin {
464         private IYang yang;
465
466         public void setYang(IYang yang) {
467             this.yang = yang;
468         }
469
470         public IYang getYang() {
471             return yang;
472         }
473     }
474
475     public static class Yang implements IYang{
476         private IYin yin;
477
478         public void setYin(IYin yin) {
479             this.yin = yin;
480         }
481
482         public IYin getYin() {
483             return yin;
484         }
485     }
486
487     // TODO PICO-188
488
// http://jira.codehaus.org/browse/PICO-188
489
public void testShouldBeAbleToHandleMutualDependenciesWithSetterInjection()
490     throws IntrospectionException JavaDoc{
491       try{
492         Container yan = new DefaultContainer();
493           /*new DefaultContainer(new CachingComponentAdapterFactory(
494                 new SetterInjectionComponentAdapterFactory()));*/

495         //call cache() twice to enlarge testing coverage.
496
final java.util.Map JavaDoc props = new HashMap JavaDoc();
497         props.put("yin", Components.useType(IYin.class));
498         props.put("yang", Components.useType(Yang.class));
499         yan.registerComponent(Components.bean(Yin.class).singleton().singleton()
500             .withArgument(1, Components.value("bad ying"))
501             .withProperties(new String JavaDoc[]{"yin","yang"},
502                 new Creator[]{Components.useType(IYin.class),
503                 Components.useType(Yang.class)}).guard());
504         yan.registerComponent(Components.bean(Yang.class).singleton(new GlobalScope())
505             .withProperties(props)
506             .withProperty("nonexistent", Components.value("bad yang"))
507             .singleton(new ThreadLocalScope()));
508
509         IYin yin = (IYin) yan.getInstanceOfType(IYin.class);
510         Yang yang = (Yang) yan.getInstance(Yang.class);
511
512         assertSame(yin, yang.getYin());
513         assertSame(yang, yin.getYang());
514       }
515       catch(YanException e){
516         e.printResolutionTrace();
517         throw e;
518       }
519     }
520     public void testShouldBeAbleToHandleMutualDependenciesWithCachingProxy()
521     throws IntrospectionException JavaDoc{
522         Container yan = new SingletonProxyContainer();
523           /*new DefaultContainer(new CachingComponentAdapterFactory(
524                 new SetterInjectionComponentAdapterFactory()));*/

525
526         yan.registerComponent(Components.bean(Yin.class)
527             .withArgument(1, Components.value("bad ying"))
528         );
529         yan.registerComponent(Components.bean(Yang.class)
530             .withProperty("nonexistent", Components.value("bad yang"))
531             );
532
533         IYin yin = (IYin) yan.getInstanceOfType(IYin.class);
534         IYang yang = (IYang) yan.getInstanceOfType(IYang.class);
535
536         assertSame(yin, yang.getYin());
537         assertSame(yang, yin.getYang());
538     }
539     public void testShouldBeAbleToHandleMutualDependenciesWithProxy()
540     throws IntrospectionException JavaDoc{
541         Container yan = new ProxyContainer();
542           /*new DefaultContainer(new CachingComponentAdapterFactory(
543                 new SetterInjectionComponentAdapterFactory()));*/

544
545         yan.registerComponent(Components.bean(Yin.class, new String JavaDoc[]{"yang"})
546             .withArgument(1, Components.value("bad ying"))
547         );
548         yan.registerComponent(Components.bean(Yang.class)
549             .withProperty("nonexistent", Components.value("bad yang"))
550             .singleton(new ThreadLocalScope())
551             );
552         yan.verify();
553         IYin yin = (IYin) yan.getInstanceOfType(IYin.class);
554         IYang yang = (IYang) yan.getInstanceOfType(IYang.class);
555
556         assertNotSame(yin, yang.getYin());
557         assertNotSame(yang, yin.getYang());
558     }
559     public static Integer JavaDoc getNullString(){return null;}
560     public void testNullBean(){
561
562     }
563 }
564
Popular Tags