001 /*****************************************************************************
002 * Copyright (C) PicoContainer Organization. All rights reserved. *
003 * ------------------------------------------------------------------------- *
004 * The software in this package is published under the terms of the BSD *
005 * style license a copy of which has been included with this distribution in *
006 * the LICENSE.txt file. *
007 * *
008 * Original code by Johan Hoogenboezem (thanks Johan) *
009 *****************************************************************************/
010 package org.picocontainer.injectors;
011
012 import static org.junit.Assert.*;
013
014 import org.junit.*;
015 import org.picocontainer.*;
016 import org.picocontainer.behaviors.*;
017
018 import java.lang.reflect.Type;
019
020 public class IntoTypeTestCase {
021
022 @Test
023 public void testThatIntoSetupCorrectlyForNestedInjectionViaAFactory() throws Exception {
024 MutablePicoContainer pico = new DefaultPicoContainer(new Caching());
025 pico.addAdapter(new AliceFactory());
026 pico.addComponent(Bob.class);
027 System.out.println("Going to ask pico for a Bob");
028 assertTrue(Bob.class.isAssignableFrom(Bob.class));
029 Bob bob = pico.getComponent(Bob.class);
030 assertNotNull(bob);
031 assertNotNull(bob.getAlice());
032 }
033
034
035 public static interface Alice {
036 }
037
038
039 public static class AliceImpl implements Alice {
040 }
041
042 public static class Bob {
043
044 private Alice alice;
045
046 public Bob(Alice alice) {
047 System.out.println("Bob gets an Alice: " + alice);
048 this.alice = alice;
049 }
050
051 public Alice getAlice() {
052 return alice;
053 }
054
055 }
056
057
058 public static class AliceFactory extends FactoryInjector<Alice> {
059 @Override
060 public Alice getComponentInstance(PicoContainer container, Type into) {
061 // System.out.println("Manufacturing an Alice for " + ((InjectInto) into).getIntoClass());
062 if (Bob.class.isAssignableFrom(((InjectInto) into).getIntoClass())) {
063 return new AliceImpl();
064 } else {
065 fail("Expected a " + Bob.class + ", but got a " + into + " instead.");
066 return null;
067 }
068 }
069
070 }
071
072
073 }