You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by lh...@apache.org on 2009/09/21 23:27:33 UTC

svn commit: r817415 - /incubator/shiro/trunk/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java

Author: lhazlewood
Date: Mon Sep 21 21:27:30 2009
New Revision: 817415

URL: http://svn.apache.org/viewvc?rev=817415&view=rev
Log:
added two test cases - execute(Callable) and execute(Runnable)

Modified:
    incubator/shiro/trunk/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java

Modified: incubator/shiro/trunk/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java
URL: http://svn.apache.org/viewvc/incubator/shiro/trunk/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java?rev=817415&r1=817414&r2=817415&view=diff
==============================================================================
--- incubator/shiro/trunk/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java (original)
+++ incubator/shiro/trunk/core/src/test/java/org/apache/shiro/subject/DelegatingSubjectTest.java Mon Sep 21 21:27:30 2009
@@ -18,16 +18,19 @@
  */
 package org.apache.shiro.subject;
 
-import java.io.Serializable;
-
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.mgt.DefaultSecurityManager;
+import org.apache.shiro.mgt.SecurityManager;
+import org.apache.shiro.session.Session;
+import org.apache.shiro.util.ThreadContext;
+import static org.easymock.EasyMock.*;
 import org.junit.After;
 import static org.junit.Assert.*;
 import org.junit.Before;
 import org.junit.Test;
 
-import org.apache.shiro.mgt.DefaultSecurityManager;
-import org.apache.shiro.session.Session;
-import org.apache.shiro.util.ThreadContext;
+import java.io.Serializable;
+import java.util.concurrent.Callable;
 
 
 /**
@@ -73,4 +76,60 @@
 
         sm.destroy();
     }
+
+    @Test
+    public void testExecuteCallable() {
+
+        String username = "jsmith";
+
+        SecurityManager securityManager = createNiceMock(SecurityManager.class);
+        PrincipalCollection identity = new SimplePrincipalCollection(username, "testRealm");
+        final Subject sourceSubject = new DelegatingSubject(identity, true, null, null, securityManager);
+
+        assertNull(ThreadContext.getSubject());
+        assertNull(ThreadContext.getSecurityManager());
+
+        Callable<String> callable = new Callable<String>() {
+            public String call() throws Exception {
+                Subject callingSubject = SecurityUtils.getSubject();
+                assertNotNull(callingSubject);
+                assertNotNull(SecurityUtils.getSecurityManager());
+                assertEquals(callingSubject, sourceSubject);
+                return "Hello " + callingSubject.getPrincipal();
+            }
+        };
+        String response = sourceSubject.execute(callable);
+
+        assertNotNull(response);
+        assertEquals("Hello " + username, response);
+
+        assertNull(ThreadContext.getSubject());
+        assertNull(ThreadContext.getSecurityManager());
+    }
+
+    @Test
+    public void testExecuteRunnable() {
+
+        String username = "jsmith";
+
+        SecurityManager securityManager = createNiceMock(SecurityManager.class);
+        PrincipalCollection identity = new SimplePrincipalCollection(username, "testRealm");
+        final Subject sourceSubject = new DelegatingSubject(identity, true, null, null, securityManager);
+
+        assertNull(ThreadContext.getSubject());
+        assertNull(ThreadContext.getSecurityManager());
+
+        Runnable runnable = new Runnable() {
+            public void run() {
+                Subject callingSubject = SecurityUtils.getSubject();
+                assertNotNull(callingSubject);
+                assertNotNull(SecurityUtils.getSecurityManager());
+                assertEquals(callingSubject, sourceSubject);
+            }
+        };
+        sourceSubject.execute(runnable);
+
+        assertNull(ThreadContext.getSubject());
+        assertNull(ThreadContext.getSecurityManager());
+    }
 }