Commits

Sebastian Sdorra committed 324a337

fix repository manager test

Comments (0)

Files changed (3)

scm-test/src/main/java/sonia/scm/AbstractTestBase.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.UnavailableSecurityManagerException;
+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;
+
 import org.junit.After;
+import org.junit.AfterClass;
 import org.junit.Before;
 
 import sonia.scm.util.IOUtil;
 public class AbstractTestBase
 {
 
+  /** Field description */
+  private static ThreadState subjectThreadState;
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   */
+  @AfterClass
+  public static void tearDownShiro()
+  {
+    doClearSubject();
+
+    try
+    {
+      org.apache.shiro.mgt.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);
+  }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  protected static org.apache.shiro.mgt.SecurityManager getSecurityManager()
+  {
+    return SecurityUtils.getSecurityManager();
+  }
+
+  //~--- set methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @param securityManager
+   */
+  protected static void setSecurityManager(
+    org.apache.shiro.mgt.SecurityManager securityManager)
+  {
+    SecurityUtils.setSecurityManager(securityManager);
+  }
+
+  //~--- methods --------------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   */
+  private static void doClearSubject()
+  {
+    if (subjectThreadState != null)
+    {
+      subjectThreadState.clear();
+      subjectThreadState = null;
+    }
+  }
+
   /**
    * Method description
    *
   public void setUpTest() throws Exception
   {
     tempDirectory = new File(System.getProperty("java.io.tmpdir"),
-                             UUID.randomUUID().toString());
+      UUID.randomUUID().toString());
     assertTrue(tempDirectory.mkdirs());
     contextProvider = MockUtil.getSCMContextProvider(tempDirectory);
     postSetUp();
   //~--- methods --------------------------------------------------------------
 
   /**
+   * Clears Shiro's thread state, ensuring the thread remains clean for future test execution.
+   */
+  protected void clearSubject()
+  {
+    doClearSubject();
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param subject
+   *
+   * @return
+   */
+  protected ThreadState createThreadState(Subject subject)
+  {
+    return new SubjectThreadState(subject);
+  }
+
+  /**
    * Method description
    *
    *
    */
   protected void preTearDown() throws Exception {}
 
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
+  protected Subject getSubject()
+  {
+    return SecurityUtils.getSubject();
+  }
+
+  //~--- set methods ----------------------------------------------------------
+
+  /**
+   * Allows subclasses to set the currently executing {@link Subject} instance.
+   *
+   * @param subject the Subject instance
+   */
+  protected void setSubject(Subject subject)
+  {
+    clearSubject();
+    subjectThreadState = createThreadState(subject);
+    subjectThreadState.bind();
+  }
+
   ;
 
   //~--- fields ---------------------------------------------------------------

scm-test/src/main/java/sonia/scm/util/MockUtil.java

 
 import com.google.inject.Provider;
 
+import org.apache.shiro.authz.Permission;
+import org.apache.shiro.subject.Subject;
+
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
 import sonia.scm.SCMContextProvider;
 import sonia.scm.user.User;
 import sonia.scm.web.security.DummyWebSecurityContext;
 
 import java.io.File;
 
+import java.util.Arrays;
+import java.util.List;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
    *
    * @return
    */
+  public static Subject createAdminSubject()
+  {
+    Subject subject = mock(Subject.class);
+
+    when(subject.isAuthenticated()).thenReturn(Boolean.TRUE);
+    when(subject.isPermitted(anyListOf(Permission.class))).then(
+      new Answer<Boolean[]>()
+    {
+
+      @Override
+      public Boolean[] answer(InvocationOnMock invocation) throws Throwable
+      {
+        List<Permission> permissions =
+          (List<Permission>) invocation.getArguments()[0];
+        Boolean[] returnArray = new Boolean[permissions.size()];
+
+        Arrays.fill(returnArray, Boolean.TRUE);
+
+        return returnArray;
+      }
+    });
+    when(subject.isPermitted(any(Permission.class))).thenReturn(Boolean.TRUE);
+    when(subject.isPermitted(any(String.class))).thenReturn(Boolean.TRUE);
+    when(subject.isPermittedAll(anyCollectionOf(Permission.class))).thenReturn(
+      Boolean.TRUE);
+    when(subject.isPermittedAll()).thenReturn(Boolean.TRUE);
+    when(subject.hasRole("admin")).thenReturn(Boolean.TRUE);
+
+    return subject;
+  }
+
+  //~--- get methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   *
+   * @return
+   */
   public static Provider<WebSecurityContext> getAdminSecurityContextProvider()
   {
     User admin = new User("scmadmin", "SCM Admin", "scmadmin@scm.org");

scm-webapp/src/test/java/sonia/scm/repository/DefaultRepositoryManagerTest.java

 
 import com.google.inject.Provider;
 
+import org.apache.shiro.subject.Subject;
+
+import org.junit.Before;
 import org.junit.Test;
 
 import sonia.scm.Type;
+import sonia.scm.config.ScmConfiguration;
 import sonia.scm.repository.xml.XmlRepositoryDAO;
-import sonia.scm.config.ScmConfiguration;
 import sonia.scm.store.JAXBStoreFactory;
 import sonia.scm.store.StoreFactory;
 import sonia.scm.util.MockUtil;
    */
   @Test
   public void getRepositoryFromRequestUriTest()
-          throws RepositoryException, IOException
+    throws RepositoryException, IOException
   {
     RepositoryManager m = createManager();
 
     assertEquals("scm-test", m.getFromUri("hg/scm-test").getName());
     assertEquals("scm-test", m.getFromUri("/hg/scm-test").getName());
     assertEquals("project1/test-1",
-                 m.getFromUri("/git/project1/test-1").getName());
+      m.getFromUri("/git/project1/test-1").getName());
     assertEquals("project1/test-1",
-                 m.getFromUri("/git/project1/test-1/ka/some/path").getName());
+      m.getFromUri("/git/project1/test-1/ka/some/path").getName());
     assertNull(m.getFromUri("/git/project1/test-3/ka/some/path"));
   }
 
+  //~--- set methods ----------------------------------------------------------
+
+  /**
+   * Method description
+   *
+   */
+  @Before
+  public void setAdminSubject()
+  {
+    Subject admin = MockUtil.createAdminSubject();
+
+    setSubject(admin);
+  }
+
   //~--- methods --------------------------------------------------------------
 
   /**
     ScmConfiguration configuration = new ScmConfiguration();
 
     return new DefaultRepositoryManager(configuration, contextProvider,
-            MockUtil.getAdminSecurityContextProvider(), repositoryDAO,
-            handlerSet, listenerProvider, hookProvider);
+      repositoryDAO, handlerSet, listenerProvider, hookProvider);
   }
 
   /**
    * @throws RepositoryException
    */
   private void createRepository(RepositoryManager m, Repository repository)
-          throws RepositoryException, IOException
+    throws RepositoryException, IOException
   {
     m.create(repository);
   }