View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2024 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
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   * The Class RolePrincipalTest.
41   */
42  class RolePrincipalTest {
43  
44      /** The role principal. */
45      private RolePrincipal rolePrincipal;
46  
47      /**
48       * Equals_other object.
49       */
50      @Test
51      void equals_otherObject() {
52          Assertions.assertNotEquals("", this.rolePrincipal);
53      }
54  
55      /**
56       * Equals_same object.
57       */
58      @Test
59      void equals_sameObject() {
60          Assertions.assertEquals(this.rolePrincipal, this.rolePrincipal);
61      }
62  
63      /**
64       * Sets the up.
65       */
66      @BeforeEach
67      void setUp() {
68          this.rolePrincipal = new RolePrincipal("localhost\\Administrator");
69      }
70  
71      /**
72       * Test equals_ symmetric.
73       */
74      @Test
75      void testEquals_Symmetric() {
76          final RolePrincipal x = new RolePrincipal("localhost\\Administrator");
77          final RolePrincipal y = new RolePrincipal("localhost\\Administrator");
78          Assertions.assertEquals(x, y);
79          Assertions.assertEquals(x.hashCode(), y.hashCode());
80      }
81  
82      /**
83       * Test is serializable.
84       *
85       * @throws IOException
86       *             Signals that an I/O exception has occurred.
87       * @throws ClassNotFoundException
88       *             the class not found exception
89       */
90      @Test
91      void testIsSerializable() throws IOException, ClassNotFoundException {
92          // serialize
93          final ByteArrayOutputStream out = new ByteArrayOutputStream();
94          try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
95              oos.writeObject(this.rolePrincipal);
96          }
97          assertThat(out.toByteArray()).isNotEmpty();
98          // deserialize
99          final InputStream in = new ByteArrayInputStream(out.toByteArray());
100         final ObjectInputStream ois = new ObjectInputStream(in);
101         final RolePrincipal copy = (RolePrincipal) ois.readObject();
102         // test
103         Assertions.assertEquals(this.rolePrincipal, copy);
104         Assertions.assertEquals(this.rolePrincipal.getName(), copy.getName());
105     }
106 
107 }