Merge branch 'develop' into feature/browse_commit_with_limit

This commit is contained in:
René Pfeuffer
2020-03-09 08:40:29 +01:00
154 changed files with 7158 additions and 233 deletions

View File

@@ -138,19 +138,6 @@ public class RepositoryRootResourceTest extends RepositoryTestBase {
assertTrue(response.getContentAsString().contains("\"name\":\"repo\""));
}
@Test
public void shouldMapProperties() throws URISyntaxException, UnsupportedEncodingException {
Repository repository = mockRepository("space", "repo");
repository.setProperty("testKey", "testValue");
MockHttpRequest request = MockHttpRequest.get("/" + RepositoryRootResource.REPOSITORIES_PATH_V2 + "space/repo");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertTrue(response.getContentAsString().contains("\"testKey\":\"testValue\""));
}
@Test
public void shouldGetAll() throws URISyntaxException, UnsupportedEncodingException {
PageResult<Repository> singletonPageResult = createSingletonPageResult(mockRepository("space", "repo"));

View File

@@ -77,16 +77,6 @@ public class RepositoryToRepositoryDtoMapperTest {
assertEquals("none@example.com", dto.getContact());
}
@Test
public void shouldMapPropertiesProperty() {
Repository repository = createTestRepository();
repository.setProperty("testKey", "testValue");
RepositoryDto dto = mapper.map(repository);
assertEquals("testValue", dto.getProperties().get("testKey"));
}
@Test
@SubjectAware(username = "unpriv")
public void shouldCreateLinksForUnprivilegedUser() {

View File

@@ -9,7 +9,10 @@ import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.event.RecreateEventBusEvent;
import sonia.scm.event.ScmEventBus;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
@@ -28,12 +31,11 @@ class InjectionContextRestartStrategyTest {
}
@Test
void shouldCallDestroyAndInitialize() throws InterruptedException {
strategy.restart(context);
verify(context).destroy();
Thread.sleep(50L);
verify(context).initialize();
void shouldCallDestroyAndInitialize() {
TestingInjectionContext ctx = new TestingInjectionContext();
strategy.restart(ctx);
await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ctx.destroyed).isTrue());
await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ctx.initialized).isTrue());
}
@Test
@@ -51,6 +53,7 @@ class InjectionContextRestartStrategyTest {
TestingInjectionContext ctx = new TestingInjectionContext();
strategy.restart(ctx);
await().atMost(1, TimeUnit.SECONDS).until(() -> ctx.initialized);
Thread.sleep(50L);
ScmEventBus.getInstance().post("hello event");
@@ -70,6 +73,8 @@ class InjectionContextRestartStrategyTest {
public static class TestingInjectionContext implements RestartStrategy.InjectionContext {
private volatile String event;
private boolean initialized = false;
private boolean destroyed = false;
@Subscribe(async = false)
public void setEvent(String event) {
@@ -78,12 +83,12 @@ class InjectionContextRestartStrategyTest {
@Override
public void initialize() {
this.initialized = true;
}
@Override
public void destroy() {
this.destroyed = true;
}
}

View File

@@ -0,0 +1,74 @@
package sonia.scm.plugin;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Resources;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(TempDirectory.class)
class UberClassLoaderTest {
private final URLClassLoader parentClassLoader = new URLClassLoader(new URL[0]);
@Test
void shouldOnlyUseClassloaderOnce(@TempDirectory.TempDir Path tempDir) throws IOException {
ClassLoader mailClassLoader = createClassLoader(tempDir, "plugin.txt", "mail");
ClassLoader reviewClassLoader = createClassLoader(mailClassLoader, tempDir, "plugin.txt", "review");
UberClassLoader uberClassLoader = new UberClassLoader(parentClassLoader, ImmutableSet.of(mailClassLoader, reviewClassLoader));
List<URL> resources = Collections.list(uberClassLoader.findResources("plugin.txt"));
assertThat(resources).hasSize(2);
assertThat(toContent(resources)).containsOnly("mail", "review");
}
@Test
void shouldReturnResourceFromEachPluginClassLoader(@TempDirectory.TempDir Path tempDir) throws IOException {
ClassLoader mailClassLoader = createClassLoader(tempDir, "scm.txt", "mail");
ClassLoader reviewClassLoader = createClassLoader(tempDir, "scm.txt", "review");
UberClassLoader uberClassLoader = new UberClassLoader(parentClassLoader, ImmutableSet.of(mailClassLoader, reviewClassLoader));
List<URL> resources = Collections.list(uberClassLoader.findResources("scm.txt"));
assertThat(toContent(resources)).containsOnly("mail", "review");
}
@SuppressWarnings("UnstableApiUsage")
private List<String> toContent(Iterable<URL> resources) throws IOException {
List<String> content = new ArrayList<>();
for (URL resource : resources) {
content.add(Resources.toString(resource, StandardCharsets.UTF_8));
}
return content;
}
private ClassLoader createClassLoader(Path tempDir, String resource, String value) throws IOException {
return createClassLoader(Thread.currentThread().getContextClassLoader(), tempDir, resource, value);
}
private ClassLoader createClassLoader(ClassLoader parent, Path tempDir, String resource, String value) throws IOException {
Path directory = tempDir.resolve(UUID.randomUUID().toString());
Files.createDirectory(directory);
Files.write(directory.resolve(resource), value.getBytes(StandardCharsets.UTF_8));
return new URLClassLoader(new URL[]{
directory.toUri().toURL()
}, parent);
}
}