1 16 package org.apache.cocoon.components.thread; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.easymock.MockControl; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import junit.framework.TestCase; 27 28 29 36 public class AbstractTestCase 37 extends TestCase 38 { 39 41 45 private List m_controls; 46 47 49 54 public AbstractTestCase( String name ) 55 { 56 super( name ); 57 } 58 59 62 public AbstractTestCase( ) 63 { 64 super( ); 65 } 66 67 69 75 protected void setUp( ) 76 throws Exception 77 { 78 super.setUp( ); 79 m_controls = new ArrayList ( ); 80 } 81 82 90 protected Configuration createBooleanConfigMock( final boolean value, 91 final boolean defaultValue ) 92 { 93 final MockControl valueConfigControl = 94 createStrictControl( Configuration.class ); 95 final Configuration valueConfig = 96 (Configuration)valueConfigControl.getMock( ); 97 valueConfig.getValueAsBoolean( defaultValue ); 98 valueConfigControl.setReturnValue( value ); 99 valueConfigControl.replay( ); 100 101 return valueConfig; 102 } 103 104 113 protected Configuration createBooleanConfigMock( final boolean value ) 114 throws ConfigurationException 115 { 116 final MockControl valueConfigControl = 117 createStrictControl( Configuration.class ); 118 final Configuration valueConfig = 119 (Configuration)valueConfigControl.getMock( ); 120 valueConfig.getValueAsBoolean( ); 121 valueConfigControl.setReturnValue( value ); 122 valueConfigControl.replay( ); 123 124 return valueConfig; 125 } 126 127 135 protected Configuration createChildConfigMock( final String name, 136 final Configuration value ) 137 { 138 final MockControl childConfigControl = 139 createStrictControl( Configuration.class ); 140 final Configuration childConfig = 141 (Configuration)childConfigControl.getMock( ); 142 childConfig.getChild( name ); 143 childConfigControl.setReturnValue( value ); 144 childConfigControl.replay( ); 145 146 return childConfig; 147 } 148 149 157 protected Configuration createChildrenConfigMock( final String name, 158 final Configuration [] value ) 159 { 160 final MockControl childrenConfigControl = 161 createStrictControl( Configuration.class ); 162 final Configuration childrenConfig = 163 (Configuration)childrenConfigControl.getMock( ); 164 childrenConfig.getChildren( name ); 165 childrenConfigControl.setReturnValue( value ); 166 childrenConfigControl.replay( ); 167 168 return childrenConfig; 169 } 170 171 179 protected Configuration createIntegerConfigMock( final int value, 180 final int defaultValue ) 181 { 182 final MockControl valueConfigControl = 183 createStrictControl( Configuration.class ); 184 final Configuration valueConfig = 185 (Configuration)valueConfigControl.getMock( ); 186 valueConfig.getValueAsInteger( defaultValue ); 187 valueConfigControl.setReturnValue( value ); 188 valueConfigControl.replay( ); 189 190 return valueConfig; 191 } 192 193 202 protected Configuration createIntegerConfigMock( final int value ) 203 throws ConfigurationException 204 { 205 final MockControl valueConfigControl = 206 createStrictControl( Configuration.class ); 207 final Configuration valueConfig = 208 (Configuration)valueConfigControl.getMock( ); 209 valueConfig.getValueAsInteger( ); 210 valueConfigControl.setReturnValue( value ); 211 valueConfigControl.replay( ); 212 213 return valueConfig; 214 } 215 216 224 protected Configuration createLongConfigMock( final long value, 225 final long defaultValue ) 226 { 227 final MockControl valueConfigControl = 228 createStrictControl( Configuration.class ); 229 final Configuration valueConfig = 230 (Configuration)valueConfigControl.getMock( ); 231 valueConfig.getValueAsLong( defaultValue ); 232 valueConfigControl.setReturnValue( value ); 233 valueConfigControl.replay( ); 234 235 return valueConfig; 236 } 237 238 247 protected Configuration createLongConfigMock( final long value ) 248 throws ConfigurationException 249 { 250 final MockControl valueConfigControl = 251 createStrictControl( Configuration.class ); 252 final Configuration valueConfig = 253 (Configuration)valueConfigControl.getMock( ); 254 valueConfig.getValueAsLong( ); 255 valueConfigControl.setReturnValue( value ); 256 valueConfigControl.replay( ); 257 258 return valueConfig; 259 } 260 261 268 protected MockControl createStrictControl( final Class clazz ) 269 { 270 final MockControl control = MockControl.createStrictControl( clazz ); 271 m_controls.add( control ); 272 273 return control; 274 } 275 276 284 protected Configuration createValueConfigMock( final String value, 285 final String defaultValue ) 286 { 287 final MockControl valueConfigControl = 288 createStrictControl( Configuration.class ); 289 final Configuration valueConfig = 290 (Configuration)valueConfigControl.getMock( ); 291 valueConfig.getValue( defaultValue ); 292 valueConfigControl.setReturnValue( value ); 293 valueConfigControl.replay( ); 294 295 return valueConfig; 296 } 297 298 307 protected Configuration createValueConfigMock( final String value ) 308 throws ConfigurationException 309 { 310 final MockControl valueConfigControl = 311 createStrictControl( Configuration.class ); 312 final Configuration valueConfig = 313 (Configuration)valueConfigControl.getMock( ); 314 valueConfig.getValue( ); 315 valueConfigControl.setReturnValue( value ); 316 valueConfigControl.replay( ); 317 318 return valueConfig; 319 } 320 321 324 protected void tearDown( ) 325 throws Exception 326 { 327 super.tearDown(); 328 m_controls = null; 329 } 330 331 334 protected void verify( ) 335 { 336 for( Iterator i = m_controls.iterator( ); i.hasNext( ); ) 337 { 338 final MockControl control = (MockControl)i.next( ); 339 control.verify( ); 340 } 341 } 342 } 343 | Popular Tags |