View Javadoc

    1   /**
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.cpd;
    5
    6   import static org.junit.Assert.assertEquals;
    7   import static org.junit.Assert.assertTrue;
    8   import static org.junit.Assert.fail;
    9
    10  import java.io.ByteArrayInputStream;
    11  import java.util.ArrayList;
    12  import java.util.List;
    13
    14  import javax.xml.parsers.DocumentBuilderFactory;
    15
    16  import org.junit.Test;
    17  import org.w3c.dom.Document;
    18  import org.w3c.dom.Node;
    19  import org.w3c.dom.NodeList;
    20
    21  /**
    22   * @author Philippe T'Seyen
    23   * @author Romain Pelisse <belaran@gmail.com>
    24   *
    25   */
    26  public class XMLRendererTest {
    27
    28  	private final static String ENCODING = (String) System.getProperties().get("file.encoding");
    29
    30      @Test
    31      public void testWithNoDuplication() {
    32
    33          Renderer renderer = new XMLRenderer();
    34          List<Match> list = new ArrayList<Match>();
    35          String report = renderer.render(list.iterator());
    36          try {
    37              Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(report.getBytes(ENCODING)));
    38              NodeList nodes = doc.getChildNodes();
    39              Node n = nodes.item(0);
    40              assertEquals("pmd-cpd", n.getNodeName());
    41              assertEquals(0, doc.getElementsByTagName("duplication").getLength());
    42          } catch (Exception e) {
    43              e.printStackTrace();
    44              fail(e.getMessage());
    45          }
    46      }
    47
    48      @Test
    49      public void testWithOneDuplication() {
    50          Renderer renderer = new XMLRenderer();
    51          List<Match> list = new ArrayList<Match>();
    52          Match match = new Match(75, new TokenEntry("public", "/var/Foo.java", 48), new TokenEntry("stuff", "/var/Foo.java", 73));
    53          match.setLineCount(6);
    54          match.setSourceCodeSlice("code\nfragment");
    55          list.add(match);
    56          String report = renderer.render(list.iterator());
    57          try {
    58              Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(report.getBytes(ENCODING)));
    59              NodeList dupes = doc.getElementsByTagName("duplication");
    60              assertEquals(1, dupes.getLength());
    61              Node file = dupes.item(0).getFirstChild();
    62              while (file != null && file.getNodeType() != Node.ELEMENT_NODE) {
    63                  file = file.getNextSibling();
    64              }
    65              if (file != null) {
    66              	assertEquals("48", file.getAttributes().getNamedItem("line").getNodeValue());
    67                  assertEquals("/var/Foo.java", file.getAttributes().getNamedItem("path").getNodeValue());
    68  	            file = file.getNextSibling();
    69  	            while (file != null && file.getNodeType() != Node.ELEMENT_NODE) {
    70  	                file = file.getNextSibling();
    71  	            }
    72              }
    73              if (file != null) assertEquals("73", file.getAttributes().getNamedItem("line").getNodeValue());
    74              assertEquals(1, doc.getElementsByTagName("codefragment").getLength());
    75              assertEquals("code\nfragment", doc.getElementsByTagName("codefragment").item(0).getTextContent());
    76          } catch (Exception e) {
    77              e.printStackTrace();
    78              fail(e.getMessage());
    79          }
    80      }
    81
    82      @Test
    83      public void testRenderWithMultipleMatch() {
    84          Renderer renderer = new XMLRenderer();
    85          List<Match> list = new ArrayList<Match>();
    86          Match match1 = new Match(75, new TokenEntry("public", "/var/Foo.java", 48), new TokenEntry("void", "/var/Foo.java", 73));
    87          match1.setLineCount(6);
    88          match1.setSourceCodeSlice("code fragment");
    89          Match match2 = new Match(76, new TokenEntry("void", "/var/Foo2.java", 49), new TokenEntry("stuff", "/var/Foo2.java", 74));
    90          match2.setLineCount(7);
    91          match2.setSourceCodeSlice("code fragment 2");
    92          list.add(match1);
    93          list.add(match2);
    94          String report = renderer.render(list.iterator());
    95          try {
    96              Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(report.getBytes(ENCODING)));
    97              assertEquals(2, doc.getElementsByTagName("duplication").getLength());
    98              assertEquals(4, doc.getElementsByTagName("file").getLength());
    99          } catch (Exception e) {
    100             e.printStackTrace();
    101             fail(e.getMessage());
    102         }
    103     }
    104
    105     @Test
    106     public void testRendererEncodedPath() {
    107         Renderer renderer = new XMLRenderer();
    108         List<Match> list = new ArrayList<Match>();
    109         final String espaceChar = "&lt;";
    110         Match match1 = new Match(75, new TokenEntry("public", "/var/F" + '<' + "oo.java", 48), new TokenEntry("void", "/var/F<oo.java", 73));
    111         match1.setLineCount(6);
    112         match1.setSourceCodeSlice("code fragment");
    113         list.add(match1);
    114         String report = renderer.render(list.iterator());
    115         assertTrue(report.contains(espaceChar));
    116     }
    117
    118     public static junit.framework.Test suite() {
    119         return new junit.framework.JUnit4TestAdapter(XMLRendererTest.class);
    120     }
    121 }
    122