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.assertTrue;
12
13 import org.junit.jupiter.api.Test;
14
15 import de.powerstat.validation.values.UUID;
16
17
18
19
20
21 public class UUIDTests
22 {
23
24
25
26 UUIDTests()
27 {
28 super();
29 }
30
31
32
33
34
35 @Test
36 void testFactory1()
37 {
38 assertEquals(36, UUID.of().stringValue().length(), "Result not as expected");
39 }
40
41
42
43
44
45 @Test
46 void testFactory2()
47 {
48 assertEquals("a5409f2d-983d-438c-bfdd-308feff7fb1f", UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f").stringValue(), "Result not as expected");
49 }
50
51
52
53
54
55 @Test
56 void testHashCode()
57 {
58 final UUID uuid1 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
59 final UUID uuid2 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
60 final UUID uuid3 = UUID.of("67803e53-28f7-42d1-910f-b01dd3fe2d48");
61 assertAll("testHashCode",
62 () -> assertEquals(uuid1.hashCode(), uuid2.hashCode(), "hashCodes are not equal"),
63 () -> assertNotEquals(uuid1.hashCode(), uuid3.hashCode(), "hashCodes are equal")
64 );
65 }
66
67
68
69
70
71 @Test
72 @SuppressWarnings("java:S5785")
73 void testEquals()
74 {
75 final UUID uuid1 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
76 final UUID uuid2 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
77 final UUID uuid3 = UUID.of("67803e53-28f7-42d1-910f-b01dd3fe2d48");
78 final UUID uuid4 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
79 assertAll("testEquals",
80 () -> assertTrue(uuid1.equals(uuid1), "uuid11 is not equal"),
81 () -> assertTrue(uuid1.equals(uuid2), "uuid12 are not equal"),
82 () -> assertTrue(uuid2.equals(uuid1), "uuid21 are not equal"),
83 () -> assertTrue(uuid2.equals(uuid4), "uuid24 are not equal"),
84 () -> assertTrue(uuid1.equals(uuid4), "uuid14 are not equal"),
85 () -> assertFalse(uuid1.equals(uuid3), "uuid13 are equal"),
86 () -> assertFalse(uuid3.equals(uuid1), "uuid31 are equal"),
87 () -> assertFalse(uuid1.equals(null), "uuid10 is equal")
88 );
89 }
90
91
92
93
94
95 @Test
96 void testToString()
97 {
98 final UUID uuid = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
99 assertEquals("UUID[uuid=a5409f2d-983d-438c-bfdd-308feff7fb1f]", uuid.toString(), "toString not equal");
100 }
101
102
103
104
105
106 @Test
107 @SuppressWarnings("java:S5785")
108 void testCompareTo()
109 {
110 final UUID uuid1 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
111 final UUID uuid2 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
112 final UUID uuid3 = UUID.of("67803e53-28f7-42d1-910f-b01dd3fe2d48");
113 final UUID uuid4 = UUID.of("758b3027-cecd-4aae-b76b-543b8439db10");
114 final UUID uuid5 = UUID.of("a5409f2d-983d-438c-bfdd-308feff7fb1f");
115 assertAll("testCompareTo",
116 () -> assertTrue(uuid1.compareTo(uuid2) == -uuid2.compareTo(uuid1), "reflexive1"),
117 () -> assertTrue(uuid1.compareTo(uuid3) == -uuid3.compareTo(uuid1), "reflexive2"),
118 () -> assertTrue((uuid4.compareTo(uuid3) > 0) && (uuid3.compareTo(uuid1) > 0) && (uuid4.compareTo(uuid1) > 0), "transitive1"),
119 () -> assertTrue((uuid1.compareTo(uuid2) == 0) && (Math.abs(uuid1.compareTo(uuid5)) == Math.abs(uuid2.compareTo(uuid5))), "sgn1"),
120 () -> assertTrue((uuid1.compareTo(uuid2) == 0) && uuid1.equals(uuid2), "equals")
121 );
122 }
123
124 }