Merge with develop branch

This commit is contained in:
Sebastian Sdorra
2020-08-12 12:32:16 +02:00
40 changed files with 1823 additions and 462 deletions

View File

@@ -90,20 +90,28 @@ class DefaultPluginManagerTest {
@Captor
private ArgumentCaptor<PluginEvent> eventCaptor;
@InjectMocks
private DefaultPluginManager manager;
@Mock
private Subject subject;
private final PluginInstallationContext context = PluginInstallationContext.empty();
@BeforeEach
void mockInstaller() {
lenient().when(installer.install(any())).then(ic -> {
AvailablePlugin plugin = ic.getArgument(0);
lenient().when(installer.install(any(), any())).then(ic -> {
AvailablePlugin plugin = ic.getArgument(1);
return new PendingPluginInstallation(plugin.install(), null);
});
}
@BeforeEach
void setUpObjectUnderTest() {
manager = new DefaultPluginManager(
loader, center, installer, restarter, eventBus, plugins -> context
);
}
@Nested
class WithAdminPermissions {
@@ -209,7 +217,7 @@ class DefaultPluginManagerTest {
manager.install("scm-git-plugin", false);
verify(installer).install(git);
verify(installer).install(context, git);
verify(restarter, never()).restart(any(), any());
}
@@ -222,8 +230,8 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
verify(installer).install(mail);
verify(installer).install(review);
verify(installer).install(context, mail);
verify(installer).install(context, review);
}
@Test
@@ -239,7 +247,7 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
ArgumentCaptor<AvailablePlugin> captor = ArgumentCaptor.forClass(AvailablePlugin.class);
verify(installer).install(captor.capture());
verify(installer).install(any(), captor.capture());
assertThat(captor.getValue().getDescriptor().getInformation().getName()).isEqualTo("scm-review-plugin");
}
@@ -256,8 +264,8 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
verify(installer).install(mail);
verify(installer).install(review);
verify(installer).install(context, mail);
verify(installer).install(context, review);
}
@Test
@@ -272,8 +280,8 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
verify(installer).install(mail);
verify(installer).install(review);
verify(installer).install(context, mail);
verify(installer).install(context, review);
}
@Test
@@ -285,8 +293,8 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
verify(installer, never()).install(mail);
verify(installer).install(review);
verify(installer, never()).install(context, mail);
verify(installer).install(context, review);
}
@Test
@@ -299,12 +307,12 @@ class DefaultPluginManagerTest {
when(center.getAvailable()).thenReturn(ImmutableSet.of(review, mail, notification));
PendingPluginInstallation pendingNotification = mock(PendingPluginInstallation.class);
doReturn(pendingNotification).when(installer).install(notification);
doReturn(pendingNotification).when(installer).install(context, notification);
PendingPluginInstallation pendingMail = mock(PendingPluginInstallation.class);
doReturn(pendingMail).when(installer).install(mail);
doReturn(pendingMail).when(installer).install(context, mail);
doThrow(new PluginChecksumMismatchException(mail, "1", "2")).when(installer).install(review);
doThrow(new PluginChecksumMismatchException(mail, "1", "2")).when(installer).install(context, review);
assertThrows(PluginInstallException.class, () -> manager.install("scm-review-plugin", false));
@@ -322,7 +330,7 @@ class DefaultPluginManagerTest {
assertThrows(NotFoundException.class, () -> manager.install("scm-review-plugin", false));
verify(installer, never()).install(any());
verify(installer, never()).install(any(), any());
}
@Test
@@ -332,7 +340,7 @@ class DefaultPluginManagerTest {
manager.install("scm-git-plugin", true);
verify(installer).install(git);
verify(installer).install(context, git);
verify(restarter).restart(any(), any());
}
@@ -353,7 +361,7 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
manager.install("scm-review-plugin", false);
// only one interaction
verify(installer).install(any());
verify(installer).install(any(), any());
}
@Test
@@ -538,7 +546,7 @@ class DefaultPluginManagerTest {
AvailablePlugin git = createAvailable("scm-git-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(git));
PendingPluginInstallation gitPendingPluginInformation = mock(PendingPluginInstallation.class);
when(installer.install(git)).thenReturn(gitPendingPluginInformation);
when(installer.install(context, git)).thenReturn(gitPendingPluginInformation);
manager.install("scm-git-plugin", false);
manager.uninstall("scm-ssh-plugin", false);
@@ -571,8 +579,8 @@ class DefaultPluginManagerTest {
manager.updateAll();
verify(installer).install(newMailPlugin);
verify(installer).install(newReviewPlugin);
verify(installer).install(context, newMailPlugin);
verify(installer).install(context, newReviewPlugin);
}
@@ -587,7 +595,7 @@ class DefaultPluginManagerTest {
manager.updateAll();
verify(installer, never()).install(oldScriptPlugin);
verify(installer, never()).install(context, oldScriptPlugin);
}
@Test
@@ -607,7 +615,7 @@ class DefaultPluginManagerTest {
void shouldFirePluginEventOnFailedInstallation() {
AvailablePlugin review = createAvailable("scm-review-plugin");
when(center.getAvailable()).thenReturn(ImmutableSet.of(review));
doThrow(new PluginDownloadException(review, new IOException())).when(installer).install(review);
doThrow(new PluginDownloadException(review, new IOException())).when(installer).install(context, review);
assertThrows(PluginDownloadException.class, () -> manager.install("scm-review-plugin", false));

View File

@@ -0,0 +1,115 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.plugin;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class PluginInstallationContextTest {
@Test
void shouldReturnInstalledPlugin() {
Set<InstalledPlugin> installed = installed("scm-git-plugin", "1.0.0");
Set<AvailablePlugin> pending = Collections.emptySet();
PluginInstallationContext context = PluginInstallationContext.from(installed, pending);
Optional<NameAndVersion> plugin = context.find("scm-git-plugin");
assertThat(plugin).contains(new NameAndVersion("scm-git-plugin", "1.0.0"));
}
@Test
void shouldReturnPendingPlugin() {
Set<InstalledPlugin> installed = Collections.emptySet();
Set<AvailablePlugin> pending = pending("scm-hg-plugin", "1.0.0");
PluginInstallationContext context = PluginInstallationContext.from(installed, pending);
Optional<NameAndVersion> plugin = context.find("scm-hg-plugin");
assertThat(plugin).contains(new NameAndVersion("scm-hg-plugin", "1.0.0"));
}
@Test
void shouldReturnPendingEvenWithInstalled() {
Set<InstalledPlugin> installed = installed("scm-svn-plugin", "1.1.0");
Set<AvailablePlugin> pending = pending("scm-svn-plugin", "1.2.0");
PluginInstallationContext context = PluginInstallationContext.from(installed, pending);
Optional<NameAndVersion> plugin = context.find("scm-svn-plugin");
assertThat(plugin).contains(new NameAndVersion("scm-svn-plugin", "1.2.0"));
}
@Test
void shouldReturnEmpty() {
Set<InstalledPlugin> installed = Collections.emptySet();
Set<AvailablePlugin> pending = Collections.emptySet();
PluginInstallationContext context = PluginInstallationContext.from(installed, pending);
Optional<NameAndVersion> plugin = context.find("scm-legacy-plugin");
assertThat(plugin).isEmpty();
}
@Test
void shouldCreateContextFromDescriptor() {
Set<InstalledPluginDescriptor> installed = mockDescriptor(InstalledPluginDescriptor.class, "scm-svn-plugin", "1.1.0");
Set<AvailablePluginDescriptor> pending = mockDescriptor(AvailablePluginDescriptor.class, "scm-svn-plugin", "1.2.0");
PluginInstallationContext context = PluginInstallationContext.fromDescriptors(installed, pending);
Optional<NameAndVersion> plugin = context.find("scm-svn-plugin");
assertThat(plugin).contains(new NameAndVersion("scm-svn-plugin", "1.2.0"));
}
private Set<InstalledPlugin> installed(String name, String version) {
return mockPlugin(InstalledPlugin.class, name, version);
}
private Set<AvailablePlugin> pending(String name, String version) {
return mockPlugin(AvailablePlugin.class, name, version);
}
private <P extends Plugin> Set<P> mockPlugin(Class<P> pluginClass, String name, String version) {
P plugin = mock(pluginClass, Answers.RETURNS_DEEP_STUBS);
when(plugin.getDescriptor().getInformation().getName()).thenReturn(name);
when(plugin.getDescriptor().getInformation().getVersion()).thenReturn(version);
return Collections.singleton(plugin);
}
private <D extends PluginDescriptor> Set<D> mockDescriptor(Class<D> descriptorClass, String name, String version) {
D desc = mock(descriptorClass, Answers.RETURNS_DEEP_STUBS);
when(desc.getInformation().getName()).thenReturn(name);
when(desc.getInformation().getVersion()).thenReturn(version);
return Collections.singleton(desc);
}
}

View File

@@ -0,0 +1,174 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* 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:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*/
package sonia.scm.plugin;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Collections;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static sonia.scm.plugin.PluginInstallationContext.empty;
@ExtendWith(MockitoExtension.class)
class PluginInstallationVerifierTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private InstalledPluginDescriptor descriptor;
// hog stands for "Heart of Gold"
private static final String HOG_PLUGIN = "scm-hog-plugin";
// iid stands for "Infinite Improbability Drive"
private static final String IID_PLUGIN = "scm-iid-plugin";
@BeforeEach
void setUpDescriptor() {
PluginInformation information = new PluginInformation();
information.setName(HOG_PLUGIN);
information.setVersion("1.0.0");
when(descriptor.getInformation()).thenReturn(information);
}
@Test
void shouldFailOnCondition() {
PluginInstallationContext context = empty();
assertThrows(PluginConditionFailedException.class, () -> PluginInstallationVerifier.verify(context, descriptor));
}
@Test
void shouldFailOnMissingDependency() {
matchConditions();
when(descriptor.getDependenciesWithVersion()).thenReturn(Collections.singleton(new NameAndVersion(IID_PLUGIN)));
PluginInstallationContext context = empty();
DependencyNotFoundException exception = assertThrows(
DependencyNotFoundException.class, () -> PluginInstallationVerifier.verify(context, descriptor)
);
assertThat(exception.getPlugin()).isEqualTo(HOG_PLUGIN);
assertThat(exception.getMissingDependency()).isEqualTo(IID_PLUGIN);
}
private void matchConditions() {
when(descriptor.getCondition().isSupported()).thenReturn(true);
}
@Test
void shouldFailOnDependencyVersionMismatch() {
matchConditions();
// mock installation of iid 1.0.0
PluginInstallationContext context = mockInstallationOf(IID_PLUGIN, "1.0.0");
// mock dependency of iid 1.1.0
mockDependingOf(IID_PLUGIN, "1.1.0");
DependencyVersionMismatchException exception = assertThrows(
DependencyVersionMismatchException.class, () -> PluginInstallationVerifier.verify(context, descriptor)
);
assertThat(exception.getPlugin()).isEqualTo(HOG_PLUGIN);
assertThat(exception.getDependency()).isEqualTo(IID_PLUGIN);
assertThat(exception.getMinVersion()).isEqualTo("1.1.0");
assertThat(exception.getCurrentVersion()).isEqualTo("1.0.0");
}
@Test
void shouldFailOnOptionalDependencyVersionMismatch() {
matchConditions();
// mock installation of iid 1.0.0
PluginInstallationContext context = mockInstallationOf(IID_PLUGIN, "1.0.0");
// mock dependency of iid 1.1.0
mockOptionalDependingOf(IID_PLUGIN, "1.1.0");
DependencyVersionMismatchException exception = assertThrows(
DependencyVersionMismatchException.class, () -> PluginInstallationVerifier.verify(context, descriptor)
);
assertThat(exception.getPlugin()).isEqualTo(HOG_PLUGIN);
assertThat(exception.getDependency()).isEqualTo(IID_PLUGIN);
assertThat(exception.getMinVersion()).isEqualTo("1.1.0");
assertThat(exception.getCurrentVersion()).isEqualTo("1.0.0");
}
@Test
@SuppressWarnings("squid:S2699") // we are happy if no exception is thrown
void shouldVerifyPlugin() {
matchConditions();
PluginInstallationContext context = empty();
PluginInstallationVerifier.verify(context, descriptor);
}
@Test
@SuppressWarnings("squid:S2699") // we are happy if no exception is thrown
void shouldVerifyPluginWithDependencies() {
matchConditions();
// mock installation of iid 1.1.0
PluginInstallationContext context = mockInstallationOf(IID_PLUGIN, "1.1.0");
// mock dependency of iid 1.1.0
mockDependingOf(IID_PLUGIN, "1.1.0");
PluginInstallationVerifier.verify(context, descriptor);
}
@Test
@SuppressWarnings("squid:S2699") // we are happy if no exception is thrown
void shouldVerifyPluginWithOptionalDependency() {
matchConditions();
PluginInstallationContext context = PluginInstallationContext.empty();
// mock dependency of iid 1.1.0
mockOptionalDependingOf(IID_PLUGIN, "1.1.0");
PluginInstallationVerifier.verify(context, descriptor);
}
private void mockOptionalDependingOf(String plugin, String version) {
when(descriptor.getOptionalDependenciesWithVersion()).thenReturn(Collections.singleton(new NameAndVersion(plugin, version)));
}
private void mockDependingOf(String plugin, String version) {
when(descriptor.getDependenciesWithVersion()).thenReturn(Collections.singleton(new NameAndVersion(plugin, version)));
}
private PluginInstallationContext mockInstallationOf(String plugin, String version) {
PluginInstallationContext context = mock(PluginInstallationContext.class);
when(context.find(IID_PLUGIN)).thenReturn(Optional.of(new NameAndVersion(plugin, version)));
return context;
}
}

View File

@@ -83,7 +83,7 @@ class PluginInstallerTest {
void shouldDownloadPlugin() throws IOException {
mockContent("42");
installer.install(createGitPlugin());
installer.install(PluginInstallationContext.empty(), createGitPlugin());
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).hasContent("42");
}
@@ -93,7 +93,7 @@ class PluginInstallerTest {
mockContent("42");
AvailablePlugin gitPlugin = createGitPlugin();
PendingPluginInstallation pending = installer.install(gitPlugin);
PendingPluginInstallation pending = installer.install(PluginInstallationContext.empty(), gitPlugin);
assertThat(pending).isNotNull();
assertThat(pending.getPlugin().getDescriptor()).isEqualTo(gitPlugin.getDescriptor());
@@ -117,14 +117,18 @@ class PluginInstallerTest {
void shouldThrowPluginDownloadException() throws IOException {
when(client.get("https://download.hitchhiker.com").request()).thenThrow(new IOException("failed to download"));
assertThrows(PluginDownloadException.class, () -> installer.install(createGitPlugin()));
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
assertThrows(PluginDownloadException.class, () -> installer.install(context, gitPlugin));
}
@Test
void shouldThrowPluginChecksumMismatchException() throws IOException {
mockContent("21");
assertThrows(PluginChecksumMismatchException.class, () -> installer.install(createGitPlugin()));
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
assertThrows(PluginChecksumMismatchException.class, () -> installer.install(context, gitPlugin));
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
}
@@ -134,7 +138,9 @@ class PluginInstallerTest {
when(stream.read(any(), anyInt(), anyInt())).thenThrow(new IOException("failed to read"));
when(client.get("https://download.hitchhiker.com").request().contentAsStream()).thenReturn(stream);
assertThrows(PluginDownloadException.class, () -> installer.install(createGitPlugin()));
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
assertThrows(PluginDownloadException.class, () -> installer.install(context, gitPlugin));
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
}
@@ -144,13 +150,44 @@ class PluginInstallerTest {
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor(false);
when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
assertThrows(PluginConditionFailedException.class, () -> installer.install(createGitPlugin()));
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
assertThrows(PluginConditionFailedException.class, () -> installer.install(context, gitPlugin));
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).doesNotExist();
}
@Test
void shouldFailForNameMismatch() throws IOException {
mockContent("42");
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor("scm-svn-plugin", "1.0.0", true);
when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
PluginInformationMismatchException exception = assertThrows(PluginInformationMismatchException.class, () -> installer.install(context, gitPlugin));
assertThat(exception.getApi().getName()).isEqualTo("scm-git-plugin");
assertThat(exception.getDownloaded().getName()).isEqualTo("scm-svn-plugin");
}
@Test
void shouldFailForVersionMismatch() throws IOException {
mockContent("42");
InstalledPluginDescriptor supportedPlugin = createPluginDescriptor("scm-git-plugin", "1.1.0", true);
when(extractor.extractPluginDescriptor(any())).thenReturn(supportedPlugin);
PluginInstallationContext context = PluginInstallationContext.empty();
AvailablePlugin gitPlugin = createGitPlugin();
PluginInformationMismatchException exception = assertThrows(PluginInformationMismatchException.class, () -> installer.install(context, gitPlugin));
assertThat(exception.getApi().getVersion()).isEqualTo("1.0.0");
assertThat(exception.getDownloaded().getVersion()).isEqualTo("1.1.0");
}
private AvailablePlugin createPlugin(String name, String url, String checksum) {
PluginInformation information = new PluginInformation();
information.setName(name);
information.setVersion("1.0.0");
AvailablePluginDescriptor descriptor = new AvailablePluginDescriptor(
information, null, Collections.emptySet(), url, checksum
);
@@ -158,9 +195,15 @@ class PluginInstallerTest {
}
private InstalledPluginDescriptor createPluginDescriptor(boolean supported) {
return createPluginDescriptor("scm-git-plugin", "1.0.0", supported);
}
private InstalledPluginDescriptor createPluginDescriptor(String name, String version, boolean supported) {
InstalledPluginDescriptor installedPluginDescriptor = mock(InstalledPluginDescriptor.class, RETURNS_DEEP_STUBS);
lenient().when(installedPluginDescriptor.getInformation().getId()).thenReturn(name);
lenient().when(installedPluginDescriptor.getInformation().getName()).thenReturn(name);
lenient().when(installedPluginDescriptor.getInformation().getVersion()).thenReturn(version);
lenient().when(installedPluginDescriptor.getCondition().isSupported()).thenReturn(supported);
lenient().when(installedPluginDescriptor.getInformation().getId()).thenReturn("scm-git-plugin");
return installedPluginDescriptor;
}
}

View File

@@ -21,164 +21,182 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Charsets;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.io.Resources;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import sonia.scm.lifecycle.classloading.ClassLoaderLifeCycle;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
//~--- JDK imports ------------------------------------------------------------
import javax.xml.bind.JAXB;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
*
* @author Sebastian Sdorra
*/
public class PluginProcessorTest
{
class PluginProcessorTest {
/** Field description */
private static final PluginResource PLUGIN_A =
new PluginResource("sonia/scm/plugin/scm-a-plugin.smp", "scm-a-plugin.smp",
"scm-a-plugin:1.0.0-SNAPSHOT");
/** Field description */
private static final PluginResource PLUGIN_B =
new PluginResource("sonia/scm/plugin/scm-b-plugin.smp", "scm-b-plugin.smp",
"scm-b-plugin:1.0.0-SNAPSHOT");
/** Field description */
private static final PluginResource PLUGIN_C =
new PluginResource("sonia/scm/plugin/scm-c-plugin.smp", "scm-c-plugin.smp",
"scm-c-plugin:1.0.0-SNAPSHOT");
/** Field description */
private static final PluginResource PLUGIN_D =
new PluginResource("sonia/scm/plugin/scm-d-plugin.smp", "scm-d-plugin.smp",
"scm-d-plugin:1.0.0-SNAPSHOT");
/** Field description */
private static final PluginResource PLUGIN_E =
new PluginResource("sonia/scm/plugin/scm-e-plugin.smp", "scm-e-plugin.smp",
"scm-e-plugin:1.0.0-SNAPSHOT");
/** Field description */
private static final PluginResource PLUGIN_F_1_0_0 =
new PluginResource("sonia/scm/plugin/scm-f-plugin-1.0.0.smp",
"scm-f-plugin.smp", "scm-f-plugin:1.0.0");
/** Field description */
private static final PluginResource PLUGIN_F_1_0_1 =
new PluginResource("sonia/scm/plugin/scm-f-plugin-1.0.1.smp",
"scm-f-plugin.smp", "scm-f-plugin:1.0.1");
//~--- methods --------------------------------------------------------------
private static final String PLUGIN_G = "scm-g-plugin";
private static final String PLUGIN_H = "scm-h-plugin";
private static final String PLUGIN_I = "scm-i-plugin";
/**
* Method description
*
*
* @throws IOException
*/
@Test(expected = PluginCircularDependencyException.class)
public void testCircularDependencies() throws IOException
{
private File pluginDirectory;
private PluginProcessor processor;
@BeforeEach
void setUp(@TempDir Path tempDirectoryPath) {
pluginDirectory = tempDirectoryPath.toFile();
processor = new PluginProcessor(ClassLoaderLifeCycle.create(), tempDirectoryPath);
}
@Test
void shouldFailOnPluginCondition() throws IOException {
createPendingPluginInstallation(PLUGIN_G);
assertThrows(PluginConditionFailedException.class, this::collectPlugins);
}
@Test
void shouldFailOnWrongDependencyVersion() throws IOException {
createPendingPluginInstallation(PLUGIN_H);
createPendingPluginInstallation(PLUGIN_I);
assertThrows(DependencyVersionMismatchException.class, this::collectPlugins);
}
@Test
void shouldNotContainDuplicatesOnUpdate() throws IOException {
createInstalledPlugin("scm-mail-plugin-2-0-0");
createInstalledPlugin("scm-review-plugin-2-0-0");
createPendingPluginInstallation("scm-mail-plugin-2-1-0");
createPendingPluginInstallation("scm-review-plugin-2-1-0");
Set<String> plugins = collectPlugins().stream()
.map(p -> p.getDescriptor().getInformation().getName(true))
.collect(Collectors.toSet());
assertThat(plugins).containsOnly("scm-mail-plugin:2.1.0", "scm-review-plugin:2.1.0");
}
@SuppressWarnings("UnstableApiUsage")
private void createPendingPluginInstallation(String descriptorResource) throws IOException {
URL resource = resource(descriptorResource);
InstalledPluginDescriptor descriptor = JAXB.unmarshal(resource, InstalledPluginDescriptor.class);
File file = new File(pluginDirectory, descriptor.getInformation().getName() + ".smp");
try (ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(file))) {
zip.putNextEntry(new ZipEntry("META-INF/scm/plugin.xml"));
Resources.copy(resource, zip);
}
}
@SuppressWarnings("UnstableApiUsage")
private void createInstalledPlugin(String descriptorResource) throws IOException {
URL resource = resource(descriptorResource);
InstalledPluginDescriptor descriptor = JAXB.unmarshal(resource, InstalledPluginDescriptor.class);
File directory = new File(pluginDirectory, descriptor.getInformation().getName());
File scmDirectory = new File(directory, "META-INF" + File.separator + "scm");
assertThat(scmDirectory.mkdirs()).isTrue();
try (OutputStream output = new FileOutputStream(new File(scmDirectory, "plugin.xml"))) {
Resources.copy(resource, output);
}
}
@SuppressWarnings("UnstableApiUsage")
private URL resource(String descriptorResource) {
return Resources.getResource("sonia/scm/plugin/" + descriptorResource + ".xml");
}
@Test
void shouldFailOnCircularDependencies() throws IOException {
copySmps(PLUGIN_C, PLUGIN_D, PLUGIN_E);
collectPlugins();
assertThrows(PluginCircularDependencyException.class, this::collectPlugins);
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testCollectPlugins() throws IOException
{
void shouldCollectPlugins() throws IOException {
copySmp(PLUGIN_A);
InstalledPlugin plugin = collectAndGetFirst();
assertThat(plugin.getId(), is(PLUGIN_A.id));
assertThat(plugin.getId()).isEqualTo(PLUGIN_A.id);
}
@Test
public void shouldCollectPluginsAndDoNotFailOnNonPluginDirectories() throws IOException {
new File(pluginDirectory, "some-directory").mkdirs();
void shouldCollectPluginsAndDoNotFailOnNonPluginDirectories() throws IOException {
assertThat(new File(pluginDirectory, "some-directory").mkdirs()).isTrue();
copySmp(PLUGIN_A);
InstalledPlugin plugin = collectAndGetFirst();
assertThat(plugin.getId(), is(PLUGIN_A.id));
assertThat(plugin.getId()).isEqualTo(PLUGIN_A.id);
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testCollectPluginsWithDependencies() throws IOException
{
void shouldCollectPluginsWithDependencies() throws IOException {
copySmps(PLUGIN_A, PLUGIN_B);
Set<InstalledPlugin> plugins = collectPlugins();
assertThat(plugins, hasSize(2));
assertThat(plugins).hasSize(2);
InstalledPlugin a = findPlugin(plugins, PLUGIN_A.id);
assertNotNull(a);
assertThat(a).isNotNull();
InstalledPlugin b = findPlugin(plugins, PLUGIN_B.id);
assertNotNull(b);
assertThat(b).isNotNull();
}
/**
* Method description
*
*
* @throws ClassNotFoundException
* @throws IOException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
@Test
public void testPluginClassLoader()
throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException, IllegalArgumentException,
InvocationTargetException
{
void shouldCreateWorkingPluginClassLoader() throws Exception {
copySmp(PLUGIN_A);
InstalledPlugin plugin = collectAndGetFirst();
@@ -187,36 +205,20 @@ public class PluginProcessorTest
// load parent class
Class<?> clazz = cl.loadClass(PluginResource.class.getName());
assertSame(PluginResource.class, clazz);
assertThat(PluginResource.class).isSameAs(clazz);
// load packaged class
clazz = cl.loadClass("sonia.scm.plugins.HelloService");
assertNotNull(clazz);
assertThat(clazz).isNotNull();
Object instance = clazz.newInstance();
Object result = clazz.getMethod("sayHello").invoke(instance);
assertEquals("hello", result);
assertThat(result).isEqualTo("hello");
}
/**
* Method description
*
*
* @throws ClassNotFoundException
* @throws IOException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
@Test
public void testPluginClassLoaderWithDependencies()
throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException, NoSuchMethodException, IllegalArgumentException,
InvocationTargetException
{
void shouldCreateWorkingPluginClassLoaderWithDependencies() throws Exception {
copySmps(PLUGIN_A, PLUGIN_B);
Set<InstalledPlugin> plugins = collectPlugins();
@@ -227,213 +229,88 @@ public class PluginProcessorTest
// load parent class
Class<?> clazz = cl.loadClass(PluginResource.class.getName());
assertSame(PluginResource.class, clazz);
assertThat(PluginResource.class).isSameAs(clazz);
// load packaged class
clazz = cl.loadClass("sonia.scm.plugins.HelloAgainService");
assertNotNull(clazz);
assertThat(clazz).isNotNull();
Object instance = clazz.newInstance();
Object result = clazz.getMethod("sayHelloAgain").invoke(instance);
assertEquals("hello again", result);
assertThat(result).isEqualTo("hello again");
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testPluginWebResourceLoader() throws IOException
{
@SuppressWarnings("UnstableApiUsage")
void shouldCreatePluginWebResourceLoader() throws IOException {
copySmp(PLUGIN_A);
InstalledPlugin plugin = collectAndGetFirst();
WebResourceLoader wrl = plugin.getWebResourceLoader();
assertNotNull(wrl);
assertThat(wrl).isNotNull();
URL url = wrl.getResource("hello");
assertThat(url).isNotNull();
assertNotNull(url);
assertThat(Resources.toString(url, Charsets.UTF_8), is("hello"));
assertThat(Resources.toString(url, Charsets.UTF_8)).isEqualTo("hello");
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testUpdate() throws IOException
{
void shouldDoPluginUpdate() throws IOException {
copySmp(PLUGIN_F_1_0_0);
InstalledPlugin plugin = collectAndGetFirst();
assertThat(plugin.getId()).isEqualTo(PLUGIN_F_1_0_0.id);
assertThat(plugin.getId(), is(PLUGIN_F_1_0_0.id));
copySmp(PLUGIN_F_1_0_1);
plugin = collectAndGetFirst();
assertThat(plugin.getId(), is(PLUGIN_F_1_0_1.id));
assertThat(plugin.getId()).isEqualTo(PLUGIN_F_1_0_1.id);
}
//~--- set methods ----------------------------------------------------------
/**
* Method description
*
*
* @throws IOException
*/
@Before
public void setUp() throws IOException
{
pluginDirectory = temp.newFolder();
processor = new PluginProcessor(ClassLoaderLifeCycle.create(), pluginDirectory.toPath());
}
//~--- methods --------------------------------------------------------------
/**
* Method description
*
*
* @return
*
* @throws IOException
*/
private InstalledPlugin collectAndGetFirst() throws IOException
{
private InstalledPlugin collectAndGetFirst() throws IOException {
Set<InstalledPlugin> plugins = collectPlugins();
assertThat(plugins, hasSize(1));
assertThat(plugins).hasSize(1);
return Iterables.get(plugins, 0);
}
/**
* Method description
*
*
* @return
*
* @throws IOException
*/
private Set<InstalledPlugin> collectPlugins() throws IOException
{
private Set<InstalledPlugin> collectPlugins() throws IOException {
return processor.collectPlugins(PluginProcessorTest.class.getClassLoader());
}
/**
* Method description
*
*
* @param plugin
*
* @throws IOException
*/
private void copySmp(PluginResource plugin) throws IOException
{
@SuppressWarnings("UnstableApiUsage")
private void copySmp(PluginResource plugin) throws IOException {
URL resource = Resources.getResource(plugin.path);
File file = new File(pluginDirectory, plugin.name);
try (OutputStream out = new FileOutputStream(file))
{
try (OutputStream out = new FileOutputStream(file)) {
Resources.copy(resource, out);
}
}
/**
* Method description
*
*
* @param plugins
*
* @throws IOException
*/
private void copySmps(PluginResource... plugins) throws IOException
{
for (PluginResource plugin : plugins)
{
private void copySmps(PluginResource... plugins) throws IOException {
for (PluginResource plugin : plugins) {
copySmp(plugin);
}
}
/**
* Method description
*
*
* @param plugin
* @param id
*
* @return
*/
private InstalledPlugin findPlugin(Iterable<InstalledPlugin> plugin,
final String id)
{
return Iterables.find(plugin, new Predicate<InstalledPlugin>()
{
@Override
public boolean apply(InstalledPlugin input)
{
return id.equals(input.getId());
}
});
private InstalledPlugin findPlugin(Iterable<InstalledPlugin> plugin, final String id) {
return Iterables.find(plugin, input -> id.equals(input.getId()));
}
private static class PluginResource {
//~--- inner classes --------------------------------------------------------
private final String path;
private final String name;
private final String id;
/**
* Class description
*
*
* @version Enter version here..., 14/12/06
* @author Enter your name here...
*/
private static class PluginResource
{
/**
* Constructs ...
*
*
* @param path
* @param name
* @param id
*/
public PluginResource(String path, String name, String id)
{
public PluginResource(String path, String name, String id) {
this.path = path;
this.name = name;
this.id = id;
}
//~--- fields -------------------------------------------------------------
/** Field description */
private final String id;
/** Field description */
private final String name;
/** Field description */
private final String path;
}
//~--- fields ---------------------------------------------------------------
/** Field description */
@Rule
public TemporaryFolder temp = new TemporaryFolder();
/** Field description */
private File pluginDirectory;
/** Field description */
private PluginProcessor processor;
}

View File

@@ -63,11 +63,13 @@ class SmpDescriptorExtractorTest {
"\n" +
"</plugin>\n";
private final SmpDescriptorExtractor extractor = new SmpDescriptorExtractor();
@Test
void shouldExtractPluginXml(@TempDir Path tempDir) throws IOException {
Path pluginFile = createZipFile(tempDir, "META-INF/scm/plugin.xml", PLUGIN_XML);
InstalledPluginDescriptor installedPluginDescriptor = new SmpDescriptorExtractor().extractPluginDescriptor(pluginFile);
InstalledPluginDescriptor installedPluginDescriptor = extractor.extractPluginDescriptor(pluginFile);
Assertions.assertThat(installedPluginDescriptor.getInformation().getName()).isEqualTo("scm-test-plugin");
}
@@ -76,14 +78,14 @@ class SmpDescriptorExtractorTest {
void shouldFailWithoutPluginXml(@TempDir Path tempDir) throws IOException {
Path pluginFile = createZipFile(tempDir, "META-INF/wrong/plugin.xml", PLUGIN_XML);
assertThrows(IOException.class, () -> new SmpDescriptorExtractor().extractPluginDescriptor(pluginFile));
assertThrows(IOException.class, () -> extractor.extractPluginDescriptor(pluginFile));
}
@Test
void shouldFailWithIllegalPluginXml(@TempDir Path tempDir) throws IOException {
Path pluginFile = createZipFile(tempDir, "META-INF/scm/plugin.xml", "<not><parsable>content</parsable></not>");
assertThrows(IOException.class, () -> new SmpDescriptorExtractor().extractPluginDescriptor(pluginFile));
assertThrows(IOException.class, () -> extractor.extractPluginDescriptor(pluginFile));
}
Path createZipFile(Path tempDir, String internalFileName, String content) throws IOException {