From e58ccbe335682bee04a755a9367d99ec9b4354de Mon Sep 17 00:00:00 2001 From: jglick Date: Thu, 5 Mar 2009 03:01:00 +0000 Subject: [PATCH] [FIXED HUDSON-3225] Hyperlink URLs in JUnit output. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@16023 71c3de6d-444a-0410-be80-ed276b4c234a --- .../java/hudson/util/HyperlinkingOutTag.java | 61 ++++++++++++ .../hudson/tasks/junit/CaseResult/index.jelly | 18 ++-- .../hudson/util/HyperlinkingOutTagTest.java | 94 +++++++++++++++++++ 3 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/hudson/util/HyperlinkingOutTag.java create mode 100644 core/src/test/java/hudson/util/HyperlinkingOutTagTest.java diff --git a/core/src/main/java/hudson/util/HyperlinkingOutTag.java b/core/src/main/java/hudson/util/HyperlinkingOutTag.java new file mode 100644 index 0000000000..9630b1d6c7 --- /dev/null +++ b/core/src/main/java/hudson/util/HyperlinkingOutTag.java @@ -0,0 +1,61 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package hudson.util; + +import org.apache.commons.jelly.JellyTagException; +import org.apache.commons.jelly.MissingAttributeException; +import org.apache.commons.jelly.TagSupport; +import org.apache.commons.jelly.XMLOutput; +import org.apache.commons.jelly.expression.Expression; +import org.kohsuke.stapler.jelly.OutTag; +import org.springframework.beans.factory.annotation.Required; +import org.xml.sax.SAXException; + +/** + * Variant of {@linkplain OutTag } that tries to hyperlink URLs. + */ +public class HyperlinkingOutTag extends TagSupport { + + private Expression value; + + @Required + public void setValue(Expression value) { + this.value = value; + } + + public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { + String text = value.evaluateAsString(context); + if (text != null) { + try { + output.write(text. + replace("&", "&"). + replace("<", "<"). + replaceAll("\\b(https?://[^\\s)>]+)", "$1")); + } catch (SAXException e) { + throw new JellyTagException("could not write the XMLOutput", e); + } + } + } +} diff --git a/core/src/main/resources/hudson/tasks/junit/CaseResult/index.jelly b/core/src/main/resources/hudson/tasks/junit/CaseResult/index.jelly index d0b1ec37df..6ede02b5e9 100644 --- a/core/src/main/resources/hudson/tasks/junit/CaseResult/index.jelly +++ b/core/src/main/resources/hudson/tasks/junit/CaseResult/index.jelly @@ -22,7 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --> - + @@ -50,20 +51,25 @@ THE SOFTWARE.
${%took(it.durationString)}
+ + + + +

${%Error Message}

-
+

${%Stacktrace}

-
+

${%Standard Output}

-
+

${%Standard Error}

-
+
-
\ No newline at end of file +
diff --git a/core/src/test/java/hudson/util/HyperlinkingOutTagTest.java b/core/src/test/java/hudson/util/HyperlinkingOutTagTest.java new file mode 100644 index 0000000000..f0aa258f33 --- /dev/null +++ b/core/src/test/java/hudson/util/HyperlinkingOutTagTest.java @@ -0,0 +1,94 @@ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package hudson.util; + +import java.io.StringWriter; +import junit.framework.TestCase; +import org.apache.commons.jelly.XMLOutput; +import org.apache.commons.jelly.expression.ConstantExpression; + +public class HyperlinkingOutTagTest extends TestCase { + + public HyperlinkingOutTagTest(String name) { + super(name); + } + + public void testPlain() throws Exception { + assertOutput("plain text", "plain text"); + } + + public void testOnItsOwnLine() throws Exception { + assertOutput("line #1\nhttp://nowhere.net/\nline #2\n", + "line #1\nhttp://nowhere.net/\nline #2\n"); + } + + public void testInline() throws Exception { + assertOutput("failed; see http://nowhere.net/", + "failed; see http://nowhere.net/"); + assertOutput("failed (see http://nowhere.net/)", + "failed (see http://nowhere.net/)"); + } + + public void testMultipleLinks() throws Exception { + assertOutput("http://nowhere.net/ - failed: http://elsewhere.net/", + "http://nowhere.net/ - failed: " + + "http://elsewhere.net/"); + } + + public void testHttps() throws Exception { + assertOutput("https://nowhere.net/", + "https://nowhere.net/"); + } + + public void testMushedUp() throws Exception { + assertOutput("stuffhttp://nowhere.net/", "stuffhttp://nowhere.net/"); + } + + public void testEscapedMetaChars() throws Exception { + assertOutput("a < b && c < d", "a < b && c < d"); + } + + public void testEscapedMetaCharsWithLinks() throws Exception { + assertOutput("see ", + "see <http://nowhere.net/>"); + assertOutput("http://google.com/?q=stuff&lang=en", + "http://google.com/?q=stuff&lang=en"); + } + + public void testPortNumber() throws Exception { + assertOutput("http://localhost:8080/stuff/", + "http://localhost:8080/stuff/"); + } + + private void assertOutput(String in, String out) throws Exception { + StringWriter w = new StringWriter(); + XMLOutput xmlo = XMLOutput.createXMLOutput(w); + HyperlinkingOutTag tag = new HyperlinkingOutTag(); + tag.setValue(new ConstantExpression(in)); + tag.doTag(xmlo); + assertEquals(out, w.toString()); + } + +}