Skip to content

Commit

Permalink
[FIXED JENKINS-19944] Links to failed tests need to be URL encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
kutzi committed Oct 17, 2013
1 parent f5be850 commit ad56d7f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/main/java/hudson/plugins/im/tools/MessageHelper.java
Expand Up @@ -49,8 +49,9 @@ public static String getBuildURL(AbstractBuild<?, ?> build) {
* Returns the full URL to the test details page for a given test result;
*/
public static String getTestUrl(hudson.tasks.test.TestResult result) {
String url = getBuildURL(result.getOwner());
AbstractTestResultAction action = result.getTestResultAction();
String buildUrl = getBuildURL(result.getOwner());
@SuppressWarnings("rawtypes")
AbstractTestResultAction action = result.getTestResultAction();

TestObject parent = result.getParent();
TestResult testResultRoot = null;
Expand All @@ -62,10 +63,19 @@ public static String getTestUrl(hudson.tasks.test.TestResult result) {
parent = parent.getParent();
}

url += action.getUrlName()
String testUrl = action.getUrlName()
+ (testResultRoot != null ? testResultRoot.getUrl() : "")
+ result.getUrl();
return url;

String[] pathComponents = testUrl.split("/");
StringBuilder buf = new StringBuilder();
for (String c : pathComponents) {
buf.append(Util.rawEncode(c)).append('/');
}
// remove last /
buf.deleteCharAt(buf.length() - 1);

return buildUrl + buf.toString();
}

/**
Expand Down Expand Up @@ -146,7 +156,7 @@ private static List<String> extractParameters(String commandLine) {
* Note: Unfortunately in Java 5 there is no Arrays#copyOfRange, yet.
* So we have to implement it ourself.
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T[] copyOfRange(T[] original, int from, int to) {
int newLength = to - from;
if (newLength < 0)
Expand Down Expand Up @@ -178,7 +188,7 @@ public static <T> T[] copyOfRange(T[] original, int from, int to) {
*
* Note: copied from java 6
*/
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T[] copyOf(T[] original, int newLength) {
Class type = original.getClass();
T[] copy = (type == Object[].class)
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/hudson/plugins/im/tools/MessageHelperTest.java
@@ -1,7 +1,12 @@
package hudson.plugins.im.tools;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import hudson.model.AbstractBuild;
import hudson.plugins.im.tools.MessageHelper;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResult;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -62,4 +67,22 @@ public void testConcat() {
concat = MessageHelper.concat(a);
Assert.assertArrayEquals(a, concat);
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testUrlShouldBeUrlEncoded() {
TestResult result = mock(TestResult.class);
AbstractBuild build = mock(AbstractBuild.class);
when(build.getUrl()).thenReturn("/a build");

AbstractTestResultAction action = mock(AbstractTestResultAction.class);
when(action.getUrlName()).thenReturn("/action");

when(result.getOwner()).thenReturn(build);
when(result.getTestResultAction()).thenReturn(action);
when(result.getUrl()).thenReturn("/some id with spaces");

String testUrl = MessageHelper.getTestUrl(result);
assertEquals("null/a%20build/action/some%20id%20with%20spaces", testUrl);
}
}

0 comments on commit ad56d7f

Please sign in to comment.