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 *****************************************************************************/
008package org.picocontainer.adapters;
009
010import static org.junit.Assert.assertEquals;
011import static org.junit.Assert.assertTrue;
012import static org.junit.Assert.fail;
013
014import java.lang.reflect.Constructor;
015import java.lang.reflect.Type;
016
017import org.junit.Test;
018import org.picocontainer.ComponentAdapter;
019import org.picocontainer.ComponentMonitor;
020import org.picocontainer.Parameter;
021import org.picocontainer.PicoCompositionException;
022import org.picocontainer.PicoContainer;
023import org.picocontainer.PicoVerificationException;
024import org.picocontainer.PicoVisitor;
025import org.picocontainer.injectors.AbstractInjector;
026import org.picocontainer.lifecycle.NullLifecycleStrategy;
027import org.picocontainer.monitors.NullComponentMonitor;
028import org.picocontainer.parameters.ConstantParameter;
029
030/**
031 * Test AbstractAdapter behaviour
032 * @author Jörg Schaible
033 */
034public 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(), false);
095        }
096        @Override
097        public void verify(PicoContainer container) throws PicoCompositionException {
098        }
099
100        public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException {
101            return null;
102        }
103
104        public T getComponentInstance(PicoContainer container) throws PicoCompositionException {
105            return null;
106        }
107
108        public String getDescriptor() {
109            return null;
110        }
111    }
112    
113    @Test public void testComponentImplementationMayNotBeNull() {
114        try {
115            new TestAdapter<Object>("Key", null);
116            fail("NullPointerException expected");
117        } catch (NullPointerException e) {
118            assertEquals("componentImplementation", e.getMessage());
119        }
120    }
121
122    @Test public void testComponentKeyCanBeNullButNotRequested() {
123        ComponentAdapter<String> componentAdapter = new TestAdapter<String>(null, String.class);
124        try {
125            componentAdapter.getComponentKey();
126            fail("NullPointerException expected");
127        } catch (NullPointerException e) {
128            assertEquals("componentKey", e.getMessage());
129        }
130    }
131
132    @Test public void testComponentMonitorMayNotBeNull() {
133        try {
134            new TestAdapter<String>("Key", String.class, null);
135            fail("NullPointerException expected");
136        } catch (NullPointerException e) {
137            assertEquals("ComponentMonitor==null", e.getMessage());
138        }
139        try {
140            new TestMonitoringComponentAdapter<Object>(null);
141            fail("NullPointerException expected");
142        } catch (NullPointerException e) {
143            assertEquals("ComponentMonitor==null", e.getMessage());
144        }
145    }
146
147    @Test public void testParameterMayNotBeNull() throws Exception {
148        try {
149            new TestInstantiatingAdapter<String>("Key", String.class, new Parameter[]{new ConstantParameter("Value"), null});
150            fail("Thrown " + NullPointerException.class.getName() + " expected");
151        } catch (final NullPointerException e) {
152            assertTrue(e.getMessage().endsWith("1 is null"));
153        }
154    }
155    
156    @Test public void testStringRepresentation() {
157        ComponentAdapter<Integer> componentAdapter = new TestAdapter<Integer>("Key", Integer.class);
158        assertEquals(TestAdapter.class.getName() + ":Key", componentAdapter.toString());
159    }
160}