diff --git a/src/test/java/org/jenkinsci/plugins/configfiles/Security2203Test.java b/src/test/java/org/jenkinsci/plugins/configfiles/Security2203Test.java index 5adebe9a..c9149e6c 100644 --- a/src/test/java/org/jenkinsci/plugins/configfiles/Security2203Test.java +++ b/src/test/java/org/jenkinsci/plugins/configfiles/Security2203Test.java @@ -6,10 +6,10 @@ import hudson.model.User; import hudson.security.ACL; import hudson.security.ACLContext; -import hudson.security.AccessDeniedException2; import hudson.security.Permission; import hudson.util.ListBoxModel; import jenkins.model.Jenkins; +import org.acegisecurity.AccessDeniedException; import org.jenkinsci.lib.configprovider.ConfigProvider; import org.jenkinsci.lib.configprovider.model.Config; import org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFile; @@ -35,6 +35,7 @@ import java.util.stream.Stream; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.startsWith; @@ -305,13 +306,13 @@ private void assertWhoCanExecute(Runnable run, Permission permission, String che try (ACLContext ctx = ACL.as(User.getOrCreateByIdOrFullName("reader"))) { run.run(); // The method should fail fail(String.format("%s should be only accessible by people with the permission %s, but it's accessible by a person with %s", checkedMethod, permission, Item.READ)); - } catch (AccessDeniedException2 e) { - assertThat(e.permission, equalTo(permission)); + } catch (AccessDeniedException e) { + assertThat(e.getMessage(), containsString(permission.group.title + "/" + permission.name)); } try (ACLContext ctx = ACL.as(User.getOrCreateByIdOrFullName(userWithPermission.get(permission)))) { run.run(); // The method doesn't fail - } catch (AccessDeniedException2 e) { + } catch (AccessDeniedException e) { fail(String.format("%s should be accessible to people with the permission %s but it failed with the exception: %s", checkedMethod, permission, e)); } } diff --git a/src/test/java/org/jenkinsci/plugins/configfiles/sec/PermissionChecker.java b/src/test/java/org/jenkinsci/plugins/configfiles/sec/PermissionChecker.java index 067f4e50..51382683 100644 --- a/src/test/java/org/jenkinsci/plugins/configfiles/sec/PermissionChecker.java +++ b/src/test/java/org/jenkinsci/plugins/configfiles/sec/PermissionChecker.java @@ -1,18 +1,18 @@ package org.jenkinsci.plugins.configfiles.sec; import edu.umd.cs.findbugs.annotations.NonNull; -import hudson.security.AccessDeniedException2; import hudson.security.Permission; -import org.hamcrest.core.IsEqual; +import org.acegisecurity.AccessDeniedException; import java.util.function.Supplier; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.fail; /** * A class to run pieces of code with a certain user and assert either the code runs successfully, without even worrying - * about the result, or the code fails with an {@link AccessDeniedException2} with the specified {@link Permission}. + * about the result, or the code fails with an {@link AccessDeniedException} with the specified {@link Permission} reason. */ public class PermissionChecker extends ProtectedCodeRunner { /** @@ -26,16 +26,16 @@ public PermissionChecker(@NonNull Runnable code, @NonNull String user) { } /** - * Assert the execution of the code by this user fails with this permission. The code throws an {@link AccessDeniedException2} - * with the permission field being permission. Otherwise it fails. + * Assert the execution of the code by this user fails with this permission. The code throws an {@link AccessDeniedException} + * with the message indicating the same permission. Otherwise it fails. * @param permission The permission thrown by the code. */ public void assertFailWithPermission(Permission permission) { Throwable t = getThrowable(); - if (t instanceof AccessDeniedException2) { - assertThat(((AccessDeniedException2) t).permission, IsEqual.equalTo(permission)); + if (t instanceof AccessDeniedException) { + assertThat(t.getMessage(), containsString(permission.group.title + "/" + permission.name)); } else { - fail(String.format("The code run by %s didn't throw an AccessDeniedException2 with %s. If failed with the unexpected throwable: %s", getUser(), permission, t)); + fail(String.format("The code run by %s didn't throw an AccessDeniedException with %s. If failed with the unexpected throwable: %s", getUser(), permission, t)); } } diff --git a/src/test/java/org/jenkinsci/plugins/configfiles/sec/ProtectedCodeRunnerTests.java b/src/test/java/org/jenkinsci/plugins/configfiles/sec/ProtectedCodeRunnerTests.java index a090bf6e..dadbd0ed 100644 --- a/src/test/java/org/jenkinsci/plugins/configfiles/sec/ProtectedCodeRunnerTests.java +++ b/src/test/java/org/jenkinsci/plugins/configfiles/sec/ProtectedCodeRunnerTests.java @@ -1,7 +1,7 @@ package org.jenkinsci.plugins.configfiles.sec; -import hudson.security.AccessDeniedException2; import jenkins.model.Jenkins; +import org.acegisecurity.AccessDeniedException; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -11,7 +11,7 @@ import java.util.function.Supplier; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -42,7 +42,7 @@ public void protectedCodeCheckerTest() { assertThat(checker.getResult(), is("allowed")); Throwable t = checker.withUser("reader").getThrowable(); - assertThat(t, instanceOf(AccessDeniedException2.class)); - assertThat(((AccessDeniedException2) t).permission, equalTo(Jenkins.ADMINISTER)); + assertThat(t, instanceOf(AccessDeniedException.class)); + assertThat(t.getMessage(), containsString(Jenkins.ADMINISTER.group.title + "/" + Jenkins.ADMINISTER.name)); } }