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