Skip to content

Commit 829993d

Browse files
committed
Applied proper setUp/tearDown to be able to configure multiple complete shiro environments in same JVM (same UT run)
1 parent 8443371 commit 829993d

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package eu.flatwhite.shiro.spatial;
2+
3+
import junit.framework.TestCase;
4+
5+
public abstract class AbstractShiroExtrasTest extends TestCase {
6+
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package eu.flatwhite.shiro.spatial;
2+
3+
import org.apache.shiro.SecurityUtils;
4+
import org.apache.shiro.config.IniSecurityManagerFactory;
5+
import org.apache.shiro.mgt.SecurityManager;
6+
import org.apache.shiro.subject.Subject;
7+
import org.apache.shiro.subject.support.SubjectThreadState;
8+
9+
public abstract class AbstractShiroFullConfigTest extends
10+
AbstractShiroExtrasTest {
11+
12+
protected IniSecurityManagerFactory config;
13+
14+
protected SecurityManager securityManager;
15+
16+
protected SubjectThreadState shiroThreadState;
17+
18+
protected void setUp() throws Exception {
19+
super.setUp();
20+
21+
config = new IniSecurityManagerFactory(getIniPath());
22+
23+
securityManager = config.getInstance();
24+
25+
Subject testSubject = new Subject.Builder(securityManager)
26+
.buildSubject();
27+
28+
shiroThreadState = new SubjectThreadState(testSubject);
29+
30+
shiroThreadState.bind();
31+
32+
SecurityUtils.setSecurityManager(securityManager);
33+
}
34+
35+
protected void tearDown() throws Exception {
36+
shiroThreadState.clear();
37+
38+
super.tearDown();
39+
}
40+
41+
protected abstract String getIniPath();
42+
}

src/test/java/eu/flatwhite/shiro/spatial/ex1/Example1Base.java

+2-20
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
import junit.framework.Assert;
77
import junit.framework.AssertionFailedError;
8-
import junit.framework.TestCase;
98

109
import org.apache.shiro.SecurityUtils;
1110
import org.apache.shiro.authc.UsernamePasswordToken;
1211
import org.apache.shiro.authz.UnauthorizedException;
13-
import org.apache.shiro.config.IniSecurityManagerFactory;
14-
import org.apache.shiro.mgt.SecurityManager;
1512
import org.apache.shiro.subject.Subject;
1613

14+
import eu.flatwhite.shiro.spatial.AbstractShiroFullConfigTest;
1715
import eu.flatwhite.shiro.spatial.ex1.domain.Person;
1816
import eu.flatwhite.shiro.spatial.ex1.domain.Person.Gender;
1917
import eu.flatwhite.shiro.spatial.ex1.domain.PersonDao;
@@ -29,11 +27,7 @@
2927
* @author cstamas
3028
*
3129
*/
32-
public abstract class Example1Base extends TestCase {
33-
34-
protected IniSecurityManagerFactory config;
35-
36-
protected SecurityManager securityManager;
30+
public abstract class Example1Base extends AbstractShiroFullConfigTest {
3731

3832
protected PersonDao personDao;
3933

@@ -44,23 +38,13 @@ public abstract class Example1Base extends TestCase {
4438
protected void setUp() throws Exception {
4539
super.setUp();
4640

47-
config = new IniSecurityManagerFactory(getIniPath());
48-
49-
securityManager = config.getInstance();
50-
51-
SecurityUtils.setSecurityManager(securityManager);
52-
5341
personDao = (PersonDao) config.getBeans().get("personDao");
5442

5543
personRoleDao = (PersonRoleDao) config.getBeans().get("personRoleDao");
5644

5745
vendingMachine = new ProtectedVendingMachine(new SimpleVendingMachine());
5846
}
5947

60-
protected void tearDown() throws Exception {
61-
super.tearDown();
62-
}
63-
6448
// Protected resources simulated with canI* methods
6549

6650
protected boolean canIHaveCoffe(Subject subject) {
@@ -157,8 +141,6 @@ protected void assertChecks(int expectedFailures)
157141

158142
// Test preparation methods
159143

160-
protected abstract String getIniPath();
161-
162144
protected abstract void applyManagerDecrete();
163145

164146
protected void populateInitialEmployees() {

0 commit comments

Comments
 (0)