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    package org.picocontainer.adapters;
009    
010    import static org.junit.Assert.assertEquals;
011    import static org.junit.Assert.assertTrue;
012    import static org.junit.Assert.fail;
013    
014    import java.lang.reflect.Constructor;
015    import java.lang.reflect.Type;
016    
017    import org.junit.Test;
018    import org.picocontainer.ComponentAdapter;
019    import org.picocontainer.ComponentMonitor;
020    import org.picocontainer.Parameter;
021    import org.picocontainer.PicoCompositionException;
022    import org.picocontainer.PicoContainer;
023    import org.picocontainer.PicoVerificationException;
024    import org.picocontainer.PicoVisitor;
025    import org.picocontainer.injectors.AbstractInjector;
026    import org.picocontainer.lifecycle.NullLifecycleStrategy;
027    import org.picocontainer.monitors.NullComponentMonitor;
028    import org.picocontainer.parameters.ConstantParameter;
029    
030    /**
031     * Test AbstractAdapter behaviour
032     * @author Jörg Schaible
033     */
034    public class ComponentAdapterTestCase {
035    
036        @SuppressWarnings("serial")
037            private static class TestAdapter<T> extends AbstractAdapter<T> {
038            
039            TestAdapter(Object componentKey, Class<T> componentImplementation, ComponentMonitor componentMonitor) {
040                super(componentKey, componentImplementation, componentMonitor);
041            }
042            TestAdapter(Object componentKey, Class<T> componentImplementation) {
043                super(componentKey, componentImplementation);
044            }
045    
046            public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
047                return null;
048            }
049    
050    
051            public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
052                return null;
053            }
054            public void verify(PicoContainer container) throws PicoVerificationException {
055            }
056    
057            public String getDescriptor() {
058                return TestAdapter.class.getName() + ":" ;
059            }
060        }
061    
062        @SuppressWarnings("serial")
063            private static class TestMonitoringComponentAdapter<T> extends AbstractAdapter<T> {
064            TestMonitoringComponentAdapter(ComponentMonitor componentMonitor) {
065                super(null, null, componentMonitor);
066            }
067    
068            public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
069                return null;
070            }
071    
072            public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
073                return null;
074            }
075            public void verify(PicoContainer container) throws PicoVerificationException {
076            }
077            public Object getComponentKey() {
078                return null;
079            }
080            public Class<T> getComponentImplementation() {
081                return null;
082            }
083            public void accept(PicoVisitor visitor) {
084            }
085    
086            public String getDescriptor() {
087                return null;
088            }
089        }
090        
091        @SuppressWarnings("serial")
092            private static class TestInstantiatingAdapter<T> extends AbstractInjector<T> {
093            TestInstantiatingAdapter(Object componentKey, Class<T> componentImplementation, Parameter... parameters) {
094                super(componentKey, componentImplementation, parameters, new NullComponentMonitor(), new NullLifecycleStrategy(), false);
095            }
096            protected Constructor<T> getGreediestSatisfiableConstructor(PicoContainer container) throws PicoCompositionException {
097                return null;
098            }
099    
100            @Override
101            public void verify(PicoContainer container) throws PicoCompositionException {
102            }
103    
104            public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
105                return null;
106            }
107    
108            public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
109                return null;
110            }
111    
112            public String getDescriptor() {
113                return null;
114            }
115        }
116        
117        @Test public void testComponentImplementationMayNotBeNull() {
118            try {
119                new TestAdapter<Object>("Key", null);
120                fail("NullPointerException expected");
121            } catch (NullPointerException e) {
122                assertEquals("componentImplementation", e.getMessage());
123            }
124        }
125    
126        @Test public void testComponentKeyCanBeNullButNotRequested() {
127            ComponentAdapter<String> componentAdapter = new TestAdapter<String>(null, String.class);
128            try {
129                componentAdapter.getComponentKey();
130                fail("NullPointerException expected");
131            } catch (NullPointerException e) {
132                assertEquals("componentKey", e.getMessage());
133            }
134        }
135    
136        @Test public void testComponentMonitorMayNotBeNull() {
137            try {
138                new TestAdapter<String>("Key", String.class, null);
139                fail("NullPointerException expected");
140            } catch (NullPointerException e) {
141                assertEquals("ComponentMonitor==null", e.getMessage());
142            }
143            try {
144                new TestMonitoringComponentAdapter<Object>(null);
145                fail("NullPointerException expected");
146            } catch (NullPointerException e) {
147                assertEquals("ComponentMonitor==null", e.getMessage());
148            }
149        }
150    
151        @Test public void testParameterMayNotBeNull() throws Exception {
152            try {
153                new TestInstantiatingAdapter<String>("Key", String.class, new Parameter[]{new ConstantParameter("Value"), null});
154                fail("Thrown " + NullPointerException.class.getName() + " expected");
155            } catch (final NullPointerException e) {
156                assertTrue(e.getMessage().endsWith("1 is null"));
157            }
158        }
159        
160        @Test public void testStringRepresentation() {
161            ComponentAdapter<Integer> componentAdapter = new TestAdapter<Integer>("Key", Integer.class);
162            assertEquals(TestAdapter.class.getName() + ":Key", componentAdapter.toString());
163        }
164    }