1
2
3
4 package de.powerstat.validation.values.test;
5
6 import static org.junit.jupiter.api.Assertions.assertAll;
7 import static org.junit.jupiter.api.Assertions.assertEquals;
8 import static org.junit.jupiter.api.Assertions.assertFalse;
9 import static org.junit.jupiter.api.Assertions.assertNotEquals;
10 import static org.junit.jupiter.api.Assertions.assertThrows;
11 import static org.junit.jupiter.api.Assertions.assertTrue;
12
13 import org.junit.jupiter.api.Test;
14 import org.junit.jupiter.params.ParameterizedTest;
15 import org.junit.jupiter.params.provider.ValueSource;
16
17 import de.powerstat.validation.values.Percent;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19
20
21
22
23 @SuppressFBWarnings({"EC_NULL_ARG", "RV_NEGATING_RESULT_OF_COMPARETO", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT", "SPP_USE_ZERO_WITH_COMPARATOR", "PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS"})
24 final class PercentTests
25 {
26
27
28
29 PercentTests()
30 {
31 super();
32 }
33
34
35
36
37
38 @Test
39 void testIsPercent()
40 {
41 final Percent percent = Percent.of(50);
42 assertEquals(50, percent.intValue(), "Percent should be 50");
43 }
44
45
46
47
48
49
50
51 @ParameterizedTest
52 @ValueSource(ints = {101, -1})
53 void testIsPercentFalse(final int percent)
54 {
55 assertThrows(IndexOutOfBoundsException.class, () ->
56 {
57 Percent.of(percent);
58 }, "Index out of bounds exception expected"
59 );
60 }
61
62
63
64
65
66 @Test
67 void testFactory1()
68 {
69 assertEquals(50, Percent.of("50").intValue(), "Percent should be 50");
70 }
71
72
73
74
75
76 @Test
77 void testIntValue()
78 {
79 final Percent percent = Percent.of(50);
80 assertEquals(50, percent.intValue(), "Percent should be 50");
81 }
82
83
84
85
86
87 @Test
88 void testStringValue()
89 {
90 final Percent percent = Percent.of(50);
91 assertEquals("50", percent.stringValue(), "Percent should be 50");
92 }
93
94
95
96
97
98 @Test
99 void testHashCode()
100 {
101 final Percent percent1 = Percent.of(50);
102 final Percent percent2 = Percent.of(50);
103 final Percent percent3 = Percent.of(51);
104 assertAll("testHashCode",
105 () -> assertEquals(percent1.hashCode(), percent2.hashCode(), "hashCodes are not equal"),
106 () -> assertNotEquals(percent1.hashCode(), percent3.hashCode(), "hashCodes are equal")
107 );
108 }
109
110
111
112
113
114 @Test
115 @SuppressWarnings("java:S5785")
116 void testEquals()
117 {
118 final Percent percent1 = Percent.of(50);
119 final Percent percent2 = Percent.of(50);
120 final Percent percent3 = Percent.of(51);
121 final Percent percent4 = Percent.of(50);
122 assertAll("testEquals",
123 () -> assertTrue(percent1.equals(percent1), "percent11 is not equal"),
124 () -> assertTrue(percent1.equals(percent2), "percent12 are not equal"),
125 () -> assertTrue(percent2.equals(percent1), "percent21 are not equal"),
126 () -> assertTrue(percent2.equals(percent4), "percent24 are not equal"),
127 () -> assertTrue(percent1.equals(percent4), "percent14 are not equal"),
128 () -> assertFalse(percent1.equals(percent3), "percent13 are equal"),
129 () -> assertFalse(percent3.equals(percent1), "percent31 are equal"),
130 () -> assertFalse(percent1.equals(null), "percent10 is equal")
131 );
132 }
133
134
135
136
137
138 @Test
139 void testToString()
140 {
141 final Percent percent = Percent.of(50);
142 assertEquals("Percent[percent=50]", percent.toString(), "toString not equal");
143 }
144
145
146
147
148
149 @Test
150 @SuppressWarnings("java:S5785")
151 void testCompareTo()
152 {
153 final Percent percent1 = Percent.of(50);
154 final Percent percent2 = Percent.of(50);
155 final Percent percent3 = Percent.of(51);
156 final Percent percent4 = Percent.of(52);
157 final Percent percent5 = Percent.of(50);
158 assertAll("testCompareTo",
159 () -> assertTrue(percent1.compareTo(percent2) == -percent2.compareTo(percent1), "reflexive1"),
160 () -> assertTrue(percent1.compareTo(percent3) == -percent3.compareTo(percent1), "reflexive2"),
161 () -> assertTrue((percent4.compareTo(percent3) > 0) && (percent3.compareTo(percent1) > 0) && (percent4.compareTo(percent1) > 0), "transitive1"),
162 () -> assertTrue((percent1.compareTo(percent2) == 0) && (Math.abs(percent1.compareTo(percent5)) == Math.abs(percent2.compareTo(percent5))), "sgn1"),
163 () -> assertTrue((percent1.compareTo(percent2) == 0) && percent1.equals(percent2), "equals")
164 );
165 }
166
167 }