#1381 CPD Cannot use CSV/VS Renderers because they don't support encoding property

This commit is contained in:
Andreas Dangel
2015-07-19 22:34:57 +02:00
parent df14ea9c95
commit e93e9224ae
4 changed files with 56 additions and 6 deletions

View File

@ -3,10 +3,12 @@
*/
package net.sourceforge.pmd.cpd;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FilenameFilter;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
@ -173,12 +175,7 @@ public class CPDConfiguration extends AbstractConfiguration {
}
try {
Renderer renderer = clazz.getDeclaredConstructor().newInstance();
PropertyDescriptor encodingProperty = new PropertyDescriptor("encoding", clazz);
Method method = encodingProperty.getWriteMethod();
if (method != null) {
method.invoke(renderer, encoding);
}
setRendererEncoding(renderer, encoding);
return renderer;
} catch (Exception e) {
System.err.println("Couldn't instantiate renderer, defaulting to SimpleRenderer: " + e);
@ -186,6 +183,19 @@ public class CPDConfiguration extends AbstractConfiguration {
}
}
private static void setRendererEncoding(Renderer renderer, String encoding)
throws IllegalAccessException, InvocationTargetException {
try {
PropertyDescriptor encodingProperty = new PropertyDescriptor("encoding", renderer.getClass());
Method method = encodingProperty.getWriteMethod();
if (method != null) {
method.invoke(renderer, encoding);
}
} catch (IntrospectionException e) {
// ignored - maybe this renderer doesn't have a encoding property
}
}
public static String[] getRenderers() {
String[] result = RENDERERS.keySet().toArray(new String[RENDERERS.size()]);
Arrays.sort(result);

View File

@ -0,0 +1,29 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
public class CPDConfigurationTest {
@Test
public void testRenderers() {
Map<String, Class<? extends Renderer>> renderersToTest = new HashMap<String, Class<? extends Renderer>>();
renderersToTest.put("csv", CSVRenderer.class);
renderersToTest.put("xml", XMLRenderer.class);
renderersToTest.put("csv_with_linecount_per_file", CSVWithLinecountPerFileRenderer.class);
renderersToTest.put("vs", VSRenderer.class);
renderersToTest.put("text", SimpleRenderer.class);
for (Map.Entry<String, Class<? extends Renderer>> entry : renderersToTest.entrySet()) {
Renderer r = CPDConfiguration.getRendererFromString(entry.getKey(), "UTF-8");
Assert.assertNotNull(r);
Assert.assertSame(entry.getValue(), r.getClass());
}
}
}

View File

@ -114,6 +114,16 @@ public class CPDCommandLineInterfaceTest {
Assert.assertTrue(out.contains("<duplication lines=\"3\" tokens=\"10\">"));
}
@Test
public void testCSVFormat() throws Exception {
runCPD("--minimum-tokens", "100",
"--files", "src/test/resources/net/sourceforge/pmd/cpd/badandgood/",
"--language", "c",
"--format", "csv");
String out = bufferStdout.toString("UTF-8");
Assert.assertFalse(out.contains("Couldn't instantiate renderer"));
}
private void runCPD(String... args) {
System.setProperty(CPDCommandLineInterface.NO_EXIT_AFTER_RUN, "true");
CPD.main(args);

View File

@ -22,5 +22,6 @@
* [#1375](https://sourceforge.net/p/pmd/bugs/1375/): CloseResource not detected properly
* [#1376](https://sourceforge.net/p/pmd/bugs/1376/): CompareObjectsWithEquals fails for type annotated method parameter
* [#1379](https://sourceforge.net/p/pmd/bugs/1379/): PMD CLI: Cannot specify multiple properties
* [#1381](https://sourceforge.net/p/pmd/bugs/1381/): CPD Cannot use CSV/VS Renderers because they don't support encoding property
**API Changes:**