Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove PowerMock #129

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 3 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.16</version>
<version>4.27</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -33,7 +33,7 @@
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.235.x</artifactId>
<version>26</version>
<version>918.vae501d2cdc99</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down Expand Up @@ -82,7 +82,7 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
Expand All @@ -91,26 +91,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<scope>test</scope>
</dependency>
<!-- JENKINS-37812. Dependencies for test -->
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
57 changes: 30 additions & 27 deletions src/test/java/hudson/tasks/MailAddressResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -40,19 +42,12 @@

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Bug;
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.MockedStatic;

/**
* @author Kohsuke Kawaguchi, ogondza
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {MailAddressResolver.class, Mailer.class, Mailer.DescriptorImpl.class})
@PowerMockIgnore({"javax.security.*", "javax.xml.*"})
public class MailAddressResolverTest {

private User user;
Expand All @@ -62,44 +57,51 @@ public class MailAddressResolverTest {
@Before
public void setUp() throws Exception {

jenkins = PowerMockito.mock(Hudson.class);
jenkins = mock(Hudson.class);

user = PowerMockito.mock(User.class);
user = mock(User.class);
when(user.getFullName()).thenReturn("Full name");
when(user.getId()).thenReturn("user_id");

PowerMockito.spy(Mailer.class);
descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class);
PowerMockito.doReturn(descriptor).when(Mailer.class, "descriptor");
descriptor = mock(Mailer.DescriptorImpl.class);
}

@Test
public void nameAlreadyIsAnAddress() throws Exception {

try (MockedStatic<Mailer> mockedMailer = mockStatic(Mailer.class)) {
mockedMailer.when(Mailer::descriptor).thenReturn(descriptor);
validateUserPropertyAddress("user@example.com", "user@example.com", "");
}
}

@Test
public void nameContainsAddress() throws Exception {

try (MockedStatic<Mailer> mockedMailer = mockStatic(Mailer.class)) {
mockedMailer.when(Mailer::descriptor).thenReturn(descriptor);
validateUserPropertyAddress("user@example.com", "User Name <user@example.com>", "");
}
}

@Bug(5164)
@Test
public void test5164() throws Exception {

try (MockedStatic<Mailer> mockedMailer = mockStatic(Mailer.class)) {
mockedMailer.when(Mailer::descriptor).thenReturn(descriptor);
validateUserPropertyAddress("user@example.com", "DOMAIN\\user", "@example.com");
}
}

private void validateUserPropertyAddress(
String address, String username, String suffix
) throws Exception {

PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn(suffix);
when(descriptor.getDefaultSuffix()).thenReturn(suffix);

PowerMockito.when(user.getFullName()).thenReturn(username);
PowerMockito.when(user.getId()).thenReturn(username.replace('\\','_'));
when(user.getFullName()).thenReturn(username);
when(user.getId()).thenReturn(username.replace('\\','_'));

String a = new UserPropertyMock(user, null).getConfiguredAddress();
assertEquals(address, a);
Expand All @@ -115,31 +117,37 @@ public UserPropertyMock(User user, String emailAddress) {

@Test
public void doNotResolveWhenUsingFastResolution() throws Exception {
try (MockedStatic<Mailer> mockedMailer = mockStatic(Mailer.class);
MockedStatic<ExtensionList> mockedExtensionList = mockStatic(ExtensionList.class)) {
mockedMailer.when(Mailer::descriptor).thenReturn(descriptor);

final MailAddressResolver resolver = mockResolver();

configure(resolver);
configure(mockedExtensionList, resolver);

final String address = MailAddressResolver.resolveFast(user);

verify(resolver, never()).findMailAddressFor(user);

assertNull(address);
}
}

@Test
public void doResolveWhenNotUsingFastResolution() throws Exception {

try (MockedStatic<ExtensionList> mockedExtensionList = mockStatic(ExtensionList.class)) {
final MailAddressResolver resolver = mockResolver();
PowerMockito.when(resolver.findMailAddressFor(user)).thenReturn("a@b.c");
when(resolver.findMailAddressFor(user)).thenReturn("a@b.c");

configure(resolver);
configure(mockedExtensionList, resolver);

final String address = MailAddressResolver.resolve(user);

verify(resolver, times(1)).findMailAddressFor(user);

assertEquals("a@b.c", address);
}
}

@Test
Expand All @@ -155,16 +163,11 @@ public void doResolveWhenUsingExplicitlUserEmail() {

private MailAddressResolver mockResolver() {

return PowerMockito.mock(MailAddressResolver.class);
return mock(MailAddressResolver.class);
}

private void configure(final MailAddressResolver... resolvers) throws Exception {

PowerMockito.spy(MailAddressResolver.class);

PowerMockito.doReturn(new MockExtensionList(jenkins, resolvers))
.when(MailAddressResolver.class, "all")
;
private void configure(final MockedStatic<ExtensionList> mockedExtensionList, final MailAddressResolver... resolvers) {
mockedExtensionList.when(() -> ExtensionList.lookup(MailAddressResolver.class)).thenReturn(new MockExtensionList(jenkins, resolvers));
}

private static class MockExtensionList extends ExtensionList<MailAddressResolver> {
Expand Down
38 changes: 18 additions & 20 deletions src/test/java/hudson/tasks/MailSenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
import org.acegisecurity.Authentication;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Issue;
import static 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import org.mockito.MockedStatic;

/**
* Test case for the {@link MailSender}
Expand All @@ -37,9 +38,6 @@
*
* @author Christoph Kutzinski
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(Jenkins.class)
@PowerMockIgnore({"javax.security.auth.Subject", "javax.security.*", "javax.xml.*"}) // otherwise as in https://groups.google.com/d/msg/jenkinsci-dev/n5sdCxrccSk/7K4yTTc7XG4J mock(ACL.class) in Java 8 fails with: java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "org.acegisecurity.Authentication$$EnhancerByMockitoWithCGLIB$$31bf4863.implies(Ljavax/security/auth/Subject;)Z" the class loader (instance of org/powermock/core/classloader/MockClassLoader) of the current class, org/acegisecurity/Authentication$$EnhancerByMockitoWithCGLIB$$31bf4863, and the class loader (instance of <bootloader>) for interface java/security/Principal have different Class objects for the type javax/security/auth/Subject used in the signature
@SuppressWarnings("rawtypes")
public class MailSenderTest {

Expand All @@ -51,11 +49,10 @@ public class MailSenderTest {
@SuppressWarnings("unchecked")
@Test
public void testIncludeUpstreamCulprits() throws Exception {
final Jenkins jenkins = PowerMockito.mock(Jenkins.class);
PowerMockito.when(jenkins.isUseSecurity()).thenReturn(false);
PowerMockito.mockStatic(Jenkins.class);
PowerMockito.doReturn(jenkins).when(Jenkins.class, "getInstanceOrNull");
PowerMockito.doReturn(jenkins).when(Jenkins.class, "get");
final Jenkins jenkins = mock(Jenkins.class);
when(jenkins.isUseSecurity()).thenReturn(false);
try (MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)) {
mockedJenkins.when(Jenkins::get).thenReturn(jenkins);

AbstractProject upstreamProject = mock(AbstractProject.class);

Expand Down Expand Up @@ -112,6 +109,7 @@ public void testIncludeUpstreamCulprits() throws Exception {
assertFalse(emailList.contains("this.one.should.not.be.included@example.com"));
assertTrue(emailList.contains("this.one.must.be.included@example.com"));
assertTrue(emailList.contains("this.one.must.be.included.too@example.com"));
}
}

/**
Expand All @@ -135,11 +133,10 @@ private static void createPreviousNextRelationShip(AbstractBuild... builds) {

@Issue("SECURITY-372")
@Test public void forbiddenMail() throws Exception {
final Jenkins jenkins = PowerMockito.mock(Jenkins.class);
PowerMockito.when(jenkins.isUseSecurity()).thenReturn(true);
PowerMockito.mockStatic(Jenkins.class);
PowerMockito.doReturn(jenkins).when(Jenkins.class, "getInstanceOrNull");
PowerMockito.doReturn(jenkins).when(Jenkins.class, "get");
final Jenkins jenkins = mock(Jenkins.class);
when(jenkins.isUseSecurity()).thenReturn(true);
try (MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)) {
mockedJenkins.when(Jenkins::get).thenReturn(jenkins);
ACL acl = mock(ACL.class);
User authorizedU = mock(User.class);
when(authorizedU.getProperty(Mailer.UserProperty.class)).thenReturn(new Mailer.UserProperty("authorized@mycorp"));
Expand Down Expand Up @@ -186,6 +183,7 @@ private static void createPreviousNextRelationShip(AbstractBuild... builds) {
} finally {
MailSender.SEND_TO_USERS_WITHOUT_READ = false;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.MockedStatic;
import org.mockito.Mockito;

/**
* @author Mudiaga Obada
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ MailAddressFilter.class, AbstractBuild.class, BuildListener.class })
@PowerMockIgnore({"javax.security.*", "javax.xml.*"})
public class MailAddressFilterTest {

private Hudson jenkins;
Expand All @@ -64,16 +58,19 @@ public class MailAddressFilterTest {
@Before
public void setUp() throws Exception {

jenkins = PowerMockito.mock(Hudson.class);
build = PowerMockito.mock(AbstractBuild.class);
listener = PowerMockito.mock(BuildListener.class);
jenkins = Mockito.mock(Hudson.class);
build = Mockito.mock(AbstractBuild.class);
listener = Mockito.mock(BuildListener.class);

}

// Without any extension, filter should return identical set
@Test
public void testIdentity() throws Exception {

try (MockedStatic<Jenkins> mocked = Mockito.mockStatic(Jenkins.class)) {
mocked.when(Jenkins::get).thenReturn(jenkins);

Set<InternetAddress> rcp = getRecipients();

configure(Collections.<MailAddressFilter> emptyList());
Expand All @@ -84,20 +81,24 @@ public void testIdentity() throws Exception {
for (InternetAddress a : rcp) {
Assert.assertTrue(filtered.contains(a));
}
}

}

// With an extension, MailAddressFilter must exclude what extension filters
@Test
public void testFilterExtension() throws Exception {

try (MockedStatic<Jenkins> mocked = Mockito.mockStatic(Jenkins.class)) {
mocked.when(Jenkins::get).thenReturn(jenkins);

InternetAddress filteredAddress = new InternetAddress("systemUser@example.com");

Set<InternetAddress> rcp = getRecipients();
rcp.add(filteredAddress);

MailAddressFilter filter = PowerMockito.mock(MailAddressFilter.class);
PowerMockito.when(filter.shouldFilter(build, listener, filteredAddress)).thenReturn(true);
MailAddressFilter filter = Mockito.mock(MailAddressFilter.class);
Mockito.when(filter.shouldFilter(build, listener, filteredAddress)).thenReturn(true);

configure(Arrays.asList(filter));

Expand All @@ -106,6 +107,7 @@ public void testFilterExtension() throws Exception {
Assert.assertEquals(rcp.size() - 1, filtered.size());

Assert.assertTrue(!filtered.contains(filteredAddress));
}

}

Expand All @@ -119,9 +121,7 @@ private Set<InternetAddress> getRecipients() throws AddressException {

private void configure(List<MailAddressFilter> filters) throws Exception {

PowerMockito.spy(MailAddressFilter.class);

PowerMockito.doReturn(new MockExtensionList(jenkins, filters)).when(MailAddressFilter.class, "allExtensions");
Mockito.when(jenkins.getExtensionList(MailAddressFilter.class)).thenReturn(new MockExtensionList(jenkins, filters));
}

private static class MockExtensionList extends ExtensionList<MailAddressFilter> {
Expand Down