merge with default branch

This commit is contained in:
Sebastian Sdorra
2019-12-05 16:14:44 +01:00
166 changed files with 2566 additions and 704 deletions

View File

@@ -35,18 +35,14 @@ package sonia.scm.cache;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Predicate;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import sonia.scm.util.IOUtil;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.*;
import org.junit.Assume;
/**
*
@@ -166,14 +162,7 @@ public abstract class CacheTestBase
cache.put("a-1", "test123");
cache.put("a-2", "test123");
Iterable<String> previous = cache.removeAll(new Predicate<String>()
{
@Override
public boolean apply(String item)
{
return item.startsWith("test");
}
});
Iterable<String> previous = cache.removeAll(item -> item != null && item.startsWith("test"));
assertThat(previous, containsInAnyOrder("test123", "test456"));
assertNull(cache.get("test-1"));
@@ -188,8 +177,8 @@ public abstract class CacheTestBase
// skip test if implementation does not support stats
Assume.assumeTrue( stats != null );
assertEquals("test", stats.getName());
assertEquals(0l, stats.getHitCount());
assertEquals(0l, stats.getMissCount());
assertEquals(0L, stats.getHitCount());
assertEquals(0L, stats.getMissCount());
cache.put("test-1", "test123");
cache.put("test-2", "test456");
cache.get("test-1");
@@ -197,11 +186,11 @@ public abstract class CacheTestBase
cache.get("test-1");
cache.get("test-3");
// check that stats have not changed
assertEquals(0l, stats.getHitCount());
assertEquals(0l, stats.getMissCount());
assertEquals(0L, stats.getHitCount());
assertEquals(0L, stats.getMissCount());
stats = cache.getStatistics();
assertEquals(3l, stats.getHitCount());
assertEquals(1l, stats.getMissCount());
assertEquals(3L, stats.getHitCount());
assertEquals(1L, stats.getMissCount());
assertEquals(0.75d, stats.getHitRate(), 0.0d);
assertEquals(0.25d, stats.getMissRate(), 0.0d);
}

View File

@@ -63,7 +63,7 @@ public class GuavaConfigurationReaderTest
GuavaCacheConfiguration cfg =
readConfiguration("gcache.001.xml").getDefaultCache();
assertCacheValues(cfg, 200l, 1200l, 2400l);
assertCacheValues(cfg, 200L, 1200L, 2400L);
}
/**
@@ -82,10 +82,10 @@ public class GuavaConfigurationReaderTest
//J+
// cache sonia.test.cache.001 override by cache.004.xml
assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 6l, 2l, 8l);
assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000l, 120l, 60l);
assertCacheValues(getCache(gcm, "sonia.test.cache.003"), 3000l, 120l,
2400l);
assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 6L, 2L, 8L);
assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000L, 120L, 60L);
assertCacheValues(getCache(gcm, "sonia.test.cache.003"), 3000L, 120L,
2400L);
}
/**
@@ -100,8 +100,8 @@ public class GuavaConfigurationReaderTest
// check default
assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 1000l, 60l, 30l);
assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000l, 120l, 60l);
assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 1000L, 60L, 30L);
assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000L, 120L, 60L);
}
/**
@@ -115,10 +115,10 @@ public class GuavaConfigurationReaderTest
Iterators.forArray("gcache.002.xml",
"gcache.003.xml"));
assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 1000l, 60l, 30l);
assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000l, 120l, 60l);
assertCacheValues(getCache(gcm, "sonia.test.cache.003"), 3000l, 120l,
2400l);
assertCacheValues(getCache(gcm, "sonia.test.cache.001"), 1000L, 60L, 30L);
assertCacheValues(getCache(gcm, "sonia.test.cache.002"), 1000L, 120L, 60L);
assertCacheValues(getCache(gcm, "sonia.test.cache.003"), 3000L, 120L,
2400L);
}
/**
@@ -131,7 +131,7 @@ public class GuavaConfigurationReaderTest
GuavaCacheConfiguration cfg =
readConfiguration("gcache.001.xml").getCaches().get(0);
assertCacheValues(cfg, 1000l, 60l, 30l);
assertCacheValues(cfg, 1000L, 60L, 30L);
}
/**
@@ -144,7 +144,7 @@ public class GuavaConfigurationReaderTest
GuavaCacheConfiguration cfg =
readConfiguration("gcache.002.xml").getCaches().get(0);
assertCacheValues(cfg, 1000l, 120l, 60l);
assertCacheValues(cfg, 1000L, 120L, 60L);
}
/**

View File

@@ -94,7 +94,6 @@ public class RepositorySimplePermissionITCase
repository.setName("test-repo");
repository.setType("git");
// repository.setPublicReadable(false);
ScmClient client = createAdminClient();

View File

@@ -70,7 +70,19 @@ class SetupContextListenerTest {
}
@Test
void shouldCreateAdminAccountAndAssignPermissions() {
void shouldCreateAdminAccountIfNoUserExistsAndAssignPermissions() {
when(passwordService.encryptPassword("scmadmin")).thenReturn("secret");
setupContextListener.contextInitialized(null);
verifyAdminCreated();
verifyAdminPermissionsAssigned();
}
@Test
void shouldCreateAdminAccountIfOnlyAnonymousUserExistsAndAssignPermissions() {
when(userManager.getAll()).thenReturn(Lists.newArrayList(SCMContext.ANONYMOUS));
when(userManager.contains(SCMContext.USER_ANONYMOUS)).thenReturn(true);
when(passwordService.encryptPassword("scmadmin")).thenReturn("secret");
setupContextListener.contextInitialized(null);

View File

@@ -12,13 +12,10 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.NotFoundException;
import sonia.scm.ScmConstraintViolationException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
import java.io.IOException;
import java.nio.file.Files;
@@ -48,9 +45,6 @@ import static sonia.scm.plugin.PluginTestHelper.createInstalled;
@ExtendWith(TempDirectory.class)
class DefaultPluginManagerTest {
@Mock
private ScmEventBus eventBus;
@Mock
private PluginLoader loader;
@@ -60,12 +54,13 @@ class DefaultPluginManagerTest {
@Mock
private PluginInstaller installer;
@InjectMocks
private DefaultPluginManager manager;
@Mock
private Subject subject;
private boolean restartTriggered = false;
@BeforeEach
void mockInstaller() {
lenient().when(installer.install(any())).then(ic -> {
@@ -74,6 +69,16 @@ class DefaultPluginManagerTest {
});
}
@BeforeEach
void createPluginManagerToTestWithCapturedRestart() {
manager = new DefaultPluginManager(null, loader, center, installer) { // event bus is only used in restart and this is replaced here
@Override
void triggerRestart(String cause) {
restartTriggered = true;
}
};
}
@Nested
class WithAdminPermissions {
@@ -180,7 +185,7 @@ class DefaultPluginManagerTest {
manager.install("scm-git-plugin", false);
verify(installer).install(git);
verify(eventBus, never()).post(any());
assertThat(restartTriggered).isFalse();
}
@Test
@@ -258,7 +263,7 @@ class DefaultPluginManagerTest {
manager.install("scm-git-plugin", true);
verify(installer).install(git);
verify(eventBus).post(any(RestartEvent.class));
assertThat(restartTriggered).isTrue();
}
@Test
@@ -267,7 +272,7 @@ class DefaultPluginManagerTest {
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(gitInstalled));
manager.install("scm-git-plugin", true);
verify(eventBus, never()).post(any());
assertThat(restartTriggered).isFalse();
}
@Test
@@ -289,14 +294,14 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
manager.executePendingAndRestart();
verify(eventBus).post(any(RestartEvent.class));
assertThat(restartTriggered).isTrue();
}
@Test
void shouldNotSendRestartEventWithoutPendingPlugins() {
manager.executePendingAndRestart();
verify(eventBus, never()).post(any());
assertThat(restartTriggered).isFalse();
}
@Test
@@ -447,7 +452,7 @@ class DefaultPluginManagerTest {
manager.executePendingAndRestart();
verify(eventBus).post(any(RestartEvent.class));
assertThat(restartTriggered).isTrue();
}
@Test

View File

@@ -74,7 +74,7 @@ public class ConfigurableLoginAttemptHandlerTest {
handler.onUnsuccessfulAuthentication(token, new SimpleAuthenticationInfo());
handler.beforeAuthentication(token);
handler.onUnsuccessfulAuthentication(token, new SimpleAuthenticationInfo());
Thread.sleep(TimeUnit.MILLISECONDS.toMillis(1200l));
Thread.sleep(TimeUnit.MILLISECONDS.toMillis(1200L));
handler.beforeAuthentication(token);
}
@@ -111,4 +111,4 @@ public class ConfigurableLoginAttemptHandlerTest {
return new ConfigurableLoginAttemptHandler(configuration);
}
}
}

View File

@@ -35,22 +35,15 @@ package sonia.scm.security;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.collect.Sets;
import org.junit.Test;
import java.util.Set;
import java.util.concurrent.*;
import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
*
* @author Sebastian Sdorra
@@ -96,28 +89,22 @@ public class DefaultKeyGeneratorTest
for (int i = 0; i < 10; i++)
{
Future<Set<String>> future = executor.submit(new Callable<Set<String>>()
{
Future<Set<String>> future = executor.submit(() -> {
Set<String> keys = Sets.newHashSet();
@Override
public Set<String> call()
for (int i1 = 0; i1 < 1000; i1++)
{
Set<String> keys = Sets.newHashSet();
String key = generator.createKey();
for (int i = 0; i < 1000; i++)
if (keys.contains(key))
{
String key = generator.createKey();
if (keys.contains(key))
{
fail("dublicate key");
}
keys.add(key);
fail("dublicate key");
}
return keys;
keys.add(key);
}
return keys;
});
futureSet.add(future);

View File

@@ -89,15 +89,8 @@ public class MustacheTemplateTest extends TemplateTestBase
@Override
protected void prepareEnv(Map<String, Object> env)
{
env.put("test", new Function<String, String>()
{
@Override
public String apply(String input)
{
throw new UnsupportedOperationException("Not supported yet.");
}
env.put("test", (Function<String, String>) input -> {
throw new UnsupportedOperationException("Not supported yet.");
});
}

View File

@@ -0,0 +1,118 @@
package sonia.scm.update.repository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.SCMContext;
import sonia.scm.repository.Repository;
import sonia.scm.repository.RepositoryPermission;
import sonia.scm.repository.RepositoryRolePermissions;
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.xml.XmlRepositoryDAO;
import sonia.scm.update.UpdateStepTestUtil;
import sonia.scm.user.User;
import sonia.scm.user.xml.XmlUserDAO;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junitpioneer.jupiter.TempDirectory.TempDir;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@ExtendWith(TempDirectory.class)
class PublicFlagUpdateStepTest {
@Mock
XmlUserDAO userDAO;
@Mock
XmlRepositoryDAO repositoryDAO;
@Captor
ArgumentCaptor<Repository> repositoryCaptor;
private UpdateStepTestUtil testUtil;
private PublicFlagUpdateStep updateStep;
private Repository REPOSITORY = RepositoryTestData.createHeartOfGold();
@BeforeEach
void mockScmHome(@TempDir Path tempDir) throws IOException {
testUtil = new UpdateStepTestUtil(tempDir);
updateStep = new PublicFlagUpdateStep(testUtil.getContextProvider(), userDAO, repositoryDAO);
//prepare backup xml
V1RepositoryFileSystem.createV1Home(tempDir);
Files.move(tempDir.resolve("config").resolve("repositories.xml"), tempDir.resolve("config").resolve("repositories.xml.v1.backup"));
when(repositoryDAO.get((String) any())).thenReturn(REPOSITORY);
}
@Test
void shouldDeleteOldAnonymousUserIfExists() throws JAXBException {
User anonymous = new User("anonymous");
when(userDAO.getAll()).thenReturn(Collections.singleton(anonymous));
doReturn(anonymous).when(userDAO).get("anonymous");
doReturn(SCMContext.ANONYMOUS).when(userDAO).get(SCMContext.USER_ANONYMOUS);
updateStep.doUpdate();
verify(userDAO).delete(anonymous);
}
@Test
void shouldNotTryToDeleteOldAnonymousUserIfNotExists() throws JAXBException {
when(userDAO.getAll()).thenReturn(Collections.emptyList());
doReturn(SCMContext.ANONYMOUS).when(userDAO).get(SCMContext.USER_ANONYMOUS);
updateStep.doUpdate();
verify(userDAO, never()).delete(any());
}
@Test
void shouldCreateNewAnonymousUserIfNotExists() throws JAXBException {
doReturn(SCMContext.ANONYMOUS).when(userDAO).get(SCMContext.USER_ANONYMOUS);
when(userDAO.getAll()).thenReturn(Collections.singleton(new User("trillian")));
updateStep.doUpdate();
verify(userDAO).add(SCMContext.ANONYMOUS);
}
@Test
void shouldNotCreateNewAnonymousUserIfAlreadyExists() throws JAXBException {
doReturn(SCMContext.ANONYMOUS).when(userDAO).get(SCMContext.USER_ANONYMOUS);
when(userDAO.getAll()).thenReturn(Collections.singleton(new User("_anonymous")));
updateStep.doUpdate();
verify(userDAO, never()).add(SCMContext.ANONYMOUS);
}
@Test
void shouldMigratePublicFlagToAnonymousRepositoryPermission() throws JAXBException {
when(userDAO.getAll()).thenReturn(Collections.emptyList());
when(userDAO.get("_anonymous")).thenReturn(SCMContext.ANONYMOUS);
updateStep.doUpdate();
verify(repositoryDAO, times(2)).modify(repositoryCaptor.capture());
RepositoryPermission migratedRepositoryPermission = repositoryCaptor.getValue().getPermissions().iterator().next();
assertThat(migratedRepositoryPermission.getName()).isEqualTo(SCMContext.USER_ANONYMOUS);
assertThat(migratedRepositoryPermission.getRole()).isEqualTo("READ");
assertThat(migratedRepositoryPermission.isGroupPermission()).isFalse();
}
}

View File

@@ -0,0 +1,41 @@
package sonia.scm.user;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import sonia.scm.HandlerEventType;
import sonia.scm.SCMContext;
import sonia.scm.config.ScmConfiguration;
import static org.junit.jupiter.api.Assertions.assertThrows;
class AnonymousUserDeletionEventHandlerTest {
private ScmConfiguration scmConfiguration;
private AnonymousUserDeletionEventHandler hook;
@BeforeEach
void initConfig() {
scmConfiguration = new ScmConfiguration();
}
@Test
void shouldThrowAnonymousUserDeletionExceptionIfAnonymousAccessIsEnabled() {
scmConfiguration.setAnonymousAccessEnabled(true);
hook = new AnonymousUserDeletionEventHandler(scmConfiguration);
UserEvent deletionEvent = new UserEvent(HandlerEventType.BEFORE_DELETE, SCMContext.ANONYMOUS);
assertThrows(AnonymousUserDeletionException.class, () -> hook.onEvent(deletionEvent));
}
@Test
void shouldNotThrowAnonymousUserDeletionException() {
scmConfiguration.setAnonymousAccessEnabled(false);
hook = new AnonymousUserDeletionEventHandler(scmConfiguration);
UserEvent deletionEvent = new UserEvent(HandlerEventType.BEFORE_DELETE, SCMContext.ANONYMOUS);
hook.onEvent(deletionEvent);
}
}