pmd: fix #1114 CPD - Tokenizer not initialized with requested properties

This commit is contained in:
Andreas Dangel
2013-08-03 12:28:25 +02:00
parent ee39aa5f3c
commit da1ee1fe72
8 changed files with 92 additions and 5 deletions

View File

@ -1,3 +1,8 @@
????? ??, 2013 - 5.0.5:
Fixed bug 1114: CPD - Tokenizer not initialized with requested properties
May 1, 2013 - 5.0.4:
Fixed bug 254: False+ : UnusedImport with Javadoc @throws

View File

@ -1,6 +1,7 @@
package net.sourceforge.pmd.cpd;
import java.io.FilenameFilter;
import java.util.Properties;
import net.sourceforge.pmd.util.filter.Filters;
@ -20,4 +21,8 @@ public abstract class AbstractLanguage implements Language {
public Tokenizer getTokenizer() {
return tokenizer;
}
public void setProperties(Properties properties) {
// needs to be implemented by subclasses.
}
}

View File

@ -24,6 +24,8 @@ public class CPD {
private static final int MISSING_ARGS = 2;
private static final int DUPLICATE_CODE_FOUND = 4;
static boolean dontExitForTests = false;
private CPDConfiguration configuration;
private Map<String, SourceCode> source = new TreeMap<String, SourceCode>();
@ -111,7 +113,7 @@ public class CPD {
source.put(sourceCode.getFileName(), sourceCode);
}
private static void setSystemProperties(String[] args) {
private static void setSystemProperties(String[] args, CPDConfiguration config) {
boolean ignoreLiterals = CPDConfiguration.findBooleanSwitch(args, "--ignore-literals");
boolean ignoreIdentifiers = CPDConfiguration.findBooleanSwitch(args, "--ignore-identifiers");
boolean ignoreAnnotations = CPDConfiguration.findBooleanSwitch(args, "--ignore-annotations");
@ -126,6 +128,7 @@ public class CPD {
properties.setProperty(JavaTokenizer.IGNORE_ANNOTATIONS, "true");
}
System.setProperties(properties);
config.language().setProperties(properties);
}
public static void main(String[] args) {
@ -139,7 +142,7 @@ public class CPD {
// Pass extra parameters as System properties to allow language
// implementation to retrieve their associate values...
setSystemProperties(args);
setSystemProperties(args, config);
CPD cpd = new CPD(config);
@ -163,7 +166,9 @@ public class CPD {
cpd.go();
if (cpd.getMatches().hasNext()) {
System.out.println(config.renderer().render(cpd.getMatches()));
System.exit(DUPLICATE_CODE_FOUND);
if (!dontExitForTests) {
System.exit(DUPLICATE_CODE_FOUND);
}
}
} catch (Exception e) {
e.printStackTrace();

View File

@ -12,7 +12,11 @@ public class JavaLanguage extends AbstractLanguage {
public JavaLanguage(Properties properties) {
super(new JavaTokenizer(), ".java");
JavaTokenizer tokenizer = (JavaTokenizer)getTokenizer();
tokenizer.setProperties(properties);
setProperties(properties);
}
public final void setProperties(Properties properties) {
JavaTokenizer tokenizer = (JavaTokenizer)getTokenizer();
tokenizer.setProperties(properties);
}
}

View File

@ -4,10 +4,13 @@
package net.sourceforge.pmd.cpd;
import java.io.FilenameFilter;
import java.util.Properties;
public interface Language {
Tokenizer getTokenizer();
FilenameFilter getFileFilter();
void setProperties(Properties properties);
}

View File

@ -0,0 +1,51 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.cpd;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link CPDCommandLineInterface}.
*
*/
public class CPDCommandLineInterfaceTest {
private ByteArrayOutputStream buffer;
private PrintStream originalStdout;
@Before
public void setup() {
originalStdout = System.out;
buffer = new ByteArrayOutputStream();
System.setOut(new PrintStream(buffer));
}
@After
public void teardown() {
System.setOut(originalStdout);
}
/**
* Test ignore identifiers argument.
*/
@Test
public void testIgnoreIdentifiers() throws Exception {
runCPD("--minimum-tokens", "34", "--language", "java", "--files", "src/test/resources/net/sourceforge/pmd/cpd/clitest/", "--ignore-identifiers");
String out = buffer.toString("UTF-8");
Assert.assertTrue(out.contains("Found a 7 line (34 tokens) duplication"));
}
private void runCPD(String... args) {
CPD.dontExitForTests = true;
CPD.main(args);
}
}

View File

@ -0,0 +1,7 @@
public class File1 {
public void dup() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}

View File

@ -0,0 +1,7 @@
public class File2 {
public void dup() {
for (int j = 0; j < 10; j++) {
System.out.println(j);
}
}
}