Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-19307] Check that Computer.currentComputer() is never called
inside buildEnvironmentFor Job since it doesn't make sense outside of a running build. (cherry picked from commit 5d56102)
- Loading branch information
1 parent
8fdc471
commit 65158b0
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
core/src/test/java/jenkins/model/CoreEnvironmentContributorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package jenkins.model; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.when; | ||
import static org.powermock.api.mockito.PowerMockito.spy; | ||
import static org.powermock.api.mockito.PowerMockito.verifyStatic; | ||
import hudson.EnvVars; | ||
import hudson.model.Computer; | ||
import hudson.model.Job; | ||
import hudson.model.TaskListener; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.powermock.api.mockito.PowerMockito; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
public class CoreEnvironmentContributorTest { | ||
CoreEnvironmentContributor instance; | ||
|
||
@Mock | ||
Job job; | ||
|
||
@Mock | ||
TaskListener listener; | ||
|
||
@Mock | ||
Jenkins jenkins; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
instance = new CoreEnvironmentContributor(); | ||
} | ||
|
||
@Test | ||
@PrepareForTest(fullyQualifiedNames={"hudson.model.Computer", "jenkins.model.Jenkins"}) | ||
public void buildEnvironmentForJobShouldntUseCurrentComputer() throws IOException, InterruptedException { | ||
PowerMockito.mockStatic(Computer.class); | ||
PowerMockito.mockStatic(Jenkins.class); | ||
PowerMockito.when(Jenkins.getInstance()).thenReturn(jenkins); | ||
when(jenkins.getRootDir()).thenReturn(new File(".")); | ||
|
||
EnvVars env = new EnvVars(); | ||
instance.buildEnvironmentFor(job, env, listener); | ||
|
||
// currentComputer shouldn't be called since it relates to a running build, | ||
// which is not the case for calls of this method (e.g. polling) | ||
verifyStatic(times(0)); | ||
Computer.currentComputer(); | ||
} | ||
|
||
} |