1 /* 2 * Copyright 2002-2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.scheduling.config; 18 19 import java.util.concurrent.Callable; 20 import java.util.concurrent.Executor; 21 import java.util.concurrent.FutureTask; 22 23 import static org.junit.Assert.*; 24 import org.junit.Before; 25 import org.junit.Test; 26 27 import org.springframework.beans.DirectFieldAccessor; 28 import org.springframework.beans.factory.BeanCreationException; 29 import org.springframework.context.ApplicationContext; 30 import org.springframework.context.support.ClassPathXmlApplicationContext; 31 import org.springframework.core.task.TaskExecutor; 32 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 33 import org.springframework.util.CustomizableThreadCreator; 34 35 /** 36 * @author Mark Fisher 37 * @author Juergen Hoeller 38 */ 39 public class ExecutorBeanDefinitionParserTests { 40 41 private ApplicationContext context; 42 43 44 @Before setup()45 public void setup() { 46 this.context = new ClassPathXmlApplicationContext( 47 "executorContext.xml", ExecutorBeanDefinitionParserTests.class); 48 } 49 50 @Test defaultExecutor()51 public void defaultExecutor() throws Exception { 52 Object executor = this.context.getBean("default"); 53 assertEquals(1, getCorePoolSize(executor)); 54 assertEquals(Integer.MAX_VALUE, getMaxPoolSize(executor)); 55 assertEquals(Integer.MAX_VALUE, getQueueCapacity(executor)); 56 assertEquals(60, getKeepAliveSeconds(executor)); 57 assertEquals(false, getAllowCoreThreadTimeOut(executor)); 58 FutureTask<String> task = new FutureTask<String>(new Callable<String>() { 59 public String call() throws Exception { 60 return "foo"; 61 } 62 }); 63 ((ThreadPoolTaskExecutor)executor).execute(task); 64 assertEquals("foo", task.get()); 65 } 66 67 @Test singleSize()68 public void singleSize() { 69 Object executor = this.context.getBean("singleSize"); 70 assertEquals(42, getCorePoolSize(executor)); 71 assertEquals(42, getMaxPoolSize(executor)); 72 } 73 74 @Test(expected = BeanCreationException.class) invalidPoolSize()75 public void invalidPoolSize() { 76 this.context.getBean("invalidPoolSize"); 77 } 78 79 @Test rangeWithBoundedQueue()80 public void rangeWithBoundedQueue() { 81 Object executor = this.context.getBean("rangeWithBoundedQueue"); 82 assertEquals(7, getCorePoolSize(executor)); 83 assertEquals(42, getMaxPoolSize(executor)); 84 assertEquals(11, getQueueCapacity(executor)); 85 } 86 87 @Test rangeWithUnboundedQueue()88 public void rangeWithUnboundedQueue() { 89 Object executor = this.context.getBean("rangeWithUnboundedQueue"); 90 assertEquals(9, getCorePoolSize(executor)); 91 assertEquals(9, getMaxPoolSize(executor)); 92 assertEquals(37, getKeepAliveSeconds(executor)); 93 assertEquals(true, getAllowCoreThreadTimeOut(executor)); 94 assertEquals(Integer.MAX_VALUE, getQueueCapacity(executor)); 95 } 96 97 @Test propertyPlaceholderWithSingleSize()98 public void propertyPlaceholderWithSingleSize() { 99 Object executor = this.context.getBean("propertyPlaceholderWithSingleSize"); 100 assertEquals(123, getCorePoolSize(executor)); 101 assertEquals(123, getMaxPoolSize(executor)); 102 assertEquals(60, getKeepAliveSeconds(executor)); 103 assertEquals(false, getAllowCoreThreadTimeOut(executor)); 104 assertEquals(Integer.MAX_VALUE, getQueueCapacity(executor)); 105 } 106 107 @Test propertyPlaceholderWithRange()108 public void propertyPlaceholderWithRange() { 109 Object executor = this.context.getBean("propertyPlaceholderWithRange"); 110 assertEquals(5, getCorePoolSize(executor)); 111 assertEquals(25, getMaxPoolSize(executor)); 112 assertEquals(false, getAllowCoreThreadTimeOut(executor)); 113 assertEquals(10, getQueueCapacity(executor)); 114 } 115 116 @Test propertyPlaceholderWithRangeAndCoreThreadTimeout()117 public void propertyPlaceholderWithRangeAndCoreThreadTimeout() { 118 Object executor = this.context.getBean("propertyPlaceholderWithRangeAndCoreThreadTimeout"); 119 assertEquals(99, getCorePoolSize(executor)); 120 assertEquals(99, getMaxPoolSize(executor)); 121 assertEquals(true, getAllowCoreThreadTimeOut(executor)); 122 } 123 124 @Test(expected = BeanCreationException.class) propertyPlaceholderWithInvalidPoolSize()125 public void propertyPlaceholderWithInvalidPoolSize() { 126 this.context.getBean("propertyPlaceholderWithInvalidPoolSize"); 127 } 128 129 @Test threadNamePrefix()130 public void threadNamePrefix() { 131 CustomizableThreadCreator executor = this.context.getBean("default", CustomizableThreadCreator.class); 132 assertEquals("default-", executor.getThreadNamePrefix()); 133 } 134 135 @Test typeCheck()136 public void typeCheck() { 137 assertTrue(this.context.isTypeMatch("default", Executor.class)); 138 assertTrue(this.context.isTypeMatch("default", TaskExecutor.class)); 139 assertTrue(this.context.isTypeMatch("default", ThreadPoolTaskExecutor.class)); 140 } 141 142 getCorePoolSize(Object executor)143 private int getCorePoolSize(Object executor) { 144 return (Integer) new DirectFieldAccessor(executor).getPropertyValue("corePoolSize"); 145 } 146 getMaxPoolSize(Object executor)147 private int getMaxPoolSize(Object executor) { 148 return (Integer) new DirectFieldAccessor(executor).getPropertyValue("maxPoolSize"); 149 } 150 getQueueCapacity(Object executor)151 private int getQueueCapacity(Object executor) { 152 return (Integer) new DirectFieldAccessor(executor).getPropertyValue("queueCapacity"); 153 } 154 getKeepAliveSeconds(Object executor)155 private int getKeepAliveSeconds(Object executor) { 156 return (Integer) new DirectFieldAccessor(executor).getPropertyValue("keepAliveSeconds"); 157 } 158 getAllowCoreThreadTimeOut(Object executor)159 private boolean getAllowCoreThreadTimeOut(Object executor) { 160 return (Boolean) new DirectFieldAccessor(executor).getPropertyValue("allowCoreThreadTimeOut"); 161 } 162 163 } 164