mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 10:41:06 +01:00
Merged 2.0.0-m3 into feature/global_config_v2_endpoint
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.github.sdorra.shiro.ShiroRule;
|
||||
import com.github.sdorra.shiro.SubjectAware;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@SubjectAware(configuration = "classpath:sonia/scm/shiro-001.ini")
|
||||
public class DefaultNamespaceStrategyTest {
|
||||
|
||||
@Rule
|
||||
public ShiroRule shiroRule = new ShiroRule();
|
||||
|
||||
private DefaultNamespaceStrategy namespaceStrategy = new DefaultNamespaceStrategy();
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "trillian", password = "secret")
|
||||
public void testNamespaceStrategy() {
|
||||
assertEquals("trillian", namespaceStrategy.getNamespace());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -96,8 +96,6 @@ public class DefaultRepositoryManagerPerfTest {
|
||||
|
||||
private final KeyGenerator keyGenerator = new DefaultKeyGenerator();
|
||||
|
||||
private final NamespaceStrategy namespaceStrategy = new DefaultNamespaceStrategy();
|
||||
|
||||
@Mock
|
||||
private RepositoryHandler repositoryHandler;
|
||||
|
||||
@@ -114,7 +112,7 @@ public class DefaultRepositoryManagerPerfTest {
|
||||
when(repositoryHandler.getType()).thenReturn(new Type(REPOSITORY_TYPE, REPOSITORY_TYPE));
|
||||
Set<RepositoryHandler> handlerSet = ImmutableSet.of(repositoryHandler);
|
||||
RepositoryMatcher repositoryMatcher = new RepositoryMatcher(Collections.<RepositoryPathMatcher>emptySet());
|
||||
|
||||
NamespaceStrategy namespaceStrategy = mock(NamespaceStrategy.class);
|
||||
repositoryManager = new DefaultRepositoryManager(
|
||||
configuration,
|
||||
contextProvider,
|
||||
@@ -132,10 +130,7 @@ public class DefaultRepositoryManagerPerfTest {
|
||||
|
||||
ThreadContext.bind(securityManager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tear down test objects.
|
||||
*/
|
||||
|
||||
@After
|
||||
public void tearDown(){
|
||||
ThreadContext.unbindSecurityManager();
|
||||
@@ -188,8 +183,8 @@ private long calculateAverage(List<Long> times) {
|
||||
when(repositoryDAO.getAll()).thenReturn(repositories.values());
|
||||
}
|
||||
|
||||
private Repository createTestRepository(int number){
|
||||
Repository repository = new Repository(keyGenerator.createKey(), REPOSITORY_TYPE, "repo-" + number);
|
||||
private Repository createTestRepository(int number) {
|
||||
Repository repository = new Repository(keyGenerator.createKey(), REPOSITORY_TYPE, "namespace", "repo-" + number);
|
||||
repository.getPermissions().add(new Permission("trillian", PermissionType.READ));
|
||||
return repository;
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ import java.util.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -83,9 +84,8 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
|
||||
private ScmConfiguration configuration;
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#create(TypedObject)}.
|
||||
*/
|
||||
private String mockedNamespace = "default_namespace";
|
||||
|
||||
@Test
|
||||
public void testCreate() throws RepositoryException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
@@ -95,9 +95,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
assertRepositoriesEquals(dbRepo, heartOfGold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#create(TypedObject)} without the required permissions.
|
||||
*/
|
||||
@SubjectAware(
|
||||
username = "unpriv"
|
||||
)
|
||||
@@ -106,26 +103,17 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
createTestRepository();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#create(TypedObject)} with a already existing repository.
|
||||
*/
|
||||
@Test(expected = RepositoryAlreadyExistsException.class)
|
||||
public void testCreateExisting() throws RepositoryException {
|
||||
createTestRepository();
|
||||
createTestRepository();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#delete(TypedObject)}.
|
||||
*/
|
||||
@Test
|
||||
public void testDelete() throws RepositoryException {
|
||||
delete(manager, createTestRepository());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#delete(TypedObject)} without the required permissions.
|
||||
*/
|
||||
@SubjectAware(
|
||||
username = "unpriv"
|
||||
)
|
||||
@@ -134,27 +122,17 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
delete(manager, createTestRepository());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#delete(TypedObject)} with a non archived repository and with
|
||||
* enabled archive mode.
|
||||
*/
|
||||
@Test(expected = RepositoryIsNotArchivedException.class)
|
||||
public void testDeleteNonArchived() throws RepositoryException {
|
||||
configuration.setEnableRepositoryArchive(true);
|
||||
delete(manager, createTestRepository());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#delete(TypedObject)} with a non existing repository.
|
||||
*/
|
||||
@Test(expected = RepositoryNotFoundException.class)
|
||||
public void testDeleteNotFound() throws RepositoryException {
|
||||
manager.delete(createRepositoryWithId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#delete(TypedObject)} with enabled archive mode.
|
||||
*/
|
||||
@Test
|
||||
public void testDeleteWithEnabledArchive()
|
||||
throws RepositoryException {
|
||||
@@ -166,9 +144,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
delete(drm, repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#get(java.lang.String)} .
|
||||
*/
|
||||
@Test
|
||||
public void testGet() throws RepositoryException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
@@ -184,9 +159,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
assertEquals(description, heartOfGold.getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#get(java.lang.String)} without required privileges.
|
||||
*/
|
||||
@Test
|
||||
@SubjectAware(
|
||||
username = "crato"
|
||||
@@ -199,9 +171,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
manager.get(heartOfGold.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#getAll()}.
|
||||
*/
|
||||
@Test
|
||||
public void testGetAll() throws RepositoryException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
@@ -238,13 +207,10 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
heartOfGold.getDescription().equals(heartReference.getDescription()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#getAll()} with permission for 2 of 3 repositories.
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
@SubjectAware(username = "dent")
|
||||
public void testGetAllWithPermissions() throws RepositoryException {
|
||||
public void testGetAllWithPermissionsForTwoOrThreeRepos() throws RepositoryException {
|
||||
// mock key generator
|
||||
KeyGenerator keyGenerator = mock(KeyGenerator.class);
|
||||
Stack<String> keys = new Stack<>();
|
||||
@@ -284,9 +250,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests repository manager events.
|
||||
*/
|
||||
@Test
|
||||
public void testEvents() throws RepositoryException {
|
||||
RepositoryManager repoManager = createRepositoryManager(false);
|
||||
@@ -318,9 +281,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
assertSame(HandlerEventType.DELETE, listener.postEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#modify(TypedObject)}.
|
||||
*/
|
||||
@Test
|
||||
public void testModify() throws RepositoryException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
@@ -333,11 +293,7 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
assertNotNull(hearReference);
|
||||
assertEquals(hearReference.getDescription(), "prototype ship");
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#modify(TypedObject)} without
|
||||
* the required permissions.
|
||||
*/
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "crato")
|
||||
public void testModifyWithoutRequiredPermissions() throws RepositoryException {
|
||||
@@ -349,18 +305,11 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
manager.modify(heartOfGold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#modify(TypedObject)} with a non
|
||||
* existing repository.
|
||||
*/
|
||||
@Test(expected = RepositoryNotFoundException.class)
|
||||
public void testModifyNotFound() throws RepositoryException {
|
||||
manager.modify(createRepositoryWithId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#refresh(ModelObject)}.
|
||||
*/
|
||||
@Test
|
||||
public void testRefresh() throws RepositoryException {
|
||||
Repository heartOfGold = createTestRepository();
|
||||
@@ -371,10 +320,6 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
assertEquals(description, heartOfGold.getDescription());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#refresh(ModelObject)} without
|
||||
* required permissions.
|
||||
*/
|
||||
@Test
|
||||
@SubjectAware(username = "crato")
|
||||
public void testRefreshWithoutRequiredPermissions() throws RepositoryException {
|
||||
@@ -386,18 +331,11 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
manager.refresh(heartOfGold);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#refresh(ModelObject)} with a non existing
|
||||
* repository.
|
||||
*/
|
||||
@Test(expected = RepositoryNotFoundException.class)
|
||||
public void testRefreshNotFound() throws RepositoryException {
|
||||
manager.refresh(createRepositoryWithId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests repository hooks.
|
||||
*/
|
||||
@Test
|
||||
public void testRepositoryHook() throws RepositoryException {
|
||||
CountingReceiveHook hook = new CountingReceiveHook();
|
||||
@@ -418,25 +356,96 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
assertEquals(2, hook.eventsReceived);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link RepositoryManager#getFromTypeAndUri(String, String)}.
|
||||
*/
|
||||
@Test
|
||||
public void getRepositoryFromRequestUriTest() throws RepositoryException {
|
||||
public void testNamespaceSet() throws Exception {
|
||||
RepositoryManager repoManager = createRepositoryManager(false);
|
||||
Repository repository = spy(createTestRepository());
|
||||
repository.setName("Testrepo");
|
||||
((DefaultRepositoryManager) repoManager).create(repository);
|
||||
assertEquals("default_namespace", repository.getNamespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withoutLeadingSlash() throws RepositoryException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
createRepository(m, new Repository("1", "hg", "scm"));
|
||||
createRepository(m, new Repository("2", "hg", "scm-test"));
|
||||
createRepository(m, new Repository("3", "git", "project1/test-1"));
|
||||
createRepository(m, new Repository("4", "git", "project1/test-2"));
|
||||
|
||||
assertEquals("scm", m.getFromUri("hg/scm").getName());
|
||||
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());
|
||||
assertEquals("project1/test-1", m.getFromUri("/git/project1/test-1/ka/some/path").getName());
|
||||
assertNull(m.getFromUri("/git/project1/test-3/ka/some/path"));
|
||||
|
||||
createUriTestRepositories(m);
|
||||
|
||||
assertEquals("scm-test", m.getFromUri("hg/namespace/scm-test").getName());
|
||||
assertEquals("namespace", m.getFromUri("hg/namespace/scm-test").getNamespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withLeadingSlash() throws RepositoryException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
createUriTestRepositories(m);
|
||||
|
||||
assertEquals("scm-test", m.getFromUri("/hg/namespace/scm-test").getName());
|
||||
assertEquals("namespace", m.getFromUri("/hg/namespace/scm-test").getNamespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withPartialName() throws RepositoryException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
createUriTestRepositories(m);
|
||||
|
||||
assertEquals("scm", m.getFromUri("hg/namespace/scm").getName());
|
||||
assertEquals("namespace", m.getFromUri("hg/namespace/scm").getNamespace());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_withTrailingFilePath() throws RepositoryException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
createUriTestRepositories(m);
|
||||
|
||||
assertEquals("test-1", m.getFromUri("/git/namespace/test-1/ka/some/path").getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_forNotExistingRepositoryName() throws RepositoryException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
createUriTestRepositories(m);
|
||||
|
||||
assertNull(m.getFromUri("/git/namespace/test-3/ka/some/path"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRepositoryFromRequestUri_forWrongNamespace() throws RepositoryException {
|
||||
RepositoryManager m = createManager();
|
||||
m.init(contextProvider);
|
||||
|
||||
createUriTestRepositories(m);
|
||||
|
||||
assertNull(m.getFromUri("/git/other/other/test-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSetNamespace() throws RepositoryException {
|
||||
Repository repository = new Repository(null, "hg", null, "scm");
|
||||
manager.create(repository);
|
||||
assertNotNull(repository.getId());
|
||||
assertNotNull(repository.getNamespace());
|
||||
}
|
||||
|
||||
private void createUriTestRepositories(RepositoryManager m) throws RepositoryException {
|
||||
mockedNamespace = "namespace";
|
||||
createRepository(m, new Repository("1", "hg", "namespace", "scm"));
|
||||
createRepository(m, new Repository("2", "hg", "namespace", "scm-test"));
|
||||
createRepository(m, new Repository("3", "git", "namespace", "test-1"));
|
||||
createRepository(m, new Repository("4", "git", "namespace", "test-2"));
|
||||
|
||||
mockedNamespace = "other";
|
||||
createRepository(m, new Repository("1", "hg", "other", "scm"));
|
||||
createRepository(m, new Repository("2", "hg", "other", "scm-test"));
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -471,10 +480,11 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase<Repository, Re
|
||||
|
||||
this.configuration = new ScmConfiguration();
|
||||
|
||||
NamespaceStrategy namespaceStrategy = new DefaultNamespaceStrategy();
|
||||
|
||||
configuration.setEnableRepositoryArchive(archiveEnabled);
|
||||
|
||||
NamespaceStrategy namespaceStrategy = mock(NamespaceStrategy.class);
|
||||
when(namespaceStrategy.getNamespace()).thenAnswer(invocation -> mockedNamespace);
|
||||
|
||||
return new DefaultRepositoryManager(configuration, contextProvider,
|
||||
keyGenerator, repositoryDAO, handlerSet, createRepositoryMatcher(), namespaceStrategy);
|
||||
}
|
||||
|
||||
@@ -31,10 +31,13 @@
|
||||
package sonia.scm.repository;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.Set;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RepositoryMatcher}.
|
||||
@@ -54,11 +57,11 @@ public class RepositoryMatcherTest {
|
||||
|
||||
@Test
|
||||
public void testMatches() {
|
||||
assertFalse(matcher.matches(repository("hg", "scm"), "hg", "scm-test/ka"));
|
||||
assertFalse(matcher.matches(repository("git", "scm-test"), "hg", "scm-test"));
|
||||
assertFalse(matcher.matches(repository("hg", "scm"), "hg", "namespace/scm-test/ka"));
|
||||
assertFalse(matcher.matches(repository("git", "scm-test"), "hg", "namespace/scm-test"));
|
||||
|
||||
assertTrue(matcher.matches(repository("hg", "scm-test"), "hg", "scm-test/ka"));
|
||||
assertTrue(matcher.matches(repository("hg", "scm-test"), "hg", "scm-test"));
|
||||
assertTrue(matcher.matches(repository("hg", "scm-test"), "hg", "namespace/scm-test/ka"));
|
||||
assertTrue(matcher.matches(repository("hg", "scm-test"), "hg", "namespace/scm-test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -68,7 +71,7 @@ public class RepositoryMatcherTest {
|
||||
}
|
||||
|
||||
private Repository repository(String type, String name) {
|
||||
return new Repository(type + "-" + name, type, name);
|
||||
return new Repository(type + "-" + name, type, "namespace", name);
|
||||
}
|
||||
|
||||
private static class AbcRepositoryPathMatcher implements RepositoryPathMatcher {
|
||||
|
||||
Reference in New Issue
Block a user