diff --git a/spring-context/src/test/java/org/springframework/context/support/SimpleThreadScopeTests.java b/spring-context/src/test/java/org/springframework/context/support/SimpleThreadScopeTests.java index 17fd142002..23c9545df1 100644 --- a/spring-context/src/test/java/org/springframework/context/support/SimpleThreadScopeTests.java +++ b/spring-context/src/test/java/org/springframework/context/support/SimpleThreadScopeTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 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. @@ -39,17 +39,17 @@ public class SimpleThreadScopeTests { @Test public void getFromScope() throws Exception { String name = "threadScopedObject"; - TestBean bean = (TestBean) this.applicationContext.getBean(name); + TestBean bean = this.applicationContext.getBean(name, TestBean.class); assertNotNull(bean); assertSame(bean, this.applicationContext.getBean(name)); - TestBean bean2 = (TestBean) this.applicationContext.getBean(name); + TestBean bean2 = this.applicationContext.getBean(name, TestBean.class); assertSame(bean, bean2); } @Test public void getMultipleInstances() throws Exception { // Arrange - final TestBean[] beans = new TestBean[2]; + TestBean[] beans = new TestBean[2]; Thread thread1 = new Thread(() -> beans[0] = applicationContext.getBean("threadScopedObject", TestBean.class)); Thread thread2 = new Thread(() -> beans[1] = applicationContext.getBean("threadScopedObject", TestBean.class)); // Act @@ -57,9 +57,9 @@ public class SimpleThreadScopeTests { thread2.start(); // Assert Awaitility.await() - .pollInterval(10, TimeUnit.MILLISECONDS) - .atMost(500, TimeUnit.MILLISECONDS) - .until(() -> beans[0] != null & beans[1] != null); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> (beans[0] != null) && (beans[1] != null)); assertNotSame(beans[0], beans[1]); } diff --git a/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java b/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java index 1eacdd1c5b..dfe1e58082 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/annotation/AsyncExecutionTests.java @@ -378,10 +378,11 @@ public class AsyncExecutionTests { context.refresh(); // Assert Awaitility.await() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> listenerCalled == 1); + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> listenerCalled == 1); assertEquals(listenerCalled, 1); + context.close(); } @Test @@ -399,9 +400,9 @@ public class AsyncExecutionTests { context.close(); // Assert Awaitility.await() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> listenerCalled == 2); + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> listenerCalled == 2); assertEquals(2, listenerCalled); assertEquals(1, listenerConstructed); } @@ -423,9 +424,9 @@ public class AsyncExecutionTests { context.close(); // Assert Awaitility.await() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> listenerCalled == 2); + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> listenerCalled == 2); assertEquals(2, listenerCalled); assertEquals(2, listenerConstructed); } diff --git a/spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java b/spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java index 3168d79ff7..ade13b7441 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/annotation/EnableAsyncTests.java @@ -77,6 +77,7 @@ public class EnableAsyncTests { AsyncBean asyncBean = ctx.getBean(AsyncBean.class); assertThat(AopUtils.isAopProxy(asyncBean), is(true)); asyncBean.work(); + ctx.close(); } @Test @@ -89,6 +90,7 @@ public class EnableAsyncTests { AsyncBean asyncBean = asyncBeanUser.getAsyncBean(); assertThat(AopUtils.isAopProxy(asyncBean), is(true)); asyncBean.work(); + ctx.close(); } @Test @@ -103,6 +105,7 @@ public class EnableAsyncTests { catch (UnsatisfiedDependencyException ex) { assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException); } + ctx.close(); } @Test @@ -117,6 +120,8 @@ public class EnableAsyncTests { catch (UnsatisfiedDependencyException ex) { assertTrue(ex.getCause() instanceof BeanNotOfRequiredTypeException); } + + ctx.close(); } @Test @@ -134,6 +139,8 @@ public class EnableAsyncTests { assertThat(workerThread2.get().getName(), startsWith("otherExecutor-")); Future workerThread3 = asyncBean.work3(); assertThat(workerThread3.get().getName(), startsWith("otherExecutor-")); + + ctx.close(); } @Test @@ -144,6 +151,8 @@ public class EnableAsyncTests { AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class); assertThat(bpp.getOrder(), is(Ordered.LOWEST_PRECEDENCE)); + + ctx.close(); } @Test @@ -154,6 +163,8 @@ public class EnableAsyncTests { AsyncAnnotationBeanPostProcessor bpp = ctx.getBean(AsyncAnnotationBeanPostProcessor.class); assertThat(bpp.getOrder(), is(Ordered.HIGHEST_PRECEDENCE)); + + ctx.close(); } @Test @@ -172,6 +183,8 @@ public class EnableAsyncTests { } } assertTrue("bean was not async advised as expected", isAsyncAdvised); + + ctx.close(); } /** @@ -179,6 +192,7 @@ public class EnableAsyncTests { */ @Test(expected = BeanDefinitionStoreException.class) public void aspectModeAspectJAttemptsToRegisterAsyncAspect() { + @SuppressWarnings("resource") AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AspectJAsyncAnnotationConfig.class); ctx.refresh(); @@ -195,9 +209,9 @@ public class EnableAsyncTests { asyncBean.work(); // Assert Awaitility.await() - .atMost(500, TimeUnit.MILLISECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> asyncBean.getThreadOfExecution() != null); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); } @@ -213,9 +227,9 @@ public class EnableAsyncTests { asyncBean.work(); // Assert Awaitility.await() - .atMost(500, TimeUnit.MILLISECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> asyncBean.getThreadOfExecution() != null); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); } @@ -235,9 +249,9 @@ public class EnableAsyncTests { asyncBean.fail(); // Assert Awaitility.await() - .pollInterval(10, TimeUnit.MILLISECONDS) - .atMost(500, TimeUnit.MILLISECONDS) - .untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class)); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class)); ctx.close(); } @@ -252,9 +266,9 @@ public class EnableAsyncTests { asyncBean.work(); // Assert Awaitility.await() - .pollInterval(10, TimeUnit.MILLISECONDS) - .atMost(500, TimeUnit.MILLISECONDS) - .until(() -> asyncBean.getThreadOfExecution() != null); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Post-")); ctx.close(); } @@ -274,9 +288,9 @@ public class EnableAsyncTests { asyncBean.fail(); // Assert Awaitility.await() - .atMost(500, TimeUnit.MILLISECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class)); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .untilAsserted(() -> exceptionHandler.assertCalledWith(method, UnsupportedOperationException.class)); ctx.close(); } @@ -289,9 +303,9 @@ public class EnableAsyncTests { asyncBean.work(); // Assert Awaitility.await() - .atMost(500, TimeUnit.MILLISECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> asyncBean.getThreadOfExecution() != null); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); } @@ -305,9 +319,9 @@ public class EnableAsyncTests { asyncBean.work(); // Assert Awaitility.await() - .atMost(500, TimeUnit.MILLISECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(()-> asyncBean.getThreadOfExecution() != null); + .atMost(500, TimeUnit.MILLISECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(()-> asyncBean.getThreadOfExecution() != null); assertThat(asyncBean.getThreadOfExecution().getName(), startsWith("Custom-")); ctx.close(); } diff --git a/spring-context/src/test/java/org/springframework/scheduling/concurrent/AbstractSchedulingTaskExecutorTests.java b/spring-context/src/test/java/org/springframework/scheduling/concurrent/AbstractSchedulingTaskExecutorTests.java index c771dd4710..023f3adafa 100644 --- a/spring-context/src/test/java/org/springframework/scheduling/concurrent/AbstractSchedulingTaskExecutorTests.java +++ b/spring-context/src/test/java/org/springframework/scheduling/concurrent/AbstractSchedulingTaskExecutorTests.java @@ -119,9 +119,9 @@ public abstract class AbstractSchedulingTaskExecutorTests { future.addCallback(result -> outcome = result, ex -> outcome = ex); // Assert Awaitility.await() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(future::isDone); + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(future::isDone); assertNull(outcome); assertThreadNamePrefix(task); } @@ -133,10 +133,10 @@ public abstract class AbstractSchedulingTaskExecutorTests { future.addCallback(result -> outcome = result, ex -> outcome = ex); Awaitility.await() - .dontCatchUncaughtExceptions() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> future.isDone() && outcome != null); + .dontCatchUncaughtExceptions() + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> future.isDone() && outcome != null); assertSame(RuntimeException.class, outcome.getClass()); } @@ -186,9 +186,9 @@ public abstract class AbstractSchedulingTaskExecutorTests { future.addCallback(result -> outcome = result, ex -> outcome = ex); // Assert Awaitility.await() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> future.isDone() && outcome != null); + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> future.isDone() && outcome != null); assertEquals(THREAD_NAME_PREFIX, outcome.toString().substring(0, THREAD_NAME_PREFIX.length())); } @@ -200,10 +200,10 @@ public abstract class AbstractSchedulingTaskExecutorTests { future.addCallback(result -> outcome = result, ex -> outcome = ex); // Assert Awaitility.await() - .dontCatchUncaughtExceptions() - .atMost(1, TimeUnit.SECONDS) - .pollInterval(10, TimeUnit.MILLISECONDS) - .until(() -> future.isDone() && outcome != null); + .dontCatchUncaughtExceptions() + .atMost(1, TimeUnit.SECONDS) + .pollInterval(10, TimeUnit.MILLISECONDS) + .until(() -> future.isDone() && outcome != null); assertSame(RuntimeException.class, outcome.getClass()); }