001    package org.picocontainer.alternatives.issues;
002    
003    import junit.framework.TestCase;
004    
005    import org.picocontainer.MutablePicoContainer;
006    import org.picocontainer.alternatives.ImplementationHidingPicoContainer;
007    import org.picocontainer.defaults.VerifyingVisitor;
008    
009    public class Issue0214TestCase extends TestCase {
010    
011        // This bug as described in the bug report, http://jira.codehaus.org/browse/PICO-214, cannot be reproduced.
012        public void testTheBug() {
013            final MutablePicoContainer pico = new ImplementationHidingPicoContainer();
014            pico.registerComponentImplementation(A.class);
015    
016            /* This is a workaround for the bug described further down. Normally
017             * this method call should only be needed if specific requirements for
018             * parameters exist, but not if PicoContainer shall resolve the
019             * dependencies itself. However, with ImplementationHidingPicoContainer
020             * this is currently the only way to register a class/interface such
021             * that the automatic resolution works.
022             */
023            pico.registerComponentImplementation(I1.class, B.class, null);
024    
025            /* The following registerComponentImplementation(Object, Class) of
026             * ImplementationHidingPicoContainer is buggy, as it contains
027             * "ComponentAdapter delegate = caf.createComponentAdapter(componentKey,
028             * componentImplementation, new Parameter[0]);". Instead of "new
029             * Parameter[0]" it should be "null" to have a behaviour consistent to
030             * DefaultPicoContainer, i.e. if PicoContainer shall resolve
031             * dependencies itself.
032             */
033            pico.registerComponentImplementation(I2.class, C.class);
034    
035            /* The following verify() throws the exception, but is expected not to
036             * throw: "org.picocontainer.PicoVerificationException:
037             * [[org.picocontainer.PicoInitializationException: Either do the
038             * specified parameters not match any of the following constructors:
039             * [public PicoContainerBugTest$C(PicoContainerBugTest$A)] or the
040             * constructors were not accessible for 'class
041             * PicoContainerBugTest$C']]".
042             *
043             * I believe that the error comes this way: In method
044             * getGreediestSatisfiableConstructor parameters are checked against
045             * null and if parameters is not null it is assumed that specific
046             * parameters have been given so that no automatic resolution takes
047             * place. As now during registration instead of "null" falsly "new
048             * Parameter[0]" was stored, this is now interpreted as if only the
049             * nullary constructor shall be used, and if that doesn't exist, the
050             * exception is thrown.
051             */
052            new VerifyingVisitor().traverse(pico);
053        }
054    
055        public static interface I1 {
056        }
057    
058        public static interface I2 {
059        }
060    
061        public static class A {
062            public A() {
063            }
064        }
065    
066        public static class B implements I1 {
067            public B(final A a) {
068            }
069        }
070    
071        public static class C implements I2 {
072            public C(final A a) {
073            }
074        }
075    }