Skip to content

Commit

Permalink
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
Vlatombe authored and olivergondza committed Oct 18, 2013
1 parent 8fdc471 commit 65158b0
Showing 1 changed file with 61 additions and 0 deletions.
@@ -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();
}

}

0 comments on commit 65158b0

Please sign in to comment.