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