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.hamcrest.CoreMatchers.instanceOf;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertSame;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.junit.jupiter.api.Assertions.fail;
25
26 import java.io.IOException;
27 import java.lang.reflect.Field;
28 import java.util.concurrent.ExecutorService;
29
30 import org.apache.commons.lang3.function.FailableConsumer;
31 import org.apache.commons.lang3.function.FailableSupplier;
32 import org.junit.jupiter.api.Test;
33
34 public class BackgroundInitializerSupplierTest extends BackgroundInitializerTest {
35
36
37
38
39
40 protected static final class SupplierBackgroundInitializerTestImpl extends AbstractBackgroundInitializerTestImpl {
41
42 SupplierBackgroundInitializerTestImpl() {
43 super();
44 setSupplierAndCloser((CloseableCounter cc) -> cc.close());
45 }
46
47 SupplierBackgroundInitializerTestImpl(final ExecutorService exec) {
48 super(exec);
49 setSupplierAndCloser((CloseableCounter cc) -> cc.close());
50 }
51
52 SupplierBackgroundInitializerTestImpl(FailableConsumer<?, ?> consumer) {
53 super();
54 setSupplierAndCloser(consumer);
55 }
56
57 private void setSupplierAndCloser(FailableConsumer<?, ?> consumer) {
58 try {
59
60 FailableSupplier<?, ?> supplier = () -> initializeInternal();
61 Field initializer = AbstractConcurrentInitializer.class.getDeclaredField("initializer");
62 initializer.setAccessible(true);
63 initializer.set(this, supplier);
64
65 Field closer = AbstractConcurrentInitializer.class.getDeclaredField("closer");
66 closer.setAccessible(true);
67 closer.set(this, consumer);
68 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
69 fail();
70 }
71 }
72 }
73
74 protected AbstractBackgroundInitializerTestImpl getBackgroundInitializerTestImpl() {
75 return new SupplierBackgroundInitializerTestImpl();
76 }
77
78 protected SupplierBackgroundInitializerTestImpl getBackgroundInitializerTestImpl(final ExecutorService exec) {
79 return new SupplierBackgroundInitializerTestImpl(exec);
80 }
81
82
83
84
85
86
87 @Test
88 public void testClose() throws Exception {
89 final AbstractBackgroundInitializerTestImpl init = getBackgroundInitializerTestImpl();
90 assertFalse(init.getCloseableCounter().isClosed(), "closed without close() call");
91 init.close();
92 assertFalse(init.getCloseableCounter().isClosed(), "closed() succeeded before start()");
93 init.start();
94 init.get();
95 assertFalse(init.getCloseableCounter().isClosed(), "closed() succeeded after start() but before close()");
96 init.close();
97 assertTrue(init.getCloseableCounter().isClosed(), "closed() did not succeed");
98 }
99
100
101
102
103
104
105 @Test
106 public void testCloseWithCheckedException() throws Exception {
107
108 final IOException ioException = new IOException();
109 final FailableConsumer<?, ?> IOExceptionConsumer = (CloseableCounter cc) -> {
110 throw ioException;
111 };
112
113 final AbstractBackgroundInitializerTestImpl init = new SupplierBackgroundInitializerTestImpl(IOExceptionConsumer);
114 init.start();
115 init.get();
116 try {
117 init.close();
118 fail();
119 } catch (Exception e) {
120 assertThat(e, instanceOf(ConcurrentException.class));
121 assertSame(ioException, e.getCause());
122 }
123 }
124
125
126
127
128
129
130 @Test
131 public void testCloseWithRuntimeException() throws Exception {
132
133 final NullPointerException npe = new NullPointerException();
134 final FailableConsumer<?, ?> NullPointerExceptionConsumer = (CloseableCounter cc) -> {
135 throw npe;
136 };
137
138 final AbstractBackgroundInitializerTestImpl init = new SupplierBackgroundInitializerTestImpl(NullPointerExceptionConsumer);
139 init.start();
140 init.get();
141 try {
142 init.close();
143 fail();
144 } catch (Exception e) {
145 assertSame(npe, e);
146 }
147 }
148 }