Fix tests that load classpath resources via URL#getFile() (#25952)

Some tests convert classpath resources to File via URL#getFile(), which returns URL-encoded paths (e.g. %20 for spaces). This can break tests (or cause them to be silently skipped) when the workspace path contains spaces.

Use URL#toURI() for URL-to-File conversion in tests and assert resource existence so tests execute reliably across platforms.

Signed-off-by: shbhmexe <shubhushukla586@gmail.com>
Co-authored-by: Mark Waite <mark.earl.waite@gmail.com>
This commit is contained in:
Shubham shuklaandMark Waite authored and GitHub committed 2026-05-21 16:26:29 -06:00
1 parent 638675bfdb
commit b20dd0c23e
3 files changed
+73 -52

No files matched your search

@@ -6,10 +6,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.spec.InvalidKeySpecException;
import java.util.Objects;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@@ -25,7 +28,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyDSA() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("dsa").getFile());
File file = resourceAsFile("dsa");
assertKeyPairNotNull(file, null);
}
@@ -41,12 +44,21 @@ class PrivateKeyProviderTest {
assertNotNull(keyPair.getPublic());
}
private File resourceAsFile(String name) {
URL url = Objects.requireNonNull(getClass().getResource(name), "Missing test resource " + name);
try {
return new File(url.toURI());
} catch (URISyntaxException e) {
throw new AssertionError("Invalid URI for test resource " + name + ": " + url, e);
}
}
/**
key command: ssh-keygen -f dsa-password -t dsa -b 1024 -m PEM -P password
*/
@Test
void loadKeyDSAPassword() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("dsa-password").getFile());
File file = resourceAsFile("dsa-password");
String password = "password";
assertKeyPairNotNull(file, password);
}
@@ -56,7 +68,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyRSA() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("rsa").getFile());
File file = resourceAsFile("rsa");
assertKeyPairNotNull(file, null);
}
@@ -65,7 +77,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyRSAPassword() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("rsa-password").getFile());
File file = resourceAsFile("rsa-password");
String password = "password";
assertKeyPairNotNull(file, password);
}
@@ -75,7 +87,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyOpenSSH() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("openssh").getFile());
File file = resourceAsFile("openssh");
assertKeyPairNotNull(file, null);
}
@@ -84,7 +96,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyOpenSSHPKCS8() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("openssh-pkcs8").getFile());
File file = resourceAsFile("openssh-pkcs8");
String password = "password";
assertKeyPairNotNull(file, password);
}
@@ -94,7 +106,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyOpenSSHRFC4716() throws IOException, GeneralSecurityException {
File file = new File(this.getClass().getResource("openssh-rfc4716").getFile());
File file = resourceAsFile("openssh-rfc4716");
String password = "password";
assertKeyPairNotNull(file, password);
}
@@ -105,7 +117,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyOpenSSHMultipleKeys() {
File file = new File(this.getClass().getResource("openssh-multiple-keys").getFile());
File file = resourceAsFile("openssh-multiple-keys");
String password = "password";
assertThrows(InvalidKeySpecException.class, () -> PrivateKeyProvider.loadKey(file, password));
}
@@ -115,7 +127,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadBlankKey() {
File file = new File(this.getClass().getResource("blank").getFile());
File file = resourceAsFile("blank");
String password = "password";
assertThrows(InvalidKeyException.class, () -> PrivateKeyProvider.loadKey(file, password));
}
@@ -126,7 +138,7 @@ class PrivateKeyProviderTest {
*/
@Test
void loadKeyBroken() {
File file = new File(this.getClass().getResource("openssh-broken").getFile());
File file = resourceAsFile("openssh-broken");
String password = "password";
assertThrows(IllegalArgumentException.class, () -> PrivateKeyProvider.loadKey(file, password));
}
+45 -39
View File
@@ -12,72 +12,78 @@ import hudson.util.RobustReflectionConverter;
import hudson.util.XStream2;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
import jenkins.model.Jenkins;
import org.junit.jupiter.api.Test;
class XmlFileTest {
@Test
void canReadXml1_0Test() throws IOException {
URL configUrl = getClass().getResource("/hudson/config_1_0.xml");
void canReadXml1_0Test() throws IOException, URISyntaxException {
URL configUrl = Objects.requireNonNull(getClass().getResource("/hudson/config_1_0.xml"),
"Missing test resource /hudson/config_1_0.xml");
XStream2 xs = new XStream2();
xs.alias("hudson", Jenkins.class);
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.getFile()));
if (xmlFile.exists()) {
Node n = (Node) xmlFile.read();
assertThat(n.getNumExecutors(), is(2));
assertThat(n.getMode().toString(), is("NORMAL"));
}
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.toURI()));
assertThat(xmlFile.exists(), is(true));
Node n = (Node) xmlFile.read();
assertThat(n.getNumExecutors(), is(2));
assertThat(n.getMode().toString(), is("NORMAL"));
}
@Test
void xml1_0_withSpecialCharsShouldFail() {
URL configUrl = getClass().getResource("/hudson/config_1_0_with_special_chars.xml");
void xml1_0_withSpecialCharsShouldFail() throws URISyntaxException {
URL configUrl = Objects.requireNonNull(getClass().getResource("/hudson/config_1_0_with_special_chars.xml"),
"Missing test resource /hudson/config_1_0_with_special_chars.xml");
XStream2 xs = new XStream2();
xs.alias("hudson", Jenkins.class);
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.getFile()));
if (xmlFile.exists()) {
IOException e = assertThrows(IOException.class, xmlFile::read);
assertThat(e.getCause(), instanceOf(ConversionException.class));
ConversionException ce = (ConversionException) e.getCause();
assertThat(ce.get("cause-exception"), is(StreamException.class.getName()));
assertThat(ce.get("class"), is(Jenkins.class.getName()));
assertThat(ce.get("required-type"), is(Jenkins.class.getName()));
assertThat(ce.get("converter-type"), is(RobustReflectionConverter.class.getName()));
assertThat(ce.get("path"), is("/hudson/label"));
assertThat(ce.get("line number"), is("7"));
}
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.toURI()));
assertThat(xmlFile.exists(), is(true));
IOException e = assertThrows(IOException.class, xmlFile::read);
assertThat(e.getCause(), instanceOf(ConversionException.class));
ConversionException ce = (ConversionException) e.getCause();
assertThat(ce.get("cause-exception"), is(StreamException.class.getName()));
assertThat(ce.get("class"), is(Jenkins.class.getName()));
assertThat(ce.get("required-type"), is(Jenkins.class.getName()));
assertThat(ce.get("converter-type"), is(RobustReflectionConverter.class.getName()));
assertThat(ce.get("path"), is("/hudson/label"));
assertThat(ce.get("line number"), is("7"));
}
@Test
void canReadXml1_1Test() throws IOException {
URL configUrl = getClass().getResource("/hudson/config_1_1.xml");
void canReadXml1_1Test() throws IOException, URISyntaxException {
URL configUrl = Objects.requireNonNull(getClass().getResource("/hudson/config_1_1.xml"),
"Missing test resource /hudson/config_1_1.xml");
XStream2 xs = new XStream2();
xs.alias("hudson", Jenkins.class);
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.getFile()));
if (xmlFile.exists()) {
Node n = (Node) xmlFile.read();
assertThat(n.getNumExecutors(), is(2));
assertThat(n.getMode().toString(), is("NORMAL"));
}
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.toURI()));
assertThat(xmlFile.exists(), is(true));
Node n = (Node) xmlFile.read();
assertThat(n.getNumExecutors(), is(2));
assertThat(n.getMode().toString(), is("NORMAL"));
}
@Test
void canReadXmlWithControlCharsTest() throws IOException {
URL configUrl = getClass().getResource("/hudson/config_1_1_with_special_chars.xml");
void canReadXmlWithControlCharsTest() throws IOException, URISyntaxException {
URL configUrl = Objects.requireNonNull(getClass().getResource("/hudson/config_1_1_with_special_chars.xml"),
"Missing test resource /hudson/config_1_1_with_special_chars.xml");
XStream2 xs = new XStream2();
xs.alias("hudson", Jenkins.class);
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.getFile()));
if (xmlFile.exists()) {
Node n = (Node) xmlFile.read();
assertThat(n.getNumExecutors(), is(2));
assertThat(n.getMode().toString(), is("NORMAL"));
assertThat(n.getLabelString(), is("LESS_TERMCAP_mb=\u001B[01;31m"));
}
XmlFile xmlFile = new XmlFile(xs, new File(configUrl.toURI()));
assertThat(xmlFile.exists(), is(true));
Node n = (Node) xmlFile.read();
assertThat(n.getNumExecutors(), is(2));
assertThat(n.getMode().toString(), is("NORMAL"));
assertThat(n.getLabelString(), is("LESS_TERMCAP_mb=\u001B[01;31m"));
}
}
@@ -33,7 +33,9 @@ import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Objects;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
@@ -109,9 +111,10 @@ class XMLUtilsTest {
* tests for each).
*/
@Test
void testGetValue() throws XPathExpressionException, SAXException, IOException {
URL configUrl = getClass().getResource("/jenkins/xml/config.xml");
File configFile = new File(configUrl.getFile());
void testGetValue() throws XPathExpressionException, SAXException, IOException, URISyntaxException {
URL configUrl = Objects.requireNonNull(getClass().getResource("/jenkins/xml/config.xml"),
"Missing test resource /jenkins/xml/config.xml");
File configFile = new File(configUrl.toURI());
assertEquals("1.480.1", XMLUtils.getValue("/hudson/version", configFile));
assertEquals("", XMLUtils.getValue("/hudson/unknown-element", configFile));