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