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.DefaultServiceSelector; 56 57 62 public final class DefaultServiceSelectorTestCase 63 extends TestCase 64 { 65 class FeatureComponent 66 { 67 Object m_feature; 68 public FeatureComponent( final Object feature ) 69 { 70 m_feature = feature; 71 } 72 73 public Object getFeature() 74 { 75 return m_feature; 76 } 77 } 78 79 class Hint 80 { 81 String m_name; 82 83 public Hint( final String name ) 84 { 85 m_name = name; 86 } 87 88 public String getName() 89 { 90 return m_name; 91 } 92 } 93 94 private DefaultServiceSelector m_componentSelector; 95 protected boolean m_exceptionThrown; 96 97 public DefaultServiceSelectorTestCase() 98 { 99 this("DefaultComponentSelector Test Case"); 100 } 101 102 public DefaultServiceSelectorTestCase( final String name ) 103 { 104 super( name ); 105 } 106 107 protected void setUp() 108 throws Exception 109 { 110 m_componentSelector = new DefaultServiceSelector(); 111 m_exceptionThrown =false; 112 } 113 114 protected void tearDown() 115 throws Exception 116 { 117 m_componentSelector = null; 118 } 119 120 126 public void testlookup() 127 throws Exception 128 { 129 Hint hintA = new Hint("a"); 130 Hint hintB = new Hint("b"); 131 m_componentSelector.put(hintA,new FeatureComponent(hintA)); 132 m_componentSelector.put(hintB,new FeatureComponent(hintB)); 133 FeatureComponent fComponent = (FeatureComponent)m_componentSelector.select(hintA); 134 assertEquals( hintA, fComponent.getFeature() ); 135 Object o = null; 136 try 137 { 138 o = (FeatureComponent)m_componentSelector.select(new Hint("no component")); 139 } 140 catch (ServiceException ce) 141 { 142 m_exceptionThrown = true; 143 } 144 if (o == null) 145 assertTrue("ComponentException was not thrown when component was not found by lookup." ,m_exceptionThrown ); 146 else 147 assertTrue("component was found by lookup ,when there was no component.",false); 148 } 149 150 public void testhasComponent() 151 throws Exception 152 { 153 Hint hintA = new Hint("a"); 154 Hint hintB = new Hint("b"); 155 m_componentSelector.put(hintA,new FeatureComponent(hintA)); 156 assertTrue(m_componentSelector.isSelectable(hintA)); 157 assertTrue(!m_componentSelector.isSelectable(hintB)); 158 } 159 160 public void testmakeReadOnly() 162 throws Exception 163 { 164 Hint hintA = new Hint("a"); 165 Hint hintB = new Hint("b"); 166 m_componentSelector.put(hintA,new FeatureComponent(hintA)); 168 FeatureComponent fComponent = (FeatureComponent)m_componentSelector.select(hintA); 169 assertEquals( hintA, fComponent.getFeature() ); 170 m_componentSelector.makeReadOnly(); 171 try 173 { 174 m_componentSelector.put(hintB,new FeatureComponent(hintB)); 175 } 176 catch (IllegalStateException se) 177 { 178 m_exceptionThrown = true; 179 } 180 assertTrue("IllegalStateException was not thrown in put after makeReadOnly." , m_exceptionThrown ); 181 } 182 } 183 184 185 186 187 188 189 | Popular Tags |