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.servlet;
8   
9   import javax.servlet.http.HttpSessionBindingEvent;
10  
11  import mockit.Expectations;
12  import mockit.Mocked;
13  
14  import org.junit.jupiter.api.Assertions;
15  import org.junit.jupiter.api.Test;
16  
17  import waffle.windows.auth.IWindowsAccount;
18  import waffle.windows.auth.IWindowsIdentity;
19  import waffle.windows.auth.PrincipalFormat;
20  
21  /**
22   * Tests for {@link AutoDisposableWindowsPrincipal}.
23   */
24  class AutoDisposableWindowsPrincipalTest {
25  
26      /** The Constant TEST_FQN. */
27      private static final String TEST_FQN = "DOMAIN\\testuser";
28  
29      /** The windows identity. */
30      @Mocked
31      private IWindowsIdentity windowsIdentity;
32  
33      /** The session binding event. */
34      @Mocked
35      private HttpSessionBindingEvent sessionBindingEvent;
36  
37      /**
38       * Test constructor with identity only.
39       */
40      @Test
41      void testConstructorWithIdentityOnly() {
42          Assertions.assertNotNull(new Expectations() {
43              {
44                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getFqn();
45                  this.result = AutoDisposableWindowsPrincipalTest.TEST_FQN;
46                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getGroups();
47                  this.result = new IWindowsAccount[0];
48              }
49          });
50          final AutoDisposableWindowsPrincipal principal = new AutoDisposableWindowsPrincipal(this.windowsIdentity);
51          Assertions.assertEquals(AutoDisposableWindowsPrincipalTest.TEST_FQN, principal.getName());
52      }
53  
54      /**
55       * Test constructor with identity and format.
56       */
57      @Test
58      void testConstructorWithIdentityAndFormat() {
59          Assertions.assertNotNull(new Expectations() {
60              {
61                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getFqn();
62                  this.result = AutoDisposableWindowsPrincipalTest.TEST_FQN;
63                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getGroups();
64                  this.result = new IWindowsAccount[0];
65              }
66          });
67          final AutoDisposableWindowsPrincipal principal = new AutoDisposableWindowsPrincipal(this.windowsIdentity,
68                  PrincipalFormat.FQN, PrincipalFormat.FQN);
69          Assertions.assertEquals(AutoDisposableWindowsPrincipalTest.TEST_FQN, principal.getName());
70      }
71  
72      /**
73       * Test value bound does nothing.
74       */
75      @Test
76      void testValueBound() {
77          Assertions.assertNotNull(new Expectations() {
78              {
79                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getFqn();
80                  this.result = AutoDisposableWindowsPrincipalTest.TEST_FQN;
81                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getGroups();
82                  this.result = new IWindowsAccount[0];
83              }
84          });
85          final AutoDisposableWindowsPrincipal principal = new AutoDisposableWindowsPrincipal(this.windowsIdentity);
86          Assertions.assertDoesNotThrow(() -> principal.valueBound(this.sessionBindingEvent));
87      }
88  
89      /**
90       * Test value unbound disposes identity.
91       */
92      @Test
93      void testValueUnboundDisposesIdentity() {
94          Assertions.assertNotNull(new Expectations() {
95              {
96                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getFqn();
97                  this.result = AutoDisposableWindowsPrincipalTest.TEST_FQN;
98                  AutoDisposableWindowsPrincipalTest.this.windowsIdentity.getGroups();
99                  this.result = new IWindowsAccount[0];
100                 AutoDisposableWindowsPrincipalTest.this.windowsIdentity.dispose();
101                 this.times = 1;
102             }
103         });
104         final AutoDisposableWindowsPrincipal principal = new AutoDisposableWindowsPrincipal(this.windowsIdentity);
105         principal.valueUnbound(this.sessionBindingEvent);
106     }
107 }