moving remoting.*, scheduling.* unit tests from .testsuite -> .context, .web
This commit is contained in:
@@ -0,0 +1,452 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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.springframework.remoting.rmi;
|
||||
|
||||
import java.lang.reflect.AccessibleObject;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Method;
|
||||
import java.rmi.ConnectException;
|
||||
import java.rmi.ConnectIOException;
|
||||
import java.rmi.MarshalException;
|
||||
import java.rmi.NoSuchObjectException;
|
||||
import java.rmi.Remote;
|
||||
import java.rmi.RemoteException;
|
||||
import java.rmi.StubNotFoundException;
|
||||
import java.rmi.UnknownHostException;
|
||||
import java.rmi.UnmarshalException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
|
||||
import org.springframework.remoting.RemoteAccessException;
|
||||
import org.springframework.remoting.RemoteConnectFailureException;
|
||||
import org.springframework.remoting.RemoteProxyFailureException;
|
||||
import org.springframework.remoting.support.RemoteInvocation;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 16.05.2003
|
||||
*/
|
||||
public class RmiSupportTests extends TestCase {
|
||||
|
||||
public void testRmiProxyFactoryBean() throws Exception {
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IRemoteBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue("Correct singleton value", factory.isSingleton());
|
||||
assertTrue(factory.getObject() instanceof IRemoteBean);
|
||||
IRemoteBean proxy = (IRemoteBean) factory.getObject();
|
||||
proxy.setName("myName");
|
||||
assertEquals(RemoteBean.name, "myName");
|
||||
assertEquals(1, factory.counter);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithRemoteException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(RemoteException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithConnectException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(ConnectException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithConnectIOException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(ConnectIOException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithUnknownHostException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(UnknownHostException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithNoSuchObjectException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(NoSuchObjectException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithStubNotFoundException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(StubNotFoundException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithMarshalException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(MarshalException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithUnmarshalException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithException(UnmarshalException.class);
|
||||
}
|
||||
|
||||
private void doTestRmiProxyFactoryBeanWithException(Class exceptionClass) throws Exception {
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IRemoteBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject() instanceof IRemoteBean);
|
||||
IRemoteBean proxy = (IRemoteBean) factory.getObject();
|
||||
try {
|
||||
proxy.setName(exceptionClass.getName());
|
||||
fail("Should have thrown " + exceptionClass.getName());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (exceptionClass.isInstance(ex)) {
|
||||
// expected
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
assertEquals(1, factory.counter);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithConnectExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithExceptionAndRefresh(ConnectException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithConnectIOExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithExceptionAndRefresh(ConnectIOException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithUnknownHostExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithExceptionAndRefresh(UnknownHostException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithNoSuchObjectExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithExceptionAndRefresh(NoSuchObjectException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithStubNotFoundExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithExceptionAndRefresh(StubNotFoundException.class);
|
||||
}
|
||||
|
||||
private void doTestRmiProxyFactoryBeanWithExceptionAndRefresh(Class exceptionClass) throws Exception {
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IRemoteBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.setRefreshStubOnConnectFailure(true);
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject() instanceof IRemoteBean);
|
||||
IRemoteBean proxy = (IRemoteBean) factory.getObject();
|
||||
try {
|
||||
proxy.setName(exceptionClass.getName());
|
||||
fail("Should have thrown " + exceptionClass.getName());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (exceptionClass.isInstance(ex)) {
|
||||
// expected
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
assertEquals(2, factory.counter);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterface() throws Exception {
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IBusinessBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject() instanceof IBusinessBean);
|
||||
IBusinessBean proxy = (IBusinessBean) factory.getObject();
|
||||
assertFalse(proxy instanceof IRemoteBean);
|
||||
proxy.setName("myName");
|
||||
assertEquals(RemoteBean.name, "myName");
|
||||
assertEquals(1, factory.counter);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithWrongBusinessInterface() throws Exception {
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IWrongBusinessBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject() instanceof IWrongBusinessBean);
|
||||
IWrongBusinessBean proxy = (IWrongBusinessBean) factory.getObject();
|
||||
assertFalse(proxy instanceof IRemoteBean);
|
||||
try {
|
||||
proxy.setOtherName("name");
|
||||
fail("Should have thrown RemoteProxyFailureException");
|
||||
}
|
||||
catch (RemoteProxyFailureException ex) {
|
||||
assertTrue(ex.getCause() instanceof NoSuchMethodException);
|
||||
assertTrue(ex.getMessage().indexOf("setOtherName") != -1);
|
||||
assertTrue(ex.getMessage().indexOf("IWrongBusinessBean") != -1);
|
||||
}
|
||||
assertEquals(1, factory.counter);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
RemoteException.class, RemoteAccessException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
ConnectException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
ConnectIOException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
UnknownHostException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
NoSuchObjectException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundException() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
StubNotFoundException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndException(
|
||||
Class rmiExceptionClass, Class springExceptionClass) throws Exception {
|
||||
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IBusinessBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject() instanceof IBusinessBean);
|
||||
IBusinessBean proxy = (IBusinessBean) factory.getObject();
|
||||
assertFalse(proxy instanceof IRemoteBean);
|
||||
try {
|
||||
proxy.setName(rmiExceptionClass.getName());
|
||||
fail("Should have thrown " + rmiExceptionClass.getName());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (springExceptionClass.isInstance(ex)) {
|
||||
// expected
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
assertEquals(1, factory.counter);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
RemoteException.class, RemoteAccessException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
ConnectException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
ConnectIOException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
UnknownHostException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
NoSuchObjectException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundExceptionAndRefresh() throws Exception {
|
||||
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
StubNotFoundException.class, RemoteConnectFailureException.class);
|
||||
}
|
||||
|
||||
private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
|
||||
Class rmiExceptionClass, Class springExceptionClass) throws Exception {
|
||||
|
||||
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
|
||||
factory.setServiceInterface(IBusinessBean.class);
|
||||
factory.setServiceUrl("rmi://localhost:1090/test");
|
||||
factory.setRefreshStubOnConnectFailure(true);
|
||||
factory.afterPropertiesSet();
|
||||
assertTrue(factory.getObject() instanceof IBusinessBean);
|
||||
IBusinessBean proxy = (IBusinessBean) factory.getObject();
|
||||
assertFalse(proxy instanceof IRemoteBean);
|
||||
try {
|
||||
proxy.setName(rmiExceptionClass.getName());
|
||||
fail("Should have thrown " + rmiExceptionClass.getName());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (springExceptionClass.isInstance(ex)) {
|
||||
// expected
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
if (RemoteConnectFailureException.class.isAssignableFrom(springExceptionClass)) {
|
||||
assertEquals(2, factory.counter);
|
||||
}
|
||||
else {
|
||||
assertEquals(1, factory.counter);
|
||||
}
|
||||
}
|
||||
|
||||
public void testRmiClientInterceptorRequiresUrl() throws Exception{
|
||||
RmiClientInterceptor client = new RmiClientInterceptor();
|
||||
client.setServiceInterface(IRemoteBean.class);
|
||||
|
||||
try {
|
||||
client.afterPropertiesSet();
|
||||
fail("url isn't set, expected IllegalArgumentException");
|
||||
}
|
||||
catch(IllegalArgumentException e){
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testRemoteInvocation() throws NoSuchMethodException {
|
||||
// let's see if the remote invocation object works
|
||||
|
||||
final RemoteBean rb = new RemoteBean();
|
||||
final Method setNameMethod = rb.getClass().getDeclaredMethod("setName", new Class[] {String.class});
|
||||
|
||||
MethodInvocation mi = new MethodInvocation() {
|
||||
public Method getMethod() {
|
||||
return setNameMethod;
|
||||
}
|
||||
public Object[] getArguments() {
|
||||
return new Object[] {"bla"};
|
||||
}
|
||||
public Object proceed() throws Throwable {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
public Object getThis() {
|
||||
return rb;
|
||||
}
|
||||
public AccessibleObject getStaticPart() {
|
||||
return setNameMethod;
|
||||
}
|
||||
};
|
||||
|
||||
RemoteInvocation inv = new RemoteInvocation(mi);
|
||||
|
||||
assertEquals("setName", inv.getMethodName());
|
||||
assertEquals("bla", inv.getArguments()[0]);
|
||||
assertEquals(String.class, inv.getParameterTypes()[0]);
|
||||
|
||||
// this is a bit BS, but we need to test it
|
||||
inv = new RemoteInvocation();
|
||||
inv.setArguments(new Object[] { "bla" });
|
||||
assertEquals("bla", inv.getArguments()[0]);
|
||||
inv.setMethodName("setName");
|
||||
assertEquals("setName", inv.getMethodName());
|
||||
inv.setParameterTypes(new Class[] {String.class});
|
||||
assertEquals(String.class, inv.getParameterTypes()[0]);
|
||||
|
||||
inv = new RemoteInvocation("setName", new Class[] {String.class}, new Object[] {"bla"});
|
||||
assertEquals("bla", inv.getArguments()[0]);
|
||||
assertEquals("setName", inv.getMethodName());
|
||||
assertEquals(String.class, inv.getParameterTypes()[0]);
|
||||
}
|
||||
|
||||
public void testRmiInvokerWithSpecialLocalMethods() throws Exception {
|
||||
String serviceUrl = "rmi://localhost:1090/test";
|
||||
RmiProxyFactoryBean factory = new RmiProxyFactoryBean() {
|
||||
protected Remote lookupStub() {
|
||||
return new RmiInvocationHandler() {
|
||||
public String getTargetInterfaceName() {
|
||||
return null;
|
||||
}
|
||||
public Object invoke(RemoteInvocation invocation) throws RemoteException {
|
||||
throw new RemoteException();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
factory.setServiceInterface(IBusinessBean.class);
|
||||
factory.setServiceUrl(serviceUrl);
|
||||
factory.afterPropertiesSet();
|
||||
IBusinessBean proxy = (IBusinessBean) factory.getObject();
|
||||
|
||||
// shouldn't go through to remote service
|
||||
assertTrue(proxy.toString().indexOf("RMI invoker") != -1);
|
||||
assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
|
||||
assertEquals(proxy.hashCode(), proxy.hashCode());
|
||||
assertTrue(proxy.equals(proxy));
|
||||
|
||||
// should go through
|
||||
try {
|
||||
proxy.setName("test");
|
||||
fail("Should have thrown RemoteAccessException");
|
||||
}
|
||||
catch (RemoteAccessException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class CountingRmiProxyFactoryBean extends RmiProxyFactoryBean {
|
||||
|
||||
private int counter = 0;
|
||||
|
||||
protected Remote lookupStub() {
|
||||
counter++;
|
||||
return new RemoteBean();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static interface IBusinessBean {
|
||||
|
||||
public void setName(String name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static interface IWrongBusinessBean {
|
||||
|
||||
public void setOtherName(String name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static interface IRemoteBean extends Remote {
|
||||
|
||||
public void setName(String name) throws RemoteException;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static class RemoteBean implements IRemoteBean {
|
||||
|
||||
private static String name;
|
||||
|
||||
public void setName(String nam) throws RemoteException {
|
||||
if (nam != null && nam.endsWith("Exception")) {
|
||||
RemoteException rex = null;
|
||||
try {
|
||||
Class exClass = Class.forName(nam);
|
||||
Constructor ctor = exClass.getConstructor(new Class[] {String.class});
|
||||
rex = (RemoteException) ctor.newInstance(new Object[] {"myMessage"});
|
||||
}
|
||||
catch (Exception ex) {
|
||||
throw new RemoteException("Illegal exception class name: " + nam, ex);
|
||||
}
|
||||
throw rex;
|
||||
}
|
||||
name = nam;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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.springframework.remoting.support;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
*/
|
||||
public class RemoteInvocationUtilsTests extends TestCase {
|
||||
|
||||
public void testFillInClientStackTraceIfPossibleSunnyDay() throws Exception {
|
||||
try {
|
||||
throw new IllegalStateException("Mmm");
|
||||
}
|
||||
catch (Exception ex) {
|
||||
int originalStackTraceLngth = ex.getStackTrace().length;
|
||||
RemoteInvocationUtils.fillInClientStackTraceIfPossible(ex);
|
||||
assertTrue("Stack trace not being filled in",
|
||||
ex.getStackTrace().length > originalStackTraceLngth);
|
||||
}
|
||||
}
|
||||
|
||||
public void testFillInClientStackTraceIfPossibleWithNullThrowable() throws Exception {
|
||||
// just want to ensure that it doesn't bomb
|
||||
RemoteInvocationUtils.fillInClientStackTraceIfPossible(null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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.springframework.scheduling.backportconcurrent;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.task.NoOpRunnable;
|
||||
|
||||
import edu.emory.mathcs.backport.java.util.concurrent.RejectedExecutionHandler;
|
||||
import edu.emory.mathcs.backport.java.util.concurrent.ScheduledExecutorService;
|
||||
import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ScheduledExecutorFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testThrowsExceptionIfPoolSizeIsLessThanZero() throws Exception {
|
||||
try {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setPoolSize(-1);
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.afterPropertiesSet();
|
||||
fail("Pool size less than zero");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdownNowIsPropagatedToTheExecutorOnDestroy() throws Exception {
|
||||
MockControl mockScheduledExecutorService = MockControl.createNiceControl(ScheduledExecutorService.class);
|
||||
final ScheduledExecutorService executor = (ScheduledExecutorService) mockScheduledExecutorService.getMock();
|
||||
executor.shutdownNow();
|
||||
mockScheduledExecutorService.setReturnValue(null);
|
||||
mockScheduledExecutorService.replay();
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
return executor;
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
|
||||
mockScheduledExecutorService.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdownIsPropagatedToTheExecutorOnDestroy() throws Exception {
|
||||
MockControl mockScheduledExecutorService = MockControl.createNiceControl(ScheduledExecutorService.class);
|
||||
final ScheduledExecutorService executor = (ScheduledExecutorService) mockScheduledExecutorService.getMock();
|
||||
executor.shutdown();
|
||||
mockScheduledExecutorService.setVoidCallable();
|
||||
mockScheduledExecutorService.replay();
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
return executor;
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.setWaitForTasksToCompleteOnShutdown(true);
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
|
||||
mockScheduledExecutorService.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneTimeExecutionIsSetUpAndFiresCorrectly() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new ScheduledExecutorTask(runnable)
|
||||
});
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(1);
|
||||
factory.destroy();
|
||||
|
||||
mockRunnable.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setFixedRate(true);
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{task});
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(2);
|
||||
factory.destroy();
|
||||
|
||||
mockRunnable.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedRepeatedExecutionIsSetUpAndFiresCorrectlyAfterException() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setFixedRate(true);
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{task});
|
||||
factory.setContinueScheduledExecutionAfterException(true);
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(2);
|
||||
factory.destroy();
|
||||
|
||||
mockRunnable.verify();
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testWithInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setDelay(3000); // nice long wait...
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[] {task});
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(1);
|
||||
// invoke destroy before tasks have even been scheduled...
|
||||
factory.destroy();
|
||||
|
||||
try {
|
||||
mockRunnable.verify();
|
||||
fail("Mock must never have been called");
|
||||
}
|
||||
catch (AssertionFailedError expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testWithInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectlyAfterException() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setDelay(3000); // nice long wait...
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[] {task});
|
||||
factory.setContinueScheduledExecutionAfterException(true);
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(1);
|
||||
// invoke destroy before tasks have even been scheduled...
|
||||
factory.destroy();
|
||||
|
||||
try {
|
||||
mockRunnable.verify();
|
||||
fail("Mock must never have been called");
|
||||
}
|
||||
catch (AssertionFailedError expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingThreadFactoryToNullForcesUseOfDefaultButIsOtherwiseCool() throws Exception {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
assertNotNull("Bah; the setThreadFactory(..) method must use a default ThreadFactory if a null arg is passed in.");
|
||||
return super.createExecutor(poolSize, threadFactory, rejectedExecutionHandler);
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.setThreadFactory(null); // the null must not propagate
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingRejectedExecutionHandlerToNullForcesUseOfDefaultButIsOtherwiseCool() throws Exception {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
assertNotNull("Bah; the setRejectedExecutionHandler(..) method must use a default RejectedExecutionHandler if a null arg is passed in.");
|
||||
return super.createExecutor(poolSize, threadFactory, rejectedExecutionHandler);
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.setRejectedExecutionHandler(null); // the null must not propagate
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectTypeReportsCorrectType() throws Exception {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
assertEquals(ScheduledExecutorService.class, factory.getObjectType());
|
||||
}
|
||||
|
||||
|
||||
private static void pauseToLetTaskStart(int seconds) {
|
||||
try {
|
||||
Thread.sleep(seconds * 1000);
|
||||
}
|
||||
catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class NoOpScheduledExecutorTask extends ScheduledExecutorTask {
|
||||
|
||||
public NoOpScheduledExecutorTask() {
|
||||
super(new NoOpRunnable());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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.springframework.scheduling.concurrent;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.core.task.NoOpRunnable;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
*/
|
||||
public class ConcurrentTaskExecutorTests extends TestCase {
|
||||
|
||||
public void testZeroArgCtorResultsInDefaultTaskExecutorBeingUsed() throws Exception {
|
||||
ConcurrentTaskExecutor executor = new ConcurrentTaskExecutor();
|
||||
// must not throw a NullPointerException
|
||||
executor.execute(new NoOpRunnable());
|
||||
}
|
||||
|
||||
public void testPassingNullExecutorToCtorResultsInDefaultTaskExecutorBeingUsed() throws Exception {
|
||||
ConcurrentTaskExecutor executor = new ConcurrentTaskExecutor(null);
|
||||
// must not throw a NullPointerException
|
||||
executor.execute(new NoOpRunnable());
|
||||
}
|
||||
|
||||
public void testPassingNullExecutorToSetterResultsInDefaultTaskExecutorBeingUsed() throws Exception {
|
||||
ConcurrentTaskExecutor executor = new ConcurrentTaskExecutor();
|
||||
executor.setConcurrentExecutor(null);
|
||||
// must not throw a NullPointerException
|
||||
executor.execute(new NoOpRunnable());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
*
|
||||
* 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.springframework.scheduling.concurrent;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.concurrent.RejectedExecutionHandler;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
import junit.framework.AssertionFailedError;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.task.NoOpRunnable;
|
||||
|
||||
/**
|
||||
* @author Rick Evans
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class ScheduledExecutorFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
public void testThrowsExceptionIfPoolSizeIsLessThanZero() throws Exception {
|
||||
try {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setPoolSize(-1);
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.afterPropertiesSet();
|
||||
fail("Pool size less than zero");
|
||||
}
|
||||
catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdownNowIsPropagatedToTheExecutorOnDestroy() throws Exception {
|
||||
MockControl mockScheduledExecutorService = MockControl.createNiceControl(ScheduledExecutorService.class);
|
||||
final ScheduledExecutorService executor = (ScheduledExecutorService) mockScheduledExecutorService.getMock();
|
||||
executor.shutdownNow();
|
||||
mockScheduledExecutorService.setReturnValue(null);
|
||||
mockScheduledExecutorService.replay();
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
return executor;
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
|
||||
mockScheduledExecutorService.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShutdownIsPropagatedToTheExecutorOnDestroy() throws Exception {
|
||||
MockControl mockScheduledExecutorService = MockControl.createNiceControl(ScheduledExecutorService.class);
|
||||
final ScheduledExecutorService executor = (ScheduledExecutorService) mockScheduledExecutorService.getMock();
|
||||
executor.shutdown();
|
||||
mockScheduledExecutorService.setVoidCallable();
|
||||
mockScheduledExecutorService.replay();
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
return executor;
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.setWaitForTasksToCompleteOnShutdown(true);
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
|
||||
mockScheduledExecutorService.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneTimeExecutionIsSetUpAndFiresCorrectly() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new ScheduledExecutorTask(runnable)
|
||||
});
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(1);
|
||||
factory.destroy();
|
||||
|
||||
mockRunnable.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setFixedRate(true);
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{task});
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(2);
|
||||
factory.destroy();
|
||||
|
||||
mockRunnable.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedRepeatedExecutionIsSetUpAndFiresCorrectlyAfterException() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setFixedRate(true);
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{task});
|
||||
factory.setContinueScheduledExecutionAfterException(true);
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(2);
|
||||
factory.destroy();
|
||||
|
||||
mockRunnable.verify();
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testWithInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectly() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
runnable.run();
|
||||
mockRunnable.setVoidCallable();
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setDelay(3000); // nice long wait...
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[] {task});
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(1);
|
||||
// invoke destroy before tasks have even been scheduled...
|
||||
factory.destroy();
|
||||
|
||||
try {
|
||||
mockRunnable.verify();
|
||||
fail("Mock must never have been called");
|
||||
}
|
||||
catch (AssertionFailedError expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testWithInitialDelayRepeatedExecutionIsSetUpAndFiresCorrectlyAfterException() throws Exception {
|
||||
MockControl mockRunnable = MockControl.createControl(Runnable.class);
|
||||
Runnable runnable = (Runnable) mockRunnable.getMock();
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
runnable.run();
|
||||
mockRunnable.setThrowable(new IllegalStateException());
|
||||
mockRunnable.replay();
|
||||
|
||||
ScheduledExecutorTask task = new ScheduledExecutorTask(runnable);
|
||||
task.setPeriod(500);
|
||||
task.setDelay(3000); // nice long wait...
|
||||
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[] {task});
|
||||
factory.setContinueScheduledExecutionAfterException(true);
|
||||
factory.afterPropertiesSet();
|
||||
pauseToLetTaskStart(1);
|
||||
// invoke destroy before tasks have even been scheduled...
|
||||
factory.destroy();
|
||||
|
||||
try {
|
||||
mockRunnable.verify();
|
||||
fail("Mock must never have been called");
|
||||
}
|
||||
catch (AssertionFailedError expected) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingThreadFactoryToNullForcesUseOfDefaultButIsOtherwiseCool() throws Exception {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
assertNotNull("Bah; the setThreadFactory(..) method must use a default ThreadFactory if a null arg is passed in.");
|
||||
return super.createExecutor(poolSize, threadFactory, rejectedExecutionHandler);
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.setThreadFactory(null); // the null must not propagate
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingRejectedExecutionHandlerToNullForcesUseOfDefaultButIsOtherwiseCool() throws Exception {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean() {
|
||||
protected ScheduledExecutorService createExecutor(int poolSize, ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) {
|
||||
assertNotNull("Bah; the setRejectedExecutionHandler(..) method must use a default RejectedExecutionHandler if a null arg is passed in.");
|
||||
return super.createExecutor(poolSize, threadFactory, rejectedExecutionHandler);
|
||||
}
|
||||
};
|
||||
factory.setScheduledExecutorTasks(new ScheduledExecutorTask[]{
|
||||
new NoOpScheduledExecutorTask()
|
||||
});
|
||||
factory.setRejectedExecutionHandler(null); // the null must not propagate
|
||||
factory.afterPropertiesSet();
|
||||
factory.destroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testObjectTypeReportsCorrectType() throws Exception {
|
||||
ScheduledExecutorFactoryBean factory = new ScheduledExecutorFactoryBean();
|
||||
assertEquals(ScheduledExecutorService.class, factory.getObjectType());
|
||||
}
|
||||
|
||||
|
||||
private static void pauseToLetTaskStart(int seconds) {
|
||||
try {
|
||||
Thread.sleep(seconds * 1000);
|
||||
}
|
||||
catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class NoOpScheduledExecutorTask extends ScheduledExecutorTask {
|
||||
|
||||
public NoOpScheduledExecutorTask() {
|
||||
super(new NoOpRunnable());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright 2002-2006 the original author or authors.
|
||||
*
|
||||
* 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.springframework.scheduling.timer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Timer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link TimerTaskExecutor} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class TimerTaskExecutorTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testExecuteChokesWithNullTimer() throws Exception {
|
||||
TimerTaskExecutor executor = new TimerTaskExecutor();
|
||||
executor.execute(new NoOpRunnable());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testExecuteChokesWithNullTask() throws Exception {
|
||||
TimerTaskExecutor executor = new TimerTaskExecutor(new Timer());
|
||||
executor.execute(null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testExecuteChokesWithNegativeDelay() throws Exception {
|
||||
TimerTaskExecutor executor = new TimerTaskExecutor(new Timer());
|
||||
executor.setDelay(-10);
|
||||
executor.execute(new NoOpRunnable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecuteReallyDoesScheduleTheSuppliedTask() throws Exception {
|
||||
final Object monitor = new Object();
|
||||
|
||||
RunAwareRunnable task = new RunAwareRunnable(monitor);
|
||||
|
||||
TimerTaskExecutor executor = new TimerTaskExecutor(new Timer());
|
||||
executor.execute(task);
|
||||
|
||||
synchronized (monitor) {
|
||||
monitor.wait(5000);
|
||||
}
|
||||
|
||||
assertTrue("Supplied task (a Runnable) is not being invoked.", task.isRunWasCalled());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWithNullTimer() throws Exception {
|
||||
new TimerTaskExecutor(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTimerMethodIsCalledIfNoTimerIsExplicitlySupplied() throws Exception {
|
||||
CreationAwareTimerTaskExecutor executor = new CreationAwareTimerTaskExecutor();
|
||||
executor.afterPropertiesSet();
|
||||
assertTrue("If no Timer is set explicitly, then the protected createTimer() " +
|
||||
"method must be called to create the Timer (it obviously isn't being called).",
|
||||
executor.isCreateTimerWasCalled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTimerMethodIsNotCalledIfTimerIsExplicitlySupplied() throws Exception {
|
||||
CreationAwareTimerTaskExecutor executor = new CreationAwareTimerTaskExecutor();
|
||||
executor.setTimer(new Timer());
|
||||
executor.afterPropertiesSet();
|
||||
assertFalse("If a Timer is set explicitly, then the protected createTimer() " +
|
||||
"method must not be called to create the Timer (it obviously is being called, in error).",
|
||||
executor.isCreateTimerWasCalled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatTheDestroyCallbackCancelsTheTimerIfNoTimerIsExplicitlySupplied() throws Exception {
|
||||
|
||||
final CancelAwareTimer timer = new CancelAwareTimer();
|
||||
|
||||
TimerTaskExecutor executor = new TimerTaskExecutor() {
|
||||
|
||||
protected Timer createTimer() {
|
||||
return timer;
|
||||
}
|
||||
};
|
||||
executor.afterPropertiesSet();
|
||||
executor.destroy();
|
||||
assertTrue("When the Timer used is created by the TimerTaskExecutor because " +
|
||||
"no Timer was set explicitly, then the destroy() callback must cancel() said Timer (it obviously isn't doing this).",
|
||||
timer.isCancelWasCalled());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThatTheDestroyCallbackDoesNotCancelTheTimerIfTheTimerWasSuppliedExplictly() throws Exception {
|
||||
TimerTaskExecutor executor = new TimerTaskExecutor();
|
||||
CancelAwareTimer timer = new CancelAwareTimer();
|
||||
executor.setTimer(timer);
|
||||
executor.afterPropertiesSet();
|
||||
executor.destroy();
|
||||
assertFalse("When the Timer used is not created by the TimerTaskExecutor because " +
|
||||
"it Timer was set explicitly, then the destroy() callback must NOT cancel() said Timer (it obviously is, in error).",
|
||||
timer.isCancelWasCalled());
|
||||
}
|
||||
|
||||
|
||||
private final static class CreationAwareTimerTaskExecutor extends TimerTaskExecutor {
|
||||
|
||||
private boolean createTimerWasCalled = false;
|
||||
|
||||
|
||||
public boolean isCreateTimerWasCalled() {
|
||||
return this.createTimerWasCalled;
|
||||
}
|
||||
|
||||
protected Timer createTimer() {
|
||||
this.createTimerWasCalled = true;
|
||||
return super.createTimer();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class CancelAwareTimer extends Timer {
|
||||
|
||||
private boolean cancelWasCalled;
|
||||
|
||||
|
||||
public boolean isCancelWasCalled() {
|
||||
return this.cancelWasCalled;
|
||||
}
|
||||
|
||||
|
||||
public void cancel() {
|
||||
this.cancelWasCalled = true;
|
||||
super.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private static class RunAwareRunnable implements Runnable {
|
||||
private boolean runWasCalled;
|
||||
private final Object monitor;
|
||||
|
||||
public RunAwareRunnable(Object monitor) {
|
||||
this.monitor = monitor;
|
||||
}
|
||||
|
||||
|
||||
public boolean isRunWasCalled() {
|
||||
return this.runWasCalled;
|
||||
}
|
||||
|
||||
|
||||
public void run() {
|
||||
this.runWasCalled = true;
|
||||
synchronized (monitor) {
|
||||
monitor.notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class NoOpRunnable implements Runnable {
|
||||
|
||||
public void run() {
|
||||
// explicit no-op
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user