Author: giacomo Date: Fri Nov 12 04:25:13 2004 New Revision: 57512 Added: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/AbstractTestCase.java cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadPoolTestCase.java Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultRunnableManagerTestCase.java cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadFactoryTestCase.java Log: more test cases
Added: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/AbstractTestCase.java ============================================================================== --- (empty file) +++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/AbstractTestCase.java Fri Nov 12 04:25:13 2004 @@ -0,0 +1,341 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.thread; + +import org.apache.avalon.framework.configuration.Configuration; +import org.apache.avalon.framework.configuration.ConfigurationException; +import org.easymock.MockControl; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import junit.framework.TestCase; + + +/** + * A [EMAIL PROTECTED] TestCase}with convenience methods to ease creation of Avalon mock + * classes. + * + * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati </a> + * @version $Id$ + */ +public class AbstractTestCase + extends TestCase +{ + //~ Instance fields -------------------------------------------------------- + + /** + * The [EMAIL PROTECTED] List}of [EMAIL PROTECTED] MockControl}s creted by the + * <code>create...Control</code> methods + */ + private List m_controls; + + //~ Constructors ----------------------------------------------------------- + + /** + * Constructor + * + * @param name + */ + public AbstractTestCase( String name ) + { + super( name ); + } + + /** + * Constructor + */ + public AbstractTestCase( ) + { + super( ); + } + + //~ Methods ---------------------------------------------------------------- + + /** + * Create an empty list for [EMAIL PROTECTED] MockControl}s created by + * <code>create...Control</code> methods + * + * @throws Exception + */ + protected void setUp( ) + throws Exception + { + super.setUp( ); + m_controls = new ArrayList( ); + } + + /** + * Create a mock [EMAIL PROTECTED] Configuration}instance that has a boolean value + * + * @param value The value to return + * @param defaultValue The value accepted as the default value + * + * @return A mock <code>Configuration</code> + */ + protected Configuration createBooleanConfigMock( final boolean value, + final boolean defaultValue ) + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValueAsBoolean( defaultValue ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a mock [EMAIL PROTECTED] Configuration}instance that has a boolean value + * + * @param value The value to return + * + * @return A mock <code>Configuration</code> + * + * @throws ConfigurationException + */ + protected Configuration createBooleanConfigMock( final boolean value ) + throws ConfigurationException + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValueAsBoolean( ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a child + * + * @param name The value accepted as the name for the child + * @param value The value to return + * + * @return A mock <code>Configuration</code> + */ + protected Configuration createChildConfigMock( final String name, + final Configuration value ) + { + final MockControl childConfigControl = + createStrictControl( Configuration.class ); + final Configuration childConfig = + (Configuration)childConfigControl.getMock( ); + childConfig.getChild( name ); + childConfigControl.setReturnValue( value ); + childConfigControl.replay( ); + + return childConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a boolean value + * + * @param name The value accepted as the name for the children + * @param value The value to return + * + * @return A mock <code>Configuration</code> + */ + protected Configuration createChildrenConfigMock( final String name, + final Configuration [] value ) + { + final MockControl childrenConfigControl = + createStrictControl( Configuration.class ); + final Configuration childrenConfig = + (Configuration)childrenConfigControl.getMock( ); + childrenConfig.getChildren( name ); + childrenConfigControl.setReturnValue( value ); + childrenConfigControl.replay( ); + + return childrenConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a int value + * + * @param value The value to return + * @param defaultValue The value accepted as the default value + * + * @return A mock <code>Configuration</code> + */ + protected Configuration createIntegerConfigMock( final int value, + final int defaultValue ) + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValueAsInteger( defaultValue ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a int value + * + * @param value The value to return + * + * @return A mock <code>Configuration</code> + * + * @throws ConfigurationException + */ + protected Configuration createIntegerConfigMock( final int value ) + throws ConfigurationException + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValueAsInteger( ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a long value + * + * @param value The value to return + * @param defaultValue The value accepted as the default value + * + * @return A mock <code>Configuration</code> + */ + protected Configuration createLongConfigMock( final long value, + final long defaultValue ) + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValueAsLong( defaultValue ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a long value + * + * @param value The value to return + * + * @return A mock <code>Configuration</code> + * + * @throws ConfigurationException + */ + protected Configuration createLongConfigMock( final long value ) + throws ConfigurationException + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValueAsLong( ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a strict mock control + * + * @param clazz The interface class the mock object should represent + * + * @return The mock instance + */ + protected MockControl createStrictControl( final Class clazz ) + { + final MockControl control = MockControl.createStrictControl( clazz ); + m_controls.add( control ); + + return control; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a string value + * + * @param value The value to return + * @param defaultValue The value accepted as the default value + * + * @return A mock <code>Configuration</code> + */ + protected Configuration createValueConfigMock( final String value, + final String defaultValue ) + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValue( defaultValue ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * Create a [EMAIL PROTECTED] Configuration}instance that has a string value + * + * @param value The value to return + * + * @return A mock <code>Configuration</code> + * + * @throws ConfigurationException + */ + protected Configuration createValueConfigMock( final String value ) + throws ConfigurationException + { + final MockControl valueConfigControl = + createStrictControl( Configuration.class ); + final Configuration valueConfig = + (Configuration)valueConfigControl.getMock( ); + valueConfig.getValue( ); + valueConfigControl.setReturnValue( value ); + valueConfigControl.replay( ); + + return valueConfig; + } + + /** + * @see TestCase#tearDown() + */ + protected void tearDown( ) + throws Exception + { + m_controls = null; + } + + /** + * Verify all <code>MockCOntrol</code>s + */ + protected void verify( ) + { + for( Iterator i = m_controls.iterator( ); i.hasNext( ); ) + { + final MockControl control = (MockControl)i.next( ); + control.verify( ); + } + } +} Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultRunnableManagerTestCase.java ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultRunnableManagerTestCase.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultRunnableManagerTestCase.java Fri Nov 12 04:25:13 2004 @@ -20,12 +20,6 @@ import org.apache.avalon.framework.logger.Logger; import org.easymock.MockControl; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import junit.framework.TestCase; - /** * The $classType$ class ... @@ -34,13 +28,8 @@ * @version $Id$ */ public class DefaultRunnableManagerTestCase - extends TestCase + extends AbstractTestCase { - //~ Instance fields -------------------------------------------------------- - - /** DOCUMENT ME! */ - private List m_controls; - //~ Constructors ----------------------------------------------------------- /** @@ -131,11 +120,11 @@ loggerControl.expectAndReturn( logger.getChildLogger( "daemon" ), childLoggerDaemon ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"daemon\" created with maximum queue-size=2147483647,max-pool-size=10,min-pool-size=1,priority=0,isDaemon=false,keep-alive-time-ms=30000,block-policy=\"WAIT\",shutdown-wait-time-ms=0" ); + logger.info( "ThreadPool named \"daemon\" created with maximum queue-size=2147483647,max-pool-size=10,min-pool-size=1,priority=5,isDaemon=false,keep-alive-time-ms=30000,block-policy=\"WAIT\",shutdown-wait-time-ms=0" ); loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Disposing all thread pools" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -162,6 +151,7 @@ } runnableManager.dispose( ); + verify( ); } /** @@ -192,7 +182,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Disposing all thread pools" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -215,6 +205,7 @@ } runnableManager.dispose( ); + verify( ); } /** @@ -292,11 +283,11 @@ loggerControl.expectAndReturn( logger.getChildLogger( "mypool" ), childLoggerMyPool ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=2147483647,max-pool-size=10,min-pool-size=1,priority=0,isDaemon=false,keep-alive-time-ms=30000,block-policy=\"WAIT\",shutdown-wait-time-ms=0" ); + logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=2147483647,max-pool-size=10,min-pool-size=1,priority=1,isDaemon=false,keep-alive-time-ms=30000,block-policy=\"WAIT\",shutdown-wait-time-ms=0" ); loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Disposing all thread pools" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -323,6 +314,7 @@ } runnableManager.dispose( ); + verify( ); } /** @@ -360,11 +352,11 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.getChildLogger( "mypool" ), childLoggerMyPool ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=0,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); + logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=1,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Disposing all thread pools" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -393,6 +385,7 @@ runnableManager.createPool( "mypool", 230, 15, 12, Thread.MIN_PRIORITY, false, 15500, "DISCARD", false, 22200 ); runnableManager.dispose( ); + verify( ); } /** @@ -430,12 +423,12 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.getChildLogger( "anon-xxx" ), childLoggerAnon ); loggerControl.setMatcher( MockControl.ALWAYS_MATCHER ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"anon-xxx\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"anon-xxx\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=10,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.setMatcher( MockControl.ALWAYS_MATCHER ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Disposing all thread pools" ); @@ -468,7 +461,15 @@ runnableManager.createPool( 200, 5, 2, Thread.MAX_PRIORITY, true, 15000, "ABORT", true, 22000 ); assertEquals( "queue-size", 200, threadPool.getMaximumQueueSize( ) ); + assertEquals( "max-pool-size", 5, threadPool.getMaximumPoolSize( ) ); + assertEquals( "min-pool-size", 2, threadPool.getMinimumPoolSize( ) ); + assertEquals( "priority", Thread.MAX_PRIORITY, + threadPool.getPriority( ) ); + assertEquals( "keep-alive-time-ms", 15000, + threadPool.getKeepAliveTime( ) ); + assertEquals( "block-policy", "ABORT", threadPool.getBlockPolicy( ) ); runnableManager.dispose( ); + verify( ); } /** @@ -503,7 +504,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLogger ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "starting heart" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -564,6 +565,8 @@ ex.printStackTrace( ); assertTrue( "Unexpected Exception", false ); } + + verify( ); } /** @@ -598,7 +601,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLogger ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "starting heart" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -659,6 +662,8 @@ ex.printStackTrace( ); assertTrue( "Unexpected Exception", false ); } + + verify( ); } /** @@ -693,7 +698,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLogger ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "starting heart" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -753,6 +758,8 @@ ex.printStackTrace( ); assertTrue( "Unexpected Exception", false ); } + + verify( ); } /** @@ -796,7 +803,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "starting heart" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -806,7 +813,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "mypool" ), childLoggerMyPool ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=0,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); + logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=1,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Command entered: EasyMock for interface java.lang.Runnable,pool=mypool,delay=0,interval=0" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -868,6 +875,8 @@ ex.printStackTrace( ); assertTrue( "Unexpected Exception", false ); } + + verify( ); } /** @@ -911,7 +920,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "starting heart" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -921,7 +930,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "mypool" ), childLoggerMyPool ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=0,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); + logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=1,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Command entered: EasyMock for interface java.lang.Runnable,pool=mypool,delay=100,interval=0" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -983,6 +992,8 @@ ex.printStackTrace( ); assertTrue( "Unexpected Exception", false ); } + + verify( ); } /** @@ -1026,7 +1037,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "default" ), childLoggerDefault ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=0,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); + logger.info( "ThreadPool named \"default\" created with maximum queue-size=2147483647,max-pool-size=5,min-pool-size=5,priority=5,isDaemon=false,keep-alive-time-ms=60000,block-policy=\"RUN\",shutdown-wait-time-ms=-1" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "starting heart" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -1036,7 +1047,7 @@ loggerControl.expectAndReturn( logger.getChildLogger( "mypool" ), childLoggerMyPool ); loggerControl.expectAndReturn( logger.isInfoEnabled( ), true ); - logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=0,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); + logger.info( "ThreadPool named \"mypool\" created with maximum queue-size=230,max-pool-size=15,min-pool-size=12,priority=1,isDaemon=false,keep-alive-time-ms=15500,block-policy=\"DISCARD\",shutdown-wait-time-ms=22200" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); logger.debug( "Command entered: EasyMock for interface java.lang.Runnable,pool=mypool,delay=100,interval=100" ); loggerControl.expectAndReturn( logger.isDebugEnabled( ), true ); @@ -1096,202 +1107,7 @@ ex.printStackTrace( ); assertTrue( "Unexpected Exception", false ); } - } - - /** - * DOCUMENT ME! - * - * @throws Exception DOCUMENT ME! - */ - protected void setUp( ) - throws Exception - { - super.setUp( ); - m_controls = new ArrayList( ); - } - - /** - * @see TestCase#tearDown() - */ - protected void tearDown( ) - throws Exception - { - for( Iterator i = m_controls.iterator( ); i.hasNext( ); ) - { - final MockControl control = (MockControl)i.next( ); - control.verify( ); - } - - m_controls = null; - } - - /** - * DOCUMENT ME! - * - * @param value DOCUMENT ME! - * @param defaultValue DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private Configuration createBooleanConfigMock( final boolean value, - final boolean defaultValue ) - { - final MockControl valueConfigControl = - createStrictControl( Configuration.class ); - final Configuration valueConfig = - (Configuration)valueConfigControl.getMock( ); - valueConfig.getValueAsBoolean( defaultValue ); - valueConfigControl.setReturnValue( value ); - valueConfigControl.replay( ); - - return valueConfig; - } - - /** - * DOCUMENT ME! - * - * @param name DOCUMENT ME! - * @param value DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private Configuration createChildConfigMock( final String name, - final Configuration value ) - { - final MockControl childConfigControl = - createStrictControl( Configuration.class ); - final Configuration childConfig = - (Configuration)childConfigControl.getMock( ); - childConfig.getChild( name ); - childConfigControl.setReturnValue( value ); - childConfigControl.replay( ); - - return childConfig; - } - - /** - * DOCUMENT ME! - * - * @param name DOCUMENT ME! - * @param value DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private Configuration createChildrenConfigMock( final String name, - final Configuration [] value ) - { - final MockControl childrenConfigControl = - createStrictControl( Configuration.class ); - final Configuration childrenConfig = - (Configuration)childrenConfigControl.getMock( ); - childrenConfig.getChildren( name ); - childrenConfigControl.setReturnValue( value ); - childrenConfigControl.replay( ); - - return childrenConfig; - } - - /** - * DOCUMENT ME! - * - * @param value DOCUMENT ME! - * @param defaultValue DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private Configuration createIntegerConfigMock( final int value, - final int defaultValue ) - { - final MockControl valueConfigControl = - createStrictControl( Configuration.class ); - final Configuration valueConfig = - (Configuration)valueConfigControl.getMock( ); - valueConfig.getValueAsInteger( defaultValue ); - valueConfigControl.setReturnValue( value ); - valueConfigControl.replay( ); - - return valueConfig; - } - - /** - * DOCUMENT ME! - * - * @param value DOCUMENT ME! - * @param defaultValue DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private Configuration createLongConfigMock( final long value, - final long defaultValue ) - { - final MockControl valueConfigControl = - createStrictControl( Configuration.class ); - final Configuration valueConfig = - (Configuration)valueConfigControl.getMock( ); - valueConfig.getValueAsLong( defaultValue ); - valueConfigControl.setReturnValue( value ); - valueConfigControl.replay( ); - - return valueConfig; - } - - /** - * DOCUMENT ME! - * - * @param clazz DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private MockControl createStrictControl( final Class clazz ) - { - final MockControl control = MockControl.createStrictControl( clazz ); - m_controls.add( control ); - - return control; - } - - /** - * DOCUMENT ME! - * - * @param value DOCUMENT ME! - * @param defaultValue DOCUMENT ME! - * - * @return DOCUMENT ME! - */ - private Configuration createValueConfigMock( final String value, - final String defaultValue ) - { - final MockControl valueConfigControl = - createStrictControl( Configuration.class ); - final Configuration valueConfig = - (Configuration)valueConfigControl.getMock( ); - valueConfig.getValue( defaultValue ); - valueConfigControl.setReturnValue( value ); - valueConfigControl.replay( ); - - return valueConfig; - } - - /** - * DOCUMENT ME! - * - * @param value DOCUMENT ME! - * - * @return DOCUMENT ME! - * - * @throws ConfigurationException DOCUMENT ME! - */ - private Configuration createValueConfigMock( final String value ) - throws ConfigurationException - { - final MockControl valueConfigControl = - createStrictControl( Configuration.class ); - final Configuration valueConfig = - (Configuration)valueConfigControl.getMock( ); - valueConfig.getValue( ); - valueConfigControl.setReturnValue( value ); - valueConfigControl.replay( ); - return valueConfig; + verify( ); } } Modified: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadFactoryTestCase.java ============================================================================== --- cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadFactoryTestCase.java (original) +++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadFactoryTestCase.java Fri Nov 12 04:25:13 2004 @@ -1,55 +1,114 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.cocoon.components.thread; import junit.framework.TestCase; + +/** + * The $classType$ class ... + * + * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati</a> + * @version $Id$ + */ public class DefaultThreadFactoryTestCase extends TestCase { - public final void testSetDaemon() + //~ Methods ---------------------------------------------------------------- + + /** + * DOCUMENT ME! + */ + public final void testGetPriority( ) { - final DefaultThreadFactory factory = new DefaultThreadFactory(); - factory.setDaemon(false); - final Thread thread = factory.newThread(new DummyRunnable() ); - assertEquals( "daemon mode", false, thread.isDaemon() ); + final DefaultThreadFactory factory = new DefaultThreadFactory( ); + factory.setPriority( Thread.MAX_PRIORITY ); + assertEquals( "priority", Thread.MAX_PRIORITY, factory.getPriority( ) ); } - public final void testIsDaemon() - { - final DefaultThreadFactory factory = new DefaultThreadFactory(); - factory.setDaemon(false); - assertEquals( "daemon mode", false, factory.isDaemon() ); + /** + * DOCUMENT ME! + */ + public final void testIsDaemon( ) + { + final DefaultThreadFactory factory = new DefaultThreadFactory( ); + factory.setDaemon( false ); + assertEquals( "daemon mode", false, factory.isDaemon( ) ); } - public final void testSetPriority() + /** + * DOCUMENT ME! + */ + public final void testNewThread( ) { - final DefaultThreadFactory factory = new DefaultThreadFactory(); - factory.setPriority( Thread.MAX_PRIORITY ); - final Thread thread = factory.newThread(new DummyRunnable() ); - assertEquals( "priority", Thread.MAX_PRIORITY, thread.getPriority() ); + final DefaultThreadFactory factory = new DefaultThreadFactory( ); + factory.setDaemon( true ); + factory.setPriority( Thread.MIN_PRIORITY ); + + final Thread thread = factory.newThread( new DummyRunnable( ) ); + assertEquals( "new thread daemon mode", true, thread.isDaemon( ) ); + assertEquals( "new thread priority", Thread.MIN_PRIORITY, + thread.getPriority( ) ); + assertEquals( "factory daemon mode", factory.isDaemon( ), + thread.isDaemon( ) ); + assertEquals( "factory priority", factory.getPriority( ), + thread.getPriority( ) ); } - public final void testGetPriority() + /** + * DOCUMENT ME! + */ + public final void testSetDaemon( ) { - final DefaultThreadFactory factory = new DefaultThreadFactory(); - factory.setPriority( Thread.MAX_PRIORITY ); - assertEquals( "priority", Thread.MAX_PRIORITY, factory.getPriority() ); + final DefaultThreadFactory factory = new DefaultThreadFactory( ); + factory.setDaemon( false ); + + final Thread thread = factory.newThread( new DummyRunnable( ) ); + assertEquals( "daemon mode", false, thread.isDaemon( ) ); } - public final void testNewThread() + /** + * DOCUMENT ME! + */ + public final void testSetPriority( ) { - final DefaultThreadFactory factory = new DefaultThreadFactory(); - factory.setDaemon(true); - factory.setPriority( Thread.MIN_PRIORITY ); - final Thread thread = factory.newThread(new DummyRunnable() ); - assertEquals( "new thread daemon mode", true, thread.isDaemon() ); - assertEquals( "new thread priority", Thread.MIN_PRIORITY, thread.getPriority() ); - assertEquals( "factory daemon mode", factory.isDaemon(), thread.isDaemon() ); - assertEquals( "factory priority", factory.getPriority(), thread.getPriority() ); + final DefaultThreadFactory factory = new DefaultThreadFactory( ); + factory.setPriority( Thread.MAX_PRIORITY ); + + final Thread thread = factory.newThread( new DummyRunnable( ) ); + assertEquals( "priority", Thread.MAX_PRIORITY, thread.getPriority( ) ); } - - private static class DummyRunnable implements Runnable - { - public void run() + + //~ Inner Classes ---------------------------------------------------------- + + /** + * The $classType$ class ... + * + * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati</a> + * @version $Id$ + */ + private static class DummyRunnable + implements Runnable + { + //~ Methods ------------------------------------------------------------ + + /** + * DOCUMENT ME! + */ + public void run( ) { // nothing } Added: cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadPoolTestCase.java ============================================================================== --- (empty file) +++ cocoon/branches/BRANCH_2_1_X/src/test/org/apache/cocoon/components/thread/DefaultThreadPoolTestCase.java Fri Nov 12 04:25:13 2004 @@ -0,0 +1,150 @@ +/* + * Copyright 1999-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cocoon.components.thread; + +import java.util.Date; +import org.apache.avalon.framework.logger.ConsoleLogger; +import org.easymock.MockControl; + + +/** + * The $classType$ class ... + * + * @author <a href="mailto:giacomo.at.apache.org">Giacomo Pati</a> + * @version $Id$ + */ +public class DefaultThreadPoolTestCase + extends AbstractTestCase +{ + //~ Methods ---------------------------------------------------------------- + + /** + * DOCUMENT ME! + */ + public final void testDefaultThreadPool( ) + { + final DefaultThreadPool pool = new DefaultThreadPool( ); + pool.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); + pool.setName( "mypool" ); + // We cannot mock the DefaultThreadFactory as the underlying + // PooledExecutor of the DefaultThreadPool will again wrapp it into a + // PooledExecutor.Worker instance that does some bookeeping. + // Using a easymocked DefaultThreadFactory will prevent the + // PooledExecutor from shutting down and thus hangs forever. + final ThreadFactory threadFactory = new DefaultThreadFactory(); + threadFactory.setPriority( Thread.MAX_PRIORITY ); + pool.setThreadFactory( threadFactory ); + pool.setQueue( 230 ); + pool.setMaximumPoolSize( 15 ); + pool.setMinimumPoolSize( 9 ); + pool.setKeepAliveTime( 11000 ); + pool.setBlockPolicy( "ABORT" ); + pool.setShutdownGraceful( false ); + pool.setShutdownWaitTimeMs( 12345 ); + assertEquals( "block-policy", "ABORT", pool.getBlockPolicy( ) ); + assertEquals( "keep-alive-time-ms", 11000L, pool.getKeepAliveTime( ) ); + assertEquals( "max-queueu-size", 230, pool.getMaximumQueueSize( ) ); + assertEquals( "max-pool-size", 15, pool.getMaximumPoolSize( ) ); + assertEquals( "min-pool-size", 9, pool.getMinimumPoolSize( ) ); + assertEquals( "name", "mypool", pool.getName( ) ); + assertEquals( "priority", Thread.MAX_PRIORITY, pool.getPriority( ) ); + assertEquals( "queue-size", 0, pool.getQueueSize( ) ); + assertEquals( "isQueued", true, pool.isQueued( ) ); + assertEquals( "isTerminatedAfterShutdown", false, + pool.isTerminatedAfterShutdown( ) ); + verify( ); + } + + /* + * Class under test for void execute(Runnable) + */ + public final void testExecuteRunnable( ) + throws InterruptedException + { + final MockControl runnableControl = + createStrictControl( Runnable.class ); + final Runnable runnable = (Runnable)runnableControl.getMock( ); + runnable.run( ); + runnableControl.replay( ); + + final DefaultThreadPool pool = new DefaultThreadPool( ); + pool.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); + pool.setName( "mypool" ); + // We cannot mock the DefaultThreadFactory as the underlying + // PooledExecutor of the DefaultThreadPool will again wrapp it into a + // PooledExecutor.Worker instance that does some bookeeping. + // Using a easymocked DefaultThreadFactory will prevent the + // PooledExecutor from shutting down and thus hangs forever. + pool.setThreadFactory( new DefaultThreadFactory() ); + pool.setQueue( 230 ); + pool.setMaximumPoolSize( 15 ); + pool.setMinimumPoolSize( 9 ); + pool.setKeepAliveTime( 100 ); + pool.setBlockPolicy( "ABORT" ); + pool.setShutdownGraceful( false ); + pool.setShutdownWaitTimeMs( 1234 ); + pool.execute( runnable ); + Thread.yield( ); + Thread.sleep( 100 ); + pool.shutdown(); + verify( ); + } + + /** + * DOCUMENT ME! + * + * @throws InterruptedException DOCUMENT ME! + */ + public final void testShutdown( ) + throws InterruptedException + { + final Runnable runnable = new Runnable(){ + public void run() + { + final ConsoleLogger logger = new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ); + logger.info( "runnable runs" ); + try + { + Thread.sleep( 1000 ); + } + catch( final InterruptedException ie ) + { + logger.info( "runnable has been interrupted "); + } + logger.info( "runnable terminated" ); + } + }; + + final DefaultThreadPool pool = new DefaultThreadPool( ); + pool.enableLogging( new ConsoleLogger( ConsoleLogger.LEVEL_DEBUG ) ); + pool.setName( "mypool" ); + pool.setThreadFactory( new DefaultThreadFactory() ); + pool.setQueue( 0 ); + pool.setMaximumPoolSize( 15 ); + pool.setMinimumPoolSize( 9 ); + pool.setKeepAliveTime( 1000 ); + pool.setBlockPolicy( "ABORT" ); + pool.setShutdownGraceful( true ); + pool.setShutdownWaitTimeMs( 100 ); + pool.execute( runnable ); + pool.execute( runnable ); + Thread.yield(); + Thread.sleep( 200 ); + pool.shutdown( ); + Thread.sleep( 200 ); + verify( ); + } +}