#1381 CPD Cannot use CSV/VS Renderers because they don't support encoding property
This commit is contained in:
@ -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);
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
|
@ -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:**
|
||||
|
Reference in New Issue
Block a user