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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.util.regex.Pattern;
23  
24  import org.apache.commons.lang3.AbstractLangTest;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Test class for {@code ConstantInitializer}.
30   */
31  class ConstantInitializerTest extends AbstractLangTest {
32  
33      /** Constant for the object managed by the initializer. */
34      private static final Integer VALUE = 42;
35  
36      /** The initializer to be tested. */
37      private ConstantInitializer<Integer> init;
38  
39      /**
40       * Helper method for testing equals() and hashCode().
41       *
42       * @param obj the object to compare with the test instance
43       * @param expected the expected result
44       */
45      private void checkEquals(final Object obj, final boolean expected) {
46          assertEquals(expected, init.equals(obj), "Wrong result of equals");
47          if (obj != null) {
48              assertEquals(expected, obj.equals(init), "Not symmetric");
49              if (expected) {
50                  assertEquals(init.hashCode(), obj.hashCode(), "Different hash codes");
51              }
52          }
53      }
54  
55      @BeforeEach
56      public void setUp() {
57          init = new ConstantInitializer<>(VALUE);
58      }
59  
60      /**
61       * Tests equals() if the expected result is false.
62       */
63      @Test
64      void testEqualsFalse() {
65          ConstantInitializer<Integer> init2 = new ConstantInitializer<>(
66                  null);
67          checkEquals(init2, false);
68          init2 = new ConstantInitializer<>(VALUE + 1);
69          checkEquals(init2, false);
70      }
71  
72      /**
73       * Tests equals() if the expected result is true.
74       */
75      @Test
76      void testEqualsTrue() {
77          checkEquals(init, true);
78          ConstantInitializer<Integer> init2 = new ConstantInitializer<>(
79                  Integer.valueOf(VALUE.intValue()));
80          checkEquals(init2, true);
81          init = new ConstantInitializer<>(null);
82          init2 = new ConstantInitializer<>(null);
83          checkEquals(init2, true);
84      }
85  
86      /**
87       * Tests equals() with objects of other classes.
88       */
89      @Test
90      void testEqualsWithOtherObjects() {
91          checkEquals(null, false);
92          checkEquals(this, false);
93          checkEquals(new ConstantInitializer<>("Test"), false);
94      }
95  
96      /**
97       * Tests whether get() returns the correct object.
98       *
99       * @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
100      */
101     @Test
102     void testGet() throws ConcurrentException {
103         assertEquals(VALUE, init.get(), "Wrong object");
104     }
105 
106     /**
107      * Tests whether the correct object is returned.
108      */
109     @Test
110     void testGetObject() {
111         assertEquals(VALUE, init.getObject(), "Wrong object");
112     }
113 
114     /**
115      * Tests a simple invocation of the isInitialized() method.
116      */
117     @Test
118     void testisInitialized() {
119         assertTrue(init.isInitialized(), "was not initialized before get()");
120         assertEquals(VALUE, init.getObject(), "Wrong object");
121         assertTrue(init.isInitialized(), "was not initialized after get()");
122     }
123 
124     /**
125      * Tests the string representation.
126      */
127     @Test
128     void testToString() {
129         final String s = init.toString();
130         final Pattern pattern = Pattern
131                 .compile("ConstantInitializer@-?\\d+ \\[ object = " + VALUE
132                         + " \\]");
133         assertTrue(pattern.matcher(s).matches(), "Wrong string: " + s);
134     }
135 
136     /**
137      * Tests the string representation if the managed object is null.
138      */
139     @Test
140     void testToStringNull() {
141         final String s = new ConstantInitializer<>(null).toString();
142         assertTrue(s.indexOf("object = null") > 0, "Object not found: " + s);
143     }
144 }