1 51 package org.apache.avalon.framework.component.test; 52 53 import junit.framework.TestCase; 54 import org.apache.avalon.framework.component.Component; 55 import org.apache.avalon.framework.component.ComponentException; 56 import org.apache.avalon.framework.component.DefaultComponentManager; 57 58 63 public final class DefaultComponentManagerTestCase 64 extends TestCase 65 { 66 67 class DefaultRoleA 68 implements Component,RoleA 69 { 70 public DefaultRoleA() 71 { 72 } 73 } 74 75 class DefaultRoleB 76 implements Component,RoleB 77 { 78 public DefaultRoleB() 79 { 80 } 81 } 82 83 84 private DefaultComponentManager m_componentManager; 85 86 protected boolean m_exceptionThrown; 87 88 89 public DefaultComponentManagerTestCase() 90 { 91 this("DefaultComponentManager Test Case"); 92 } 93 94 public DefaultComponentManagerTestCase( final String name ) 95 { 96 super( name ); 97 } 98 99 protected void setUp() 100 throws Exception 101 { 102 m_componentManager = new DefaultComponentManager(); 103 m_exceptionThrown = false; 104 } 105 106 protected void tearDown() 107 throws Exception 108 { 109 m_componentManager = null; 110 } 111 112 119 120 121 public void testlookup1() 122 throws Exception 123 { 124 DefaultRoleB roleBinBase = new DefaultRoleB(); 125 DefaultRoleB roleBinParent = new DefaultRoleB(); 126 DefaultRoleA roleAinParent = new DefaultRoleA(); 127 128 m_componentManager.put(RoleA.ROLE,roleAinParent); 129 m_componentManager.put(RoleB.ROLE,roleBinParent); 130 DefaultComponentManager baseComponentManager = new DefaultComponentManager(m_componentManager); 131 baseComponentManager.put(RoleB.ROLE,roleBinBase); 132 Object lookupAinBase = baseComponentManager.lookup(RoleA.ROLE); 133 Object lookupBinBase = baseComponentManager.lookup(RoleB.ROLE); 134 Object lookupBinParent = m_componentManager.lookup(RoleB.ROLE); 135 assertTrue( lookupAinBase instanceof RoleA); 136 assertEquals( lookupBinBase, roleBinBase ); 137 assertEquals( lookupBinParent, roleBinParent ); 138 assertEquals( lookupAinBase,roleAinParent); 139 } 140 141 public void testlookup2() 142 throws Exception 143 { 144 m_componentManager.put(RoleA.ROLE,new DefaultRoleA()); 145 Object o = null; 146 try 147 { 148 o = m_componentManager.lookup(RoleB.ROLE); 149 } 150 catch (ComponentException ce) 151 { 152 m_exceptionThrown = true; 153 } 154 if (o == null) 155 assertTrue("ComponentException was not thrown when component was not found by lookup." ,m_exceptionThrown ); 156 else 157 assertTrue("component was found by lookup ,when there was no component.",false); 158 159 } 160 161 public void testhasComponent() 162 throws Exception 163 { 164 m_componentManager.put(RoleA.ROLE,new DefaultRoleA()); 165 assertTrue(m_componentManager.hasComponent(RoleA.ROLE)); 166 assertTrue(!m_componentManager.hasComponent(RoleB.ROLE)); 167 } 168 169 public void testmakeReadOnly() 170 throws Exception 171 { 172 m_componentManager.put(RoleA.ROLE,new DefaultRoleA()); 174 Object a = m_componentManager.lookup(RoleA.ROLE); 175 assertTrue( a instanceof RoleA); 176 m_componentManager.makeReadOnly(); 177 try 179 { 180 m_componentManager.put(RoleB.ROLE,new DefaultRoleB()); 181 } 182 catch (IllegalStateException se) 183 { 184 m_exceptionThrown = true; 185 } 186 assertTrue("IllegalStateException was not thrown in put after makeReadOnly." , m_exceptionThrown ); 187 } 188 } 189 190 191 192 193 194 195 196 | Popular Tags |