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.monitors;
011    
012    import org.picocontainer.ComponentAdapter;
013    import org.picocontainer.MutablePicoContainer;
014    import org.picocontainer.PicoLifecycleException;
015    
016    import java.lang.reflect.Constructor;
017    import java.lang.reflect.Method;
018    
019    import org.jmock.MockObjectTestCase;
020    
021    public class NullComponentMonitorTestCase extends MockObjectTestCase {
022    
023        public void testItAll() throws NoSuchMethodException {
024    
025            NullComponentMonitor ncm = new NullComponentMonitor();
026            ncm.instantiated(makePico(), makeCA(), makeConstructor(), "foo", new Object[0], 10);
027            assertEquals(makeConstructor(), ncm.instantiating(makePico(), makeCA(), makeConstructor()));
028            ncm.instantiationFailed(makePico(), makeCA(), makeConstructor(), new Exception());
029            ncm.invocationFailed(makeConstructor(), "foo", new Exception());
030            ncm.invoked(makePico(), makeCA(), makeMethod(), "foo", 10);
031            ncm.invoking(makePico(), makeCA(), makeMethod(), "foo");
032            try {
033                ncm.lifecycleInvocationFailed(makePico(), makeCA(), makeMethod(), "foo", new RuntimeException());
034            } catch (PicoLifecycleException e) {
035                assertEquals(makeMethod(), e.getMethod());
036                assertEquals("foo", e.getInstance());
037                assertEquals("PicoLifecycleException: method 'public java.lang.String java.lang.String.toString()', instance 'foo, java.lang.RuntimeException", e.getMessage());
038            }
039            assertNull(ncm.noComponentFound(makePico(), String.class));
040    
041        }
042    
043        private MutablePicoContainer makePico() {
044            return (MutablePicoContainer)mock(MutablePicoContainer.class).proxy();
045        }
046    
047        private ComponentAdapter makeCA() {
048            return (ComponentAdapter)mock(ComponentAdapter.class).proxy();
049        }
050    
051        private Constructor makeConstructor() {
052            return String.class.getConstructors()[0];
053        }
054    
055        private Method makeMethod() throws NoSuchMethodException {
056            return String.class.getMethod("toString");
057        }
058    
059    
060    }