View Javadoc
1   /*
2    * MIT License
3    *
4    * Copyright (c) 2010-2022 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 GroupPrincipalTest.
41   *
42   * @author rockchip[dot]tv[at]gmail[dot]com
43   */
44  class GroupPrincipalTest {
45  
46      /** The group principal. */
47      private GroupPrincipal groupPrincipal;
48  
49      /**
50       * Equals_other object.
51       */
52      @Test
53      void equals_otherObject() {
54          Assertions.assertNotEquals("", this.groupPrincipal);
55      }
56  
57      /**
58       * Equals_same object.
59       */
60      @Test
61      void equals_sameObject() {
62          Assertions.assertEquals(this.groupPrincipal, this.groupPrincipal);
63      }
64  
65      /**
66       * Sets the up.
67       */
68      @BeforeEach
69      void setUp() {
70          this.groupPrincipal = new GroupPrincipal("localhost\\Administrator");
71      }
72  
73      /**
74       * Test equals_ symmetric.
75       */
76      @Test
77      void testEquals_Symmetric() {
78          final GroupPrincipal x = new GroupPrincipal("localhost\\Administrator");
79          final GroupPrincipal y = new GroupPrincipal("localhost\\Administrator");
80          Assertions.assertEquals(x, y);
81          Assertions.assertEquals(x.hashCode(), y.hashCode());
82      }
83  
84      /**
85       * Test is serializable.
86       *
87       * @throws IOException
88       *             Signals that an I/O exception has occurred.
89       * @throws ClassNotFoundException
90       *             the class not found exception
91       */
92      @Test
93      void testIsSerializable() throws IOException, ClassNotFoundException {
94          // serialize
95          final ByteArrayOutputStream out = new ByteArrayOutputStream();
96          try (final ObjectOutputStream oos = new ObjectOutputStream(out)) {
97              oos.writeObject(this.groupPrincipal);
98          }
99          assertThat(out.toByteArray()).isNotEmpty();
100         // deserialize
101         final InputStream in = new ByteArrayInputStream(out.toByteArray());
102         final ObjectInputStream ois = new ObjectInputStream(in);
103         final GroupPrincipal copy = (GroupPrincipal) ois.readObject();
104         // test
105         Assertions.assertEquals(this.groupPrincipal, copy);
106         Assertions.assertEquals(this.groupPrincipal.getName(), copy.getName());
107     }
108 
109 }