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 org.junit.Test;
012 import org.picocontainer.DefaultPicoContainer;
013 import org.picocontainer.containers.EmptyPicoContainer;
014 import org.picocontainer.lifecycle.NullLifecycleStrategy;
015
016 import static junit.framework.Assert.assertNull;
017 import static org.junit.Assert.assertEquals;
018 import static org.junit.Assert.assertNotNull;
019 import static org.junit.Assert.assertTrue;
020
021 /**
022 * @author Paul Hammant
023 */
024 public class NamedMethodInjectionTestCase {
025
026 public static class Bean {
027 private String something;
028
029 public void setSomething(String blah) {
030 this.something = blah;
031 }
032 }
033
034 @Test
035 public void containerShouldMakeUsableNamedMethodInjector() {
036 DefaultPicoContainer picoContainer = new DefaultPicoContainer(new NamedMethodInjection(), new NullLifecycleStrategy(), new EmptyPicoContainer());
037 picoContainer.addComponent(Bean.class);
038 picoContainer.addConfig("something", "hello there");
039 assertTrue(picoContainer.getComponentAdapter(Bean.class) instanceof NamedMethodInjector);
040 assertEquals("hello there", picoContainer.getComponent(Bean.class).something);
041 }
042
043 @Test
044 public void containerShouldMakeNamedMethodInjectorThatIsOptionalInUse() {
045 DefaultPicoContainer picoContainer = new DefaultPicoContainer(new NamedMethodInjection(true), new NullLifecycleStrategy(), new EmptyPicoContainer());
046 picoContainer.addComponent(Bean.class);
047 assertTrue(picoContainer.getComponentAdapter(Bean.class) instanceof NamedMethodInjector);
048 assertNull(picoContainer.getComponent(Bean.class).something);
049 }
050
051 public static class MyConnectionPoolDataSource {
052 private String serverName;
053 private String databaseName;
054 private String user;
055
056 public void setServerName(String a0) {
057 this.serverName = a0;
058 }
059 public void setDatabaseName(String a0) {
060 this.databaseName = a0;
061 }
062 public void setUser(String a0) {
063 this.user = a0;
064 }
065 }
066
067 @Test
068 public void vincentVanBeverensExample() {
069 MyConnectionPoolDataSource windmill = new DefaultPicoContainer(new NamedMethodInjection())
070 .addComponent(MyConnectionPoolDataSource.class)
071 .addConfig("user", "fred")
072 .addConfig("serverName", "example.com")
073 .addConfig("databaseName", "big://table")
074 .getComponent(MyConnectionPoolDataSource.class);
075 assertNotNull(windmill);
076 assertNotNull(windmill.serverName);
077 assertEquals("example.com", windmill.serverName);
078 assertNotNull(windmill.user);
079 assertEquals("fred", windmill.user);
080 assertNotNull(windmill.databaseName);
081 assertEquals("big://table", windmill.databaseName);
082 }
083
084 }