1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.concurrent;
18
19 import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.util.concurrent.Callable;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.TimeUnit;
26
27 import org.apache.commons.lang3.AbstractLangTest;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class CallableBackgroundInitializerTest extends AbstractLangTest {
34
35
36
37
38
39 private static final class TestCallable implements Callable<Integer> {
40
41
42 int callCount;
43
44
45
46
47 @Override
48 public Integer call() {
49 callCount++;
50 return RESULT;
51 }
52 }
53
54
55 private static final Integer RESULT = Integer.valueOf(42);
56
57
58
59
60
61 @Test
62 void testInitExecutor() throws InterruptedException {
63 final ExecutorService exec = Executors.newSingleThreadExecutor();
64 final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<>(
65 new TestCallable(), exec);
66 assertEquals(exec, init.getExternalExecutor(), "Executor not set");
67 exec.shutdown();
68 exec.awaitTermination(1, TimeUnit.SECONDS);
69 }
70
71
72
73
74
75 @Test
76 void testInitExecutorNullCallable() throws InterruptedException {
77 final ExecutorService exec = Executors.newSingleThreadExecutor();
78 try {
79 assertNullPointerException(() -> new CallableBackgroundInitializer<Integer>(null, exec));
80 } finally {
81 exec.shutdown();
82 exec.awaitTermination(1, TimeUnit.SECONDS);
83 }
84
85 }
86
87
88
89
90
91
92 @Test
93 void testInitialize() throws Exception {
94 final TestCallable call = new TestCallable();
95 final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<>(
96 call);
97 assertEquals(RESULT, init.initialize(), "Wrong result");
98 assertEquals(1, call.callCount, "Wrong number of invocations");
99 }
100
101
102
103
104
105 @Test()
106 void testInitNullCallable() {
107 assertNullPointerException(() -> new CallableBackgroundInitializer<>(null));
108 }
109 }