mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-21 06:52:11 +01:00
PluginInstaller returns now PendingPluginInstallation, to abort the installation before restart
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
class PendingPluginInstallation {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(PendingPluginInstallation.class);
|
||||
|
||||
private final AvailablePlugin plugin;
|
||||
private final File file;
|
||||
|
||||
PendingPluginInstallation(AvailablePlugin plugin, File file) {
|
||||
this.plugin = plugin;
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public AvailablePlugin getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
String name = plugin.getDescriptor().getInformation().getName();
|
||||
LOG.info("cancel installation of plugin {}", name);
|
||||
if (!file.delete()) {
|
||||
throw new PluginFailedToCancelInstallationException("failed to cancel installation of plugin " + name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
public class PluginFailedToCancelInstallationException extends RuntimeException {
|
||||
public PluginFailedToCancelInstallationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.hash.HashFunction;
|
||||
import com.google.common.hash.Hashing;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.Files;
|
||||
@@ -28,12 +26,16 @@ class PluginInstaller {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public void install(AvailablePlugin plugin) {
|
||||
public PendingPluginInstallation install(AvailablePlugin plugin) {
|
||||
File file = createFile(plugin);
|
||||
try (InputStream input = download(plugin); OutputStream output = new FileOutputStream(file)) {
|
||||
ByteStreams.copy(input, output);
|
||||
|
||||
verifyChecksum(plugin, file);
|
||||
|
||||
// TODO clean up in case of error
|
||||
|
||||
return new PendingPluginInstallation(plugin, file);
|
||||
} catch (IOException ex) {
|
||||
throw new PluginDownloadException("failed to install plugin", ex);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junitpioneer.jupiter.TempDirectory;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith({MockitoExtension.class, TempDirectory.class})
|
||||
class PendingPluginInstallationTest {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private AvailablePlugin plugin;
|
||||
|
||||
@Test
|
||||
void shouldDeleteFileOnCancel(@TempDirectory.TempDir Path directory) throws IOException {
|
||||
Path file = directory.resolve("file");
|
||||
Files.write(file, "42".getBytes());
|
||||
|
||||
when(plugin.getDescriptor().getInformation().getName()).thenReturn("scm-awesome-plugin");
|
||||
|
||||
PendingPluginInstallation installation = new PendingPluginInstallation(plugin, file.toFile());
|
||||
installation.cancel();
|
||||
|
||||
assertThat(file).doesNotExist();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfCancelFailed(@TempDirectory.TempDir Path directory) {
|
||||
Path file = directory.resolve("file");
|
||||
when(plugin.getDescriptor().getInformation().getName()).thenReturn("scm-awesome-plugin");
|
||||
|
||||
PendingPluginInstallation installation = new PendingPluginInstallation(plugin, file.toFile());
|
||||
assertThrows(PluginFailedToCancelInstallationException.class, installation::cancel);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -51,6 +51,17 @@ class PluginInstallerTest {
|
||||
assertThat(directory.resolve("plugins").resolve("scm-git-plugin.smp")).hasContent("42");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnPendingPluginInstallation() throws IOException {
|
||||
mockContent("42");
|
||||
AvailablePlugin gitPlugin = createGitPlugin();
|
||||
|
||||
PendingPluginInstallation pending = installer.install(gitPlugin);
|
||||
|
||||
assertThat(pending).isNotNull();
|
||||
assertThat(pending.getPlugin()).isSameAs(gitPlugin);
|
||||
}
|
||||
|
||||
private void mockContent(String content) throws IOException {
|
||||
when(client.get("https://download.hitchhiker.com").request().contentAsStream())
|
||||
.thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
Reference in New Issue
Block a user