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 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant *
009 *****************************************************************************/
010 package org.picocontainer.injectors;
011
012 import org.picocontainer.PicoCompositionException;
013 import org.picocontainer.ComponentFactory;
014 import org.picocontainer.DefaultPicoContainer;
015 import org.picocontainer.injectors.AnnotatedMethodInjection;
016 import org.picocontainer.injectors.AnnotatedMethodInjector;
017 import org.picocontainer.monitors.NullComponentMonitor;
018 import org.picocontainer.tck.AbstractComponentFactoryTestCase;
019 import org.picocontainer.tck.AbstractComponentAdapterTestCase.RecordingLifecycleStrategy;
020 import org.picocontainer.testmodel.NullLifecycle;
021 import org.picocontainer.testmodel.RecordingLifecycle.One;
022
023 /**
024 * @author Jörg Schaible
025 */
026 public class AnnotatedMethodInjectionTestCase extends AbstractComponentFactoryTestCase {
027 protected void setUp() throws Exception {
028 picoContainer = new DefaultPicoContainer(createComponentFactory());
029 }
030
031 protected ComponentFactory createComponentFactory() {
032 return new AnnotatedMethodInjection();
033 }
034
035 public static interface Bean {
036 }
037
038 public static class NamedBean implements Bean {
039 private String name;
040
041 public String getName() {
042 return name;
043 }
044
045 public void setName(String name) {
046 this.name = name;
047 }
048 }
049
050 public static class NamedBeanWithPossibleDefault extends NamedBean {
051 private boolean byDefault;
052
053 public NamedBeanWithPossibleDefault() {
054 }
055
056 public NamedBeanWithPossibleDefault(String name) {
057 setName(name);
058 byDefault = true;
059 }
060
061 public boolean getByDefault() {
062 return byDefault;
063 }
064 }
065
066 public static class NoBean extends NamedBean {
067 public NoBean(String name) {
068 setName(name);
069 }
070 }
071
072 public void testContainerUsesStandardConstructor() {
073 picoContainer.addComponent(Bean.class, NamedBeanWithPossibleDefault.class);
074 picoContainer.addComponent("Tom");
075 NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponent(Bean.class);
076 assertFalse(bean.getByDefault());
077 }
078
079 public void testContainerUsesOnlyStandardConstructor() {
080 picoContainer.addComponent(Bean.class, NoBean.class);
081 picoContainer.addComponent("Tom");
082 try {
083 picoContainer.getComponent(Bean.class);
084 fail("Instantiation should have failed.");
085 } catch (PicoCompositionException e) {
086 }
087 }
088
089 public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException {
090 RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
091 AnnotatedMethodInjection componentFactory = new AnnotatedMethodInjection();
092 AnnotatedMethodInjector aica = (AnnotatedMethodInjector)componentFactory.createComponentAdapter(new NullComponentMonitor(), strategy, null, NullLifecycle.class, NullLifecycle.class);
093 One one = new One(new StringBuffer());
094 aica.start(one);
095 aica.stop(one);
096 aica.dispose(one);
097 assertEquals("<start<stop<dispose", strategy.recording());
098 }
099 }