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    *      http://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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
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  public class CallableBackgroundInitializerTest extends AbstractLangTest {
34      /**
35       * A test Callable implementation for checking the initializer's
36       * implementation of the initialize() method.
37       */
38      private static final class TestCallable implements Callable<Integer> {
39          /** A counter for the number of call() invocations. */
40          int callCount;
41  
42          /**
43           * Records this invocation and returns the test result.
44           */
45          @Override
46          public Integer call() {
47              callCount++;
48              return RESULT;
49          }
50      }
51  
52      /** Constant for the result of the call() invocation. */
53      private static final Integer RESULT = Integer.valueOf(42);
54  
55      /**
56       * Tests whether the executor service is correctly passed to the super
57       * class.
58       */
59      @Test
60      public void testInitExecutor() throws InterruptedException {
61          final ExecutorService exec = Executors.newSingleThreadExecutor();
62          final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<>(
63                  new TestCallable(), exec);
64          assertEquals(exec, init.getExternalExecutor(), "Executor not set");
65          exec.shutdown();
66          exec.awaitTermination(1, TimeUnit.SECONDS);
67      }
68  
69      /**
70       * Tries to pass a null Callable to the constructor that takes an executor.
71       * This should cause an exception.
72       */
73      @Test
74      public void testInitExecutorNullCallable() throws InterruptedException {
75          final ExecutorService exec = Executors.newSingleThreadExecutor();
76          try {
77              assertThrows(NullPointerException.class, () -> new CallableBackgroundInitializer<Integer>(null, exec));
78          } finally {
79              exec.shutdown();
80              exec.awaitTermination(1, TimeUnit.SECONDS);
81          }
82  
83      }
84  
85      /**
86       * Tests the implementation of initialize().
87       *
88       * @throws Exception so we don't have to catch it
89       */
90      @Test
91      public void testInitialize() throws Exception {
92          final TestCallable call = new TestCallable();
93          final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<>(
94                  call);
95          assertEquals(RESULT, init.initialize(), "Wrong result");
96          assertEquals(1, call.callCount, "Wrong number of invocations");
97      }
98  
99      /**
100      * Tries to create an instance without a Callable. This should cause an
101      * exception.
102      */
103     @Test()
104     public void testInitNullCallable() {
105         assertThrows(NullPointerException.class, () -> new CallableBackgroundInitializer<>(null));
106     }
107 }