Coverage Report - com.jeantessier.metrics.TestXMLPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
TestXMLPrinter
88%
60/68
N/A
1.571
TestXMLPrinter$1
100%
3/3
N/A
1.571
TestXMLPrinter$2
100%
3/3
N/A
1.571
TestXMLPrinter$3
100%
3/3
N/A
1.571
TestXMLPrinter$4
100%
3/3
N/A
1.571
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *  
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *  
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *  
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *  
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *  
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.metrics;
 34  
 
 35  
 import java.io.*;
 36  
 import java.util.*;
 37  
 import javax.xml.parsers.*;
 38  
 import javax.xml.xpath.*;
 39  
 
 40  
 import org.apache.oro.text.perl.*;
 41  
 import org.xml.sax.*;
 42  
 import org.jmock.integration.junit3.*;
 43  
 import org.jmock.*;
 44  
 import org.w3c.dom.*;
 45  
 
 46  
 import com.jeantessier.classreader.*;
 47  
 
 48  9
 public class TestXMLPrinter extends MockObjectTestCase {
 49  
     private static final String TEST_CLASS = "test";
 50  1
     private static final String TEST_FILENAME = "classes" + File.separator + "test.class";
 51  1
     private static final String CONFIGURATION_FILENAME = "etc" + File.separator + "MetricsConfig.xml";
 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 XMLReader reader;
 59  
     private ErrorHandler errorHandler;
 60  
 
 61  
     private Perl5Util perl;
 62  
 
 63  
     protected void setUp() throws Exception {
 64  5
         buffer = new StringWriter();
 65  5
         configuration = new MetricsConfigurationLoader().load(CONFIGURATION_FILENAME);
 66  
 
 67  5
         boolean validate = Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE");
 68  
 
 69  5
         reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
 70  5
         reader.setFeature("http://xml.org/sax/features/validation", validate);
 71  5
         reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validate);
 72  
 
 73  5
         errorHandler = mock(ErrorHandler.class);
 74  5
         reader.setErrorHandler(errorHandler);
 75  
 
 76  5
         perl = new Perl5Util();
 77  5
     }
 78  
 
 79  
     public void testDefaultDTDPrefix() throws Exception {
 80  1
         checking(new Expectations() {{
 81  1
             one (errorHandler).fatalError(with(any(SAXParseException.class)));
 82  1
         }});
 83  
 
 84  1
         XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer), configuration);
 85  
 
 86  1
         String xmlDocument = buffer.toString();
 87  1
         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
 88  1
         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
 89  
         
 90  
         try {
 91  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 92  0
             fail("Parsed non-existant document\n" + xmlDocument);
 93  1
         } catch (SAXException ex) {
 94  
             // Ignore
 95  0
         }
 96  1
     }
 97  
     
 98  
     public void testSpecificDTDPrefix() throws Exception {
 99  1
         checking(new Expectations() {{
 100  1
             one (errorHandler).fatalError(with(any(SAXParseException.class)));
 101  1
         }});
 102  
 
 103  1
         XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer), configuration, XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 104  
 
 105  1
         String xmlDocument = buffer.toString();
 106  1
         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
 107  1
         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX));
 108  
         
 109  
         try {
 110  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 111  0
             fail("Parsed non-existant document\n" + xmlDocument);
 112  1
         } catch (SAXException ex) {
 113  
             // Ignore
 114  0
         }
 115  1
     }
 116  
 
 117  
     public void testDefaultEncoding() throws Exception {
 118  1
         checking(new Expectations() {{
 119  1
             one (errorHandler).fatalError(with(any(SAXParseException.class)));
 120  1
         }});
 121  
 
 122  1
         XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer), configuration);
 123  
 
 124  1
         String xmlDocument = buffer.toString();
 125  1
         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
 126  1
         assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl.group(1));
 127  
         
 128  
         try {
 129  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 130  0
             fail("Parsed non-existant document\n" + xmlDocument);
 131  1
         } catch (SAXException ex) {
 132  
             // Ignore
 133  0
         }
 134  1
     }
 135  
 
 136  
     public void testSpecificEncoding() throws Exception {
 137  1
         checking(new Expectations() {{
 138  1
             one (errorHandler).fatalError(with(any(SAXParseException.class)));
 139  1
         }});
 140  
 
 141  1
         XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer), configuration, SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
 142  
 
 143  1
         String xmlDocument = buffer.toString();
 144  1
         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
 145  1
         assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
 146  
         
 147  
         try {
 148  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 149  0
             fail("Parsed non-existant document\n" + xmlDocument);
 150  1
         } catch (SAXException ex) {
 151  
             // Ignore
 152  0
         }
 153  1
     }
 154  
 
 155  
     public void testOneClass() throws Exception {
 156  1
         MetricsFactory factory = new MetricsFactory("test", configuration);
 157  
 
 158  1
         ClassfileLoader loader = new AggregatingClassfileLoader();
 159  1
         loader.load(Collections.singleton(TEST_FILENAME));
 160  1
         loader.getClassfile(TEST_CLASS).accept(new MetricsGatherer(factory));
 161  
 
 162  1
         XMLPrinter printer = new XMLPrinter(new PrintWriter(buffer), configuration, XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 163  1
         printer.visitMetrics(factory.getProjectMetrics());
 164  
 
 165  1
         String xmlDocument = buffer.toString();
 166  1
         reader.parse(new InputSource(new StringReader(xmlDocument)));
 167  1
         assertXPath(xmlDocument, "metrics/project/group/class/measurement[short-name='PARAM' and value=0.5]/median", 1);
 168  1
     }
 169  
 
 170  
     private void assertXPath(String xmlDocument, String xPathExpression, int i) throws Exception {
 171  1
         XPath xPath = XPathFactory.newInstance().newXPath();
 172  1
         InputSource in = new InputSource(new StringReader(xmlDocument));
 173  
 
 174  1
         NodeList nodeList = (NodeList) xPath.evaluate(xPathExpression, in, XPathConstants.NODESET);
 175  1
         assertEquals("XPath \"" + xPathExpression + "\" in \n" + xmlDocument, i, nodeList.getLength());
 176  1
     }
 177  
 }