public void
     testAddComponentNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.change(Characteristics.NO_HIDE_IMPL).addComponent("foo", String.class);
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(ConstructorInjector.class, foo.getClass());
 }
  public void testParameterCanBePrimitive() throws Exception {
    DefaultPicoContainer pico = new DefaultPicoContainer();
    pico.addComponent(Animal.class, Dino2.class, new ConstantParameter(22));

    Animal animal = pico.getComponent(Animal.class);
    assertNotNull("Component not null", animal);
    assertEquals("22", animal.getFood());
  }
 public void testAddComponentUsesImplementationHidingBehavior() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.addComponent("foo", String.class);
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(HiddenImplementation.class, foo.getClass());
   assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
 }
 public void testAddComponentUsesImplementationHidingBehaviorWithRedundantHideImplProperty() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.change(Characteristics.HIDE_IMPL).addComponent("foo", String.class);
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(HiddenImplementation.class, foo.getClass());
   assertEquals(ConstructorInjector.class, ((AbstractBehavior) foo).getDelegate().getClass());
 }
  // TODO - move to AbstractComponentRegistryTestCase
  public void testGetComponentSpecification() throws PicoCompositionException {
    DefaultPicoContainer pico = new DefaultPicoContainer();

    assertNull(pico.getComponentAdapter(Touchable.class, null));
    pico.addComponent(SimpleTouchable.class);
    assertNotNull(pico.getComponentAdapter(SimpleTouchable.class, null));
    assertNotNull(pico.getComponentAdapter(Touchable.class, null));
  }
  public void testParameterCanBePassedToConstructor() throws Exception {
    DefaultPicoContainer pico = new DefaultPicoContainer();
    pico.addComponent(Animal.class, Dino.class, new ConstantParameter("bones"));

    Animal animal = pico.getComponent(Animal.class);
    assertNotNull("Component not null", animal);
    assertEquals("bones", animal.getFood());
  }
 public void testAddAdapterUsesImplementationHidingBehavior() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.addAdapter(
       new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(HiddenImplementation.class, foo.getClass());
   assertEquals(InstanceAdapter.class, ((AbstractBehavior) foo).getDelegate().getClass());
 }
  public void testMultipleParametersCanBePassed() throws Exception {
    DefaultPicoContainer pico = new DefaultPicoContainer();
    pico.addComponent(
        Animal.class, Dino3.class, new ConstantParameter("a"), new ConstantParameter("b"));

    Animal animal = pico.getComponent(Animal.class);
    assertNotNull("Component not null", animal);
    assertEquals("ab", animal.getFood());
  }
 public void testAddAdapterNoesNotUseImplementationHidingBehaviorWhenNoCachePropertyIsSpecified() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(new ImplementationHiding().wrap(new ConstructorInjection()));
   pico.change(Characteristics.NO_HIDE_IMPL)
       .addAdapter(
           new InstanceAdapter(
               "foo", "bar", new NullLifecycleStrategy(), new NullComponentMonitor()));
   ComponentAdapter foo = pico.getComponentAdapter("foo");
   assertEquals(InstanceAdapter.class, foo.getClass());
 }
 @Test
 public void testWithNonNullLifecycleStrategy() {
   DefaultPicoContainer dpc =
       new DefaultPicoContainer(
           new CompositeInjection(new ConstructorInjection(), new AnnotatedMethodInjection()),
           new NonNullLifecycleStrategy(),
           new EmptyPicoContainer());
   dpc.addComponent(Bar.class);
   assertNotNull(dpc.getComponent(Bar.class));
 }
  @Test
  public void testAsParentContainer() {
    CommandLineArgumentsPicoContainer apc =
        new CommandLineArgumentsPicoContainer(new String[] {"a=aaa", "b=bbb", "d=22"});
    assertEquals("aaa", apc.getComponent("a"));
    assertEquals("bbb", apc.getComponent("b"));
    assertEquals("22", apc.getComponent("d"));

    DefaultPicoContainer dpc = new DefaultPicoContainer(apc);
    dpc.addComponent(NeedsString.class);
    assertEquals("bbb", dpc.getComponent(NeedsString.class).val);
  }
 @Test
 public void testAnnotatedFieldInjectionComponentCanDependOnConfig() {
   CommandLineArgumentsPicoContainer apc =
       new CommandLineArgumentsPicoContainer(new String[] {"a=a", "b=2", "c=true"});
   DefaultPicoContainer pico = new DefaultPicoContainer(new AnnotatedFieldInjection(), apc);
   pico.addConfig("zzz", "zzz");
   pico.as(Characteristics.USE_NAMES).addComponent(NeedsAFew3.class);
   NeedsAFew3 needsAFew = pico.getComponent(NeedsAFew3.class);
   assertNotNull(needsAFew);
   assertEquals("a", needsAFew.a);
   assertEquals(2, needsAFew.b);
   assertEquals(true, needsAFew.c);
 }
  public void testDuplicateRegistrationWithTypeAndObject() throws PicoCompositionException {
    DefaultPicoContainer pico = new DefaultPicoContainer();

    pico.addComponent(SimpleTouchable.class);
    try {
      pico.addComponent(SimpleTouchable.class, new SimpleTouchable());
      fail("Should have barfed with dupe registration");
    } catch (PicoCompositionException e) {
      // expected
      assertTrue(e.getMessage().startsWith("Duplicate"));
      assertTrue(e.getMessage().indexOf(SimpleTouchable.class.getName()) > 0);
    }
  }
 @Test
 public void testUnsatisfiableIfNoSuitableTyesForInjection() {
   CommandLineArgumentsPicoContainer apc =
       new CommandLineArgumentsPicoContainer(new String[] {"zz=zz"});
   DefaultPicoContainer pico = new DefaultPicoContainer(apc);
   pico.as(Characteristics.USE_NAMES).addComponent(NeedsAFew.class);
   try {
     Object foo = pico.getComponent(NeedsAFew.class);
     fail();
   } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
     // expetced;
   }
 }
  public void testParametersCanBeMixedWithComponentsCanBePassed() throws Exception {
    DefaultPicoContainer pico = new DefaultPicoContainer();
    pico.addComponent(Touchable.class, SimpleTouchable.class);
    pico.addComponent(
        Animal.class,
        Dino4.class,
        new ConstantParameter("a"),
        new ConstantParameter(3),
        new ConstantParameter("b"),
        ComponentParameter.DEFAULT);

    Animal animal = pico.getComponent(Animal.class);
    assertNotNull("Component not null", animal);
    assertEquals("a3b org.picocontainer.testmodel.SimpleTouchable", animal.getFood());
  }
  // TODO move
  public void testMultipleImplementationsAccessedThroughKey() throws PicoCompositionException {
    SimpleTouchable Touchable1 = new SimpleTouchable();
    SimpleTouchable Touchable2 = new SimpleTouchable();
    DefaultPicoContainer pico = new DefaultPicoContainer();
    pico.addComponent("Touchable1", Touchable1);
    pico.addComponent("Touchable2", Touchable2);
    pico.addComponent("fred1", DependsOnTouchable.class, new ComponentParameter("Touchable1"));
    pico.addComponent("fred2", DependsOnTouchable.class, new ComponentParameter("Touchable2"));

    DependsOnTouchable fred1 = (DependsOnTouchable) pico.getComponent("fred1");
    DependsOnTouchable fred2 = (DependsOnTouchable) pico.getComponent("fred2");

    assertFalse(fred1 == fred2);
    assertSame(Touchable1, fred1.getTouchable());
    assertSame(Touchable2, fred2.getTouchable());
  }
 @Test
 public void testComponentWithCtorAndSetterDiCanHaveAllDepsSatisfiedWithANonSetInjectMethod()
     throws NoSuchMethodException {
   DefaultPicoContainer dpc =
       new DefaultPicoContainer(
           new CompositeInjection(new ConstructorInjection(), new SetterInjection("inject")),
           new NullLifecycleStrategy(),
           new EmptyPicoContainer());
   dpc.addComponent(Bar.class);
   dpc.addComponent(Baz.class);
   dpc.addComponent(Foo2.class);
   Foo2 foo = dpc.getComponent(Foo2.class);
   assertNotNull(foo);
   assertNotNull(foo.bar);
   assertNotNull(foo.baz);
 }
 @Test
 public void testComponentWithCtorAndNamedFieldWorkToegether() throws NoSuchMethodException {
   DefaultPicoContainer dpc =
       new DefaultPicoContainer(
           new CompositeInjection(new ConstructorInjection(), new NamedFieldInjection()),
           new NullLifecycleStrategy(),
           new EmptyPicoContainer());
   dpc.addComponent(Bar.class);
   dpc.addConfig("one", "1");
   dpc.addConfig("two", "2");
   dpc.as(injectionFieldNames("one", "two")).addComponent(Foo4.class);
   Foo4 foo4 = dpc.getComponent(Foo4.class);
   assertNotNull(foo4);
   assertNotNull(foo4.bar);
   assertNotNull(foo4.one);
   assertEquals("1", foo4.one);
   assertNotNull(foo4.two);
   assertEquals("2", foo4.two);
 }
  public void testRegistrationByNameAndClassWithResolving() throws Exception {
    DefaultPicoContainer pico = new DefaultPicoContainer();

    pico.addComponent(List.class, new ArrayList());
    pico.addComponent("one", Webster.class);
    pico.addComponent("two", SimpleTouchable.class);

    assertEquals("Wrong number of comps in the internals", 3, pico.getComponents().size());

    assertNotNull("Object one the same", pico.getComponent("one"));
    assertNotNull("Object two the same", pico.getComponent("two"));

    assertNull("Lookup of unknown key should return null", pico.getComponent("unknown"));
  }
 public void testLazyInstantiationSideEffectWhenForceOfDelayedInstantiationOfDependantClass() {
   DefaultPicoContainer pico =
       new DefaultPicoContainer(
           new ImplementationHiding().wrap(new Caching().wrap(new ConstructorInjection())));
   pico.addComponent(StringBuilder.class);
   pico.addComponent(NeedsStringBuilder.class, NeedsStringBuilderImpl.class);
   pico.addComponent(NeedsNeedsStringBuilder.class);
   NeedsNeedsStringBuilder nnsb = pico.getComponent(NeedsNeedsStringBuilder.class);
   assertNotNull(nnsb);
   StringBuilder sb = pico.getComponent(StringBuilder.class);
   assertEquals("", sb.toString()); // not instantiated yet
   nnsb.foo();
   assertEquals("<init>foo()", sb.toString()); // instantiated
 }
  // TODO - move
  public void testRegistrationByName() throws Exception {
    DefaultPicoContainer pico = new DefaultPicoContainer();

    Webster one = new Webster(new ArrayList());
    Touchable two = new SimpleTouchable();

    pico.addComponent("one", one);
    pico.addComponent("two", two);

    assertEquals("Wrong number of comps in the internals", 2, pico.getComponents().size());

    assertEquals("Looking up one Touchable", one, pico.getComponent("one"));
    assertEquals("Looking up two Touchable", two, pico.getComponent("two"));

    assertTrue("Object one the same", one == pico.getComponent("one"));
    assertTrue("Object two the same", two == pico.getComponent("two"));

    assertEquals("Lookup of unknown key should return null", null, pico.getComponent("unknown"));
  }
 /**
  * replace the class being mocked in the container by the mock
  *
  * @param container
  */
 public void replace(DefaultPicoContainer container) {
   container.removeComponent(clazz);
   container.addComponent(getMock());
 }