    001    package org.picocontainer.behaviors;
    002
    003    import org.junit.Test;
    004    import static org.junit.Assert.assertNotNull;
    005    import org.picocontainer.DefaultPicoContainer;
    006    import org.picocontainer.Characteristics;
    007    import org.picocontainer.ComponentAdapter;
    008    import org.picocontainer.injectors.SetterInjection;import static junit.framework.Assert.assertEquals;
    009
    010    public class CircularTestCase {
    011
    012        public static interface IFish {
    013            IWater getWater();
    014        }
    015        public static class Fish implements IFish {
    016            IWater water;
    017
    018            public void setWater(IWater water) {
    019                this.water = water;
    020            }
    021
    022            public IWater getWater() {
    023                return water;
    024            }
    025        }
    026
    027        public static interface IWater {
    028            IFish getFish();
    029
    030        }
    031        public static class Water implements IWater {
    032            IFish fish;
    033
    034            public void setFish(IFish fish) {
    035                this.fish = fish;
    036            }
    037
    038            public IFish getFish() {
    039                return fish;
    040            }
    041        }
    042
    043        @Test
    044        public void circularIsPossibleWithACharacteristic() {
    045            DefaultPicoContainer pico = new DefaultPicoContainer(new Caching().wrap(new SetterInjection()));
    046            pico.as(Characteristics.ENABLE_CIRCULAR).addComponent(IFish.class, Fish.class);
    047            pico.addComponent(IWater.class, Water.class);
    048            IWater water = pico.getComponent(IWater.class);
    049            IFish fish = pico.getComponent(IFish.class);
    050            assertNotNull(water.getFish());
    051            assertNotNull(fish.getWater());
    052        }
    053
    054        @Test
    055        public void enableCircularCharacteristicIsRedundantForImplementationHiding() {
    056            DefaultPicoContainer pico = new DefaultPicoContainer(new ImplementationHiding().wrap(new SetterInjection()));
    057            pico.addComponent(IFish.class, Fish.class);
    058            pico.addComponent(IWater.class, Water.class);
    059            IWater water = pico.getComponent(IWater.class);
    060            IFish fish = pico.getComponent(IFish.class);
    061            assertNotNull(water.getFish());
    062            assertNotNull(fish.getWater());
    063
    064            ComponentAdapter<?> ca = pico.getComponentAdapter(IFish.class);
    065            assertEquals("Hidden:SetterInjector-" + IFish.class,ca.toString());
    066
    067        }
    068
    069
    070    }