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

http://testability-explorer.googlecode.com/ · Java · 68 lines · 51 code · 11 blank · 6 comment · 0 complexity · 4cec4ef4f40536d560b633ac6aee8a9a MD5 · raw file

  1. package com.google.test.metric.report;
  2. import java.io.Reader;
  3. import java.io.StringReader;
  4. import junit.framework.TestCase;
  5. import org.w3c.dom.Document;
  6. import org.xml.sax.SAXException;
  7. /**
  8. * Tests for {@link XMLReportLoader}.
  9. *
  10. * @author alexeagle@google.com (Alex Eagle)
  11. */
  12. public class XMLReportLoaderTest extends TestCase {
  13. private XMLReportLoader loader;
  14. @Override
  15. protected void setUp() throws Exception {
  16. super.setUp();
  17. loader = new XMLReportLoader();
  18. }
  19. public void testReadBlank() throws Exception {
  20. try {
  21. loader.loadXML(new StringReader(""));
  22. fail("Should throw exception on empty document");
  23. } catch (SAXException e) {
  24. // expected
  25. }
  26. }
  27. public void testSingleClass() throws Exception {
  28. Reader in = new StringReader("<class></class>");
  29. Document classCosts = loader.loadXML(in);
  30. assertEquals(1, classCosts.getChildNodes().getLength());
  31. assertEquals("class", classCosts.getChildNodes().item(0).getNodeName());
  32. }
  33. public void testClassWithCost() throws Exception {
  34. Reader in = new StringReader("<class cost=\"12\" class=\"com.google.Foobar\"></class>");
  35. Document classCosts = loader.loadXML(in);
  36. assertEquals("12", classCosts.getElementsByTagName("class").
  37. item(0).getAttributes().getNamedItem("cost").getNodeValue());
  38. assertEquals("com.google.Foobar", classCosts.getElementsByTagName("class").
  39. item(0).getAttributes().getNamedItem("class").getNodeValue());
  40. }
  41. public void testMethods() throws Exception {
  42. Reader in = new StringReader("<class cost=\"12\">" +
  43. "<method cyclomatic=\"2\" global=\"2\" line=\"123\" "
  44. + "lod=\"0\" name=\"methodName\" overall=\"22\"></method></class>");
  45. Document classCosts = loader.loadXML(in);
  46. assertEquals("22", classCosts.getElementsByTagName("method").
  47. item(0).getAttributes().getNamedItem("overall").getNodeValue());
  48. assertEquals("methodName", classCosts.getElementsByTagName("method").
  49. item(0).getAttributes().getNamedItem("name").getNodeValue());
  50. assertEquals("2", classCosts.getElementsByTagName("method").
  51. item(0).getAttributes().getNamedItem("cyclomatic").getNodeValue());
  52. assertEquals("123", classCosts.getElementsByTagName("method").
  53. item(0).getAttributes().getNamedItem("line").getNodeValue());
  54. assertEquals("2", classCosts.getElementsByTagName("method").
  55. item(0).getAttributes().getNamedItem("global").getNodeValue());
  56. }
  57. }