Problems with named setter injection

View: New views
3 Messages — Rating Filter:   Alert me  

Problems with named setter injection

by Martin Fuzzey-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
I'm having weird problems with named setter injection.
The attached test case demonstrates this.

The problem occurs when a class requires the injection of two objects
of the same type (disambiguated by USE_NAMES) _and_ another object.
Depending on the declaration order of the setters it works or fails.
I think this is related to the method
IterativeInjector.getMatchingParameterListForSetters() which contains
two nested loops that seem to mismatch types..

The test case also shows another strange behaviour :
it contains two tests, testGood and testBad which invoke the same code
on two classes GoodTarget and BadTarget whose only difference is the
order of the setters.

If just testGood is run it passes  [normal]
if just testBad is run it fails  [the problem]
if testGood then testBad are run they both pass  [!!}
if testBad then testGood are run they both fail... [!!]

This is inspite of each test creating its own picocontainer instance.
Looks like some data is being kept around (static fields??)

Martin

[MultiTestCase.java]

package javadi.pico;

import org.junit.Test;
import org.picocontainer.MutablePicoContainer;
import org.picocontainer.PicoBuilder;
import org.picocontainer.Characteristics;

import org.picocontainer.injectors.SetterInjection;
import static junit.framework.Assert.assertEquals;

public class MultiTestCase {

    //@Test
    public void testGood() {
        doTest(GoodTarget.class);
    }
   
    @Test
    public void testBad() {
        doTest(BadTarget.class);
    }
   
   
    private void doTest(Class targetClass) {
        MutablePicoContainer pico = new PicoBuilder(new SetterInjection("inject")).build();
        pico.addComponent("fruit1", Apple.class);
        pico.addComponent("fruit2", Pear.class);
        pico.addComponent(new Integer(42));
        pico.as(Characteristics.USE_NAMES).addComponent(targetClass);
       
        Target target = (Target)pico.getComponent(targetClass);
        System.out.println("target is " + target.getClass().getName());
        assertEquals("apple", target.eat1());
        assertEquals("pear", target.eat2());
        assertEquals(42, target.getNumber());
    }
   
   
    public static interface Fruit {
        public String eat();
    }
    public static class Apple implements Fruit {
        public String eat() { return "apple"; }
    }
    public static class Pear implements Fruit {
        public String eat() { return "pear"; }
    }
   
    public static interface Target {
        String eat1();
        String eat2();
        int getNumber();
    }
   
    public static class GoodTarget implements Target {
        private Fruit fruit1;
        private Fruit fruit2;
        private Integer number;

        public void injectNumber(Integer number)  {
            this.number = number;
        }

        public void injectFruit1(Fruit fruit1) {
            this.fruit1 = fruit1;
        }

        public void injectFruit2(Fruit fruit2) {
            this.fruit2 = fruit2;
        }
        public int getNumber() {
            return number.intValue();
        }
       
        public String eat1() { return fruit1.eat(); }
        public String eat2() { return fruit2.eat(); }
    }
   
    public static class BadTarget implements Target {
        private Fruit fruit1;
        private Fruit fruit2;
        private Integer number;

        public void injectFruit1(Fruit fruit1) {
            this.fruit1 = fruit1;
        }

        public void injectFruit2(Fruit fruit2) {
            this.fruit2 = fruit2;
        }
        public void injectNumber(Integer number)  {
            this.number = number;
        }
        public int getNumber() {
            return number.intValue();
        }
       
        public String eat1() { return fruit1.eat(); }
        public String eat2() { return fruit2.eat(); }
    }
   
}


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Re: Problems with named setter injection

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

That is interesting Martin.

I'll try to work thru it later ...

- Paul


On Jun 23, 2008, at 9:39 AM, Martin Fuzzey wrote:

> Hi,
> I'm having weird problems with named setter injection.
> The attached test case demonstrates this.
>
> The problem occurs when a class requires the injection of two objects
> of the same type (disambiguated by USE_NAMES) _and_ another object.
> Depending on the declaration order of the setters it works or fails.
> I think this is related to the method
> IterativeInjector.getMatchingParameterListForSetters() which contains
> two nested loops that seem to mismatch types..
>
> The test case also shows another strange behaviour :
> it contains two tests, testGood and testBad which invoke the same code
> on two classes GoodTarget and BadTarget whose only difference is the
> order of the setters.
>
> If just testGood is run it passes  [normal]
> if just testBad is run it fails  [the problem]
> if testGood then testBad are run they both pass  [!!}
> if testBad then testGood are run they both fail... [!!]
>
> This is inspite of each test creating its own picocontainer instance.
> Looks like some data is being kept around (static fields??)
>
> Martin
> <
> MultiTestCase
> .java
> >---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Problems with named setter injection

by Paul Hammant-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not sure if you noticed dude, but we fixed this bug yesterday ..

sorry for inconvenience and thanks a bunch for writing the test.

- Paul

On Jun 23, 2008, at 9:39 AM, Martin Fuzzey wrote:

> Hi,
> I'm having weird problems with named setter injection.
> The attached test case demonstrates this.
>
> The problem occurs when a class requires the injection of two objects
> of the same type (disambiguated by USE_NAMES) _and_ another object.
> Depending on the declaration order of the setters it works or fails.
> I think this is related to the method
> IterativeInjector.getMatchingParameterListForSetters() which contains
> two nested loops that seem to mismatch types..
>
> The test case also shows another strange behaviour :
> it contains two tests, testGood and testBad which invoke the same code
> on two classes GoodTarget and BadTarget whose only difference is the
> order of the setters.
>
> If just testGood is run it passes  [normal]
> if just testBad is run it fails  [the problem]
> if testGood then testBad are run they both pass  [!!}
> if testBad then testGood are run they both fail... [!!]
>
> This is inspite of each test creating its own picocontainer instance.
> Looks like some data is being kept around (static fields??)
>
> Martin
> <
> MultiTestCase
> .java
> >---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price