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 *****************************************************************************/
009 package org.picocontainer.injectors;
010
011 import static org.junit.Assert.assertNotNull;
012 import static org.junit.Assert.assertNull;
013 import static org.junit.Assert.assertTrue;
014
015 import org.junit.Test;
016 import org.picocontainer.DefaultPicoContainer;
017 import org.picocontainer.annotations.Inject;
018
019 /**
020 * @author Paul Hammant
021 */
022 public class MultiInjectionTestCase {
023
024 public static class Bar {
025 }
026 public static class Baz {
027 }
028 public static class Foo {
029 private final Bar bar;
030 private Baz baz;
031
032 public Foo(Bar bar) {
033 this.bar = bar;
034 }
035
036 public void setBaz(Baz baz) {
037 this.baz = baz;
038 }
039 }
040
041 public static class Foo2 {
042 private final Bar bar;
043 private Baz baz;
044
045 public Foo2(Bar bar) {
046 this.bar = bar;
047 }
048
049 public void injectBaz(Baz baz) {
050 this.baz = baz;
051 }
052 }
053
054 public static class Foo3 {
055 private final Bar bar;
056 private Baz baz;
057
058 public Foo3(Bar bar) {
059 this.bar = bar;
060 }
061
062 @Inject
063 public void fjshdfkjhsdkfjh(Baz baz) {
064 this.baz = baz;
065 }
066 }
067
068 @Test public void testComponentWithCtorAndSetterDiCanHaveAllDepsSatisfied() throws NoSuchMethodException {
069 DefaultPicoContainer dpc = new DefaultPicoContainer(new MultiInjection());
070 dpc.addComponent(Bar.class);
071 dpc.addComponent(Baz.class);
072 dpc.addComponent(Foo.class);
073 Foo foo = dpc.getComponent(Foo.class);
074 assertNotNull(foo);
075 assertNotNull(foo.bar);
076 assertNotNull(foo.baz);
077 }
078
079 @Test public void testComponentWithCtorAndSetterDiCanHaveAllDepsSatisfiedWithANonSetInjectMethod() throws NoSuchMethodException {
080 DefaultPicoContainer dpc = new DefaultPicoContainer(new MultiInjection("inject"));
081 dpc.addComponent(Bar.class);
082 dpc.addComponent(Baz.class);
083 dpc.addComponent(Foo2.class);
084 Foo2 foo = dpc.getComponent(Foo2.class);
085 assertNotNull(foo);
086 assertNotNull(foo.bar);
087 assertNotNull(foo.baz);
088 }
089
090 @Test public void testComponentWithCtorAndMethodAnnotatedDiCanHaveAllDepsSatisfied() throws NoSuchMethodException {
091 DefaultPicoContainer dpc = new DefaultPicoContainer(new MultiInjection());
092 dpc.addComponent(Bar.class);
093 dpc.addComponent(Baz.class);
094 dpc.addComponent(Foo3.class);
095 Foo3 foo = dpc.getComponent(Foo3.class);
096 assertNotNull(foo);
097 assertNotNull(foo.bar);
098 assertNotNull(foo.baz);
099 }
100
101
102 @Test public void testComponentWithCtorAndSetterDiCanNoteMissingSetterDependency() throws NoSuchMethodException {
103 DefaultPicoContainer dpc = new DefaultPicoContainer(new MultiInjection());
104 dpc.addComponent(Bar.class);
105 dpc.addComponent(Foo.class);
106 try {
107 Foo foo = dpc.getComponent(Foo.class);
108 } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
109 assertTrue(e.getMessage().contains(Baz.class.getName()));
110 }
111 }
112
113 }