|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2025 Damian Szczepanik |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package org.jenkinsci.plugins.workflow.steps; |
| 26 | + |
| 27 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | +import static org.hamcrest.Matchers.containsString; |
| 29 | +import static org.hamcrest.Matchers.equalTo; |
| 30 | + |
| 31 | +import hudson.model.Result; |
| 32 | +import hudson.model.Run; |
| 33 | +import jenkins.model.CauseOfInterruption; |
| 34 | +import org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty; |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Test; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests some specific {@link FlowInterruptedException} APIs |
| 42 | + */ |
| 43 | +public class FlowInterruptedExceptionTest { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void getMessageReturnsCauses() { |
| 47 | + // given |
| 48 | + Result result = Result.ABORTED; |
| 49 | + CauseOfInterruption cause1 = new ExceptionCause(new IllegalStateException("something went wrong")); |
| 50 | + CauseOfInterruption cause2 = new CauseOfInterruption.UserInterruption("admin"); |
| 51 | + CauseOfInterruption[] causes = {cause1, cause2}; |
| 52 | + |
| 53 | + // when |
| 54 | + FlowInterruptedException exception = new FlowInterruptedException(result, true, causes); |
| 55 | + |
| 56 | + // then |
| 57 | + assertThat(exception.getMessage(), equalTo(cause1.getShortDescription() + ", " + cause2.getShortDescription())); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void toStringContainsCauses() { |
| 62 | + // given |
| 63 | + Result result = Result.FAILURE; |
| 64 | + Run run = Mockito.mock(Run.class); |
| 65 | + Mockito.when(run.getDisplayName()).thenReturn("fracture.account"); |
| 66 | + CauseOfInterruption cause = new DisableConcurrentBuildsJobProperty.CancelledCause(run); |
| 67 | + |
| 68 | + // when |
| 69 | + FlowInterruptedException exception = new FlowInterruptedException(result, true, cause); |
| 70 | + |
| 71 | + // then |
| 72 | + assertThat(exception.toString(), containsString(cause.getShortDescription())); |
| 73 | + } |
| 74 | +} |
0 commit comments