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 void decorateComponentInstance(PicoContainer container, Type into, T instance) {
113 }
114
115 public String getDescriptor() {
116 return null;
117 }
118 }
119
120 @Test public void testComponentImplementationMayNotBeNull() {
121 try {
122 new TestAdapter<Object>("Key", null);
123 fail("NullPointerException expected");
124 } catch (NullPointerException e) {
125 assertEquals("componentImplementation", e.getMessage());
126 }
127 }
128
129 @Test public void testComponentKeyCanBeNullButNotRequested() {
130 ComponentAdapter<String> componentAdapter = new TestAdapter<String>(null, String.class);
131 try {
132 componentAdapter.getComponentKey();
133 fail("NullPointerException expected");
134 } catch (NullPointerException e) {
135 assertEquals("componentKey", e.getMessage());
136 }
137 }
138
139 @Test public void testComponentMonitorMayNotBeNull() {
140 try {
141 new TestAdapter<String>("Key", String.class, null);
142 fail("NullPointerException expected");
143 } catch (NullPointerException e) {
144 assertEquals("ComponentMonitor==null", e.getMessage());
145 }
146 try {
147 new TestMonitoringComponentAdapter<Object>(null);
148 fail("NullPointerException expected");
149 } catch (NullPointerException e) {
150 assertEquals("ComponentMonitor==null", e.getMessage());
151 }
152 }
153
154 @Test public void testParameterMayNotBeNull() throws Exception {
155 try {
156 new TestInstantiatingAdapter<String>("Key", String.class, new Parameter[]{new ConstantParameter("Value"), null});
157 fail("Thrown " + NullPointerException.class.getName() + " expected");
158 } catch (final NullPointerException e) {
159 assertTrue(e.getMessage().endsWith("1 is null"));
160 }
161 }
162
163 @Test public void testStringRepresentation() {
164 ComponentAdapter<Integer> componentAdapter = new TestAdapter<Integer>("Key", Integer.class);
165 assertEquals(TestAdapter.class.getName() + ":Key", componentAdapter.toString());
166 }
167 }