1 32 33 package com.jeantessier.metrics; 34 35 import java.io.*; 36 import java.util.*; 37 38 import org.xml.sax.*; 39 import org.xml.sax.helpers.*; 40 41 import org.apache.oro.text.perl.*; 42 43 import junit.framework.*; 44 45 import com.jeantessier.classreader.*; 46 47 public class TestXMLPrinter extends TestCase implements ErrorHandler { 48 private static final String TEST_CLASS = "test"; 49 private static final String TEST_FILENAME = "classes" + File.separator + "test.class"; 50 private static final String CONFIGURATION_FILENAME = "etc" + File.separator + "MetricsConfig.xml"; 51 private static final String READER_CLASSNAME = "org.apache.xerces.parsers.SAXParser"; 52 53 private static final String SPECIFIC_ENCODING = "iso-latin-1"; 54 private static final String SPECIFIC_DTD_PREFIX = "./etc"; 55 56 private StringWriter buffer; 57 private MetricsConfiguration configuration; 58 private com.jeantessier.metrics.XMLPrinter printer; 59 private XMLReader reader; 60 61 private Perl5Util perl; 62 63 protected void setUp() throws Exception { 64 buffer = new StringWriter(); 65 configuration = new MetricsConfigurationLoader().load(CONFIGURATION_FILENAME); 66 67 reader = XMLReaderFactory.createXMLReader(READER_CLASSNAME); 68 reader.setFeature("http://xml.org/sax/features/validation", true); 69 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", true); 70 reader.setErrorHandler(this); 71 72 perl = new Perl5Util(); 73 } 74 75 public void testDefaultDTDPrefix() { 76 printer = new XMLPrinter(new PrintWriter(buffer), configuration); 77 78 String xmlDocument = buffer.toString(); 79 assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument)); 80 assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(XMLPrinter.DEFAULT_DTD_PREFIX)); 81 82 try { 83 reader.parse(new InputSource(new StringReader(xmlDocument))); 84 fail("Parsed non-existant document\n" + xmlDocument); 85 } catch (SAXException ex) { 86 } catch (IOException ex) { 88 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 89 } 90 } 91 92 public void testSpecificDTDPrefix() { 93 printer = new XMLPrinter(new PrintWriter(buffer), configuration, XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); 94 95 String xmlDocument = buffer.toString(); 96 assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument)); 97 assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX)); 98 99 try { 100 reader.parse(new InputSource(new StringReader(xmlDocument))); 101 fail("Parsed non-existant document\n" + xmlDocument); 102 } catch (SAXException ex) { 103 } catch (IOException ex) { 105 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 106 } 107 } 108 109 public void testDefaultEncoding() { 110 printer = new XMLPrinter(new PrintWriter(buffer), configuration); 111 112 String xmlDocument = buffer.toString(); 113 assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument)); 114 assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl.group(1)); 115 116 try { 117 reader.parse(new InputSource(new StringReader(xmlDocument))); 118 fail("Parsed non-existant document\n" + xmlDocument); 119 } catch (SAXException ex) { 120 } catch (IOException ex) { 122 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 123 } 124 } 125 126 public void testSpecificEncoding() { 127 printer = new XMLPrinter(new PrintWriter(buffer), configuration, SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX); 128 129 String xmlDocument = buffer.toString(); 130 assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument)); 131 assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1)); 132 133 try { 134 reader.parse(new InputSource(new StringReader(xmlDocument))); 135 fail("Parsed non-existant document\n" + xmlDocument); 136 } catch (SAXException ex) { 137 } catch (IOException ex) { 139 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 140 } 141 } 142 143 public void testOneClass() { 144 MetricsFactory factory = new MetricsFactory("test", configuration); 145 146 ClassfileLoader loader = new AggregatingClassfileLoader(); 147 loader.load(Collections.singleton(TEST_FILENAME)); 148 loader.getClassfile(TEST_CLASS).accept(new MetricsGatherer("test", factory)); 149 150 printer = new XMLPrinter(new PrintWriter(buffer), configuration, com.jeantessier.metrics.XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX); 151 printer.visitMetrics(factory.getProjectMetrics()); 152 153 String xmlDocument = buffer.toString(); 154 155 try { 156 reader.parse(new InputSource(new StringReader(xmlDocument))); 157 } catch (SAXException ex) { 158 fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument); 159 } catch (IOException ex) { 160 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument); 161 } 162 } 163 164 public void error(SAXParseException ex) { 165 } 167 168 public void fatalError(SAXParseException ex) { 169 } 171 172 public void warning(SAXParseException ex) { 173 } 175 } 176 | Popular Tags |