View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6    */
7   package waffle.apache;
8   
9   import static org.assertj.core.api.Assertions.assertThat;
10  
11  import java.io.ByteArrayInputStream;
12  import java.io.ByteArrayOutputStream;
13  import java.io.IOException;
14  import java.io.InputStream;
15  import java.io.ObjectInputStream;
16  import java.io.ObjectOutputStream;
17  
18  import org.junit.jupiter.api.Assertions;
19  import org.junit.jupiter.api.BeforeEach;
20  import org.junit.jupiter.api.Test;
21  
22  import waffle.mock.MockWindowsAccount;
23  import waffle.windows.auth.WindowsAccount;
24  
25  /**
26   * Windows Account Test.
27   */
28  class WindowsAccountTest {
29  
30      /** The mock windows account. */
31      private final MockWindowsAccount mockWindowsAccount = new MockWindowsAccount("localhost\\Administrator");
32  
33      /** The windows account. */
34      private WindowsAccount windowsAccount;
35  
36      /**
37       * Sets the up.
38       */
39      @BeforeEach
40      void setUp() {
41          this.windowsAccount = new WindowsAccount(this.mockWindowsAccount);
42      }
43  
44      /**
45       * Test equals.
46       */
47      @Test
48      void testEquals() {
49          Assertions.assertEquals(this.windowsAccount, new WindowsAccount(this.mockWindowsAccount));
50          final MockWindowsAccount mockWindowsAccount2 = new MockWindowsAccount("localhost\\Administrator2");
51          Assertions.assertNotEquals(this.windowsAccount, new WindowsAccount(mockWindowsAccount2));
52      }
53  
54      /**
55       * Test is serializable.
56       *
57       * @throws IOException
58       *             Signals that an I/O exception has occurred.
59       * @throws ClassNotFoundException
60       *             the class not found exception
61       */
62      @Test
63      void testIsSerializable() throws IOException, ClassNotFoundException {
64          // serialize
65          final ByteArrayOutputStream out = new ByteArrayOutputStream();
66          try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
67              oos.writeObject(this.windowsAccount);
68          }
69          assertThat(out.toByteArray()).isNotEmpty();
70          // deserialize
71          final InputStream in = new ByteArrayInputStream(out.toByteArray());
72          final ObjectInputStream ois = new ObjectInputStream(in);
73          final WindowsAccount copy = (WindowsAccount) ois.readObject();
74          // test
75          Assertions.assertEquals(this.windowsAccount, copy);
76          Assertions.assertEquals(this.windowsAccount.getDomain(), copy.getDomain());
77          Assertions.assertEquals(this.windowsAccount.getFqn(), copy.getFqn());
78          Assertions.assertEquals(this.windowsAccount.getName(), copy.getName());
79          Assertions.assertEquals(this.windowsAccount.getSidString(), copy.getSidString());
80      }
81  
82      /**
83       * Test properties.
84       */
85      @Test
86      void testProperties() {
87          Assertions.assertEquals("localhost", this.windowsAccount.getDomain());
88          Assertions.assertEquals("localhost\\Administrator", this.windowsAccount.getFqn());
89          Assertions.assertEquals("Administrator", this.windowsAccount.getName());
90          Assertions.assertTrue(this.windowsAccount.getSidString().startsWith("S-"));
91      }
92  
93  }