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 * Original code by * 009 *****************************************************************************/ 010package org.picocontainer.injectors; 011 012import static junit.framework.Assert.assertEquals; 013import static junit.framework.Assert.fail; 014import static org.junit.Assert.assertNotNull; 015import static org.junit.Assert.assertThat; 016import static org.junit.Assert.assertTrue; 017 018import org.junit.Test; 019import org.picocontainer.DefaultPicoContainer; 020import org.picocontainer.MutablePicoContainer; 021import org.picocontainer.lifecycle.NullLifecycleStrategy; 022import org.picocontainer.monitors.NullComponentMonitor; 023 024public class TypedFieldInjectorTestCase { 025 026 public static class Helicopter { 027 private PogoStick pogo; 028 } 029 030 public static class PogoStick { 031 } 032 033 public static class Hulahoop { 034 } 035 036 @Test public void testFieldInjectionByTypeWhereMatch() { 037 MutablePicoContainer pico = new DefaultPicoContainer(); 038 pico.addAdapter(new TypedFieldInjector(Helicopter.class, Helicopter.class, null, new NullComponentMonitor(), 039 Integer.class.getName() + " " + PogoStick.class.getName() + " " + Float.class.getName())); 040 pico.addComponent(PogoStick.class, new PogoStick()); 041 Helicopter chopper = pico.getComponent(Helicopter.class); 042 assertNotNull(chopper); 043 assertNotNull(chopper.pogo); 044 } 045 046 @Test public void testFieldInjectionByTypeWhereNoMatch() { 047 MutablePicoContainer pico = new DefaultPicoContainer(); 048 pico.setName("parent"); 049 pico.addAdapter(new TypedFieldInjector(Helicopter.class, Helicopter.class, null, new NullComponentMonitor(), 050 Integer.class.getName() + " " + PogoStick.class.getName() + " " + Float.class.getName())); 051 pico.addComponent(Hulahoop.class, new Hulahoop()); 052 try { 053 pico.getComponent(Helicopter.class); 054 fail("should have barfed"); 055 } catch (AbstractInjector.UnsatisfiableDependenciesException e) { 056 String expected = "Helicopter has unsatisfied dependency for fields [PogoStick.pogo] from parent:2<|"; 057 String actual = e.getMessage(); 058 actual = actual.replace(TypedFieldInjectorTestCase.class.getName() + "$", ""); 059 assertEquals(expected, actual); 060 } 061 } 062 063}