Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 38f7c78

Browse files
committedJan 23, 2015
Updated serializer to be streaming #89
1 parent 7782fe5 commit 38f7c78

File tree

3 files changed

+74
-65
lines changed

3 files changed

+74
-65
lines changed
 

‎scalac-scoverage-plugin/src/main/scala/scoverage/Serializer.scala

Lines changed: 69 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,83 @@ package scoverage
33
import java.io._
44

55
import scala.io.Source
6-
import scala.xml.{XML, Utility, Node}
6+
import scala.xml.XML
77

88
object Serializer {
99

10-
/**
11-
* Write out coverage data to the given data directory
12-
*/
10+
// Write out coverage data to the given data directory, using the default coverage filename
1311
def serialize(coverage: Coverage, dataDir: String): Unit = serialize(coverage, coverageFile(dataDir))
14-
def serialize(coverage: Coverage, file: File): Unit = IOUtils.writeToFile(file, serialize(coverage).toString)
1512

16-
def coverageFile(dataDir: File): File = coverageFile(dataDir.getAbsolutePath)
17-
def coverageFile(dataDir: String): File = new File(dataDir, Constants.CoverageFileName)
13+
// Write out coverage data to given file.
14+
def serialize(coverage: Coverage, file: File): Unit = {
15+
val writer = new BufferedWriter(new FileWriter(file))
16+
serialize(coverage, writer)
17+
writer.close()
18+
}
1819

19-
def serialize(coverage: Coverage): Node = {
20-
val lines = coverage.statements.map(stmt => {
21-
<statement>
22-
<source>
23-
{stmt.source}
24-
</source>
25-
<package>
26-
{stmt.location.packageName}
27-
</package>
28-
<class>
29-
{stmt.location.className}
30-
</class>
31-
<classType>
32-
{stmt.location.classType.toString}
33-
</classType>
34-
<topLevelClass>
35-
{stmt.location.topLevelClass}
36-
</topLevelClass>
37-
<method>
38-
{stmt.location.method}
39-
</method>
40-
<path>
41-
{stmt.location.sourcePath}
42-
</path>
43-
<id>
44-
{stmt.id.toString}
45-
</id>
46-
<start>
47-
{stmt.start.toString}
48-
</start>
49-
<end>
50-
{stmt.end.toString}
51-
</end>
52-
<line>
53-
{stmt.line.toString}
54-
</line>
55-
<description>
56-
{escape(stmt.desc)}
57-
</description>
58-
<symbolName>
59-
{escape(stmt.symbolName)}
60-
</symbolName>
61-
<treeName>
62-
{escape(stmt.treeName)}
63-
</treeName>
64-
<branch>
65-
{stmt.branch.toString}
66-
</branch>
67-
<count>
68-
{stmt.count.toString}
69-
</count>
70-
</statement>
71-
})
72-
Utility.trim(<statements>
73-
{lines}
74-
</statements>)
20+
def serialize(coverage: Coverage, writer: Writer): Unit = {
21+
def writeStatement(stmt: Statement, writer: Writer): Unit = {
22+
writer.write {
23+
<statement>
24+
<source>
25+
{stmt.source}
26+
</source>
27+
<package>
28+
{stmt.location.packageName}
29+
</package>
30+
<class>
31+
{stmt.location.className}
32+
</class>
33+
<classType>
34+
{stmt.location.classType.toString}
35+
</classType>
36+
<topLevelClass>
37+
{stmt.location.topLevelClass}
38+
</topLevelClass>
39+
<method>
40+
{stmt.location.method}
41+
</method>
42+
<path>
43+
{stmt.location.sourcePath}
44+
</path>
45+
<id>
46+
{stmt.id.toString}
47+
</id>
48+
<start>
49+
{stmt.start.toString}
50+
</start>
51+
<end>
52+
{stmt.end.toString}
53+
</end>
54+
<line>
55+
{stmt.line.toString}
56+
</line>
57+
<description>
58+
{escape(stmt.desc)}
59+
</description>
60+
<symbolName>
61+
{escape(stmt.symbolName)}
62+
</symbolName>
63+
<treeName>
64+
{escape(stmt.treeName)}
65+
</treeName>
66+
<branch>
67+
{stmt.branch.toString}
68+
</branch>
69+
<count>
70+
{stmt.count.toString}
71+
</count>
72+
</statement>.toString + "\n"
73+
}
74+
}
75+
writer.write("<statements>\n")
76+
coverage.statements.foreach(stmt => writeStatement(stmt, writer))
77+
writer.write("</statements>")
7578
}
7679

80+
def coverageFile(dataDir: File): File = coverageFile(dataDir.getAbsolutePath)
81+
def coverageFile(dataDir: String): File = new File(dataDir, Constants.CoverageFileName)
82+
7783
def deserialize(str: String): Coverage = {
7884
val xml = XML.loadString(str)
7985
val statements = xml \ "statement" map (node => {

‎scalac-scoverage-plugin/src/test/scala/scoverage/SerializerTest.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package scoverage
22

3+
import java.io.StringWriter
4+
35
import org.scalatest.{OneInstancePerTest, FunSuite}
46
import org.scalatest.mock.MockitoSugar
57

@@ -21,8 +23,9 @@ class SerializerTest extends FunSuite with MockitoSugar with OneInstancePerTest
2123
<source>mysource</source> <package>org.scoverage</package> <class>test</class> <classType>Trait</classType> <topLevelClass>test</topLevelClass> <method>mymethod</method> <path>mypath</path> <id>14</id> <start>100</start> <end>200</end> <line>4</line> <description>def test : String</description> <symbolName>test</symbolName> <treeName>DefDef</treeName> <branch>true</branch> <count>32</count>
2224
</statement>
2325
</statements>
24-
val actual = Serializer.serialize(coverage)
25-
assert(Utility.trim(expected) === Utility.trim(actual))
26+
val writer = new StringWriter()
27+
val actual = Serializer.serialize(coverage, writer)
28+
assert(Utility.trim(expected) === Utility.trim(xml.XML.loadString(writer.toString)))
2629
}
2730

2831
test("coverage should be deserializable from xml") {

0 commit comments

Comments
 (0)
Please sign in to comment.