Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up tests #29

Merged
merged 1 commit into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;
import static com.google.common.collect.Lists.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
Expand Down Expand Up @@ -52,7 +54,7 @@ public void setInstallersWhenCalledWhileToolSetThenSetsToolOnInstallers() throws
// Given
final TestToolInstaller installer1 = mock(TestToolInstaller.class);
final TestToolInstaller installer2 = mock(TestToolInstaller.class);
final List<ToolInstaller> installerList = newArrayList(installer1, installer2);
final List<ToolInstaller> installerList = Arrays.asList(installer1, installer2);
final InstallSourceProperty installers = new InstallSourceProperty(installerList);
final AnyOfInstaller instance = new AnyOfInstaller();
final ToolInstallation mockToolInstallation = mock(ToolInstallation.class);
Expand Down Expand Up @@ -123,7 +125,7 @@ public void getAttemptsOfWholeListGivenNegativeNumberThenReturnsOne() {
}

@Test
public void setToolWhenCalledNoInstancesThenJustSetsTool() throws Exception {
public void setToolWhenCalledNoInstancesThenJustSetsTool() {
// Given
final AnyOfInstaller instance = new AnyOfInstaller();
final ToolInstallation mockToolInstallation = mock(ToolInstallation.class);
Expand All @@ -140,7 +142,7 @@ public void setToolWhenCalledWithInstancesThenSetsToolOnThoseAsWell() throws Exc
// Given
final TestToolInstaller installer1 = mock(TestToolInstaller.class);
final TestToolInstaller installer2 = mock(TestToolInstaller.class);
final List<ToolInstaller> installerList = newArrayList(installer1, installer2);
final List<ToolInstaller> installerList = Arrays.asList(installer1, installer2);
final InstallSourceProperty installers = new InstallSourceProperty(installerList);
final AnyOfInstaller instance = new AnyOfInstaller();
instance.setInstallers(installers);
Expand Down Expand Up @@ -175,7 +177,7 @@ public void appliesToGivenNoApplicableInstallersThenReturnsFalse() throws Except
final ToolInstaller installer2 = mock(ToolInstaller.class);
when(installer1.appliesTo(mockNode)).thenReturn(false);
when(installer2.appliesTo(mockNode)).thenReturn(false);
final List<ToolInstaller> installerList = newArrayList(installer1, installer2);
final List<ToolInstaller> installerList = Arrays.asList(installer1, installer2);
final InstallSourceProperty installers = new InstallSourceProperty(installerList);
final AnyOfInstaller instance = new AnyOfInstaller();
instance.setInstallers(installers);
Expand All @@ -195,7 +197,7 @@ public void appliesToGivenOneApplicableInstallerThenReturnsTrue() throws Excepti
final ToolInstaller installer2 = mock(ToolInstaller.class);
when(installer1.appliesTo(mockNode)).thenReturn(false);
when(installer2.appliesTo(mockNode)).thenReturn(true);
final List<ToolInstaller> installerList = newArrayList(installer1, installer2);
final List<ToolInstaller> installerList = Arrays.asList(installer1, installer2);
final InstallSourceProperty installers = new InstallSourceProperty(installerList);
final AnyOfInstaller instance = new AnyOfInstaller();
instance.setInstallers(installers);
Expand All @@ -212,17 +214,17 @@ public void performInstallationGiven1Loop1FailingInstaller1AttemptThenFails() th
// Given
final Node mockNode = mock(Node.class);
final ToolInstallation mockTool = mock(ToolInstallation.class);
final List<String> actualLogRecord = newArrayList();
final List<String> actualLogRecord = new ArrayList<>();
final TaskListener mockLog = mockTaskListener(actualLogRecord);
final String installerDisplayName = "MyInstaller";
final ToolInstaller installer = mockInstaller(installerDisplayName, mockNode);
final PretendInstallerFailureException expectedCause = new PretendInstallerFailureException();
when(installer.performInstallation(mockTool, mockNode, mockLog)).thenThrow(expectedCause);
final List<ToolInstaller> installerList = newArrayList(installer);
final List<ToolInstaller> installerList = Collections.singletonList(installer);
final InstallSourceProperty installers = new InstallSourceProperty(installerList);
final AnyOfInstaller instance = new AnyOfInstaller();
instance.setInstallers(installers);
final List<String> expectedLogRecord = newArrayList(Messages.AnyOfInstaller_1loop_1installer_1attempt(1, 1, 1,
final List<String> expectedLogRecord = Collections.singletonList(Messages.AnyOfInstaller_1loop_1installer_1attempt(1, 1, 1,
1, installerDisplayName, 1, 1, expectedCause));

// When
Expand All @@ -243,7 +245,7 @@ public void performInstallationGivenOneLoopAFailingInapplicableInstallerAndAPass
// Given
final Node mockNode = mock(Node.class);
final ToolInstallation mockTool = mock(ToolInstallation.class);
final List<String> actualLogRecord = newArrayList();
final List<String> actualLogRecord = new ArrayList<>();
final TaskListener mockLog = mockTaskListener(actualLogRecord);
final String workingInstallerDisplayName = "WorkingInstaller";
final String failingInstallerDisplayName = "FailingInapplicableInstaller";
Expand All @@ -253,11 +255,11 @@ public void performInstallationGivenOneLoopAFailingInapplicableInstallerAndAPass
.thenThrow(new PretendInstallerFailureException());
final FilePath expected = stubFilePath();
when(applicableInstaller.performInstallation(mockTool, mockNode, mockLog)).thenReturn(expected);
final List<ToolInstaller> installerList = newArrayList(inapplicableInstaller, applicableInstaller);
final List<ToolInstaller> installerList = Arrays.asList(inapplicableInstaller, applicableInstaller);
final InstallSourceProperty installers = new InstallSourceProperty(installerList);
final AnyOfInstaller instance = new AnyOfInstaller();
instance.setInstallers(installers);
final List<String> expectedLogRecord = newArrayList();
final List<String> expectedLogRecord = new ArrayList<>();

// When
final FilePath actual = instance.performInstallation(mockTool, mockNode, mockLog);
Expand All @@ -275,7 +277,7 @@ public void performInstallationGivenTwoLoopsFourUnreliableInstallersThreeAttempt
// Given
final Node mockNode = mock(Node.class);
final ToolInstallation mockTool = mock(ToolInstallation.class);
final List<String> actualLogRecord = newArrayList();
final List<String> actualLogRecord = new ArrayList<>();
final TaskListener mockLog = mockTaskListener(actualLogRecord);
final String inapplicableInstallerDisplayName1 = "ShouldNotAppearAsThisIsNotApplicableToNode1";
final String inapplicableInstallerDisplayName2 = "ShouldNotAppearAsThisIsNotApplicableToNode2";
Expand Down Expand Up @@ -311,7 +313,7 @@ public void performInstallationGivenTwoLoopsFourUnreliableInstallersThreeAttempt
final ToolInstaller unreliableInstaller = mockInstaller(unreliableInstallerName, mockNode);
when(unreliableInstaller.performInstallation(mockTool, mockNode, mockLog)).thenThrow(unreliableInstallerCause1,
unreliableInstallerCause2, unreliableInstallerCause3, unreliableInstallerCause4).thenReturn(expected);
final List<ToolInstaller> installerList = newArrayList(inapplicableInstaller1, failingInstaller,
final List<ToolInstaller> installerList = Arrays.asList(inapplicableInstaller1, failingInstaller,
inapplicableInstaller2, unreliableInstaller);
final int failingInstallerIndex = 2;
final int unreliableInstallerIndex = 4;
Expand All @@ -323,7 +325,7 @@ public void performInstallationGivenTwoLoopsFourUnreliableInstallersThreeAttempt
final int insts = installerList.size();
instance.setAttemptsOfWholeList(loops);
instance.setAttemptsPerInstaller(tries);
final List<String> expectedLogRecord = newArrayList(
final List<String> expectedLogRecord = Arrays.asList(
Messages.AnyOfInstaller_loops_installers_attempts(1, loops, failingInstallerIndex, insts,
failingInstallerName, 1, tries, failingInstallerCause1),
Messages.AnyOfInstaller_loops_installers_attempts(1, loops, failingInstallerIndex, insts,
Expand Down Expand Up @@ -388,7 +390,7 @@ private static TaskListener mockTaskListener(List<String> whereToRecord) {
final PrintStream ps = mock(PrintStream.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
public Void answer(InvocationOnMock invocation) {
final Object[] args = invocation.getArguments();
final String arg = (String) args[0];
whereToRecord.add(arg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.jenkins.plugins.extratoolinstallers.installers;

import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
Expand All @@ -16,14 +15,15 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang3.SystemUtils;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import hudson.FilePath;
import hudson.Functions;
import hudson.Launcher;
import hudson.model.Node;
import hudson.model.TaskListener;
Expand Down Expand Up @@ -117,7 +117,7 @@ public void performInstallationGivenNoExeThenThrows() throws Exception {
// Given
final Node mockNode = mock(Node.class);
final ToolInstallation mockTool = mock(ToolInstallation.class);
final List<String> actualLogRecord = newArrayList();
final List<String> actualLogRecord = new ArrayList<>();
final TaskListener mockLog = mockTaskListener(actualLogRecord);
final String expectedExceptionMessage = Messages.IsAlreadyOnPath_executableNameIsEmpty();
final String expectedLabel = "foobar";
Expand All @@ -140,7 +140,7 @@ public void performInstallationGivenAbsentExeThenThrows() throws Exception {
final FilePath nodeRootPath = new FilePath(new File(".."));
when(mockNode.getRootPath()).thenReturn(nodeRootPath);
final ToolInstallation mockTool = mock(ToolInstallation.class);
final List<String> actualLogRecord = newArrayList();
final List<String> actualLogRecord = new ArrayList<>();
final TaskListener mockLog = mockTaskListener(actualLogRecord);
final String expectedLabel = "foobar";
final String expectedExecutableName = "someExeThatIsNotPresent";
Expand All @@ -162,7 +162,7 @@ public void performInstallationGivenAbsentExeThenThrows() throws Exception {
@Test
public void performInstallationGivenExeIsntExecutableThenThrows() throws Exception {
// Given
assumeFalse("Can't test this on Windows as all files are executable", SystemUtils.IS_OS_WINDOWS);
assumeFalse("Can't test this on Windows as all files are executable", Functions.isWindows());
final TestFOPInstaller instance = new TestFOPInstaller("somelabel");
final String executableName = "somevalue";
instance.setExecutableName(executableName);
Expand Down Expand Up @@ -271,7 +271,7 @@ public void performInstallationGivenFindableExeWithUnacceptableVersionThenThrows
}

@Test
public void parseVersionCmdOutputForVersionGivenNonMatchingPatternThenReturnsNull() throws Exception {
public void parseVersionCmdOutputForVersionGivenNonMatchingPatternThenReturnsNull() {
// Given
final Pattern versionPattern = Pattern.compile("git version ([0-9.]*)");
final String cmdOutput = "command\nnot\nfound";
Expand All @@ -285,7 +285,7 @@ public void parseVersionCmdOutputForVersionGivenNonMatchingPatternThenReturnsNul
}

@Test
public void parseVersionCmdOutputForVersionGivenMatchingPatternThenReturnsGroups() throws Exception {
public void parseVersionCmdOutputForVersionGivenMatchingPatternThenReturnsGroups() {
// Given
final Pattern versionPattern = Pattern.compile("git version ([0-9.]*)");
final String cmdOutput = "git version 1.2.3\n";
Expand All @@ -299,7 +299,7 @@ public void parseVersionCmdOutputForVersionGivenMatchingPatternThenReturnsGroups
}

@Test
public void checkVersionIsInRangeGivenSimpleVersionWithinRangeThenReturnsZero() throws Exception {
public void checkVersionIsInRangeGivenSimpleVersionWithinRangeThenReturnsZero() {
// Given
final String versionMin = "1.0.0";
final String versionMax = "1.99";
Expand All @@ -314,7 +314,7 @@ public void checkVersionIsInRangeGivenSimpleVersionWithinRangeThenReturnsZero()
}

@Test
public void checkVersionIsInRangeGivenVersionSlightlyBeyondRangeWithinRangeThenReturnsPositive() throws Exception {
public void checkVersionIsInRangeGivenVersionSlightlyBeyondRangeWithinRangeThenReturnsPositive() {
// Given
final String versionMin = "1.0.0";
final String versionMax = "1.2.3";
Expand All @@ -329,7 +329,7 @@ public void checkVersionIsInRangeGivenVersionSlightlyBeyondRangeWithinRangeThenR
}

@Test
public void checkVersionIsInRangeGivenAllKindsOfVersionsThenReturnsAsExpected() throws Exception {
public void checkVersionIsInRangeGivenAllKindsOfVersionsThenReturnsAsExpected() {
// Given
final String[] versionsInOrder = { null, "A", "A.", "A.1", "A1", "B", "0.1", "1", "1.A", "1.2", "1.2.3.4",
"1.2.3.4A", "1A", "2.something" };
Expand Down Expand Up @@ -381,7 +381,7 @@ private FilePath doPerformInstallationSucceeds(final TestFOPInstaller instance,
final FilePath nodeRootPath = new FilePath(new File(".."));
when(mockNode.getRootPath()).thenReturn(nodeRootPath);
final ToolInstallation mockTool = mock(ToolInstallation.class);
final List<String> actualLogRecord = newArrayList();
final List<String> actualLogRecord = new ArrayList<>();
final TaskListener mockLog = mockTaskListener(actualLogRecord);
final TestFOPCallable callable = new TestFOPCallable(expectedExecutableName, mockLog);
exeParentDir.mkdirs();
Expand Down Expand Up @@ -434,7 +434,7 @@ private static TaskListener mockTaskListener(List<String> whereToRecord) {
final PrintStream ps = mock(PrintStream.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
public Void answer(InvocationOnMock invocation) {
final Object[] args = invocation.getArguments();
final String arg = (String) args[0];
whereToRecord.add(arg);
Expand All @@ -449,7 +449,7 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
private static class TestFOPCallable extends FindOnPathCallable {
private static final long serialVersionUID = 2L;

private static interface IMock {
private interface IMock {
String getPath();
}

Expand All @@ -466,7 +466,7 @@ String getPath() {
}

private static class TestFOPInstaller extends IsAlreadyOnPath {
private static interface IMock {
private interface IMock {
FindOnPathCallable mkCallable(String exeName, TaskListener logOrNull);
void launchCmd(String[] cmd, OutputStream output) throws IOException, InterruptedException;
}
Expand Down