1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package waffle.jaas;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 class UserPrincipalTest {
43
44
45 private UserPrincipal userPrincipal;
46
47
48
49
50 @Test
51 void equals_otherObject() {
52 Assertions.assertNotEquals("", this.userPrincipal);
53 }
54
55
56
57
58 @Test
59 void equals_sameObject() {
60 Assertions.assertEquals(this.userPrincipal, this.userPrincipal);
61 }
62
63
64
65
66 @BeforeEach
67 void setUp() {
68 this.userPrincipal = new UserPrincipal("localhost\\Administrator");
69 }
70
71
72
73
74 @Test
75 void testEquals_Symmetric() {
76 final UserPrincipal x = new UserPrincipal("localhost\\Administrator");
77 final UserPrincipal y = new UserPrincipal("localhost\\Administrator");
78 Assertions.assertEquals(x, y);
79 Assertions.assertEquals(x.hashCode(), y.hashCode());
80 }
81
82
83
84
85
86
87
88
89
90 @Test
91 void testIsSerializable() throws IOException, ClassNotFoundException {
92
93 final ByteArrayOutputStream out = new ByteArrayOutputStream();
94 try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
95 oos.writeObject(this.userPrincipal);
96 }
97 assertThat(out.toByteArray()).isNotEmpty();
98
99 final InputStream in = new ByteArrayInputStream(out.toByteArray());
100 final ObjectInputStream ois = new ObjectInputStream(in);
101 final UserPrincipal copy = (UserPrincipal) ois.readObject();
102
103 Assertions.assertEquals(this.userPrincipal, copy);
104 Assertions.assertEquals(this.userPrincipal.getName(), copy.getName());
105 }
106
107 }