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.assertNull;
21  
22  import java.util.concurrent.atomic.AtomicBoolean;
23  import java.util.concurrent.atomic.AtomicInteger;
24  
25  import org.apache.commons.lang3.function.FailableConsumer;
26  import org.apache.commons.lang3.function.FailableSupplier;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Test class for {@code AtomicSafeInitializer} which also serves as a simple example.
32   */
33  public class AtomicSafeInitializerSupplierTest extends AbstractConcurrentInitializerCloseAndExceptionsTest {
34  
35      /** An initCounter used in testing. Reset before each test */
36      private AtomicInteger initCounter = new AtomicInteger();
37  
38      /**
39       * Creates the initializer to be tested.
40       *
41       * @return the {@code AtomicSafeInitializer} under test
42       */
43      @Override
44      protected ConcurrentInitializer<Object> createInitializer() {
45          return AtomicSafeInitializer.<Object>builder().setInitializer(this::incAndMakeObject).get();
46      }
47  
48      @Override
49      protected ConcurrentInitializer<CloseableObject> createInitializerThatThrowsException(
50              final FailableSupplier<CloseableObject, ? extends Exception> supplier,
51              final FailableConsumer<CloseableObject, ? extends Exception> closer) {
52          return AtomicSafeInitializer.<CloseableObject>builder().setInitializer(supplier).setCloser(closer).get();
53      }
54  
55      /** A supplier method used in testing */
56      private Object incAndMakeObject() {
57          initCounter.incrementAndGet();
58          return new Object();
59      }
60  
61      @BeforeEach
62      public void setUp() {
63          initCounter = new AtomicInteger();
64      }
65  
66      @Test
67      public void testGetThatReturnsNullFirstTime() throws ConcurrentException {
68          final AtomicSafeInitializer<Object> initializer = new AtomicSafeInitializer<Object>() {
69              final AtomicBoolean firstRun = new AtomicBoolean(true);
70  
71              @Override
72              protected Object initialize() {
73                  if (firstRun.getAndSet(false)) {
74                      return null;
75                  } else {
76                      return new Object();
77                  }
78              }
79          };
80  
81          assertNull(initializer.get());
82          assertNull(initializer.get());
83      }
84  
85      /**
86       * Tests that initialize() is called only once.
87       *
88       * @throws org.apache.commons.lang3.concurrent.ConcurrentException because {@link #testGetConcurrent()} may throw it
89       * @throws InterruptedException because {@link #testGetConcurrent()} may throw it
90       */
91      @Test
92      public void testNumberOfInitializeInvocations() throws ConcurrentException, InterruptedException {
93          testGetConcurrent();
94          assertEquals(1, initCounter.get(), "Wrong number of invocations");
95      }
96  }