package net.sf.gazpachoquest.dbpopulator.support; import static org.easymock.EasyMock.createNiceMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import net.sf.gazpachoquest.domain.user.User; import org.apache.shiro.SecurityUtils; import org.apache.shiro.UnavailableSecurityManagerException; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.support.SubjectThreadState; import org.apache.shiro.util.LifecycleUtils; import org.apache.shiro.util.ThreadState; public class ShiroLogin { public static void login() { Subject subjectUnderTest = createNiceMock(Subject.class); User support = User.with().id(1).build(); expect(subjectUnderTest.getPrincipal()).andReturn(support).anyTimes(); replay(subjectUnderTest); // 2. Bind the subject to the current thread: setSubject(subjectUnderTest); } public static void logout() { // 3. Unbind the subject from the current thread: clearSubject(); tearDownShiro(); } private static ThreadState subjectThreadState; /** * Allows subclasses to set the currently executing {@link Subject} * instance. * * @param subject * the Subject instance */ protected static void setSubject(Subject subject) { clearSubject(); subjectThreadState = createThreadState(subject); subjectThreadState.bind(); } protected Subject getSubject() { return SecurityUtils.getSubject(); } protected static ThreadState createThreadState(Subject subject) { return new SubjectThreadState(subject); } /** * Clears Shiro's thread state, ensuring the thread remains clean for future * test execution. */ protected static void clearSubject() { doClearSubject(); } private static void doClearSubject() { if (subjectThreadState != null) { subjectThreadState.clear(); subjectThreadState = null; } } protected static void setSecurityManager(SecurityManager securityManager) { SecurityUtils.setSecurityManager(securityManager); } protected static SecurityManager getSecurityManager() { return SecurityUtils.getSecurityManager(); } public static void tearDownShiro() { doClearSubject(); try { SecurityManager securityManager = getSecurityManager(); LifecycleUtils.destroy(securityManager); } catch (UnavailableSecurityManagerException e) { // we don't care about this when cleaning up the test environment // (for example, maybe the subclass is a unit test and it didn't // need a SecurityManager instance because it was using only // mock Subject instances) } setSecurityManager(null); } }