KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > alternatives > issues > Issue0214TestCase


1 package org.picocontainer.alternatives.issues;
2
3 import junit.framework.TestCase;
4
5 import org.picocontainer.MutablePicoContainer;
6 import org.picocontainer.alternatives.ImplementationHidingPicoContainer;
7 import org.picocontainer.defaults.VerifyingVisitor;
8
9 public class Issue0214TestCase extends TestCase {
10
11     // This bug as described in the bug report, http://jira.codehaus.org/browse/PICO-214, cannot be reproduced.
12
public void testTheBug() {
13         final MutablePicoContainer pico = new ImplementationHidingPicoContainer();
14         pico.registerComponentImplementation(A.class);
15
16         /* This is a workaround for the bug described further down. Normally
17          * this method call should only be needed if specific requirements for
18          * parameters exist, but not if PicoContainer shall resolve the
19          * dependencies itself. However, with ImplementationHidingPicoContainer
20          * this is currently the only way to register a class/interface such
21          * that the automatic resolution works.
22          */

23         pico.registerComponentImplementation(I1.class, B.class, null);
24
25         /* The following registerComponentImplementation(Object, Class) of
26          * ImplementationHidingPicoContainer is buggy, as it contains
27          * "ComponentAdapter delegate = caf.createComponentAdapter(componentKey,
28          * componentImplementation, new Parameter[0]);". Instead of "new
29          * Parameter[0]" it should be "null" to have a behaviour consistent to
30          * DefaultPicoContainer, i.e. if PicoContainer shall resolve
31          * dependencies itself.
32          */

33         pico.registerComponentImplementation(I2.class, C.class);
34
35         /* The following verify() throws the exception, but is expected not to
36          * throw: "org.picocontainer.PicoVerificationException:
37          * [[org.picocontainer.PicoInitializationException: Either do the
38          * specified parameters not match any of the following constructors:
39          * [public PicoContainerBugTest$C(PicoContainerBugTest$A)] or the
40          * constructors were not accessible for 'class
41          * PicoContainerBugTest$C']]".
42          *
43          * I believe that the error comes this way: In method
44          * getGreediestSatisfiableConstructor parameters are checked against
45          * null and if parameters is not null it is assumed that specific
46          * parameters have been given so that no automatic resolution takes
47          * place. As now during registration instead of "null" falsly "new
48          * Parameter[0]" was stored, this is now interpreted as if only the
49          * nullary constructor shall be used, and if that doesn't exist, the
50          * exception is thrown.
51          */

52         new VerifyingVisitor().traverse(pico);
53     }
54
55     public static interface I1 {
56     }
57
58     public static interface I2 {
59     }
60
61     public static class A {
62         public A() {
63         }
64     }
65
66     public static class B implements I1 {
67         public B(final A a) {
68         }
69     }
70
71     public static class C implements I2 {
72         public C(final A a) {
73         }
74     }
75 }
76
Popular Tags