1
2
3
4 package de.powerstat.validation.values.test;
5
6
7 import static org.junit.jupiter.api.Assertions.assertAll;
8 import static org.junit.jupiter.api.Assertions.assertEquals;
9 import static org.junit.jupiter.api.Assertions.assertFalse;
10 import static org.junit.jupiter.api.Assertions.assertNotEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
13
14 import org.junit.jupiter.api.Test;
15 import org.junit.jupiter.params.ParameterizedTest;
16 import org.junit.jupiter.params.provider.ValueSource;
17
18 import de.powerstat.validation.values.Neighbourhood;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20
21
22
23
24
25 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR"})
26 final class NeighbourhoodTests
27 {
28
29
30
31 private static final String UNKNOWN = "Unknown";
32
33
34
35
36 private static final String UNKNOWN2 = "Unknown2";
37
38
39
40
41 private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected";
42
43
44
45
46 private static final String NEIGHBOURHOOD_NOT_AS_EXPECTED = "Neighbourhood not as expected";
47
48
49
50
51
52 NeighbourhoodTests()
53 {
54 super();
55 }
56
57
58
59
60
61
62
63 @ParameterizedTest
64 @ValueSource(strings = {"A", "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl"})
65 void testNeighbourhoodCorrect(final String neighbourhood)
66 {
67 final Neighbourhood cleanNeighbourhood = Neighbourhood.of(neighbourhood);
68 assertEquals(neighbourhood, cleanNeighbourhood.stringValue(), NeighbourhoodTests.NEIGHBOURHOOD_NOT_AS_EXPECTED);
69 }
70
71
72
73
74
75
76
77 @ParameterizedTest
78 @ValueSource(strings = {"", "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm"})
79 void testNeighbourhoodLength(final String neighbourhood)
80 {
81 assertThrows(IllegalArgumentException.class, () ->
82 {
83 Neighbourhood.of(neighbourhood);
84 }, NeighbourhoodTests.ILLEGAL_ARGUMENT
85 );
86 }
87
88
89
90
91
92
93
94 @ParameterizedTest
95 @ValueSource(strings = {"abc_def"})
96 void testNeighbourhoodWrong(final String neighbourhood)
97 {
98 assertThrows(IllegalArgumentException.class, () ->
99 {
100 Neighbourhood.of(neighbourhood);
101 }, NeighbourhoodTests.ILLEGAL_ARGUMENT
102 );
103 }
104
105
106
107
108
109 @Test
110 void testStringValue()
111 {
112 final Neighbourhood neighbourhood = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
113 assertEquals(NeighbourhoodTests.UNKNOWN, neighbourhood.stringValue(), NeighbourhoodTests.NEIGHBOURHOOD_NOT_AS_EXPECTED);
114 }
115
116
117
118
119
120 @Test
121 void testHashCode()
122 {
123 final Neighbourhood neighbourhood1 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
124 final Neighbourhood neighbourhood2 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
125 final Neighbourhood neighbourhood3 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN2);
126 assertAll("testHashCode",
127 () -> assertEquals(neighbourhood1.hashCode(), neighbourhood2.hashCode(), "hashCodes are not equal"),
128 () -> assertNotEquals(neighbourhood1.hashCode(), neighbourhood3.hashCode(), "hashCodes are equal")
129 );
130 }
131
132
133
134
135
136 @Test
137 @SuppressWarnings("java:S5785")
138 void testEquals()
139 {
140 final Neighbourhood neighbourhood1 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
141 final Neighbourhood neighbourhood2 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
142 final Neighbourhood neighbourhood3 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN2);
143 final Neighbourhood neighbourhood4 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
144 assertAll("testEquals",
145 () -> assertTrue(neighbourhood1.equals(neighbourhood1), "neighbourhood11 is not equal"),
146 () -> assertTrue(neighbourhood1.equals(neighbourhood2), "neighbourhood12 are not equal"),
147 () -> assertTrue(neighbourhood2.equals(neighbourhood1), "neighbourhood21 are not equal"),
148 () -> assertTrue(neighbourhood2.equals(neighbourhood4), "neighbourhood24 are not equal"),
149 () -> assertTrue(neighbourhood1.equals(neighbourhood4), "neighbourhood14 are not equal"),
150 () -> assertFalse(neighbourhood1.equals(neighbourhood3), "neighbourhood13 are equal"),
151 () -> assertFalse(neighbourhood3.equals(neighbourhood1), "neighbourhood31 are equal"),
152 () -> assertFalse(neighbourhood1.equals(null), "neighbourhood10 is equal")
153 );
154 }
155
156
157
158
159
160 @Test
161 void testToString()
162 {
163 final Neighbourhood neighbourhood = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
164 assertEquals("Neighbourhood[neighbourhood=Unknown]", neighbourhood.toString(), "toString not equal");
165 }
166
167
168
169
170
171 @Test
172 @SuppressWarnings("java:S5785")
173 void testCompareTo()
174 {
175 final Neighbourhood neighbourhood1 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
176 final Neighbourhood neighbourhood2 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
177 final Neighbourhood neighbourhood3 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN2);
178 final Neighbourhood neighbourhood4 = Neighbourhood.of("Unknown3r");
179 final Neighbourhood neighbourhood5 = Neighbourhood.of(NeighbourhoodTests.UNKNOWN);
180 assertAll("testCompareTo",
181 () -> assertTrue(neighbourhood1.compareTo(neighbourhood2) == -neighbourhood2.compareTo(neighbourhood1), "reflexive1"),
182 () -> assertTrue(neighbourhood1.compareTo(neighbourhood3) == -neighbourhood3.compareTo(neighbourhood1), "reflexive2"),
183 () -> assertTrue((neighbourhood4.compareTo(neighbourhood3) > 0) && (neighbourhood3.compareTo(neighbourhood1) > 0) && (neighbourhood4.compareTo(neighbourhood1) > 0), "transitive1"),
184 () -> assertTrue((neighbourhood1.compareTo(neighbourhood2) == 0) && (Math.abs(neighbourhood1.compareTo(neighbourhood5)) == Math.abs(neighbourhood2.compareTo(neighbourhood5))), "sgn1"),
185 () -> assertTrue((neighbourhood1.compareTo(neighbourhood2) == 0) && neighbourhood1.equals(neighbourhood2), "equals")
186 );
187 }
188
189 }