Skip to content

Commit

Permalink
Deprecated APIs replaced and code clean up (#92)
Browse files Browse the repository at this point in the history
* Replace usages of deprecated APIs

* Simplify Optional usage

* Clean up exception declaratinos, imports, remove type

* Fix javadoc

* Simplify throw
  • Loading branch information
offa committed Jun 15, 2022
1 parent 9334a10 commit cfe07af
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,16 @@ private DisplayURLContext(DisplayURLContext parent) {
*/
private void guessPlugin() {
StackTraceElement[] stack = (new Throwable()).getStackTrace();
PluginManager manager = Jenkins.getInstance().getPluginManager();
PluginManager manager = Jenkins.get().getPluginManager();
ClassLoader loader = manager.uberClassLoader;
for (StackTraceElement frame : stack) {
String cname = frame.getClassName();
if (ourPluginClassNames.contains(cname)) {
continue;
}
Optional<PluginWrapper> wrapper = CACHE.getIfPresent( cname);
Optional<PluginWrapper> wrapper = CACHE.getIfPresent(cname);
if(wrapper != null){
if(wrapper.isPresent()){
plugin = wrapper.get();
}
wrapper.ifPresent(pluginWrapper -> plugin = pluginWrapper);
} else
{
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ private static String encode(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
AssertionError ae = new AssertionError("UTF-8 encoding is mandated by the JLS");
ae.initCause(e);
throw ae;
throw new AssertionError("UTF-8 encoding is mandated by the JLS", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.displayurlapi;

import com.google.common.annotations.VisibleForTesting;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.ExtensionList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import hudson.model.Run;
import hudson.model.TaskListener;

import java.io.IOException;

@Extension
public class EnvironmentContributorImpl extends EnvironmentContributor {

@Override
public void buildEnvironmentFor(@NonNull Run r, @NonNull EnvVars envs, @NonNull TaskListener listener) throws IOException, InterruptedException {
public void buildEnvironmentFor(@NonNull Run r, @NonNull EnvVars envs, @NonNull TaskListener listener) {
try (DisplayURLContext ctx = DisplayURLContext.open(false)) { // environment contributor "comes from" core
ctx.run(r);
DisplayURLProvider urlProvider = DisplayURLProvider.get();
Expand All @@ -26,7 +24,7 @@ public void buildEnvironmentFor(@NonNull Run r, @NonNull EnvVars envs, @NonNull
}

@Override
public void buildEnvironmentFor(@NonNull Job j, @NonNull EnvVars envs, @NonNull TaskListener listener) throws IOException, InterruptedException {
public void buildEnvironmentFor(@NonNull Job j, @NonNull EnvVars envs, @NonNull TaskListener listener) {
try (DisplayURLContext ctx = DisplayURLContext.open(false)) {
ctx.job(j);
envs.put("JOB_DISPLAY_URL", DisplayURLProvider.get().getJobURL(j));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.annotations.VisibleForTesting;
import hudson.ExtensionList;
import hudson.model.Action;
import hudson.model.User;
import java.util.Objects;
import java.util.function.Predicate;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -79,11 +78,11 @@ DisplayURLProvider lookupProvider() {
return displayURLProvider;
}

@VisibleForTesting
@Deprecated
/**
* @deprecated use {@link DisplayURLProvider#getUserPReferredProviderProperty}
* @deprecated use {@link DisplayURLProvider#getUserPreferredProviderProperty()}
*/
@VisibleForTesting
@Deprecated
protected PreferredProviderUserProperty getUserPreferredProviderProperty() {
return DisplayURLProvider.getUserPreferredProviderProperty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public static class DisplayURLDecoratorImpl extends DisplayURLDecorator {
@NonNull
@Override
protected Map<String, String> parameters(@NonNull DisplayURLContext context) {
Map<String, String> result = new HashMap<String, String>();
Map<String, String> result = new HashMap<>();
PluginWrapper wrapper = context.plugin();
result.put("utm_source", "Jenkins");
result.put("utm_campaign", wrapper == null ? "jenkins" : wrapper.getShortName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void testRedirectForArtifactsURL() throws Exception {
}

@Test
public void testRedirectForChangesURL() throws Exception {
public void testRedirectForChangesURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -81,7 +81,7 @@ public void testRedirectForChangesURL() throws Exception {
}

@Test
public void testRedirectForTestsURL() throws Exception {
public void testRedirectForTestsURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.jenkinsci.plugins.displayurlapi.user.PreferredProviderUserProperty;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.TestExtension;

import javax.servlet.http.HttpServletResponse;
import java.util.Collection;

import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;

public class ActionRedirectEligibilityTest extends AbstractActionRedirectTest {

Expand All @@ -30,7 +30,7 @@ public void tearDown() {
}

@Test
public void testUserChooseImplementation() throws Exception {
public void testUserChooseImplementation() {
// no user then different than Classic
propToUse = null;
given()
Expand Down Expand Up @@ -70,18 +70,18 @@ public void testUserChooseImplementation() throws Exception {
}

@Test
public void testUserDefaultImplementation() throws Exception {
Assert.assertThat(DisplayURLProvider.getDefault(), Matchers.instanceOf(ClassicDisplayURLProvider.class));
public void testUserDefaultImplementation() {
assertThat(DisplayURLProvider.getDefault(), Matchers.instanceOf(ClassicDisplayURLProvider.class));

System.setProperty("jenkins.displayurl.provider", EligibleDisplayURLProvider.class.getName());
Assert.assertThat(DisplayURLProvider.getDefault(), Matchers.instanceOf(EligibleDisplayURLProvider.class));
assertThat(DisplayURLProvider.getDefault(), Matchers.instanceOf(EligibleDisplayURLProvider.class));

System.setProperty("jenkins.displayurl.provider", ClassicDisplayURLProvider.class.getName());
Assert.assertThat(DisplayURLProvider.getDefault(), Matchers.instanceOf(ClassicDisplayURLProvider.class));
assertThat(DisplayURLProvider.getDefault(), Matchers.instanceOf(ClassicDisplayURLProvider.class));
}

@Test
public void testUserDefaultImplementationForRedirection() throws Exception {
public void testUserDefaultImplementationForRedirection() {
//set Classic provider
System.setProperty("jenkins.displayurl.provider", ClassicDisplayURLProvider.class.getName());
given()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Job;
import hudson.model.Run;
import org.hamcrest.Matchers;
import org.jenkinsci.plugins.displayurlapi.ClassicDisplayURLProvider;
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
import org.junit.Assert;
import org.junit.Test;
import org.jvnet.hudson.test.TestExtension;

Expand All @@ -20,7 +17,7 @@
public class ActionRedirectExtendedTest extends AbstractActionRedirectTest {

@Test
public void testRedirectForJobURL() throws Exception {
public void testRedirectForJobURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -30,7 +27,7 @@ public void testRedirectForJobURL() throws Exception {
}

@Test
public void testRedirectForRunURL() throws Exception {
public void testRedirectForRunURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -40,7 +37,7 @@ public void testRedirectForRunURL() throws Exception {
}

@Test
public void testRedirectForArtifactsURL() throws Exception {
public void testRedirectForArtifactsURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -50,7 +47,7 @@ public void testRedirectForArtifactsURL() throws Exception {
}

@Test
public void testRedirectForChangesURL() throws Exception {
public void testRedirectForChangesURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -60,7 +57,7 @@ public void testRedirectForChangesURL() throws Exception {
}

@Test
public void testRedirectForTestsURL() throws Exception {
public void testRedirectForTestsURL() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -70,7 +67,7 @@ public void testRedirectForTestsURL() throws Exception {
}

@Test
public void testRedirectForYetAnotherProviderParameter() throws Exception {
public void testRedirectForYetAnotherProviderParameter() {
given()
.urlEncodingEnabled(false)
.redirects().follow(false)
Expand All @@ -80,7 +77,7 @@ public void testRedirectForYetAnotherProviderParameter() throws Exception {
}

@Test
public void testUrls() throws Exception {
public void testUrls() {
String root = DisplayURLProvider.get().getRoot();
assertEquals("http://localhost:" + rule.getLocalPort() + "/jenkins/", root);
assertEquals(root + "job/my%20folder/job/my%20job/1/another", getRedirectedProvider().getRunURL(run));
Expand Down

0 comments on commit cfe07af

Please sign in to comment.