From fecbcd4e936ba66e33479617eff20a1d3187e312 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 12 Apr 2018 10:56:40 +0200 Subject: [PATCH] Expand scope of SpringFailOnTimeoutTests Issue: SPR-16716 --- .../statements/SpringFailOnTimeout.java | 2 +- .../SpringFailOnTimeoutExceptionTest.java | 45 ---------- .../spr16716/SpringFailOnTimeoutTests.java | 90 +++++++++++++++++++ 3 files changed, 91 insertions(+), 46 deletions(-) delete mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java create mode 100644 spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutTests.java diff --git a/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java b/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java index e9870703e6c..5b6e3816037 100644 --- a/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java +++ b/spring-test/src/main/java/org/springframework/test/context/junit4/statements/SpringFailOnTimeout.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2018 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. diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java deleted file mode 100644 index 1ff04472766..00000000000 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutExceptionTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.springframework.test.context.junit4.spr16716; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.model.Statement; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; -import org.springframework.test.context.junit4.statements.SpringFailOnTimeout; - -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doThrow; - -/** - * Validate SpringFailOnTimeout contract - * SPR-16716. - * - * @author Igor Suhorukov - * @since 5.0.6 - */ -@RunWith(MockitoJUnitRunner.class) -public class SpringFailOnTimeoutExceptionTest { - - @Mock - private Statement statement; - - @Test(expected = IllegalArgumentException.class) - public void validateOriginalExceptionFromEvaluateMethod() throws Throwable { - IllegalArgumentException expectedException = new IllegalArgumentException(); - doThrow(expectedException).when(statement).evaluate(); - new SpringFailOnTimeout(statement, TimeUnit.SECONDS.toMillis(1)).evaluate(); - } - - @Test(expected = TimeoutException.class) - public void validateTimeoutException() throws Throwable { - doAnswer((Answer) invocation -> { - TimeUnit.MILLISECONDS.sleep(50); - return null; - }).when(statement).evaluate(); - new SpringFailOnTimeout(statement, TimeUnit.MILLISECONDS.toMillis(1)).evaluate(); - } -} diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutTests.java new file mode 100644 index 00000000000..05cf95fe938 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/spr16716/SpringFailOnTimeoutTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2002-2018 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.test.context.junit4.spr16716; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runners.model.Statement; +import org.mockito.stubbing.Answer; + +import org.springframework.test.context.junit4.statements.SpringFailOnTimeout; + +import static org.mockito.Mockito.*; + +/** + * Unit tests for {@link SpringFailOnTimeout}. + * + * @author Igor Suhorukov + * @author Sam Brannen + * @since 4.3.17 + */ +public class SpringFailOnTimeoutTests { + + private Statement statement = mock(Statement.class); + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + + @Test + public void nullNextStatement() throws Throwable { + exception.expect(IllegalArgumentException.class); + new SpringFailOnTimeout(null, 1); + } + + @Test + public void negativeTimeout() throws Throwable { + exception.expect(IllegalArgumentException.class); + new SpringFailOnTimeout(statement, -1); + } + + @Test + public void userExceptionPropagates() throws Throwable { + doThrow(new Boom()).when(statement).evaluate(); + + exception.expect(Boom.class); + new SpringFailOnTimeout(statement, 1).evaluate(); + } + + @Test + public void timeoutExceptionThrownIfNoUserException() throws Throwable { + doAnswer((Answer) invocation -> { + TimeUnit.MILLISECONDS.sleep(50); + return null; + }).when(statement).evaluate(); + + exception.expect(TimeoutException.class); + new SpringFailOnTimeout(statement, 1).evaluate(); + } + + @Test + public void noExceptionThrownIfNoUserExceptionAndTimeoutDoesNotOccur() throws Throwable { + doAnswer((Answer) invocation -> { + return null; + }).when(statement).evaluate(); + + new SpringFailOnTimeout(statement, 100).evaluate(); + } + + private static class Boom extends RuntimeException { + } + +}