Skip to content

Commit

Permalink
💣 Remove powermock (#5736)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored Sep 24, 2021
1 parent ffeadea commit ddfacc2
Show file tree
Hide file tree
Showing 25 changed files with 695 additions and 752 deletions.
12 changes: 1 addition & 11 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -418,17 +418,7 @@ THE SOFTWARE.
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<dependency><!-- needed by Jelly -->
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/jenkins/security/ClassFilterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void unregister() {
}

private static void mockOff() {
LOGGER.warning("Disabling class filtering since we appear to be in a special test environment, perhaps Mockito/PowerMock");
LOGGER.warning("Disabling class filtering since we appear to be in a special test environment, perhaps Mockito");
ClassFilter.setDefault(ClassFilter.NONE); // even Method on the standard blacklist is going to explode
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/hudson/FilePathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ private void checkTarUntarRoundTrip(String filePrefix, long fileSize) throws Exc
}

@Test public void copyToWithPermissionSpecialPermissions() throws IOException, InterruptedException {
assumeFalse("Test uses POSIX-specific features", Functions.isWindows());
assumeFalse("Test uses POSIX-specific features", Functions.isWindows() || Platform.isDarwin());
File tmp = temp.getRoot();
File original = new File(tmp,"original");
FilePath originalP = new FilePath(channels.french, original.getPath());
Expand Down
181 changes: 95 additions & 86 deletions core/src/test/java/hudson/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import hudson.model.Action;
import hudson.model.Computer;
Expand All @@ -53,17 +53,12 @@
import jenkins.model.Jenkins;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.Issue;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.mockito.MockedStatic;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
public class FunctionsTest {
@Test
public void testGetActionUrl_absoluteUriWithAuthority(){
Expand Down Expand Up @@ -94,24 +89,24 @@ public void testGetActionUrl_absoluteUriWithoutAuthority(){
}

@Test
@PrepareForTest(Stapler.class)
public void testGetActionUrl_absolutePath() {
String contextPath = "/jenkins";
StaplerRequest req = createMockRequest(contextPath);
String[] paths = {
"/",
"/foo/bar",
};
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
for(String path : paths) {
String result = Functions.getActionUrl(null, createMockAction(path));
assertEquals(contextPath + path, result);

try (MockedStatic<Stapler> mocked = mockStatic(Stapler.class)) {
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
for(String path : paths) {
String result = Functions.getActionUrl(null, createMockAction(path));
assertEquals(contextPath + path, result);
}
}
}

@Test
@PrepareForTest(Stapler.class)
public void testGetActionUrl_relativePath() {
String contextPath = "/jenkins";
String itUrl = "iturl/";
Expand All @@ -121,105 +116,121 @@ public void testGetActionUrl_relativePath() {
"./foo/bar",
"../foo/bar",
};
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
for(String path : paths) {
String result = Functions.getActionUrl(itUrl, createMockAction(path));
assertEquals(contextPath + "/" + itUrl + path, result);
try (MockedStatic<Stapler> mocked = mockStatic(Stapler.class)) {
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
for (String path : paths) {
String result = Functions.getActionUrl(itUrl, createMockAction(path));
assertEquals(contextPath + "/" + itUrl + path, result);
}
}
}

@Test
@PrepareForTest({Stapler.class, Jenkins.class})
public void testGetRelativeLinkTo_JobContainedInView() {
Jenkins j = createMockJenkins();
ItemGroup parent = j;
String contextPath = "/jenkins";
StaplerRequest req = createMockRequest(contextPath);
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
View view = mock(View.class);
when(view.getOwner()).thenReturn(j);
when(j.getItemGroup()).thenReturn(j);
createMockAncestors(req, createAncestor(view, "."), createAncestor(j, "../.."));
TopLevelItem i = createMockItem(parent, "job/i/");
when(view.getItems()).thenReturn(Collections.singletonList(i));
String result = Functions.getRelativeLinkTo(i);
assertEquals("job/i/", result);
try (
MockedStatic<Stapler> mocked = mockStatic(Stapler.class);
MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)
) {
Jenkins j = createMockJenkins(mockedJenkins);
ItemGroup parent = j;
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
View view = mock(View.class);
when(view.getOwner()).thenReturn(j);
when(j.getItemGroup()).thenReturn(j);
createMockAncestors(req, createAncestor(view, "."), createAncestor(j, "../.."));
TopLevelItem i = createMockItem(parent, "job/i/");
when(view.getItems()).thenReturn(Collections.singletonList(i));
String result = Functions.getRelativeLinkTo(i);
assertEquals("job/i/", result);
}
}

@Test
@PrepareForTest({Stapler.class, Jenkins.class})
public void testGetRelativeLinkTo_JobFromComputer() {
Jenkins j = createMockJenkins();
ItemGroup parent = j;
String contextPath = "/jenkins";
StaplerRequest req = createMockRequest(contextPath);
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
Computer computer = mock(Computer.class);
createMockAncestors(req, createAncestor(computer, "."), createAncestor(j, "../.."));
TopLevelItem i = createMockItem(parent, "job/i/");
String result = Functions.getRelativeLinkTo(i);
assertEquals("/jenkins/job/i/", result);
try (
MockedStatic<Stapler> mocked = mockStatic(Stapler.class);
MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)
) {
Jenkins j = createMockJenkins(mockedJenkins);
ItemGroup parent = j;
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
Computer computer = mock(Computer.class);
createMockAncestors(req, createAncestor(computer, "."), createAncestor(j, "../.."));
TopLevelItem i = createMockItem(parent, "job/i/");
String result = Functions.getRelativeLinkTo(i);
assertEquals("/jenkins/job/i/", result);
}
}

@Ignore("too expensive to make it correct")
@Test
@PrepareForTest({Stapler.class, Jenkins.class})
public void testGetRelativeLinkTo_JobNotContainedInView() {
Jenkins j = createMockJenkins();
ItemGroup parent = j;
String contextPath = "/jenkins";
StaplerRequest req = createMockRequest(contextPath);
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
View view = mock(View.class);
when(view.getOwner().getItemGroup()).thenReturn(parent);
createMockAncestors(req, createAncestor(j, "../.."), createAncestor(view, "."));
TopLevelItem i = createMockItem(parent, "job/i/");
when(view.getItems()).thenReturn(Collections.emptyList());
String result = Functions.getRelativeLinkTo(i);
assertEquals("/jenkins/job/i/", result);
try (
MockedStatic<Stapler> mocked = mockStatic(Stapler.class);
MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)
) {
Jenkins j = createMockJenkins(mockedJenkins);
ItemGroup parent = j;
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
View view = mock(View.class);
when(view.getOwner().getItemGroup()).thenReturn(parent);
createMockAncestors(req, createAncestor(j, "../.."), createAncestor(view, "."));
TopLevelItem i = createMockItem(parent, "job/i/");
when(view.getItems()).thenReturn(Collections.emptyList());
String result = Functions.getRelativeLinkTo(i);
assertEquals("/jenkins/job/i/", result);
}
}

private interface TopLevelItemAndItemGroup <T extends TopLevelItem> extends TopLevelItem, ItemGroup<T>, ViewGroup {}

@Test
@PrepareForTest({Stapler.class,Jenkins.class})
public void testGetRelativeLinkTo_JobContainedInViewWithinItemGroup() {
Jenkins j = createMockJenkins();
TopLevelItemAndItemGroup parent = mock(TopLevelItemAndItemGroup.class);
when(parent.getShortUrl()).thenReturn("parent/");
String contextPath = "/jenkins";
StaplerRequest req = createMockRequest(contextPath);
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
View view = mock(View.class);
when(view.getOwner()).thenReturn(parent);
when(parent.getItemGroup()).thenReturn(parent);
createMockAncestors(req, createAncestor(j, "../../.."), createAncestor(parent, "../.."), createAncestor(view, "."));
TopLevelItem i = createMockItem(parent, "job/i/", "parent/job/i/");
when(view.getItems()).thenReturn(Collections.singletonList(i));
String result = Functions.getRelativeLinkTo(i);
assertEquals("job/i/", result);
try (
MockedStatic<Stapler> mocked = mockStatic(Stapler.class);
MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)
) {
Jenkins j = createMockJenkins(mockedJenkins);
TopLevelItemAndItemGroup parent = mock(TopLevelItemAndItemGroup.class);
when(parent.getShortUrl()).thenReturn("parent/");
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
View view = mock(View.class);
when(view.getOwner()).thenReturn(parent);
when(parent.getItemGroup()).thenReturn(parent);
createMockAncestors(req, createAncestor(j, "../../.."), createAncestor(parent, "../.."), createAncestor(view, "."));
TopLevelItem i = createMockItem(parent, "job/i/", "parent/job/i/");
when(view.getItems()).thenReturn(Collections.singletonList(i));
String result = Functions.getRelativeLinkTo(i);
assertEquals("job/i/", result);
}
}

@Issue("JENKINS-17713")
@PrepareForTest({Stapler.class, Jenkins.class})
@Test public void getRelativeLinkTo_MavenModules() {
Jenkins j = createMockJenkins();
StaplerRequest req = createMockRequest("/jenkins");
mockStatic(Stapler.class);
when(Stapler.getCurrentRequest()).thenReturn(req);
TopLevelItemAndItemGroup ms = mock(TopLevelItemAndItemGroup.class);
when(ms.getShortUrl()).thenReturn("job/ms/");
// TODO "." (in second ancestor) is what Stapler currently fails to do. Could edit test to use ".." but set a different request path?
createMockAncestors(req, createAncestor(j, "../.."), createAncestor(ms, "."));
Item m = mock(Item.class);
when(m.getParent()).thenReturn(ms);
when(m.getShortUrl()).thenReturn("grp$art/");
assertEquals("grp$art/", Functions.getRelativeLinkTo(m));
try (
MockedStatic<Stapler> mocked = mockStatic(Stapler.class);
MockedStatic<Jenkins> mockedJenkins = mockStatic(Jenkins.class)
) {
Jenkins j = createMockJenkins(mockedJenkins);
mocked.when(Stapler::getCurrentRequest).thenReturn(req);
TopLevelItemAndItemGroup ms = mock(TopLevelItemAndItemGroup.class);
when(ms.getShortUrl()).thenReturn("job/ms/");
// TODO "." (in second ancestor) is what Stapler currently fails to do. Could edit test to use ".." but set a different request path?
createMockAncestors(req, createAncestor(j, "../.."), createAncestor(ms, "."));
Item m = mock(Item.class);
when(m.getParent()).thenReturn(ms);
when(m.getShortUrl()).thenReturn("grp$art/");
assertEquals("grp$art/", Functions.getRelativeLinkTo(m));
}
}

@Test
Expand Down Expand Up @@ -267,10 +278,9 @@ private TopLevelItem createMockItem(ItemGroup p, String shortUrl, String url) {
return i;
}

private Jenkins createMockJenkins() {
mockStatic(Jenkins.class);
private Jenkins createMockJenkins(MockedStatic<Jenkins> mockedJenkins) {
Jenkins j = mock(Jenkins.class);
when(Jenkins.get()).thenReturn(j);
mockedJenkins.when(Jenkins::get).thenReturn(j);
return j;
}

Expand All @@ -282,7 +292,6 @@ private static Ancestor createAncestor(Object o, String relativePath) {
}

@Test
@PrepareForTest(Stapler.class)
public void testGetActionUrl_unparseable() {
assertNull(Functions.getActionUrl(null, createMockAction("http://example.net/stuff?something=^woohoo")));
}
Expand Down
Loading

0 comments on commit ddfacc2

Please sign in to comment.