    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 Paul Hammant                                             *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.containers;
    012
    013    import static org.junit.Assert.assertEquals;
    014    import static org.junit.Assert.assertFalse;
    015    import static org.junit.Assert.fail;
    016    import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme;
    017
    018    import org.jmock.Expectations;
    019    import org.jmock.Mockery;
    020    import org.jmock.integration.junit4.JMock;
    021    import org.junit.Test;
    022    import org.junit.runner.RunWith;
    023    import org.picocontainer.ComponentAdapter;
    024    import org.picocontainer.DefaultPicoContainer;
    025    import org.picocontainer.NameBinding;
    026    import org.picocontainer.PicoContainer;
    027    import org.picocontainer.PicoVisitor;
    028    import org.picocontainer.injectors.AdaptingInjection;
    029
    030
    031    /**
    032     * @author Paul Hammant
    033     * @author J&ouml;rg Schaible
    034     */
    035    @RunWith(JMock.class)
    036    public class ImmutablePicoContainerTestCase {
    037
    038            private Mockery mockery = mockeryWithCountingNamingScheme();
    039
    040        @Test public void testImmutingOfNullBarfs() {
    041            try {
    042                new ImmutablePicoContainer(null);
    043                fail("Should have barfed");
    044            } catch (NullPointerException e) {
    045                // expected
    046            }
    047        }
    048
    049        @Test public void testVisitingOfImmutableContainerWorks() {
    050            final AdaptingInjection ai = new AdaptingInjection();
    051            final DefaultPicoContainer pico = new DefaultPicoContainer(ai);
    052            Object foo = new Object();
    053            final ComponentAdapter componentAdapter = pico.addComponent(foo).getComponentAdapter(foo.getClass(), (NameBinding) null);
    054
    055            final PicoVisitor fooVisitor = mockery.mock(PicoVisitor.class);
    056            mockery.checking(new Expectations() {{
    057                one(fooVisitor).visitContainer(with(same(pico))); will(returnValue(true));
    058                    one(fooVisitor).visitComponentFactory(with(same(ai)));
    059                    one(fooVisitor).visitComponentAdapter(with(same(componentAdapter)));
    060            }});
    061            PicoContainer ipc = new ImmutablePicoContainer(pico);
    062            ipc.accept(fooVisitor);
    063        }
    064
    065        @Test public void testProxyEquals() throws Exception {
    066            DefaultPicoContainer pico = new DefaultPicoContainer();
    067            PicoContainer ipc = new ImmutablePicoContainer(pico);
    068            assertEquals(ipc, ipc);
    069            assertEquals(ipc, new ImmutablePicoContainer(pico));
    070        }
    071
    072        @Test public void testHashCodeIsSame() throws Exception {
    073            DefaultPicoContainer pico = new DefaultPicoContainer();
    074            assertEquals(pico.hashCode(), new ImmutablePicoContainer(pico).hashCode());
    075        }
    076
    077        @Test public void testDoesNotEqualsToNull() {
    078            DefaultPicoContainer pico = new DefaultPicoContainer();
    079            PicoContainer ipc = new ImmutablePicoContainer(pico);
    080            assertFalse(ipc.equals(null));
    081        }
    082    }