From 977b282a534f0340a84b2fa7a57c7da2910ce839 Mon Sep 17 00:00:00 2001 From: arus2023 Date: Wed, 27 Sep 2023 19:12:13 -0500 Subject: [PATCH 1/2] Removed 11 unnecessary stubbings in ChangesSinceLastSuccessfulBuildMacroTest.java --- ...ngesSinceLastSuccessfulBuildMacroTest.java | 47 +++++++++++++------ 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java b/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java index c7007fb..d035848 100644 --- a/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java +++ b/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java @@ -53,7 +53,7 @@ public void testGetContent_shouldGetPreviousBuildFailures() throws Exception { AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(currentBuild); @@ -76,9 +76,8 @@ public void testGetContent_whenReverseOrderIsTrueShouldReverseOrderOfChanges() AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); - when(failureBuild.getNextBuild()).thenReturn(currentBuild); String contentStr = content.evaluate(currentBuild, listener, ChangesSinceLastSuccessfulBuildMacro.MACRO_NAME); @@ -91,11 +90,10 @@ public void testGetContent_shouldGetPreviousBuildsThatArentSuccessful_HUDSON3519 throws Exception { // Test for HUDSON-3519 - AbstractBuild successfulBuild = createBuild(Result.SUCCESS, 2, "Changes for a successful build."); + AbstractBuild successfulBuild = createBuild2(Result.SUCCESS, 2, "Changes for a successful build."); AbstractBuild unstableBuild = createBuild(Result.UNSTABLE, 3, "Changes for an unstable build."); when(unstableBuild.getPreviousBuild()).thenReturn(successfulBuild); - when(successfulBuild.getNextBuild()).thenReturn(unstableBuild); AbstractBuild abortedBuild = createBuild(Result.ABORTED, 4, "Changes for an aborted build."); when(abortedBuild.getPreviousBuild()).thenReturn(unstableBuild); @@ -109,7 +107,7 @@ public void testGetContent_shouldGetPreviousBuildsThatArentSuccessful_HUDSON3519 when(notBuiltBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(notBuiltBuild); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 7, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 7, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(notBuiltBuild); when(notBuiltBuild.getNextBuild()).thenReturn(currentBuild); @@ -144,7 +142,7 @@ public void testShouldPrintDate() AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(currentBuild); @@ -162,7 +160,7 @@ public void testShouldPrintRevision() content.changesFormat = "%r"; AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(currentBuild); @@ -177,7 +175,7 @@ public void testShouldPrintPath() content.changesFormat = "%p"; AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(currentBuild); @@ -193,7 +191,7 @@ public void testWhenShowPathsIsTrueShouldPrintPath() AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(currentBuild); @@ -213,7 +211,7 @@ public void testRegexReplace() AbstractBuild failureBuild = createBuild(Result.FAILURE, 41, "DEFECT-666Changes for a failed build."); - AbstractBuild currentBuild = createBuild(Result.SUCCESS, 42, "DEFECT-666Changes for a successful build."); + AbstractBuild currentBuild = createBuild3(Result.SUCCESS, 42, "DEFECT-666Changes for a successful build."); when(currentBuild.getPreviousBuild()).thenReturn(failureBuild); when(failureBuild.getNextBuild()).thenReturn(currentBuild); @@ -336,9 +334,7 @@ public void testShouldEscapeHtmlWhenArgumentEscapeHtmlSetToTrue() private AbstractBuild createBuildWithNoChanges(Result result, int buildNumber) { AbstractBuild build = mock(AbstractBuild.class); - when(build.getResult()).thenReturn(result); ChangeLogSet changes1 = createEmptyChangeLog(); - when(build.getChangeSet()).thenReturn(changes1); when(build.getChangeSets()).thenReturn(Collections.singletonList(changes1)); when(build.getNumber()).thenReturn(buildNumber); return build; @@ -346,7 +342,6 @@ private AbstractBuild createBuildWithNoChanges(Result result, int buildNumber) { private AbstractBuild createBuildWithNoChangeSets(Result result, int buildNumber) { AbstractBuild build = mock(AbstractBuild.class); - when(build.getResult()).thenReturn(result); when(build.getChangeSets()).thenReturn(Collections.EMPTY_LIST); when(build.getNumber()).thenReturn(buildNumber); @@ -367,7 +362,6 @@ private AbstractBuild createBuild(Result result, int buildNumber, String message public ChangeLogSet createEmptyChangeLog() { ChangeLogSet changes = mock(ChangeLogSet.class); List entries = Collections.emptyList(); - when(changes.iterator()).thenReturn(entries.iterator()); when(changes.isEmptySet()).thenReturn(true); return changes; @@ -429,4 +423,27 @@ public long getTimestamp() { return 1382409540000L; } } + + public ChangeLogSet createChangeLog2(String message) { + ChangeLogSet changes = mock(ChangeLogSet.class); + List entries = new LinkedList(); + ChangeLogSet.Entry entry = new ChangeLogEntry(message, "Ash Lux"); + entries.add(entry); + return changes; + } + + private AbstractBuild createBuild2(Result result, int buildNumber, String message) { + AbstractBuild build = mock(AbstractBuild.class); + ChangeLogSet changes1 = createChangeLog2(message); + when(build.getResult()).thenReturn(result); + return build; + } + + private AbstractBuild createBuild3(Result result, int buildNumber, String message) { + AbstractBuild build = mock(AbstractBuild.class); + ChangeLogSet changes1 = createChangeLog(message); + when(build.getChangeSets()).thenReturn(Collections.singletonList(changes1)); + when(build.getNumber()).thenReturn(buildNumber); + return build; + } } From 91d10df44a6f8bc926d98472c08e1109e4214cd5 Mon Sep 17 00:00:00 2001 From: arus2023 Date: Fri, 20 Oct 2023 18:18:43 -0500 Subject: [PATCH 2/2] Removed 1 unnecessary stubbings in ChangesSinceLastSuccessfulBuildMacroTest.java --- .../impl/ChangesSinceLastSuccessfulBuildMacroTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java b/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java index d035848..1f486ee 100644 --- a/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java +++ b/src/test/java/org/jenkinsci/plugins/tokenmacro/impl/ChangesSinceLastSuccessfulBuildMacroTest.java @@ -352,7 +352,6 @@ private AbstractBuild createBuild(Result result, int buildNumber, String message AbstractBuild build = mock(AbstractBuild.class); when(build.getResult()).thenReturn(result); ChangeLogSet changes1 = createChangeLog(message); - when(build.getChangeSet()).thenReturn(changes1); when(build.getChangeSets()).thenReturn(Collections.singletonList(changes1)); when(build.getNumber()).thenReturn(buildNumber);