1 /****************************************************************************** 2 * Copyright (C) PicoContainer Organization. All rights reserved. * 3 * ------------------------------------------------------------------------- * 4 * The software in this package is published under the terms of the BSD * 5 * style license a copy of which has been included with this distribution in * 6 * the LICENSE.txt file. * 7 * * 8 * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant * 9 *****************************************************************************/ 10 11 package org.picocontainer.extras; 12 13 import junit.framework.TestCase; 14 import org.picocontainer.internals.ComponentRegistry; 15 import org.picocontainer.PicoContainer; 16 import org.picocontainer.PicoInitializationException; 17 import org.picocontainer.PicoInstantiationException; 18 import org.picocontainer.PicoRegistrationException; 19 import org.picocontainer.RegistrationPicoContainer; 20 import org.picocontainer.extras.CompositePicoContainer; 21 import org.picocontainer.defaults.DefaultComponentRegistry; 22 import org.picocontainer.defaults.DefaultPicoContainer; 23 import org.picocontainer.testmodel.Touchable; 24 import org.picocontainer.testmodel.SimpleTouchable; 25 26 import java.util.HashSet; 27 import java.util.Set; 28 import java.util.Collection; 29 30 public class CompositePicoContainerTestCase extends TestCase { 31 private RegistrationPicoContainer pico; 32 private CompositePicoContainer.WithContainerArray composite; 33 34 public void setUp() throws PicoRegistrationException, PicoInitializationException { 35 pico = new DefaultPicoContainer.Default(); 36 pico.registerComponentByClass(SimpleTouchable.class); 37 pico.instantiateComponents(); 38 composite = new CompositePicoContainer.WithContainerArray(new PicoContainer[]{pico}); 39 } 40 41 public void testGetComponents() { 42 assertEquals("Collections of Component Keys should be the same", pico.getComponentKeys(), composite.getComponentKeys()); 43 } 44 45 public void testGetComponentKeys() { 46 assertEquals("Collections of Component Keys should be the same", pico.getComponents(), composite.getComponents()); 47 } 48 49 public void testGetComponent() { 50 assertSame("Touchable should be the same", pico.getComponent(SimpleTouchable.class), composite.getComponent(SimpleTouchable.class)); 51 } 52 53 public void testHasComponent() { 54 assertEquals("Containers should contain the same", pico.hasComponent(SimpleTouchable.class), composite.hasComponent(SimpleTouchable.class)); 55 } 56 57 public void testNullContainer() { 58 try { 59 new CompositePicoContainer.WithContainerArray(null); 60 fail("Should have failed with an NPE"); 61 } catch (NullPointerException e) { 62 // fine 63 } 64 } 65 66 public void todo_testEmptyArrayOfContainers() { 67 try { 68 new CompositePicoContainer.WithContainerArray(new PicoContainer[0]); 69 fail("Should have failed with an NPE"); 70 } catch (NullPointerException e) { 71 // fine 72 } 73 } 74 75 public void testNullInArrayOfContainers() { 76 try { 77 new CompositePicoContainer.WithContainerArray(new PicoContainer[1]); 78 fail("Should have failed with an NPE"); 79 } catch (NullPointerException e) { 80 // fine 81 } 82 } 83 84 public void testUnsupportedOperations() throws PicoInitializationException { 85 try { 86 composite.getComponentMulticaster(); 87 fail("should have barfed"); 88 } catch (UnsupportedOperationException e) { 89 // expected 90 } 91 try { 92 composite.getComponentMulticaster(true, true); 93 fail("should have barfed"); 94 } catch (UnsupportedOperationException e) { 95 // expected 96 } 97 try { 98 composite.instantiateComponents(); 99 fail("should have barfed"); 100 } catch (UnsupportedOperationException e) { 101 // expected 102 } 103 } 104 105 106 public void testBasic() { 107 108 final String acomp = "hello"; 109 final Integer bcomp = new Integer(123); 110 111 PicoContainer a = new PicoContainer() { 112 public boolean hasComponent(Object compKey) { 113 return compKey == String.class; 114 } 115 116 public Object getComponent(Object compKey) { 117 return compKey == String.class ? acomp : null; 118 } 119 120 public Collection getComponents() { 121 Set result = new HashSet(); 122 result.add(acomp); 123 return result; 124 } 125 126 public Collection getComponentKeys() { 127 Set result = new HashSet(); 128 result.add(String.class); 129 return result; 130 } 131 132 public void instantiateComponents() throws PicoInstantiationException { 133 } 134 135 public Object getComponentMulticaster() { 136 return null; 137 } 138 139 public Object getComponentMulticaster(boolean callInInstantiationOrder, boolean callUnmanagedComponents) { 140 return null; 141 } 142 }; 143 144 PicoContainer b = new PicoContainer() { 145 public boolean hasComponent(Object compKey) { 146 return compKey == Integer.class; 147 } 148 149 public Object getComponent(Object compKey) { 150 return compKey == Integer.class ? bcomp : null; 151 } 152 153 public Collection getComponents() { 154 Set result = new HashSet(); 155 result.add(bcomp); 156 return result; 157 } 158 159 public Collection getComponentKeys() { 160 Set result = new HashSet(); 161 result.add(Integer.class); 162 return result; 163 164 } 165 166 public void instantiateComponents() throws PicoInstantiationException { 167 } 168 169 public Object getComponentMulticaster() { 170 return null; 171 } 172 173 public Object getComponentMulticaster(boolean callInInstantiationOrder, boolean callUnmanagedComponents) { 174 return null; 175 } 176 }; 177 178 CompositePicoContainer acc = new CompositePicoContainer.WithContainerArray(new PicoContainer[]{a, b}); 179 180 assertTrue(acc.hasComponent(String.class)); 181 assertTrue(acc.hasComponent(Integer.class)); 182 assertTrue(acc.getComponent(String.class) == acomp); 183 assertTrue(acc.getComponent(Integer.class) == bcomp); 184 assertTrue(acc.getComponents().size() == 2); 185 assertTrue(acc.getComponentKeys().size() == 2); 186 187 } 188 189 public void testEmpty() { 190 191 CompositePicoContainer acc = new CompositePicoContainer.WithContainerArray(new PicoContainer[0]); 192 assertTrue(acc.hasComponent(String.class) == false); 193 assertTrue(acc.getComponent(String.class) == null); 194 assertTrue(acc.getComponents().size() == 0); 195 } 196 197 public void testParentComponentRegistryDominance() { 198 ComponentRegistry cr = new DefaultComponentRegistry(); 199 cr.putComponent(Touchable.class, new SimpleTouchable()); 200 CompositePicoContainer acc = new CompositePicoContainer(cr, new PicoContainer[0]); 201 assertTrue(acc.hasComponent(Touchable.class)); 202 assertTrue(acc.getComponent(Touchable.class) instanceof SimpleTouchable); 203 } 204 205 public void testAdditiveFeatures() { 206 207 CompositePicoContainer addContainer = new CompositePicoContainer.Default(); 208 addContainer.addContainer(pico); 209 assertTrue("Should have a Touchable", addContainer.hasComponent(SimpleTouchable.class)); 210 addContainer.removeContainer(pico); 211 assertFalse("Should not have a Touchable", addContainer.hasComponent(SimpleTouchable.class)); 212 213 } 214 215 }

This page was automatically generated by Maven