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.PostalCode;
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 PostalCodeTests
27 {
28
29
30
31 private static final String POSTCODE_28000 = "28000";
32
33
34
35
36 private static final String POSTCODE_30000 = "30000";
37
38
39
40
41 private static final String ILLEGAL_ARGUMENT = "Illegal argument exception expected";
42
43
44
45
46 private static final String POSTAL_CODE_NOT_AS_EXPECTED = "PostalCode not as expected";
47
48
49
50
51
52 PostalCodeTests()
53 {
54 super();
55 }
56
57
58
59
60
61
62
63 @ParameterizedTest
64 @ValueSource(strings = {PostalCodeTests.POSTCODE_28000, "123", "1234567-890", "AD123"})
65 void testPostalCodeCorrect(final String postalCode)
66 {
67 final PostalCode cleanBic = PostalCode.of(postalCode);
68 assertEquals(postalCode, cleanBic.stringValue(), PostalCodeTests.POSTAL_CODE_NOT_AS_EXPECTED);
69 }
70
71
72
73
74
75
76
77 @ParameterizedTest
78 @ValueSource(strings = {"12", "123456789012"})
79 void testPostalCodeLength(final String postalCode)
80 {
81 assertThrows(IllegalArgumentException.class, () ->
82 {
83 PostalCode.of(postalCode);
84 }, PostalCodeTests.ILLEGAL_ARGUMENT
85 );
86 }
87
88
89
90
91
92
93
94 @ParameterizedTest
95 @ValueSource(strings = {"123_45"})
96 void testPostalCodeWrong(final String postalCode)
97 {
98 assertThrows(IllegalArgumentException.class, () ->
99 {
100 PostalCode.of(postalCode);
101 }, PostalCodeTests.ILLEGAL_ARGUMENT
102 );
103 }
104
105
106
107
108
109 @Test
110 void testStringValue()
111 {
112 final PostalCode postalCode = PostalCode.of(PostalCodeTests.POSTCODE_28000);
113 assertEquals(PostalCodeTests.POSTCODE_28000, postalCode.stringValue(), PostalCodeTests.POSTAL_CODE_NOT_AS_EXPECTED);
114 }
115
116
117
118
119
120 @Test
121 void testHashCode()
122 {
123 final PostalCode postalCode1 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
124 final PostalCode postalCode2 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
125 final PostalCode postalCode3 = PostalCode.of(PostalCodeTests.POSTCODE_30000);
126 assertAll("testHashCode",
127 () -> assertEquals(postalCode1.hashCode(), postalCode2.hashCode(), "hashCodes are not equal"),
128 () -> assertNotEquals(postalCode1.hashCode(), postalCode3.hashCode(), "hashCodes are equal")
129 );
130 }
131
132
133
134
135
136 @Test
137 @SuppressWarnings("java:S5785")
138 void testEquals()
139 {
140 final PostalCode postalCode1 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
141 final PostalCode postalCode2 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
142 final PostalCode postalCode3 = PostalCode.of(PostalCodeTests.POSTCODE_30000);
143 final PostalCode postalCode4 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
144 assertAll("testEquals",
145 () -> assertTrue(postalCode1.equals(postalCode1), "postalCode11 is not equal"),
146 () -> assertTrue(postalCode1.equals(postalCode2), "postalCode12 are not equal"),
147 () -> assertTrue(postalCode2.equals(postalCode1), "postalCode21 are not equal"),
148 () -> assertTrue(postalCode2.equals(postalCode4), "postalCode24 are not equal"),
149 () -> assertTrue(postalCode1.equals(postalCode4), "postalCode14 are not equal"),
150 () -> assertFalse(postalCode1.equals(postalCode3), "postalCode13 are equal"),
151 () -> assertFalse(postalCode3.equals(postalCode1), "postalCode31 are equal"),
152 () -> assertFalse(postalCode1.equals(null), "postalCode10 is equal")
153 );
154 }
155
156
157
158
159
160 @Test
161 void testToString()
162 {
163 final PostalCode postalCode = PostalCode.of(PostalCodeTests.POSTCODE_28000);
164 assertEquals("PostalCode[postalCode=28000]", postalCode.toString(), "toString not equal");
165 }
166
167
168
169
170
171 @Test
172 @SuppressWarnings("java:S5785")
173 void testCompareTo()
174 {
175 final PostalCode bic1 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
176 final PostalCode bic2 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
177 final PostalCode bic3 = PostalCode.of(PostalCodeTests.POSTCODE_30000);
178 final PostalCode bic4 = PostalCode.of("80000");
179 final PostalCode bic5 = PostalCode.of(PostalCodeTests.POSTCODE_28000);
180 assertAll("testCompareTo",
181 () -> assertTrue(bic1.compareTo(bic2) == -bic2.compareTo(bic1), "reflexive1"),
182 () -> assertTrue(bic1.compareTo(bic3) == -bic3.compareTo(bic1), "reflexive2"),
183 () -> assertTrue((bic4.compareTo(bic3) > 0) && (bic3.compareTo(bic1) > 0) && (bic4.compareTo(bic1) > 0), "transitive1"),
184 () -> assertTrue((bic1.compareTo(bic2) == 0) && (Math.abs(bic1.compareTo(bic5)) == Math.abs(bic2.compareTo(bic5))), "sgn1"),
185 () -> assertTrue((bic1.compareTo(bic2) == 0) && bic1.equals(bic2), "equals")
186 );
187 }
188
189 }