public void testComponentsCanBeRemovedByInstance() {
   MutablePicoContainer pico = createPicoContainer(null);
   pico.addComponent(HashMap.class);
   pico.addComponent(ArrayList.class);
   List list = pico.getComponent(List.class);
   pico.removeComponentByInstance(list);
   assertEquals(1, pico.getComponentAdapters().size());
   assertEquals(1, pico.getComponents().size());
   assertEquals(HashMap.class, pico.getComponent(Serializable.class).getClass());
 }
  public void testCanRegisterTwoComponentsWithSameImplementionAndDifferentKey() throws Exception {
    MutablePicoContainer container = createPicoContainer(null);

    container.addComponent(SimpleA.class);
    container.addComponent("A", SimpleA.class);

    container.start();

    assertNotNull(container.getComponent("A"));
    assertNotNull(container.getComponent(SimpleA.class));
    assertNotSame(container.getComponent("A"), container.getComponent(SimpleA.class));
  }
  public void testMixingOfSDIandCDIWithTemporaryCharacterizationsDifferently() {

    MutablePicoContainer container = createPicoContainer(null).change(Characteristics.CACHE);
    container.as(SDI).addComponent(SdiDonkey.class);
    container.as(SDI).addComponent(SdiRabbit.class);
    container.addComponent(Horse.class);
    container.addComponent(CdiTurtle.class);

    SdiDonkey donkey = container.getComponent(SdiDonkey.class);
    SdiRabbit rabbit = container.getComponent(SdiRabbit.class);
    CdiTurtle turtle = container.getComponent(CdiTurtle.class);

    assertions(donkey, rabbit, turtle);
  }
 public void testPicoCanDifferentiateBetweenNamedIntsThatWouldOtherwiseBeAmbiguous() {
   MutablePicoContainer mpc = createPicoContainer(null);
   mpc.addComponent("one", 1);
   mpc.addComponent("two", 2);
   mpc.addComponent(NeedsTwo.class);
   assertEquals(2, mpc.getComponent(NeedsTwo.class).two);
 }
 public void testPicoCanDifferentiateBetweenNamedStringsThatWouldOtherwiseBeAmbiguous() {
   MutablePicoContainer mpc = createPicoContainer(null);
   mpc.addComponent("greeting", "1");
   mpc.addComponent("message", "2");
   mpc.addComponent(PicoCompositionException.class, PicoCompositionException.class);
   assertEquals("2", mpc.getComponent(PicoCompositionException.class).getMessage());
 }
  /*
  When pico tries to resolve DecoratedTouchable it find as dependency itself and SimpleTouchable.
  Problem is basically the same as above. Pico should not consider self as solution.

  JS
  fixed it ( PICO-222 )
  KP
  */
  public void testUnambiguouSelfDependency() {
    MutablePicoContainer pico = createPicoContainer(null);
    pico.addComponent(SimpleTouchable.class);
    pico.addComponent(DecoratedTouchable.class);
    Touchable t = (Touchable) pico.getComponent((Object) DecoratedTouchable.class);
    assertNotNull(t);
  }
 public void
     testComponentsWithCommonSupertypeWhichIsAConstructorArgumentCanBeLookedUpByConcreteType() {
   MutablePicoContainer pico = createPicoContainer(null);
   pico.addComponent(LinkedList.class, LinkedList.class, Parameter.ZERO);
   pico.addComponent(ArrayList.class);
   assertEquals(ArrayList.class, pico.getComponent((Class) ArrayList.class).getClass());
 }
  public void testMixingOfSDIandCDIInBuilderStyle() {

    MutablePicoContainer container = createPicoContainer(null).change(Characteristics.CACHE);
    container
        .addComponent(Horse.class)
        .change(SDI)
        .addComponent(SdiDonkey.class)
        .addComponent(SdiRabbit.class)
        .change(CDI)
        .addComponent(CdiTurtle.class);

    SdiDonkey donkey = container.getComponent(SdiDonkey.class);
    SdiRabbit rabbit = container.getComponent(SdiRabbit.class);
    CdiTurtle turtle = container.getComponent(CdiTurtle.class);

    assertions(donkey, rabbit, turtle);
  }
 @SuppressWarnings({"unchecked"})
 public final void testInstancesAreNotSharedBetweenContainers() {
   final MutablePicoContainer picoA = new DefaultPicoContainer();
   final MutablePicoContainer picoB = new DefaultPicoContainer();
   picoA.addAdapter(
       new ThreadLocalizing.ThreadLocalized(
           new ConstructorInjection.ConstructorInjector(List.class, ArrayList.class, null)));
   picoB.addAdapter(
       new ThreadLocalizing.ThreadLocalized(
           new ConstructorInjection.ConstructorInjector(List.class, ArrayList.class, null)));
   final List<String> hello1 = picoA.getComponent(List.class);
   final List hello2 = picoA.getComponent(List.class);
   hello1.add("foo");
   assertEquals(hello1, hello2);
   final List hello3 = picoB.getComponent(List.class);
   assertEquals(0, hello3.size());
 }
 public void testPicoCanDifferentiateBetweenNamedObjectsThatWouldOtherwiseBeAmbiguous() {
   MutablePicoContainer mpc = createPicoContainer(null);
   Horse dobbin = new Horse();
   Horse redRum = new Horse();
   mpc.addComponent("dobbin", dobbin);
   mpc.addComponent("horse", redRum);
   mpc.addComponent(CdiTurtle.class);
   assertEquals(redRum, mpc.getComponent(CdiTurtle.class).horse);
 }
  public void testCanRegisterTwoComponentsImplementingSameInterfaceOneWithInterfaceAsKey()
      throws Exception {
    MutablePicoContainer container = createPicoContainer(null);

    container.addComponent(SimpleA.class);
    container.addComponent(A.class, WrappingA.class);

    container.start();

    assertEquals(WrappingA.class, container.getComponent(A.class).getClass());
  }
 public void testPicoUsedInBuilderStyle() {
   MutablePicoContainer pico = createPicoContainer(null);
   Touchable t =
       pico.change(Characteristics.CACHE)
           .addComponent(SimpleTouchable.class)
           .addComponent(DecoratedTouchable.class)
           .getComponent(DecoratedTouchable.class);
   SimpleTouchable t2 = pico.getComponent(SimpleTouchable.class);
   assertNotNull(t);
   assertNotNull(t2);
   t.touch();
   assertTrue(t2.wasTouched);
 }
 public void testShouldProvideInfoAboutDependingWhenAmbiguityHappens() {
   MutablePicoContainer pico = this.createPicoContainer(null);
   pico.addComponent(new ArrayList());
   pico.addComponent(new LinkedList());
   pico.addComponent(DependsOnCollection.class);
   try {
     pico.getComponent(DependsOnCollection.class);
     fail();
   } catch (AbstractInjector.AmbiguousComponentResolutionException expected) {
     String doc = DependsOnCollection.class.getName();
     assertEquals(
         "class "
             + doc
             + " needs a 'java.util.Collection' injected, but there are too many choices to inject. These:[class java.util.ArrayList, class java.util.LinkedList], refer http://picocontainer.org/ambiguous-injectable-help.html",
         expected.getMessage());
   }
 }
  public void testUpDownDependenciesCannotBeFollowed() {
    MutablePicoContainer parent = createPicoContainer(null);
    MutablePicoContainer child = createPicoContainer(parent);

    // ComponentF -> ComponentA -> ComponentB+C
    child.addComponent(ComponentF.class);
    parent.addComponent(ComponentA.class);
    child.addComponent(ComponentB.class);
    child.addComponent(ComponentC.class);

    try {
      child.getComponent(ComponentF.class);
      fail(
          "Thrown "
              + AbstractInjector.UnsatisfiableDependenciesException.class.getName()
              + " expected");
    } catch (final AbstractInjector.UnsatisfiableDependenciesException e) {
      assertEquals(ComponentB.class, e.getUnsatisfiedDependencyType());
    }
  }
 public void testThangCanBeInstantiatedWithArrayList() {
   MutablePicoContainer pico = new DefaultPicoContainer();
   pico.addComponent(Thingie.class);
   pico.addComponent(ArrayList.class);
   assertNotNull(pico.getComponent(Thingie.class));
 }