1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31 public class ConstantInitializerTest extends AbstractLangTest {
32
33 private static final Integer VALUE = 42;
34
35
36 private ConstantInitializer<Integer> init;
37
38
39
40
41
42
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
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
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
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
97
98
99
100 @Test
101 public void testGet() throws ConcurrentException {
102 assertEquals(VALUE, init.get(), "Wrong object");
103 }
104
105
106
107
108 @Test
109 public void testGetObject() {
110 assertEquals(VALUE, init.getObject(), "Wrong object");
111 }
112
113
114
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
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
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 }