View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test class for {@code CallableBackgroundInitializer}
32   */
33  class CallableBackgroundInitializerTest extends AbstractLangTest {
34  
35      /**
36       * A test Callable implementation for checking the initializer's
37       * implementation of the initialize() method.
38       */
39      private static final class TestCallable implements Callable<Integer> {
40  
41          /** A counter for the number of call() invocations. */
42          int callCount;
43  
44          /**
45           * Records this invocation and returns the test result.
46           */
47          @Override
48          public Integer call() {
49              callCount++;
50              return RESULT;
51          }
52      }
53  
54      /** Constant for the result of the call() invocation. */
55      private static final Integer RESULT = Integer.valueOf(42);
56  
57      /**
58       * Tests whether the executor service is correctly passed to the super
59       * class.
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       * Tries to pass a null Callable to the constructor that takes an executor.
73       * This should cause an exception.
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       * Tests the implementation of initialize().
89       *
90       * @throws Exception so we don't have to catch it
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      * Tries to create an instance without a Callable. This should cause an
103      * exception.
104      */
105     @Test()
106     void testInitNullCallable() {
107         assertNullPointerException(() -> new CallableBackgroundInitializer<>(null));
108     }
109 }