diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index fb5d44c966..d92d02d253 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/src/main/java/net/sourceforge/pmd/cpd/AbstractLanguage.java b/pmd/src/main/java/net/sourceforge/pmd/cpd/AbstractLanguage.java index a335abcdc9..c836ccf2b7 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cpd/AbstractLanguage.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cpd/AbstractLanguage.java @@ -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. + } } diff --git a/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java b/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java index cc14ff7550..18aa51c36a 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cpd/CPD.java @@ -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 source = new TreeMap(); @@ -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(); diff --git a/pmd/src/main/java/net/sourceforge/pmd/cpd/JavaLanguage.java b/pmd/src/main/java/net/sourceforge/pmd/cpd/JavaLanguage.java index 658b3017e2..b5eb80e2da 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cpd/JavaLanguage.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cpd/JavaLanguage.java @@ -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); } } diff --git a/pmd/src/main/java/net/sourceforge/pmd/cpd/Language.java b/pmd/src/main/java/net/sourceforge/pmd/cpd/Language.java index 2431c00e9b..137e0d9f8f 100644 --- a/pmd/src/main/java/net/sourceforge/pmd/cpd/Language.java +++ b/pmd/src/main/java/net/sourceforge/pmd/cpd/Language.java @@ -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); } diff --git a/pmd/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java b/pmd/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java new file mode 100644 index 0000000000..9ea2df6888 --- /dev/null +++ b/pmd/src/test/java/net/sourceforge/pmd/cpd/CPDCommandLineInterfaceTest.java @@ -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); + } +} diff --git a/pmd/src/test/resources/net/sourceforge/pmd/cpd/clitest/File1.java b/pmd/src/test/resources/net/sourceforge/pmd/cpd/clitest/File1.java new file mode 100644 index 0000000000..9e810060c6 --- /dev/null +++ b/pmd/src/test/resources/net/sourceforge/pmd/cpd/clitest/File1.java @@ -0,0 +1,7 @@ +public class File1 { + public void dup() { + for (int i = 0; i < 10; i++) { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/pmd/src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java b/pmd/src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java new file mode 100644 index 0000000000..03ef25f631 --- /dev/null +++ b/pmd/src/test/resources/net/sourceforge/pmd/cpd/clitest/File2.java @@ -0,0 +1,7 @@ +public class File2 { + public void dup() { + for (int j = 0; j < 10; j++) { + System.out.println(j); + } + } +} \ No newline at end of file