Coverage Report - com.jeantessier.diff.TestReport
 
Classes in this File Line Coverage Branch Coverage Complexity
TestReport
76%
87/114
N/A
2.2
 
 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.diff;
 34  
 
 35  
 import java.io.*;
 36  
 import java.util.*;
 37  
 import javax.xml.parsers.*;
 38  
 import javax.xml.xpath.*;
 39  
 
 40  
 import junit.framework.*;
 41  
 
 42  
 import org.apache.oro.text.perl.*;
 43  
 import org.w3c.dom.*;
 44  
 import org.xml.sax.*;
 45  
 
 46  
 import com.jeantessier.classreader.*;
 47  
 
 48  6
 public class TestReport extends TestCase implements ErrorHandler {
 49  
     private static final String SPECIFIC_ENCODING   = "iso-latin-1";
 50  
     private static final String SPECIFIC_DTD_PREFIX = "./etc";
 51  
 
 52  1
     private static final String OLD_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "old";
 53  1
     private static final String NEW_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "new";
 54  
 
 55  1
     private static final String OLD_PUBLISHED_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "oldpublished";
 56  1
     private static final String NEW_PUBLISHED_CLASSPATH = "tests" + File.separator + "JarJarDiff" + File.separator + "newpublished";
 57  
 
 58  
     private XMLReader reader;
 59  
     private Perl5Util perl;
 60  
 
 61  
     protected void setUp() throws Exception {
 62  6
         boolean validate = Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE");
 63  
 
 64  6
         reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
 65  6
         reader.setFeature("http://xml.org/sax/features/validation", validate);
 66  6
         reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validate);
 67  6
         reader.setErrorHandler(this);
 68  
 
 69  6
         perl = new Perl5Util();
 70  6
     }
 71  
 
 72  
     public void testDefaultDTDPrefix() {
 73  1
         Report report = new Report();
 74  
 
 75  1
         String xmlDocument = report.render();
 76  1
         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
 77  1
         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + Report.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(Report.DEFAULT_DTD_PREFIX));
 78  
         
 79  
         try {
 80  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 81  0
         } catch (SAXException ex) {
 82  0
             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 83  0
         } catch (IOException ex) {
 84  0
             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 85  1
         }
 86  1
     }
 87  
     
 88  
     public void testSpecificDTDPrefix() {
 89  1
         Report report = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 90  
 
 91  1
         String xmlDocument = report.render();
 92  1
         assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
 93  1
         assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX));
 94  
         
 95  
         try {
 96  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 97  0
         } catch (SAXException ex) {
 98  0
             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 99  0
         } catch (IOException ex) {
 100  0
             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 101  1
         }
 102  1
     }
 103  
 
 104  
     public void testDefaultEncoding() {
 105  1
         Report report = new Report();
 106  
 
 107  1
         String xmlDocument = report.render();
 108  1
         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
 109  1
         assertEquals("Encoding", Report.DEFAULT_ENCODING, perl.group(1));
 110  
         
 111  
         try {
 112  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 113  0
         } catch (SAXException ex) {
 114  0
             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 115  0
         } catch (IOException ex) {
 116  0
             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 117  1
         }
 118  1
     }
 119  
 
 120  
     public void testSpecificEncoding() {
 121  1
         Report report = new Report(SPECIFIC_ENCODING, Report.DEFAULT_DTD_PREFIX);
 122  
 
 123  1
         String xmlDocument = report.render();
 124  1
         assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
 125  1
         assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
 126  
         
 127  
         try {
 128  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 129  0
         } catch (SAXException ex) {
 130  0
             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 131  0
         } catch (IOException ex) {
 132  0
             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 133  1
         }
 134  1
     }
 135  
 
 136  
     public void testContent() throws Exception {
 137  1
         PackageMapper oldPackages = new PackageMapper();
 138  1
         ClassfileLoader oldJar = new AggregatingClassfileLoader();
 139  1
         oldJar.addLoadListener(oldPackages);
 140  1
         oldJar.load(Collections.singleton(OLD_CLASSPATH));
 141  
 
 142  1
         PackageMapper newPackages = new PackageMapper();
 143  1
         ClassfileLoader newJar = new AggregatingClassfileLoader();
 144  1
         newJar.addLoadListener(newPackages);
 145  1
         newJar.load(Collections.singleton(NEW_CLASSPATH));
 146  
 
 147  1
         DifferencesFactory factory = new DifferencesFactory();
 148  1
         ProjectDifferences projectDifferences = (ProjectDifferences) factory.createProjectDifferences("test", "old", oldPackages, "new", newPackages);
 149  
 
 150  1
         Report report = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 151  1
         projectDifferences.accept(report);
 152  
 
 153  1
         String xmlDocument = report.render();
 154  
 
 155  
         try {
 156  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 157  0
         } catch (SAXException ex) {
 158  0
             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 159  0
         } catch (IOException ex) {
 160  0
             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 161  1
         }
 162  
 
 163  1
         XPath xPath = XPathFactory.newInstance().newXPath();
 164  1
         InputSource in = new InputSource(new StringReader(xmlDocument));
 165  1
         Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
 166  
 
 167  1
         assertNotNull("//differences", xPath.evaluate("//differences", doc));
 168  1
         assertNotNull("*/old[text()='old']", xPath.evaluate("*/old[text()='old']", doc));
 169  1
         assertEquals("*/modified-classes/class", 3, ((NodeList) xPath.evaluate("*/modified-classes/class", doc, XPathConstants.NODESET)).getLength());
 170  1
     }
 171  
 
 172  
     public void testIncompatibleContent() throws Exception {
 173  1
         PackageMapper oldPackages = new PackageMapper();
 174  1
         ClassfileLoader oldJar = new AggregatingClassfileLoader();
 175  1
         oldJar.addLoadListener(oldPackages);
 176  1
         oldJar.load(Collections.singleton(OLD_PUBLISHED_CLASSPATH + File.separator + "ModifiedPackage" + File.separator + "CompatibleClass.class"));
 177  
 
 178  1
         PackageMapper newPackages = new PackageMapper();
 179  1
         ClassfileLoader newJar = new AggregatingClassfileLoader();
 180  1
         newJar.addLoadListener(newPackages);
 181  1
         newJar.load(Collections.singleton(NEW_PUBLISHED_CLASSPATH + File.separator + "ModifiedPackage" + File.separator + "CompatibleClass.class"));
 182  
 
 183  1
         DifferencesFactory factory = new DifferencesFactory(new IncompatibleDifferenceStrategy(new NoDifferenceStrategy()));
 184  1
         ProjectDifferences projectDifferences = (ProjectDifferences) factory.createProjectDifferences("test", "old", oldPackages, "new", newPackages);
 185  
 
 186  1
         Report report = new Report(Report.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 187  1
         projectDifferences.accept(report);
 188  
 
 189  1
         String xmlDocument = report.render();
 190  
 
 191  
         try {
 192  1
             reader.parse(new InputSource(new StringReader(xmlDocument)));
 193  0
         } catch (SAXException ex) {
 194  0
             fail("Could not parse XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 195  0
         } catch (IOException ex) {
 196  0
             fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 197  1
         }
 198  
 
 199  1
         XPath xPath = XPathFactory.newInstance().newXPath();
 200  1
         InputSource in  = new InputSource(new StringReader(xmlDocument));
 201  1
         Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
 202  
 
 203  1
         assertNotNull("//differences", xPath.evaluate("//differences", doc));
 204  1
         assertNotNull("*/old[text()='old']", xPath.evaluate("*/old[text()='old']", doc));
 205  1
         assertEquals("*/modified-classes/class", 1, ((NodeList) xPath.evaluate("*/modified-classes/class", doc, XPathConstants.NODESET)).getLength());
 206  1
         assertEquals("*/modified-classes/class/modified-declaration", "", xPath.evaluate("*/modified-classes/class/modified-declaration", doc));
 207  1
         assertEquals("*/modified-classes/class/modified-methods/feature", 1, ((NodeList) xPath.evaluate("*/modified-classes/class/modified-methods/feature", doc, XPathConstants.NODESET)).getLength());
 208  1
         assertNotNull("*/modified-classes/class/modified-methods/feature/modified-declaration", xPath.evaluate("*/modified-classes/class/modified-methods/feature/modified-declaration", doc));
 209  1
     }
 210  
 
 211  
     public void error(SAXParseException ex) {
 212  
         // Ignore
 213  0
     }
 214  
 
 215  
     public void fatalError(SAXParseException ex) {
 216  
         // Ignore
 217  0
     }
 218  
 
 219  
     public void warning(SAXParseException ex) {
 220  
         // Ignore
 221  0
     }
 222  
 }