2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
2010-12-06 20:05:04 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
2010-12-06 20:05:04 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2010-12-06 20:05:04 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
2010-12-06 20:05:04 +01:00
|
|
|
*
|
2020-03-23 15:35:58 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2010-12-06 20:05:04 +01:00
|
|
|
*/
|
2020-12-16 10:58:29 +01:00
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
package sonia.scm;
|
|
|
|
|
|
|
|
|
|
//~--- non-JDK imports --------------------------------------------------------
|
|
|
|
|
|
2012-08-29 10:05:15 +02:00
|
|
|
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;
|
2010-12-06 20:05:04 +01:00
|
|
|
import org.junit.After;
|
2012-08-29 10:05:15 +02:00
|
|
|
import org.junit.AfterClass;
|
2010-12-06 20:05:04 +01:00
|
|
|
import org.junit.Before;
|
2020-12-16 10:58:29 +01:00
|
|
|
import org.junit.jupiter.api.AfterAll;
|
|
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
2018-11-27 11:35:02 +01:00
|
|
|
import sonia.scm.io.DefaultFileSystem;
|
|
|
|
|
import sonia.scm.repository.InitialRepositoryLocationResolver;
|
|
|
|
|
import sonia.scm.repository.RepositoryDAO;
|
|
|
|
|
import sonia.scm.repository.RepositoryLocationResolver;
|
2010-12-06 20:05:04 +01:00
|
|
|
import sonia.scm.util.IOUtil;
|
|
|
|
|
import sonia.scm.util.MockUtil;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
2017-05-10 13:56:13 +02:00
|
|
|
import java.io.IOException;
|
2010-12-06 20:05:04 +01:00
|
|
|
import java.util.UUID;
|
2017-05-10 13:56:13 +02:00
|
|
|
import java.util.logging.Logger;
|
2010-12-06 20:05:04 +01:00
|
|
|
|
2020-12-16 10:58:29 +01:00
|
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @author Sebastian Sdorra
|
|
|
|
|
*/
|
|
|
|
|
public class AbstractTestBase
|
|
|
|
|
{
|
|
|
|
|
|
2012-08-29 10:05:15 +02:00
|
|
|
private static ThreadState subjectThreadState;
|
|
|
|
|
|
2018-11-27 11:35:02 +01:00
|
|
|
protected SCMContextProvider contextProvider;
|
|
|
|
|
|
|
|
|
|
private File tempDirectory;
|
|
|
|
|
|
|
|
|
|
protected RepositoryLocationResolver repositoryLocationResolver;
|
|
|
|
|
|
2020-12-16 10:58:29 +01:00
|
|
|
@BeforeEach
|
2018-11-27 11:35:02 +01:00
|
|
|
@Before
|
|
|
|
|
public void setUpTest() throws Exception
|
|
|
|
|
{
|
|
|
|
|
tempDirectory = new File(System.getProperty("java.io.tmpdir"),
|
|
|
|
|
UUID.randomUUID().toString());
|
|
|
|
|
assertTrue(tempDirectory.mkdirs());
|
|
|
|
|
contextProvider = MockUtil.getSCMContextProvider(tempDirectory);
|
2018-11-29 16:59:04 +01:00
|
|
|
InitialRepositoryLocationResolver initialRepoLocationResolver = new InitialRepositoryLocationResolver();
|
2019-05-09 14:08:18 +02:00
|
|
|
repositoryLocationResolver = new TempDirRepositoryLocationResolver(tempDirectory);
|
2018-11-27 11:35:02 +01:00
|
|
|
postSetUp();
|
|
|
|
|
}
|
2012-08-29 10:05:15 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*/
|
2020-12-16 10:58:29 +01:00
|
|
|
@AfterAll
|
2012-08-29 10:05:15 +02:00
|
|
|
@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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-16 10:58:29 +01:00
|
|
|
@AfterEach
|
2010-12-06 20:05:04 +01:00
|
|
|
@After
|
|
|
|
|
public void tearDownTest() throws Exception
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
preTearDown();
|
2013-05-02 18:29:00 +02:00
|
|
|
clearSubject();
|
2010-12-06 20:05:04 +01:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2017-05-10 13:56:13 +02:00
|
|
|
try {
|
|
|
|
|
IOUtil.delete(tempDirectory);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
Logger.getGlobal().warning(String.format("deleting temp <%s> failed: %s", tempDirectory.getAbsolutePath(), e.getMessage()));
|
|
|
|
|
}
|
2010-12-06 20:05:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-29 10:05:15 +02:00
|
|
|
/**
|
2013-05-02 18:29:00 +02:00
|
|
|
* Clears Shiro's thread state, ensuring the thread remains clean for
|
2013-01-29 21:55:08 +01:00
|
|
|
* future test execution.
|
2012-08-29 10:05:15 +02:00
|
|
|
*/
|
|
|
|
|
protected void clearSubject()
|
|
|
|
|
{
|
|
|
|
|
doClearSubject();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param subject
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
protected ThreadState createThreadState(Subject subject)
|
|
|
|
|
{
|
|
|
|
|
return new SubjectThreadState(subject);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected void postSetUp() throws Exception {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Method description
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
protected void preTearDown() throws Exception {}
|
|
|
|
|
|
2012-08-29 10:05:15 +02:00
|
|
|
//~--- 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();
|
|
|
|
|
}
|
2019-05-09 14:08:18 +02:00
|
|
|
|
2010-12-06 20:05:04 +01:00
|
|
|
}
|