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