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 static org.junit.Assert.assertEquals; 013 import static org.junit.Assert.assertNull; 014 import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme; 015 016 import java.lang.reflect.Constructor; 017 import java.lang.reflect.Method; 018 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.MutablePicoContainer; 025 import org.picocontainer.PicoLifecycleException; 026 027 @RunWith(JMock.class) 028 public class NullComponentMonitorTestCase { 029 030 private Mockery mockery = mockeryWithCountingNamingScheme(); 031 032 @Test public void testItAll() throws NoSuchMethodException { 033 034 NullComponentMonitor ncm = new NullComponentMonitor(); 035 ncm.instantiated(makePico(), makeCA(), makeConstructor(), "foo", new Object[0], 10); 036 assertEquals(makeConstructor(), ncm.instantiating(makePico(), makeCA(), makeConstructor())); 037 ncm.instantiationFailed(makePico(), makeCA(), makeConstructor(), new Exception()); 038 ncm.invocationFailed(makeConstructor(), "foo", new Exception()); 039 ncm.invoked(makePico(), makeCA(), makeMethod(), "foo", 10, new Object[0], null); 040 ncm.invoking(makePico(), makeCA(), makeMethod(), "foo", new Object[0]); 041 try { 042 ncm.lifecycleInvocationFailed(makePico(), makeCA(), makeMethod(), "foo", new RuntimeException()); 043 } catch (PicoLifecycleException e) { 044 assertEquals(makeMethod(), e.getMethod()); 045 assertEquals("foo", e.getInstance()); 046 assertEquals("PicoLifecycleException: method 'public java.lang.String java.lang.String.toString()', instance 'foo, java.lang.RuntimeException", e.getMessage()); 047 } 048 assertNull(ncm.noComponentFound(makePico(), String.class)); 049 050 } 051 052 private MutablePicoContainer makePico() { 053 return mockery.mock(MutablePicoContainer.class); 054 } 055 056 private ComponentAdapter makeCA() { 057 return mockery.mock(ComponentAdapter.class); 058 } 059 060 private Constructor makeConstructor() { 061 return String.class.getConstructors()[0]; 062 } 063 064 private Method makeMethod() throws NoSuchMethodException { 065 return String.class.getMethod("toString"); 066 } 067 068 069 }