XMLRenderer implements Renderer, CPDRenderer { @Override public String render(Iterator<Match> matches) { StringWriter writer = new StringWriter(); try { render(matches, writer); } catch (IOException ignored) { } return writer.toString(); } XMLRenderer();  XMLRenderer(String encoding); void setEncoding(String encoding); String getEncoding(); @Override String render(Iterator<Match> matches); @Override void render(Iterator<Match> matches, Writer writer);  }
@Test public void testWithNoDuplication() throws IOException { CPDRenderer renderer = new XMLRenderer(); List<Match> list = new ArrayList<>(); StringWriter sw = new StringWriter(); renderer.render(list.iterator(), sw); String report = sw.toString(); try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(report.getBytes(ENCODING))); NodeList nodes = doc.getChildNodes(); Node n = nodes.item(0); assertEquals("pmd-cpd", n.getNodeName()); assertEquals(0, doc.getElementsByTagName("duplication").getLength()); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testWithOneDuplication() throws IOException { CPDRenderer renderer = new XMLRenderer(); List<Match> list = new ArrayList<>(); int lineCount = 6; String codeFragment = "code\nfragment"; Mark mark1 = createMark("public", "/var/Foo.java", 1, lineCount, codeFragment); Mark mark2 = createMark("stuff", "/var/Foo.java", 73, lineCount, codeFragment); Match match = new Match(75, mark1, mark2); list.add(match); StringWriter sw = new StringWriter(); renderer.render(list.iterator(), sw); String report = sw.toString(); try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(report.getBytes(ENCODING))); NodeList dupes = doc.getElementsByTagName("duplication"); assertEquals(1, dupes.getLength()); Node file = dupes.item(0).getFirstChild(); while (file != null && file.getNodeType() != Node.ELEMENT_NODE) { file = file.getNextSibling(); } if (file != null) { assertEquals("1", file.getAttributes().getNamedItem("line").getNodeValue()); assertEquals("/var/Foo.java", file.getAttributes().getNamedItem("path").getNodeValue()); assertEquals("6", file.getAttributes().getNamedItem("endline").getNodeValue()); assertEquals(null, file.getAttributes().getNamedItem("column")); assertEquals(null, file.getAttributes().getNamedItem("endcolumn")); file = file.getNextSibling(); while (file != null && file.getNodeType() != Node.ELEMENT_NODE) { file = file.getNextSibling(); } } if (file != null) { assertEquals("73", file.getAttributes().getNamedItem("line").getNodeValue()); assertEquals("78", file.getAttributes().getNamedItem("endline").getNodeValue()); assertEquals(null, file.getAttributes().getNamedItem("column")); assertEquals(null, file.getAttributes().getNamedItem("endcolumn")); } assertEquals(1, doc.getElementsByTagName("codefragment").getLength()); assertEquals(codeFragment, doc.getElementsByTagName("codefragment").item(0).getTextContent()); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testRenderWithMultipleMatch() throws IOException { CPDRenderer renderer = new XMLRenderer(); List<Match> list = new ArrayList<>(); int lineCount1 = 6; String codeFragment1 = "code fragment"; Mark mark1 = createMark("public", "/var/Foo.java", 48, lineCount1, codeFragment1); Mark mark2 = createMark("void", "/var/Foo.java", 73, lineCount1, codeFragment1); Match match1 = new Match(75, mark1, mark2); int lineCount2 = 7; String codeFragment2 = "code fragment 2"; Mark mark3 = createMark("void", "/var/Foo2.java", 49, lineCount2, codeFragment2); Mark mark4 = createMark("stuff", "/var/Foo2.java", 74, lineCount2, codeFragment2); Match match2 = new Match(76, mark3, mark4); list.add(match1); list.add(match2); StringWriter sw = new StringWriter(); renderer.render(list.iterator(), sw); String report = sw.toString(); try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(report.getBytes(ENCODING))); assertEquals(2, doc.getElementsByTagName("duplication").getLength()); assertEquals(4, doc.getElementsByTagName("file").getLength()); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testWithOneDuplicationWithColumns() throws IOException { CPDRenderer renderer = new XMLRenderer(); List<Match> list = new ArrayList<>(); int lineCount = 6; String codeFragment = "code\nfragment"; Mark mark1 = createMark("public", "/var/Foo.java", 1, lineCount, codeFragment, 2, 3); Mark mark2 = createMark("stuff", "/var/Foo.java", 73, lineCount, codeFragment, 4, 5); Match match = new Match(75, mark1, mark2); list.add(match); StringWriter sw = new StringWriter(); renderer.render(list.iterator(), sw); String report = sw.toString(); try { Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new ByteArrayInputStream(report.getBytes(ENCODING))); NodeList dupes = doc.getElementsByTagName("duplication"); assertEquals(1, dupes.getLength()); Node file = dupes.item(0).getFirstChild(); while (file != null && file.getNodeType() != Node.ELEMENT_NODE) { file = file.getNextSibling(); } if (file != null) { assertEquals("1", file.getAttributes().getNamedItem("line").getNodeValue()); assertEquals("/var/Foo.java", file.getAttributes().getNamedItem("path").getNodeValue()); assertEquals("6", file.getAttributes().getNamedItem("endline").getNodeValue()); assertEquals("2", file.getAttributes().getNamedItem("column").getNodeValue()); assertEquals("3", file.getAttributes().getNamedItem("endcolumn").getNodeValue()); file = file.getNextSibling(); while (file != null && file.getNodeType() != Node.ELEMENT_NODE) { file = file.getNextSibling(); } } if (file != null) { assertEquals("73", file.getAttributes().getNamedItem("line").getNodeValue()); assertEquals("78", file.getAttributes().getNamedItem("endline").getNodeValue()); assertEquals("4", file.getAttributes().getNamedItem("column").getNodeValue()); assertEquals("5", file.getAttributes().getNamedItem("endcolumn").getNodeValue()); } assertEquals(1, doc.getElementsByTagName("codefragment").getLength()); assertEquals(codeFragment, doc.getElementsByTagName("codefragment").item(0).getTextContent()); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
@Test public void testRendererEncodedPath() throws IOException { CPDRenderer renderer = new XMLRenderer(); List<Match> list = new ArrayList<>(); final String espaceChar = "&lt;"; Mark mark1 = createMark("public", "/var/A<oo.java" + FORM_FEED, 48, 6, "code fragment"); Mark mark2 = createMark("void", "/var/B<oo.java", 73, 6, "code fragment"); Match match1 = new Match(75, mark1, mark2); list.add(match1); StringWriter sw = new StringWriter(); renderer.render(list.iterator(), sw); String report = sw.toString(); assertTrue(report.contains(espaceChar)); assertFalse(report.contains(FORM_FEED)); assertFalse(report.contains(FORM_FEED_ENTITY)); }
@Test public void testRendererXMLEscaping() throws IOException { String codefragment = "code fragment" + FORM_FEED + "\nline2\nline3\nno & escaping necessary in CDATA\nx=\"]]>\";"; CPDRenderer renderer = new XMLRenderer(); List<Match> list = new ArrayList<>(); Mark mark1 = createMark("public", "file1", 1, 5, codefragment); Mark mark2 = createMark("public", "file2", 5, 5, codefragment); Match match1 = new Match(75, mark1, mark2); list.add(match1); StringWriter sw = new StringWriter(); renderer.render(list.iterator(), sw); String report = sw.toString(); assertFalse(report.contains(FORM_FEED)); assertFalse(report.contains(FORM_FEED_ENTITY)); assertTrue(report.contains("no & escaping necessary in CDATA")); assertFalse(report.contains("x=\"]]>\";")); assertTrue(report.contains("x=\"]]]]><![CDATA[>\";")); }