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 *****************************************************************************/
010 package org.picocontainer.defaults.issues;
011
012 import static org.junit.Assert.fail;
013 import static org.junit.Assert.assertNotNull;
014
015 import org.junit.Test;
016 import org.junit.Assert;
017 import org.picocontainer.DefaultPicoContainer;
018 import org.picocontainer.MutablePicoContainer;
019 import org.picocontainer.injectors.AbstractInjector;
020
021 public final class Issue0342TestCase {
022
023 interface Interface {
024 }
025
026 interface SubInterface extends Interface {
027 }
028
029 public static class Generic<I extends Interface> {
030 private final I iface;
031
032 public Generic(final I iface) {
033 this.iface = iface;
034 }
035 }
036
037 public static class Implementation implements Interface {
038 }
039
040 public static class SubImplementation implements SubInterface {
041 }
042
043
044 @Test
045 public void testNotTheBug() {
046 //hard coded instantitation
047 Generic<Implementation> generic1 = new Generic<Implementation>(new Implementation());
048 Assert.assertNotNull(generic1);
049 Generic<SubImplementation> generic2 = new Generic<SubImplementation>(new SubImplementation());
050 Assert.assertNotNull(generic2);
051 }
052
053
054 @Test
055 public void testTheBug() {
056
057 //using picocontainer
058 DefaultPicoContainer container = new DefaultPicoContainer();
059 container.addComponent(Implementation.class);
060 container.addComponent(Generic.class);
061 Generic result = container.getComponent(Generic.class); // fails here.
062 Assert.assertNotNull(result);
063 Assert.assertNotNull(result.iface);
064
065 }
066
067
068 @Test
069 public void testTheBug2() {
070
071 DefaultPicoContainer container = new DefaultPicoContainer();
072 container.addComponent(SubImplementation.class);
073 container.addComponent(Generic.class);
074 //should be Generic<SubImplementation> but requires unsafe cast
075 Generic<?> result2 = container.getComponent(Generic.class); // fails here
076 Assert.assertNotNull(result2);
077 Assert.assertNotNull(result2.iface);
078
079 }
080
081 }