escape CData ]]> in sysout

This commit is contained in:
Bruno Bieth
2014-02-24 17:54:16 +01:00
parent 3addbe39b0
commit 96bf175464
5 changed files with 53 additions and 4 deletions
@@ -4,7 +4,7 @@ import java.io.{StringWriter, PrintWriter, File}
import java.net.InetAddress
import scala.collection.mutable.ListBuffer
import scala.util.DynamicVariable
import scala.xml.{Elem, Node, XML}
import scala.xml.{PCData, Elem, Node, XML}
import testing.{Event => TEvent, Status => TStatus, OptionalThrowable, TestSelector}
/**
@@ -32,7 +32,11 @@ class JUnitXmlTestsListener(val outputDir:String, logger: Logger) extends TestsL
}
</properties>
private def cdata(content: String) = scala.xml.Unparsed("<![CDATA[%s]]>".format(content))
private def cdata(content: String) : Node = {
val escapedCData = content.replaceAll("]]>", "]]]]><![CDATA[>")
// unfortunately PCData does not escape ']]>'
PCData(escapedCData)
}
private def stackTraceToString(t: Throwable) : String = {
val stringWriter = new StringWriter()
@@ -0,0 +1,22 @@
package sbt
import org.backuity.matchete.{XmlMatchers, JunitMatchers}
import org.junit.Test
class JUnitXmlTestsListenerTest extends JunitMatchers with XmlMatchers {
@Test
def sysoutCDataShouldBeEscaped() {
val listener = new JUnitXmlTestsListener("/", ConsoleLogger())
val event = new JUnitEvent(1234L, "org.the.ATest")
val xml = listener.toXml("a test", SuiteReport(Seq(TestReport("<![CDATA[ESCAPE ]]> ME! AND ]]> ME!!", event))))
xml \ "testcase" must containExactly(
a("'a test' test-case") { case tc =>
tc must haveAttribute("classname", equalTo("a test"))
tc must haveAttribute("name", equalTo("ATest"))
(tc \ "system-out").toString must_== "<system-out><![CDATA[<![CDATA[ESCAPE ]]]]><![CDATA[> ME! AND ]]]]><![CDATA[> ME!!]]></system-out>"
})
}
}
@@ -0,0 +1,22 @@
package sbt
import sbt.testing._
class SimpleEvent(val duration: Long, val fullyQualifiedName: String, val fingerprint: Fingerprint,
val status: Status = Status.Success, error: Option[Throwable] = None) extends Event {
final val throwable: OptionalThrowable = error match {
case Some(err) => new OptionalThrowable(err)
case None => new OptionalThrowable
}
val selector: Selector = new TestSelector(fullyQualifiedName)
}
object JUnitAnnotation extends AnnotatedFingerprint {
override def annotationName(): String = "org.junit.Test"
override def isModule: Boolean = false
}
class JUnitEvent(duration: Long, fullyQualifiedName: String, status: Status = Status.Success, error: Option[Throwable] = None) extends SimpleEvent(
duration, fullyQualifiedName, JUnitAnnotation, status, error)