Skip to content

Commit

Permalink
Remove PowerMock (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Sep 26, 2021
1 parent 804ae40 commit b64d039
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 345 deletions.
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.25</version>
<version>4.27</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -135,13 +135,8 @@

<!-- Test dependencies -->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,77 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.github.RateLimitHandler;
import org.kohsuke.github.extras.OkHttpConnector;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.MockitoAnnotations;

import java.io.IOException;

import static org.junit.Assert.assertEquals;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
@PrepareForTest({GitHub.class, GitHubBuilder.class, Jenkins.class, GithubSecurityRealm.class})
public class GithubAuthenticationTokenTest {

@Mock
private Jenkins jenkins;

@Mock
private GithubSecurityRealm securityRealm;

private AutoCloseable closeable;

@Before
public void setUp() {
PowerMockito.mockStatic(Jenkins.class);
PowerMockito.when(Jenkins.get()).thenReturn(jenkins);
PowerMockito.when(jenkins.getSecurityRealm()).thenReturn(securityRealm);
PowerMockito.when(securityRealm.getOauthScopes()).thenReturn("read:org");
closeable = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
closeable.close();
}

private void mockJenkins(MockedStatic<Jenkins> mockedJenkins) {
Jenkins jenkins = Mockito.mock(Jenkins.class);
mockedJenkins.when(Jenkins::get).thenReturn(jenkins);
Mockito.when(jenkins.getSecurityRealm()).thenReturn(securityRealm);
Mockito.when(securityRealm.getOauthScopes()).thenReturn("read:org");
}

@Test
public void testTokenSerialization() throws IOException {
mockGHMyselfAs("bob");
GithubAuthenticationToken authenticationToken = new GithubAuthenticationToken("accessToken", "https://api.github.com");
byte[] serializedToken = SerializationUtils.serialize(authenticationToken);
GithubAuthenticationToken deserializedToken = (GithubAuthenticationToken) SerializationUtils.deserialize(serializedToken);
assertEquals(deserializedToken.getAccessToken(), authenticationToken.getAccessToken());
assertEquals(deserializedToken.getPrincipal(), authenticationToken.getPrincipal());
assertEquals(deserializedToken.getGithubServer(), authenticationToken.getGithubServer());
assertEquals(deserializedToken.getMyself().getLogin(), deserializedToken.getMyself().getLogin());
try (MockedStatic<Jenkins> mockedJenkins = Mockito.mockStatic(Jenkins.class);
MockedStatic<GitHubBuilder> mockedGitHubBuilder = Mockito.mockStatic(GitHubBuilder.class)) {
mockJenkins(mockedJenkins);
mockGHMyselfAs(mockedGitHubBuilder, "bob");
GithubAuthenticationToken authenticationToken = new GithubAuthenticationToken("accessToken", "https://api.github.com");
byte[] serializedToken = SerializationUtils.serialize(authenticationToken);
GithubAuthenticationToken deserializedToken = (GithubAuthenticationToken) SerializationUtils.deserialize(serializedToken);
assertEquals(deserializedToken.getAccessToken(), authenticationToken.getAccessToken());
assertEquals(deserializedToken.getPrincipal(), authenticationToken.getPrincipal());
assertEquals(deserializedToken.getGithubServer(), authenticationToken.getGithubServer());
assertEquals(deserializedToken.getMyself().getLogin(), deserializedToken.getMyself().getLogin());
}
}

@After
public void after() {
GithubAuthenticationToken.clearCaches();
}

private GHMyself mockGHMyselfAs(String username) throws IOException {
GitHub gh = PowerMockito.mock(GitHub.class);
GitHubBuilder builder = PowerMockito.mock(GitHubBuilder.class);
PowerMockito.mockStatic(GitHub.class);
PowerMockito.mockStatic(GitHubBuilder.class);
PowerMockito.when(GitHubBuilder.fromEnvironment()).thenReturn(builder);
PowerMockito.when(builder.withEndpoint("https://api.github.com")).thenReturn(builder);
PowerMockito.when(builder.withOAuthToken("accessToken")).thenReturn(builder);
PowerMockito.when(builder.withRateLimitHandler(RateLimitHandler.FAIL)).thenReturn(builder);
PowerMockito.when(builder.withConnector(Mockito.any(OkHttpConnector.class))).thenReturn(builder);
PowerMockito.when(builder.build()).thenReturn(gh);
GHMyself me = PowerMockito.mock(GHMyself.class);
PowerMockito.when(gh.getMyself()).thenReturn(me);
PowerMockito.when(me.getLogin()).thenReturn(username);
private GHMyself mockGHMyselfAs(MockedStatic<GitHubBuilder> mockedGitHubBuilder, String username) throws IOException {
GitHub gh = Mockito.mock(GitHub.class);
GitHubBuilder builder = Mockito.mock(GitHubBuilder.class);
mockedGitHubBuilder.when(GitHubBuilder::fromEnvironment).thenReturn(builder);
Mockito.when(builder.withEndpoint("https://api.github.com")).thenReturn(builder);
Mockito.when(builder.withOAuthToken("accessToken")).thenReturn(builder);
Mockito.when(builder.withRateLimitHandler(RateLimitHandler.FAIL)).thenReturn(builder);
Mockito.when(builder.withConnector(Mockito.any(OkHttpConnector.class))).thenReturn(builder);
Mockito.when(builder.build()).thenReturn(gh);
GHMyself me = Mockito.mock(GHMyself.class);
Mockito.when(gh.getMyself()).thenReturn(me);
Mockito.when(me.getLogin()).thenReturn(username);
return me;
}

Expand Down
58 changes: 34 additions & 24 deletions src/test/java/org/jenkinsci/plugins/GithubLogoutActionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,63 @@ of this software and associated documentation files (the "Software"), to deal
import jenkins.model.Jenkins;
import junit.framework.TestCase;
import org.jenkinsci.plugins.GithubSecurityRealm.DescriptorImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
@PrepareForTest({Jenkins.class, GithubSecurityRealm.class, DescriptorImpl.class})
public class GithubLogoutActionTest extends TestCase {
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

@Mock
private Jenkins jenkins;
public class GithubLogoutActionTest extends TestCase {

@Mock
private GithubSecurityRealm securityRealm;

@Mock
private DescriptorImpl descriptor;

private AutoCloseable closeable;

@Before
public void setUp() {
PowerMockito.mockStatic(Jenkins.class);
PowerMockito.when(Jenkins.get()).thenReturn(jenkins);
PowerMockito.when(jenkins.getSecurityRealm()).thenReturn(securityRealm);
PowerMockito.when(securityRealm.getDescriptor()).thenReturn(descriptor);
PowerMockito.when(descriptor.getDefaultGithubWebUri()).thenReturn("https://github.com");
closeable = MockitoAnnotations.openMocks(this);
}

@After
public void tearDown() throws Exception {
closeable.close();
}

private void mockJenkins(MockedStatic<Jenkins> mockedJenkins) {
Jenkins jenkins = Mockito.mock(Jenkins.class);
mockedJenkins.when(Jenkins::get).thenReturn(jenkins);
Mockito.when(jenkins.getSecurityRealm()).thenReturn(securityRealm);
Mockito.when(securityRealm.getDescriptor()).thenReturn(descriptor);
Mockito.when(descriptor.getDefaultGithubWebUri()).thenReturn("https://github.com");
}

private void mockGithubSecurityRealmWebUriFor(String host) {
PowerMockito.when(securityRealm.getGithubWebUri()).thenReturn(host);
Mockito.when(securityRealm.getGithubWebUri()).thenReturn(host);
}

@Test
public void testGetGitHubText_gh() {
mockGithubSecurityRealmWebUriFor("https://github.com");
GithubLogoutAction ghlogout = new GithubLogoutAction();
assertEquals("GitHub", ghlogout.getGitHubText());
try (MockedStatic<Jenkins> mockedJenkins = Mockito.mockStatic(Jenkins.class)) {
mockJenkins(mockedJenkins);
mockGithubSecurityRealmWebUriFor("https://github.com");
GithubLogoutAction ghlogout = new GithubLogoutAction();
assertEquals("GitHub", ghlogout.getGitHubText());
}
}

@Test
public void testGetGitHubText_ghe() {
mockGithubSecurityRealmWebUriFor("https://ghe.example.com");
GithubLogoutAction ghlogout = new GithubLogoutAction();
assertEquals("GitHub Enterprise", ghlogout.getGitHubText());
try (MockedStatic<Jenkins> mockedJenkins = Mockito.mockStatic(Jenkins.class)) {
mockJenkins(mockedJenkins);
mockGithubSecurityRealmWebUriFor("https://ghe.example.com");
GithubLogoutAction ghlogout = new GithubLogoutAction();
assertEquals("GitHub Enterprise", ghlogout.getGitHubText());
}
}
}
Loading

0 comments on commit b64d039

Please sign in to comment.