PageRenderTime 34ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/testability-explorer/src/test/java/com/google/test/metric/report/XMLReportDifferTest.java

http://testability-explorer.googlecode.com/
Java | 129 lines | 110 code | 14 blank | 5 comment | 0 complexity | 9bcf6f59f40427dda0f2310fdf767c5e MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.google.test.metric.report;
  2. import java.io.IOException;
  3. import java.io.StringReader;
  4. import javax.xml.parsers.DocumentBuilder;
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import javax.xml.parsers.ParserConfigurationException;
  7. import junit.framework.TestCase;
  8. import org.w3c.dom.Document;
  9. import org.xml.sax.InputSource;
  10. import org.xml.sax.SAXException;
  11. /**
  12. * Tests for {@link com.google.test.metric.report.XMLReportDiffer}.
  13. *
  14. * @author alexeagle@google.com (Alex Eagle)
  15. */
  16. public class XMLReportDifferTest extends TestCase {
  17. public void testDiffSameClass() throws Exception {
  18. XMLReportDiffer differ = new XMLReportDiffer();
  19. Document oldDoc = makeTestDoc("<a></a>");
  20. Document newDoc = makeTestDoc("<a></a>");
  21. Diff diff = differ.diff(oldDoc, newDoc);
  22. assertTrue(diff.getClassDiffs().isEmpty());
  23. }
  24. public void testRemovedClass() throws Exception {
  25. XMLReportDiffer differ = new XMLReportDiffer();
  26. Document oldDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  27. Document newDoc = makeTestDoc("<a></a>");
  28. Diff diff = differ.diff(oldDoc, newDoc);
  29. assertEquals(1, diff.getClassDiffs().size());
  30. Diff.ClassDiff diff1 = diff.getClassDiffs().get(0);
  31. assertEquals("Foo", diff1.getClassName());
  32. assertEquals(12, diff1.getOldMetric().intValue());
  33. assertEquals(null, diff1.getNewMetric());
  34. }
  35. public void testAddedClass() throws Exception {
  36. XMLReportDiffer differ = new XMLReportDiffer();
  37. Document oldDoc = makeTestDoc("<a></a>");
  38. Document newDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  39. Diff diff = differ.diff(oldDoc, newDoc);
  40. assertEquals(1, diff.getClassDiffs().size());
  41. Diff.ClassDiff diff1 = diff.getClassDiffs().get(0);
  42. assertEquals("Foo", diff1.getClassName());
  43. assertEquals(null, diff1.getOldMetric());
  44. assertEquals(12, diff1.getNewMetric().intValue());
  45. }
  46. public void testChangedClass() throws Exception {
  47. XMLReportDiffer differ = new XMLReportDiffer();
  48. Document oldDoc = makeTestDoc("<class class='Foo' cost='21'></class>");
  49. Document newDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  50. Diff diff = differ.diff(oldDoc, newDoc);
  51. assertEquals(1, diff.getClassDiffs().size());
  52. Diff.ClassDiff diff1 = diff.getClassDiffs().get(0);
  53. assertEquals("Foo", diff1.getClassName());
  54. assertEquals(21, diff1.getOldMetric().intValue());
  55. assertEquals(12, diff1.getNewMetric().intValue());
  56. }
  57. public void testUnchangedClass() throws Exception {
  58. XMLReportDiffer differ = new XMLReportDiffer();
  59. Document oldDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  60. Document newDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  61. Diff diff = differ.diff(oldDoc, newDoc);
  62. assertEquals(0, diff.getClassDiffs().size());
  63. }
  64. public void testAddedMethod() throws Exception {
  65. XMLReportDiffer differ = new XMLReportDiffer();
  66. Document oldDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  67. Document newDoc = makeTestDoc("<class class='Foo' cost='12'>" +
  68. "<method cyclomatic='135' global='10' line='233' lod='1' " +
  69. " name='execute()' overall='236'/></class>");
  70. Diff diff = differ.diff(oldDoc, newDoc);
  71. assertEquals(1, diff.getClassDiffs().size());
  72. Diff.ClassDiff classDiff = diff.getClassDiffs().get(0);
  73. assertEquals(1, classDiff.getMethodDiffs().size());
  74. Diff.MethodDiff methodDiff = classDiff.getMethodDiffs().get(0);
  75. assertEquals("execute()", methodDiff.getMethodName());
  76. assertEquals(null, methodDiff.getOldMetric());
  77. assertEquals(236, methodDiff.getNewMetric().intValue());
  78. }
  79. public void testRemovedMethod() throws Exception {
  80. XMLReportDiffer differ = new XMLReportDiffer();
  81. Document oldDoc = makeTestDoc("<class class='Foo' cost='12'>" +
  82. "<method cyclomatic='135' global='10' line='233' lod='1' " +
  83. " name='execute()' overall='236'/></class>");
  84. Document newDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  85. Diff diff = differ.diff(oldDoc, newDoc);
  86. assertEquals(1, diff.getClassDiffs().size());
  87. Diff.ClassDiff classDiff = diff.getClassDiffs().get(0);
  88. assertEquals(1, classDiff.getMethodDiffs().size());
  89. Diff.MethodDiff methodDiff = classDiff.getMethodDiffs().get(0);
  90. assertEquals("execute()", methodDiff.getMethodName());
  91. assertEquals(236, methodDiff.getOldMetric().intValue());
  92. assertEquals(null, methodDiff.getNewMetric());
  93. }
  94. public void testChangedMethod() throws Exception {
  95. XMLReportDiffer differ = new XMLReportDiffer();
  96. Document oldDoc = makeTestDoc("<class class='Foo' cost='12'>" +
  97. "<method cyclomatic='135' global='10' line='233' lod='1' " +
  98. " name='execute()' overall='236'/></class>");
  99. Document newDoc = makeTestDoc("<class class='Foo' cost='12'></class>");
  100. Diff diff = differ.diff(oldDoc, newDoc);
  101. assertEquals(1, diff.getClassDiffs().size());
  102. Diff.ClassDiff classDiff = diff.getClassDiffs().get(0);
  103. assertEquals(1, classDiff.getMethodDiffs().size());
  104. Diff.MethodDiff methodDiff = classDiff.getMethodDiffs().get(0);
  105. assertEquals("execute()", methodDiff.getMethodName());
  106. assertEquals(236, methodDiff.getOldMetric().intValue());
  107. assertEquals(null, methodDiff.getNewMetric());
  108. }
  109. private Document makeTestDoc(String content)
  110. throws ParserConfigurationException, IOException, SAXException {
  111. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  112. DocumentBuilder builder = factory.newDocumentBuilder();
  113. return builder.parse(new InputSource(new StringReader(content)));
  114. }
  115. }