Clover coverage report - Dependency Finder
Coverage timestamp: Mon Nov 29 2010 15:00:50 PST
file stats: LOC: 270   Methods: 12
NCLOC: 187   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TestXMLCyclePrinter.java - 94% 83.3% 93.1%
coverage coverage
 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.dependency;
 34   
 35    import java.io.*;
 36    import java.util.*;
 37   
 38    import javax.xml.parsers.*;
 39   
 40    import junit.framework.*;
 41    import org.xml.sax.*;
 42    import org.apache.oro.text.perl.*;
 43   
 44    public class TestXMLCyclePrinter extends TestCase implements ErrorHandler {
 45    private static final String SPECIFIC_ENCODING = "iso-latin-1";
 46    private static final String SPECIFIC_DTD_PREFIX = "./etc";
 47   
 48    private XMLReader reader;
 49    private Perl5Util perl;
 50    private NodeFactory factory;
 51    private StringWriter out;
 52   
 53    private Node a_package;
 54    private Node b_package;
 55    private Node c_package;
 56   
 57  8 protected void setUp() throws Exception {
 58  8 boolean validate = Boolean.getBoolean("DEPENDENCYFINDER_TESTS_VALIDATE");
 59   
 60  8 reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
 61  8 reader.setFeature("http://xml.org/sax/features/validation", validate);
 62  8 reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", validate);
 63  8 reader.setErrorHandler(this);
 64   
 65  8 perl = new Perl5Util();
 66   
 67  8 factory = new NodeFactory();
 68  8 out = new StringWriter();
 69   
 70  8 a_package = factory.createPackage("a");
 71  8 b_package = factory.createPackage("b");
 72  8 c_package = factory.createPackage("c");
 73    }
 74   
 75  1 public void testDefaultDTDPrefix() {
 76  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out));
 77   
 78  1 String xmlDocument = out.toString();
 79  1 assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
 80  1 assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"" + XMLPrinter.DEFAULT_DTD_PREFIX + "\"", perl.group(1).startsWith(XMLPrinter.DEFAULT_DTD_PREFIX));
 81   
 82  1 try {
 83  1 reader.parse(new InputSource(new StringReader(xmlDocument)));
 84  0 fail("Parsed non-existant document\n" + xmlDocument);
 85    } catch (SAXException ex) {
 86    // Ignore
 87    } catch (IOException ex) {
 88  0 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 89    }
 90    }
 91   
 92  1 public void testSpecificDTDPrefix() {
 93  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 94   
 95  1 String xmlDocument = out.toString();
 96  1 assertTrue(xmlDocument + "Missing DTD", perl.match("/DOCTYPE \\S+ SYSTEM \"(.*)\"/", xmlDocument));
 97  1 assertTrue("DTD \"" + perl.group(1) + "\" does not have prefix \"./etc\"", perl.group(1).startsWith(SPECIFIC_DTD_PREFIX));
 98   
 99  1 try {
 100  1 reader.parse(new InputSource(new StringReader(xmlDocument)));
 101  0 fail("Parsed non-existant document\n" + xmlDocument);
 102    } catch (SAXException ex) {
 103    // Ignore
 104    } catch (IOException ex) {
 105  0 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 106    }
 107    }
 108   
 109  1 public void testDefaultEncoding() {
 110  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out));
 111   
 112  1 String xmlDocument = out.toString();
 113  1 assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
 114  1 assertEquals("Encoding", XMLPrinter.DEFAULT_ENCODING, perl.group(1));
 115   
 116  1 try {
 117  1 reader.parse(new InputSource(new StringReader(xmlDocument)));
 118  0 fail("Parsed non-existant document\n" + xmlDocument);
 119    } catch (SAXException ex) {
 120    // Ignore
 121    } catch (IOException ex) {
 122  0 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 123    }
 124    }
 125   
 126  1 public void testSpecificEncoding() {
 127  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), SPECIFIC_ENCODING, XMLPrinter.DEFAULT_DTD_PREFIX);
 128   
 129  1 String xmlDocument = out.toString();
 130  1 assertTrue(xmlDocument + "Missing encoding", perl.match("/encoding=\"([^\"]*)\"/", xmlDocument));
 131  1 assertEquals("Encoding", SPECIFIC_ENCODING, perl.group(1));
 132   
 133  1 try {
 134  1 reader.parse(new InputSource(new StringReader(xmlDocument)));
 135  0 fail("Parsed non-existant document\n" + xmlDocument);
 136    } catch (SAXException ex) {
 137    // Ignore
 138    } catch (IOException ex) {
 139  0 fail("Could not read XML Document: " + ex.getMessage() + "\n" + xmlDocument);
 140    }
 141    }
 142   
 143  1 public void testVisitCyclesWith2NodeCycle() throws IOException {
 144  1 List<Node> nodes = new ArrayList<Node>();
 145  1 nodes.add(a_package);
 146  1 nodes.add(b_package);
 147  1 Cycle cycle = new Cycle(nodes);
 148   
 149  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 150  1 printer.visitCycles(Collections.singletonList(cycle));
 151  1 int lineNumber = 0;
 152  1 BufferedReader in = new BufferedReader(new StringReader(out.toString()));
 153   
 154  1 assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
 155  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 156  1 assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
 157  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 158  1 assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
 159  1 assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
 160  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
 161  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
 162  1 assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
 163  1 assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
 164   
 165  1 assertEquals("End of file", null, in.readLine());
 166    }
 167   
 168  1 public void testVisitCyclesWith2NodeCycleWithIndentText() throws IOException {
 169  1 List<Node> nodes = new ArrayList<Node>();
 170  1 nodes.add(a_package);
 171  1 nodes.add(b_package);
 172  1 Cycle cycle = new Cycle(nodes);
 173   
 174  1 CyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 175  1 printer.setIndentText("*");
 176  1 printer.visitCycles(Collections.singletonList(cycle));
 177  1 int lineNumber = 0;
 178  1 BufferedReader in = new BufferedReader(new StringReader(out.toString()));
 179   
 180  1 assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
 181  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 182  1 assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
 183  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 184  1 assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
 185  1 assertEquals("line " + ++lineNumber, "*<cycle>", in.readLine());
 186  1 assertEquals("line " + ++lineNumber, "**<node type=\"package\">a</node>", in.readLine());
 187  1 assertEquals("line " + ++lineNumber, "**<node type=\"package\">b</node>", in.readLine());
 188  1 assertEquals("line " + ++lineNumber, "*</cycle>", in.readLine());
 189  1 assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
 190   
 191  1 assertEquals("End of file", null, in.readLine());
 192    }
 193   
 194  1 public void testVisitCycleWith3NodeCycle() throws IOException {
 195  1 List<Node> nodes = new ArrayList<Node>();
 196  1 nodes.add(a_package);
 197  1 nodes.add(b_package);
 198  1 nodes.add(c_package);
 199  1 Cycle cycle = new Cycle(nodes);
 200   
 201  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 202  1 printer.visitCycles(Collections.singletonList(cycle));
 203  1 int lineNumber = 0;
 204  1 BufferedReader in = new BufferedReader(new StringReader(out.toString()));
 205   
 206  1 assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
 207  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 208  1 assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
 209  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 210  1 assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
 211  1 assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
 212  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
 213  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
 214  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">c</node>", in.readLine());
 215  1 assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
 216  1 assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
 217   
 218  1 assertEquals("End of file", null, in.readLine());
 219    }
 220   
 221  1 public void testVisitCycleWith2Cycles() throws IOException {
 222  1 List<Cycle> cycles = new ArrayList<Cycle>();
 223   
 224  1 List<Node> nodes1 = new ArrayList<Node>();
 225  1 nodes1.add(a_package);
 226  1 nodes1.add(b_package);
 227  1 cycles.add(new Cycle(nodes1));
 228   
 229  1 List<Node> nodes2 = new ArrayList<Node>();
 230  1 nodes2.add(a_package);
 231  1 nodes2.add(b_package);
 232  1 nodes2.add(c_package);
 233  1 cycles.add(new Cycle(nodes2));
 234   
 235  1 XMLCyclePrinter printer = new XMLCyclePrinter(new PrintWriter(out), XMLPrinter.DEFAULT_ENCODING, SPECIFIC_DTD_PREFIX);
 236  1 printer.visitCycles(cycles);
 237  1 int lineNumber = 0;
 238  1 BufferedReader in = new BufferedReader(new StringReader(out.toString()));
 239   
 240  1 assertEquals("line " + ++lineNumber, "<?xml version=\"1.0\" encoding=\"utf-8\" ?>", in.readLine());
 241  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 242  1 assertEquals("line " + ++lineNumber, "<!DOCTYPE dependencies SYSTEM \"./etc/cycles.dtd\">", in.readLine());
 243  1 assertEquals("line " + ++lineNumber, "", in.readLine());
 244  1 assertEquals("line " + ++lineNumber, "<cycles>", in.readLine());
 245  1 assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
 246  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
 247  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
 248  1 assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
 249  1 assertEquals("line " + ++lineNumber, " <cycle>", in.readLine());
 250  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">a</node>", in.readLine());
 251  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">b</node>", in.readLine());
 252  1 assertEquals("line " + ++lineNumber, " <node type=\"package\">c</node>", in.readLine());
 253  1 assertEquals("line " + ++lineNumber, " </cycle>", in.readLine());
 254  1 assertEquals("line " + ++lineNumber, "</cycles>", in.readLine());
 255   
 256  1 assertEquals("End of file", null, in.readLine());
 257    }
 258   
 259  0 public void error(SAXParseException ex) {
 260    // Ignore
 261    }
 262   
 263  4 public void fatalError(SAXParseException ex) {
 264    // Ignore
 265    }
 266   
 267  0 public void warning(SAXParseException ex) {
 268    // Ignore
 269    }
 270    }