1 8 package org.picocontainer.defaults; 9 10 import junit.framework.TestCase; 11 import org.picocontainer.ComponentAdapter; 12 import org.picocontainer.ComponentMonitor; 13 import org.picocontainer.Parameter; 14 import org.picocontainer.PicoContainer; 15 import org.picocontainer.PicoInitializationException; 16 import org.picocontainer.PicoIntrospectionException; 17 import org.picocontainer.PicoVerificationException; 18 import org.picocontainer.PicoVisitor; 19 20 import java.lang.reflect.Constructor ; 21 22 26 public class ComponentAdapterTestCase 27 extends TestCase { 28 29 private static class TestComponentAdapter extends AbstractComponentAdapter { 30 TestComponentAdapter(Object componentKey, Class componentImplementation, ComponentMonitor componentMonitor) throws AssignabilityRegistrationException { 31 super(componentKey, componentImplementation, componentMonitor); 32 } 33 TestComponentAdapter(Object componentKey, Class componentImplementation) throws AssignabilityRegistrationException { 34 super(componentKey, componentImplementation); 35 } 36 public Object getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException { 37 return null; 38 } 39 public void verify(PicoContainer container) throws PicoVerificationException { 40 } 41 42 } 43 44 private static class TestMonitoringComponentAdapter extends MonitoringComponentAdapter { 45 TestMonitoringComponentAdapter(ComponentMonitor componentMonitor) throws AssignabilityRegistrationException { 46 super(componentMonitor); 47 } 48 public Object getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException { 49 return null; 50 } 51 public void verify(PicoContainer container) throws PicoVerificationException { 52 } 53 public Object getComponentKey() { 54 return null; 55 } 56 public Class getComponentImplementation() { 57 return null; 58 } 59 public void accept(PicoVisitor visitor) { 60 } 61 } 62 63 private static class TestInstantiatingComponentAdapter extends InstantiatingComponentAdapter { 64 TestInstantiatingComponentAdapter(Object componentKey, Class componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses) { 65 super(componentKey, componentImplementation, parameters, allowNonPublicClasses); 66 } 67 protected Constructor getGreediestSatisfiableConstructor(PicoContainer container) throws PicoIntrospectionException, UnsatisfiableDependenciesException, AmbiguousComponentResolutionException, AssignabilityRegistrationException, NotConcreteRegistrationException { 68 return null; 69 } 70 public Object getComponentInstance(PicoContainer container) throws PicoInitializationException, PicoIntrospectionException { 71 return null; 72 } 73 } 74 75 public void testComponentImplementationMayNotBeNull() { 76 try { 77 new TestComponentAdapter("Key", null); 78 fail("NullPointerException expected"); 79 } catch (NullPointerException e) { 80 assertEquals("componentImplementation", e.getMessage()); 81 } 82 } 83 84 public void testComponentKeyCanBeNullButNotRequested() { 85 ComponentAdapter componentAdapter = new TestComponentAdapter(null, String .class); 86 try { 87 componentAdapter.getComponentKey(); 88 fail("NullPointerException expected"); 89 } catch (NullPointerException e) { 90 assertEquals("componentKey", e.getMessage()); 91 } 92 } 93 94 public void testComponentMonitorMayNotBeNull() { 95 try { 96 new TestComponentAdapter("Key", String .class, null); 97 fail("NullPointerException expected"); 98 } catch (NullPointerException e) { 99 assertEquals("monitor", e.getMessage()); 100 } 101 try { 102 new TestMonitoringComponentAdapter(null); 103 fail("NullPointerException expected"); 104 } catch (NullPointerException e) { 105 assertEquals("monitor", e.getMessage()); 106 } 107 } 108 109 public void testParameterMayNotBeNull() throws Exception { 110 try { 111 new TestInstantiatingComponentAdapter("Key", String .class, new Parameter[]{new ConstantParameter("Value"), null}, false); 112 fail("Thrown " + NullPointerException .class.getName() + " expected"); 113 } catch (final NullPointerException e) { 114 assertTrue(e.getMessage().endsWith("1 is null")); 115 } 116 } 117 118 public void testStringRepresentation() { 119 ComponentAdapter componentAdapter = new TestComponentAdapter("Key", Integer .class); 120 assertEquals(TestComponentAdapter.class.getName() + "[Key]", componentAdapter.toString()); 121 } 122 } 123 | Popular Tags |