View Javadoc
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.config.Ini;
23  import org.apache.shiro.mgt.DefaultSecurityManager;
24  import org.apache.shiro.mgt.SecurityManager;
25  import org.apache.shiro.realm.text.IniRealm;
26  import org.apache.shiro.subject.Subject;
27  import org.apache.shiro.util.LifecycleUtils;
28  import org.apache.shiro.util.ThreadContext;
29  import org.junit.After;
30  import org.junit.Before;
31  
32  /**
33   * Utility methods for use by Shiro test case subclasses.  You can use these methods as examples for your own
34   * test cases, but you SHOULD NOT use any ThreadContext API calls in your actual application code.
35   * The utility methods here make heavy assumptions about Shiro's implementation details, and your
36   * application code should definitely not.
37   * <p/>
38   * See the <a href="http://cwiki.apache.org/confluence/display/SHIRO/Subject">wiki Subject documentation</a>
39   * for proper application practices using Subject instances with threads.
40   */
41  public class SecurityManagerTestSupport {
42  
43      protected static SecurityManager createTestSecurityManager() {
44          Ini ini = new Ini();
45          ini.setSectionProperty("users", "test", "test");
46          return new DefaultSecurityManager(new IniRealm(ini));
47      }
48  
49      protected void destroy(SecurityManager sm) {
50          LifecycleUtils.destroy(sm);
51      }
52  
53      protected SecurityManager createAndBindTestSecurityManager() {
54          SecurityManager sm = createTestSecurityManager();
55          ThreadContext.bind(sm);
56          return sm;
57      }
58  
59      protected Subject createAndBindTestSubject() {
60          SecurityManager sm = ThreadContext.getSecurityManager();
61          if (sm == null) {
62              createAndBindTestSecurityManager();
63          }
64          return SecurityUtils.getSubject();
65      }
66  
67      @Before
68      public void setup() {
69          createAndBindTestSubject();
70      }
71  
72      @After
73      public void teardown() {
74          ThreadContext.remove();
75      }
76  }