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 class ConstantInitializerTest extends AbstractLangTest {
32
33
34 private static final Integer VALUE = 42;
35
36
37 private ConstantInitializer<Integer> init;
38
39
40
41
42
43
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
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
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
88
89 @Test
90 void testEqualsWithOtherObjects() {
91 checkEquals(null, false);
92 checkEquals(this, false);
93 checkEquals(new ConstantInitializer<>("Test"), false);
94 }
95
96
97
98
99
100
101 @Test
102 void testGet() throws ConcurrentException {
103 assertEquals(VALUE, init.get(), "Wrong object");
104 }
105
106
107
108
109 @Test
110 void testGetObject() {
111 assertEquals(VALUE, init.getObject(), "Wrong object");
112 }
113
114
115
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
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
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 }