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