1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.test;
20  
21  import org.apache.shiro.SecurityUtils;
22  import org.apache.shiro.UnavailableSecurityManagerException;
23  import org.apache.shiro.mgt.SecurityManager;
24  import org.apache.shiro.subject.Subject;
25  import org.apache.shiro.subject.support.SubjectThreadState;
26  import org.apache.shiro.util.LifecycleUtils;
27  import org.apache.shiro.util.ThreadState;
28  import org.junit.AfterClass;
29  
30  /**
31   * Abstract test case showing how to use Shiro in testing environments.
32   *
33   * @since 1.2
34   */
35  public abstract class AbstractShiroTest {
36  
37      private static ThreadState subjectThreadState;
38  
39      public AbstractShiroTest() {
40      }
41  
42      /**
43       * Allows subclasses to set the currently executing {@link Subject} instance.
44       *
45       * @param subject the Subject instance
46       */
47      protected void setSubject(Subject subject) {
48          clearSubject();
49          subjectThreadState = createThreadState(subject);
50          subjectThreadState.bind();
51      }
52  
53      protected Subject getSubject() {
54          return SecurityUtils.getSubject();
55      }
56  
57      protected ThreadState createThreadState(Subject subject) {
58          return new SubjectThreadState(subject);
59      }
60  
61      /**
62       * Clears Shiro's thread state, ensuring the thread remains clean for future test execution.
63       */
64      protected void clearSubject() {
65          doClearSubject();
66      }
67  
68      private static void doClearSubject() {
69          if (subjectThreadState != null) {
70              subjectThreadState.clear();
71              subjectThreadState = null;
72          }
73      }
74  
75      protected static void setSecurityManager(SecurityManager securityManager) {
76          SecurityUtils.setSecurityManager(securityManager);
77      }
78  
79      protected static SecurityManager getSecurityManager() {
80          return SecurityUtils.getSecurityManager();
81      }
82  
83      @AfterClass
84      public static void tearDownShiro() {
85          doClearSubject();
86          try {
87              SecurityManager securityManager = getSecurityManager();
88              LifecycleUtils.destroy(securityManager);
89          } catch (UnavailableSecurityManagerException e) {
90              //we don't care about this when cleaning up the test environment
91              //(for example, maybe the subclass is a unit test and it didn't
92              // need a SecurityManager instance because it was using only mock Subject instances)
93          }
94          setSecurityManager(null);
95      }
96  }