Skip to content

Commit

Permalink
made-changes
Browse files Browse the repository at this point in the history
Signed-off-by: shinigami-777 <[email protected]>
  • Loading branch information
shinigami-777 committed Dec 21, 2024
1 parent 8227058 commit 72b023d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
16 changes: 2 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-${jenkins.baseline}.x</artifactId>
<version>3814.v9563d972079a_</version>
<version>3850.vb_c5319efa_e29</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -92,18 +92,6 @@
<artifactId>throttle-concurrents</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand All @@ -120,4 +108,4 @@
</pluginRepository>
</pluginRepositories>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public Action getAction(AbstractBuild<?, ?> build, TaskListener listener) throws
labelExpanded = TokenMacro.expandAll(build, listener, labelExpanded);
} catch (MacroEvaluationException e) {
labelExpanded = nodeLabel;
listener.getLogger().println("Token expansion failed.");
e.printStackTrace(listener.getLogger());
}
LabelParameterValue parameterValue =
new LabelParameterValue(name, labelExpanded, false, new AllNodeEligibility());
listener.getLogger().println("define: " + parameterValue);

return new ParametersAction(parameterValue);
}

Expand All @@ -54,4 +54,4 @@ public String getDisplayName() {
return "NodeLabel parameter";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
*/
package org.jvnet.jenkins.plugins.nodelabelparameter.parameterizedtrigger;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import hudson.model.AbstractBuild;
import hudson.model.Action;
import hudson.model.AutoCompletionCandidates;
Expand All @@ -45,11 +42,11 @@
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.BuildWrapper;
import hudson.util.FormValidation;
import hudson.util.StreamTaskListener;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -62,7 +59,6 @@
import org.jvnet.jenkins.plugins.nodelabelparameter.NodeParameterValue;
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;
import org.jvnet.jenkins.plugins.nodelabelparameter.wrapper.TriggerNextBuildWrapper;
import org.mockito.Mockito;

public class NodeLabelBuildParameterTest {

Expand Down Expand Up @@ -279,26 +275,24 @@ public void testValidateBuildNoExceptionIfConcurrentBuildsAllowed() throws Excep

@Test
public void testMacroEvaluationExceptionHandling() throws Exception {
String name = "TestName";
String nodeLabel = "DefaultLabel";
String name = "Dummy";
String nodeLabel = "${TEST, arg = \"a \n b \r\n c\"}\n";
// nodeLabel string causes the MacroEvaluationException to occur
NodeLabelBuildParameter nodeLabelBuildParameter = new NodeLabelBuildParameter(name, nodeLabel);
// Mock the AbstractBuild, TaskListener and its logger
AbstractBuild<?, ?> build = mock(AbstractBuild.class);
TaskListener listener = mock(TaskListener.class);
PrintStream logger = mock(PrintStream.class);
when(listener.getLogger()).thenReturn(logger);

// Mock TokenMacro to throw MacroEvaluationException
Mockito.mockStatic(TokenMacro.class);
when(TokenMacro.expandAll(build, listener, nodeLabel))
.thenThrow(new MacroEvaluationException("Token expansion failed."));

Action result = nodeLabelBuildParameter.getAction(build, listener);

// Verify
assertNotNull(result, "The result action should not be null.");
verify(listener.getLogger(), times(1)).println(contains("define:"));
verify(logger, atLeastOnce()).println(contains("Token expansion failed."));

ByteArrayOutputStream logStream = new ByteArrayOutputStream();
TaskListener listener = new StreamTaskListener(new PrintStream(logStream));

FreeStyleProject project = j.createFreeStyleProject("projectB");
FreeStyleBuild build = new FreeStyleBuild(project);
AbstractBuild<?, ?> abstractBuild = build;

Action result = nodeLabelBuildParameter.getAction(abstractBuild, listener);

Assert.assertNotNull("The result action should not be null.", result);
String loggedOutput = logStream.toString();
Assert.assertTrue(
loggedOutput, loggedOutput.contains("org.jenkinsci.plugins.tokenmacro.MacroEvaluationException"));
}

@Test
Expand Down

0 comments on commit 72b023d

Please sign in to comment.