Skip to content

Commit 99767ef

Browse files
committed
Handle old core baselines gracefully
1 parent e3ba8dc commit 99767ef

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/main/java/org/jvnet/hudson/test/PluginAutomaticTestBuilder.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import io.github.classgraph.ClassInfo;
3131
import io.github.classgraph.ScanResult;
3232
import jenkins.model.Jenkins;
33-
import jenkins.security.stapler.TypedFilter;
3433
import junit.framework.TestCase;
3534
import junit.framework.TestSuite;
3635
import org.apache.commons.lang3.StringUtils;
@@ -48,6 +47,7 @@
4847
import java.util.ArrayList;
4948
import java.util.List;
5049
import java.util.Map;
50+
import java.util.logging.Logger;
5151

5252
import static org.hamcrest.CoreMatchers.is;
5353
import static org.hamcrest.Matchers.empty;
@@ -129,8 +129,10 @@ public void testPluginActive() {
129129
public void testStaplerDispatches() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
130130
List<String> methodsFound = new ArrayList<>();
131131

132-
Method isStaplerRoutableMethod = TypedFilter.class.getDeclaredMethod("isRoutableMethod", Method.class);
133-
isStaplerRoutableMethod.setAccessible(true);
132+
Method isStaplerRoutableMethod = findIsRoutableMethod();
133+
if (isStaplerRoutableMethod == null) {
134+
return;
135+
}
134136

135137
PluginWrapper thisPlugin = determineCurrentPlugin();
136138
if (thisPlugin == null) {
@@ -152,7 +154,18 @@ public void testStaplerDispatches() throws InvocationTargetException, IllegalAcc
152154
}
153155
}
154156
}
155-
Assert.assertThat("Web methods lack verb annotations like @RequirePOST, @GET, @POST, etc.", methodsFound, is(empty()));
157+
Assert.assertThat("There should be no web methods that lack HTTP verb annotations like @RequirePOST, @GET, @POST, etc. -- see https://jenkins.io/redirect/developer/csrf-protection", methodsFound, is(empty()));
158+
}
159+
160+
private Method findIsRoutableMethod() throws NoSuchMethodException {
161+
try {
162+
Method method = Class.forName("jenkins.security.stapler.TypedFilter").getDeclaredMethod("isRoutableMethod", Method.class);
163+
method.setAccessible(true);
164+
return method;
165+
} catch (ClassNotFoundException e) {
166+
LOGGER.warning("This test requires Jenkins 2.154, Jenkins LTS 2.138.4, or newer to run, use e.g. -Djenkins.version=2.138.4");
167+
return null;
168+
}
156169
}
157170

158171
private PluginWrapper determineCurrentPlugin() {
@@ -193,5 +206,7 @@ private boolean isStaplerDispatchable(Method m) {
193206
}
194207
return true;
195208
}
209+
210+
private static Logger LOGGER = Logger.getLogger(OtherTests.class.getName());
196211
}
197212
}

0 commit comments

Comments
 (0)