    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.tck;
    011
    012
    013    import static org.junit.Assert.assertEquals;
    014    import static org.junit.Assert.assertFalse;
    015    import static org.junit.Assert.assertNotNull;
    016    import static org.junit.Assert.assertTrue;
    017    import static org.junit.Assert.fail;
    018
    019    import java.awt.event.ActionEvent;
    020    import java.awt.event.ActionListener;
    021    import java.io.IOException;
    022    import java.util.HashMap;
    023    import java.util.Map;
    024
    025    import org.junit.Test;
    026    import org.picocontainer.MutablePicoContainer;
    027    import org.picocontainer.PicoException;
    028
    029
    030    /**
    031     * @author Paul Hammant
    032     */
    033    public abstract class AbstractImplementationHidingPicoContainerTest extends AbstractPicoContainerTest {
    034
    035
    036        @Test public void testInstanceIsNotAutomaticallyHidden() {
    037            MutablePicoContainer pc = createImplementationHidingPicoContainer();
    038            pc.as(this.getProperties()).addComponent(Map.class, new HashMap());
    039            Map map = pc.getComponent(Map.class);
    040            assertNotNull(map);
    041            assertTrue(map instanceof HashMap);
    042        }
    043
    044        protected abstract MutablePicoContainer createImplementationHidingPicoContainer();
    045
    046
    047        @Test public void testImplementaionIsAutomaticallyHidden() {
    048            MutablePicoContainer pc = createImplementationHidingPicoContainer();
    049            pc.addComponent(Map.class, HashMap.class);
    050            Map map = pc.getComponent(Map.class);
    051            assertNotNull(map);
    052            assertFalse(map instanceof HashMap);
    053        }
    054
    055        @Test public void testNonInterfaceImplementaionIsAutomaticallyHidden() {
    056            MutablePicoContainer pc = createImplementationHidingPicoContainer();
    057            pc.addComponent(HashMap.class, HashMap.class);
    058            Map map = pc.getComponent(HashMap.class);
    059            assertNotNull(map);
    060            assertTrue(map instanceof HashMap);
    061        }
    062
    063        @Test public void testNonInterfaceImplementaionWithParametersIsAutomaticallyHidden() {
    064            MutablePicoContainer pc = createImplementationHidingPicoContainer();
    065            pc.addComponent(HashMap.class, HashMap.class);
    066            Map map = pc.getComponent(HashMap.class);
    067            assertNotNull(map);
    068            assertTrue(map instanceof HashMap);
    069        }
    070
    071
    072        @Test public void testImplementaionWithParametersIsAutomaticallyHidden() {
    073            MutablePicoContainer pc = createImplementationHidingPicoContainer();
    074            pc.addComponent(Map.class, HashMap.class);
    075            Map map = pc.getComponent(Map.class);
    076            assertNotNull(map);
    077            assertFalse(map instanceof HashMap);
    078        }
    079
    080        @Test public void testSerializedContainerCanRetrieveImplementation() throws PicoException,
    081                                                                              IOException, ClassNotFoundException {
    082            try {
    083                super.testSerializedContainerCanRetrieveImplementation();
    084                fail("The ImplementationHidingPicoContainer should not be able to retrieve the component impl");
    085            } catch (ClassCastException cce) {
    086                // expected.
    087            }
    088        }
    089
    090        @Test public void testExceptionThrowingFromHiddenComponent() {
    091            MutablePicoContainer pc = createImplementationHidingPicoContainer();
    092            pc.addComponent(ActionListener.class, Burp.class);
    093            try {
    094                ActionListener ac = pc.getComponent(ActionListener.class);
    095                ac.actionPerformed(null);
    096                fail("Oh no.");
    097            } catch (RuntimeException e) {
    098                assertEquals("woohoo", e.getMessage());
    099            }
    100        }
    101
    102        public static class Burp implements ActionListener {
    103
    104            public void actionPerformed(ActionEvent e) {
    105                throw new RuntimeException("woohoo");
    106            }
    107        }
    108
    109    }