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 static org.junit.Assert.assertEquals; 013 import static org.junit.Assert.assertNotNull; 014 import static org.picocontainer.Characteristics.USE_NAMES; 015 016 import java.util.Properties; 017 018 import org.junit.Before; 019 import org.junit.Test; 020 import org.picocontainer.ComponentFactory; 021 import org.picocontainer.DefaultPicoContainer; 022 import org.picocontainer.lifecycle.NullLifecycleStrategy; 023 import org.picocontainer.monitors.NullComponentMonitor; 024 import org.picocontainer.parameters.ConstantParameter; 025 import org.picocontainer.tck.AbstractComponentFactoryTest; 026 import org.picocontainer.tck.AbstractComponentAdapterTest.RecordingLifecycleStrategy; 027 import org.picocontainer.testmodel.NullLifecycle; 028 import org.picocontainer.testmodel.RecordingLifecycle; 029 import org.picocontainer.testmodel.RecordingLifecycle.One; 030 031 /** 032 * @author Mauro Talevi 033 */ 034 public class ConstructorInjectionTestCase extends AbstractComponentFactoryTest { 035 036 @Before 037 public void setUp() throws Exception { 038 picoContainer = new DefaultPicoContainer(createComponentFactory()); 039 } 040 041 protected ComponentFactory createComponentFactory() { 042 return new ConstructorInjection(); 043 } 044 045 public static class ClassA { 046 private int x; 047 public ClassA(int x) { 048 this.x = x; 049 } 050 } 051 @Test public void testAutoConversionOfIntegerParam() { 052 picoContainer.as(USE_NAMES).addComponent(ClassA.class); 053 picoContainer.addComponent("x", "12"); 054 assertNotNull(picoContainer.getComponent(ClassA.class)); 055 assertEquals(12,picoContainer.getComponent(ClassA.class).x); 056 } 057 058 public static class ClassB { 059 private float x; 060 public ClassB(float x) { 061 this.x = x; 062 } 063 } 064 @Test public void testAutoConversionOfFloatParam() { 065 picoContainer.as(USE_NAMES).addComponent(ClassB.class); 066 picoContainer.addComponent("x", "1.2"); 067 assertNotNull(picoContainer.getComponent(ClassB.class)); 068 assertEquals(1.2,picoContainer.getComponent(ClassB.class).x, 0.0001); 069 } 070 071 072 /** 073 * Test class to verify the CICA can handle 074 * a constant parameter class type. 075 * 076 */ 077 @SuppressWarnings({"unchecked", "unused"}) 078 public static class ClassAsConstructor { 079 private final Class type; 080 081 public ClassAsConstructor(Class type) { 082 this.type = type; 083 } 084 } 085 086 @Test 087 @SuppressWarnings("unchecked") 088 public void allowClassTypesForComponentAdapter() { 089 ConstructorInjection componentFactory = new ConstructorInjection(); 090 091 ConstructorInjector cica = (ConstructorInjector) 092 componentFactory.createComponentAdapter(new NullComponentMonitor(), new NullLifecycleStrategy(), new Properties(), ClassAsConstructor.class, ClassAsConstructor.class, new ConstantParameter(String.class)); 093 094 ClassAsConstructor instance = (ClassAsConstructor) cica.getComponentInstance(picoContainer); 095 assertNotNull(instance); 096 097 } 098 099 100 }