2011-11-09 35 views
2

我正在使用ant 1.8.2。讓我們說一個測試失敗,並帶有stackoverflow錯誤。junitreport:xslt在StackOverflowError失敗時有很多換行符/換行符

import junit.framework.TestCase; 

/**a failing test */ 
public class FailingTest extends TestCase 
{ 

    public void testFail() { 
     testFail();// gives stackoverflow- result xml is now a large document 
    } 
} 

運行junitreport將失敗,下面的錯誤

[junitreport]的jar:文件://lib/ant-junit.jar /組織/阿帕奇/工具/螞蟻/任務定義/可選/ junit/xsl/junit-frames.xsl:65:57: 致命錯誤! java.lang.StackOverflowError的原因: java.lang.StackOverflowError的

的原因似乎是在測試結果XML文件大的文本內容。

<testcase classname="chs.FailingTest" name="testFail" time="0.012"> 
     <error type="java.lang.StackOverflowError">java.lang.StackOverflowError 
      at chs.FailingTest.testFail(FailingTest.java:14) 
      at chs.FailingTest.testFail(FailingTest.java:14) 
      at chs.FailingTest.testFail(FailingTest.java:14) .... 

我想xslt需要修剪並跳過大的錯誤信息。什麼是可能的修復?

//臨時的解決辦法:跳過從結果HTML這樣大的文本編輯 需要在JUnit中,XSLT文件

<xsl:template name="br-replace"> 
    <xsl:param name="word"/> 
    <xsl:if test="string-length($word) &lt; 31024 "> //very large size here causes stackoverflow 
    <xsl:choose> 
     <xsl:when test="contains($word, '&#xa;')"> 
     <xsl:value-of select="substring-before($word, '&#xa;')"/> 
     <br/> 
     <xsl:call-template name="br-replace"> 
      <xsl:with-param name="word" select="substring-after($word, '&#xa;')"/> 
     </xsl:call-template> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:value-of select="$word"/> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:if> 
</xsl:template> 

最終的解決辦法:看下面的答案後,我檢查蟻開發網站。 的SVN具有新的XSLT:http://svn.apache.org/viewvc/ant/core/trunk/src/etc/junit-frames-xalan1.xsl?view=co&content-type=text%2Fplain,其更新所述模板是如下面

<xsl:template name="br-replace"> 
    <xsl:param name="word"/> 
    <xsl:param name="br"><br/></xsl:param> 
    <xsl:value-of select='stringutils:replace(string($word),"&#xA;",$br)'/> 
</xsl:template> 

回答

1

轉換的代碼XSLT 2.0:

<xsl:template name="br-replace"> 
    <xsl:param name="word"/> 
    <xsl:for-each select="tokenize($word, '&#xa;')"> 
     <xsl:if test="position() != 1"><br/></xsl:if> 
     <xsl:value-of select="."/> 
    </xsl:for-each> 
</xsl:template> 

或者,使用撒克遜作爲XSLT處理器運行現有代碼。撒克遜執行tail-call優化,將此遞歸模板轉換爲普通循環。

+0

螞蟻JUnit使用XSLT 1.0。可悲的。 – Jayan

+0

這提供了一個線索,以查看svn中的新br替換:http://svn.apache.org/viewvc/ant/core/trunk/src/etc/junit-frames-xalan1.xsl?view=co&content-type=文本%2Fplain - 使用這個作爲答案 – Jayan

1
public void testFail() { 
     testFail();// gives stackoverflow- result xml is now a large document 
    } 

這種方法被調用本身....沒有出口點

+0

這是爲了顯示junit xslt的問題 – Jayan