11/*
2- * Copyright 2002-2020 the original author or authors.
2+ * Copyright 2002-2021 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2525
2626import org .junit .jupiter .api .Test ;
2727
28- import org .springframework .context .ApplicationContext ;
2928import org .springframework .context .ConfigurableApplicationContext ;
3029import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
3130import org .springframework .context .annotation .Bean ;
3231import org .springframework .context .annotation .Configuration ;
32+ import org .springframework .context .support .GenericApplicationContext ;
3333
3434import static org .assertj .core .api .Assertions .assertThat ;
3535import static org .mockito .Mockito .mock ;
3636import static org .mockito .Mockito .never ;
3737import static org .mockito .Mockito .verify ;
3838
3939/**
40+ * Tests for {@link ThreadPoolExecutorFactoryBean}.
41+ *
4042 * @author Juergen Hoeller
4143 */
4244class ThreadPoolExecutorFactoryBeanTests {
@@ -53,19 +55,25 @@ void defaultExecutor() throws Exception {
5355 }
5456
5557 @ Test
56- public void executorWithPreStartedThreads () throws Exception {
57- ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (ExecutorConfigWithPreStartedThreads .class );
58- ThreadPoolExecutor executor = context .getBean ("childExecutor" , ThreadPoolExecutor .class );
59-
60- verify (executor ).prestartAllCoreThreads ();
58+ void executorWithDefaultSettingsDoesNotPrestartAllCoreThreads () {
59+ GenericApplicationContext context = new GenericApplicationContext ();
60+ context .registerBean ("taskExecutor" , ThreadPoolExecutorFactoryBean .class , TestThreadPoolExecutorFactoryBean ::new );
61+ context .refresh ();
62+ ThreadPoolExecutor threadPoolExecutor = context .getBean (ThreadPoolExecutor .class );
63+ verify (threadPoolExecutor , never ()).prestartAllCoreThreads ();
6164 }
6265
6366 @ Test
64- public void executorWithNoPreStartedThreads () throws Exception {
65- ConfigurableApplicationContext context = new AnnotationConfigApplicationContext (ExecutorConfigWithNoPreStartedThreads .class );
66- ThreadPoolExecutor executor = context .getBean ("childExecutor" , ThreadPoolExecutor .class );
67-
68- verify (executor , never ()).prestartAllCoreThreads ();
67+ void executorWithPrestartAllCoreThreads () {
68+ GenericApplicationContext context = new GenericApplicationContext ();
69+ context .registerBean ("taskExecutor" , ThreadPoolExecutorFactoryBean .class , () -> {
70+ TestThreadPoolExecutorFactoryBean factoryBean = new TestThreadPoolExecutorFactoryBean ();
71+ factoryBean .setPrestartAllCoreThreads (true );
72+ return factoryBean ;
73+ });
74+ context .refresh ();
75+ ThreadPoolExecutor threadPoolExecutor = context .getBean (ThreadPoolExecutor .class );
76+ verify (threadPoolExecutor ).prestartAllCoreThreads ();
6977 }
7078
7179 @ Configuration
@@ -78,37 +86,8 @@ ThreadPoolExecutorFactoryBean executor() {
7886
7987 }
8088
81- @ Configuration
82- public static class ExecutorConfigWithPreStartedThreads {
83-
84- @ Bean
85- public ThreadPoolExecutorFactoryBean executorChildFactory () {
86- ThreadPoolExecutorFactoryBeanMockingChild threadPoolExecutorFactoryBeanMockingChild = new ThreadPoolExecutorFactoryBeanMockingChild ();
87- threadPoolExecutorFactoryBeanMockingChild .setPrestartAllCoreThreads (true );
88- return threadPoolExecutorFactoryBeanMockingChild ;
89- }
90-
91- @ Bean
92- public ExecutorService childExecutor () {
93- return executorChildFactory ().getObject ();
94- }
95- }
89+ private static class TestThreadPoolExecutorFactoryBean extends ThreadPoolExecutorFactoryBean {
9690
97- @ Configuration
98- public static class ExecutorConfigWithNoPreStartedThreads {
99-
100- @ Bean
101- public ThreadPoolExecutorFactoryBean executorChildFactory () {
102- return new ThreadPoolExecutorFactoryBeanMockingChild ();
103- }
104-
105- @ Bean
106- public ExecutorService childExecutor () {
107- return executorChildFactory ().getObject ();
108- }
109- }
110-
111- private static class ThreadPoolExecutorFactoryBeanMockingChild extends ThreadPoolExecutorFactoryBean {
11291 @ Override
11392 protected ThreadPoolExecutor createExecutor (
11493 int corePoolSize , int maxPoolSize , int keepAliveSeconds , BlockingQueue <Runnable > queue ,
@@ -118,5 +97,4 @@ protected ThreadPoolExecutor createExecutor(
11897 }
11998 }
12099
121-
122100}
0 commit comments