From 61f5f86b0c927117445b17ea2f213cd91f97c859 Mon Sep 17 00:00:00 2001 From: offa Date: Fri, 23 Dec 2022 09:04:49 +0100 Subject: [PATCH 1/2] Replace deprecated method calls --- .../java/jenkins/scm/api/SCMHeadMixinEqualityGenerator.java | 4 ++-- src/test/java/jenkins/scm/api/SCMHeadObserverTest.java | 2 +- src/test/java/jenkins/scm/api/SCMNameTest.java | 2 +- src/test/java/jenkins/scm/api/SCMRevisionActionTest.java | 2 +- .../jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java | 2 +- src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java | 2 +- .../jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java | 4 ++-- .../java/jenkins/scm/impl/mock/AbstractSampleRepoRule.java | 5 +++-- .../scm/impl/trait/WildcardSCMHeadFilterTraitTest.java | 2 +- 9 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/jenkins/scm/api/SCMHeadMixinEqualityGenerator.java b/src/main/java/jenkins/scm/api/SCMHeadMixinEqualityGenerator.java index 1302b49d..8847c807 100644 --- a/src/main/java/jenkins/scm/api/SCMHeadMixinEqualityGenerator.java +++ b/src/main/java/jenkins/scm/api/SCMHeadMixinEqualityGenerator.java @@ -243,8 +243,8 @@ private SCMHeadMixin.Equality create(@NonNull Class type) { SCMHeadMixin.Equality.class); try { - return c.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + return c.getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { // fallback to reflection } return new ReflectiveEquality(properties.values().toArray(new Method[0])); diff --git a/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java b/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java index f80e9e50..f1f40b10 100644 --- a/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java +++ b/src/test/java/jenkins/scm/api/SCMHeadObserverTest.java @@ -29,6 +29,7 @@ import org.hamcrest.Matchers; import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.hasEntry; @@ -36,7 +37,6 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; diff --git a/src/test/java/jenkins/scm/api/SCMNameTest.java b/src/test/java/jenkins/scm/api/SCMNameTest.java index cafc953f..8b8f5267 100644 --- a/src/test/java/jenkins/scm/api/SCMNameTest.java +++ b/src/test/java/jenkins/scm/api/SCMNameTest.java @@ -26,9 +26,9 @@ import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.*; public class SCMNameTest { @Test diff --git a/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java b/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java index 1a0370f0..d93f01ba 100644 --- a/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java +++ b/src/test/java/jenkins/scm/api/SCMRevisionActionTest.java @@ -8,8 +8,8 @@ import jenkins.scm.impl.mock.MockSCMSource; import org.junit.Test; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; public class SCMRevisionActionTest { @Test diff --git a/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java b/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java index d342a0b0..4c9aef35 100644 --- a/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java +++ b/src/test/java/jenkins/scm/impl/ChangeRequestSCMHeadCategoryTest.java @@ -32,8 +32,8 @@ import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.*; @RunWith(Theories.class) public class ChangeRequestSCMHeadCategoryTest { diff --git a/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java b/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java index a05f1544..d6f1441b 100644 --- a/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java +++ b/src/test/java/jenkins/scm/impl/avatars/AvatarCacheTest.java @@ -243,7 +243,7 @@ public String getUrlName() { } public HttpResponse doIndex() { - return HttpResponses.plainText(AvatarCache.buildUrl(url, size)); + return HttpResponses.text(AvatarCache.buildUrl(url, size)); } } diff --git a/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java b/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java index 7a017b46..3486a763 100644 --- a/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java +++ b/src/test/java/jenkins/scm/impl/mock/AbstractSampleDVCSRepoRule.java @@ -59,7 +59,7 @@ protected void before() throws Throwable { * @param text text to write */ public final void write(String rel, String text) throws IOException { - FileUtils.write(new File(sampleRepo, rel), text); + FileUtils.write(new File(sampleRepo, rel), text, StandardCharsets.UTF_8); } /** @@ -91,7 +91,7 @@ protected final void run(String tool, String... cmds) throws Exception { * Like {@link #fileUrl} but expressed only as a path, not a URL with protocol. */ public final String bareUrl() throws UnsupportedEncodingException { - return URLEncoder.encode(toString(), StandardCharsets.UTF_8.name()); + return URLEncoder.encode(toString(), StandardCharsets.UTF_8); } /** diff --git a/src/test/java/jenkins/scm/impl/mock/AbstractSampleRepoRule.java b/src/test/java/jenkins/scm/impl/mock/AbstractSampleRepoRule.java index 0892656b..beb7936e 100644 --- a/src/test/java/jenkins/scm/impl/mock/AbstractSampleRepoRule.java +++ b/src/test/java/jenkins/scm/impl/mock/AbstractSampleRepoRule.java @@ -30,8 +30,9 @@ import hudson.util.StreamTaskListener; import java.io.File; import java.util.Arrays; + +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import org.junit.Assert; import org.junit.Assume; import org.junit.AssumptionViolatedException; import org.junit.Rule; @@ -60,7 +61,7 @@ public static void run(boolean probing, File cwd, String... cmds) throws Excepti if (probing) { Assume.assumeThat(message, r, is(0)); } else { - Assert.assertThat(message, r, is(0)); + assertThat(message, r, is(0)); } } catch (Exception x) { if (probing) { diff --git a/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java b/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java index ec23605a..095883fe 100644 --- a/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java +++ b/src/test/java/jenkins/scm/impl/trait/WildcardSCMHeadFilterTraitTest.java @@ -37,8 +37,8 @@ import org.junit.Test; import org.jvnet.hudson.test.JenkinsRule; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.junit.Assert.*; public class WildcardSCMHeadFilterTraitTest { @ClassRule From dea9b3e5c38c34d2979e63ffc4a8bc6fab6840e5 Mon Sep 17 00:00:00 2001 From: offa Date: Fri, 23 Dec 2022 09:21:24 +0100 Subject: [PATCH 2/2] Replace deprecated Acegi security --- src/main/java/jenkins/scm/api/SCMEvent.java | 24 ++++++++++----------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/jenkins/scm/api/SCMEvent.java b/src/main/java/jenkins/scm/api/SCMEvent.java index 7218bdd4..4bd70509 100644 --- a/src/main/java/jenkins/scm/api/SCMEvent.java +++ b/src/main/java/jenkins/scm/api/SCMEvent.java @@ -33,6 +33,7 @@ import hudson.model.Run; import hudson.model.TaskListener; import hudson.security.ACL; +import hudson.security.ACLContext; import hudson.util.ClassLoaderSanityThreadFactory; import hudson.util.DaemonThreadFactory; import hudson.util.NamingThreadFactory; @@ -47,8 +48,6 @@ import javax.servlet.http.HttpServletRequest; import jenkins.security.ImpersonatingScheduledExecutorService; import jenkins.util.SystemProperties; -import org.acegisecurity.context.SecurityContext; -import org.acegisecurity.context.SecurityContextHolder; import org.apache.commons.lang.StringUtils; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.NoExternalUse; @@ -541,17 +540,16 @@ public void run() { event.getClass(), event.getTimestamp(), oldName) ); for (final SCMEventListener l : ExtensionList.lookup(SCMEventListener.class)) { - SecurityContext context = ACL.impersonate(ACL.SYSTEM); - try { - fire(l, event); - } catch (LinkageError e) { - log(l, e); - } catch (Error e) { - throw e; - } catch (Throwable e) { - log(l, e); - } finally { - SecurityContextHolder.setContext(context); + try (ACLContext ctx = ACL.as2(ACL.SYSTEM2)) { + try { + fire(l, event); + } catch (LinkageError e) { + log(l, e); + } catch (Error e) { + throw e; + } catch (Throwable e) { + log(l, e); + } } } } finally {