added unit tests for StrictDuplicateCode

This commit is contained in:
Lars Kühne 2005-07-03 10:12:15 +00:00
parent b106eab041
commit 46fc598003
7 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,10 @@
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class A
{
public static final int X = 0;
public static final int Y = 1;
public static final int Z = 2;
}

View File

@ -0,0 +1,10 @@
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class B
{
public static final int Y = 1;
public static final int X = 0;
public static final int Z = 2;
}

View File

@ -0,0 +1,53 @@
public class InnerDup
{
int x = 0;
public void m1()
{
x += 1;
x += 2;
x += 3;
x += 4;
x += 5;
x += 6;
x += 7;
x += 8;
x += 9;
x += 10;
x += 11;
x += 12;
}
public void m2()
{
x += 1;
x += 2;
x += 3;
x += 4;
x += 5;
x += 6;
x += 7;
x += 8;
x += 9;
x += 10;
x += 11;
x += 12;
x += 13;
x += 14;
}
public void m3()
{
x += 1;
x += 2;
x += 3;
x += 4;
x += 5;
x += 6;
x += 7;
x += 8;
x += 9;
x += 10;
}
}

View File

@ -0,0 +1,4 @@
// A class that is shorter than the number of lines considered a duplicate
public class Shorty {
}

View File

@ -31,6 +31,7 @@ public class AllTests {
suite.addTest(com.puppycrawl.tools.checkstyle.checks.blocks.AllTests.suite());
suite.addTest(com.puppycrawl.tools.checkstyle.checks.coding.AllTests.suite());
suite.addTest(com.puppycrawl.tools.checkstyle.checks.design.AllTests.suite());
suite.addTest(com.puppycrawl.tools.checkstyle.checks.duplicates.AllTests.suite());
suite.addTest(com.puppycrawl.tools.checkstyle.checks.header.AllTests.suite());
suite.addTest(com.puppycrawl.tools.checkstyle.checks.imports.AllTests.suite());
suite.addTest(com.puppycrawl.tools.checkstyle.checks.indentation.AllTests.suite());

View File

@ -0,0 +1,20 @@
package com.puppycrawl.tools.checkstyle.checks.duplicates;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static void main(String[] args) {
junit.textui.TestRunner.run(AllTests.class);
}
public static Test suite() {
TestSuite suite =
new TestSuite("Test for com.puppycrawl.tools.checkstyle.checks.duplicates");
suite.addTest(new TestSuite(StrictDuplicateCodeCheckTest.class));
return suite;
}
}

View File

@ -0,0 +1,50 @@
package com.puppycrawl.tools.checkstyle.checks.duplicates;
import java.io.File;
import com.puppycrawl.tools.checkstyle.BaseCheckTestCase;
import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.api.Configuration;
public class StrictDuplicateCodeCheckTest extends BaseCheckTestCase {
protected DefaultConfiguration createCheckerConfig(
Configuration aCheckConfig)
{
final DefaultConfiguration dc = new DefaultConfiguration("root");
dc.addChild(aCheckConfig);
return dc;
}
public void testDefaultSettings() throws Exception
{
final Configuration checkConfig = createCheckConfig(StrictDuplicateCodeCheck.class);
final String innerDupPath = getPath("duplicates/InnerDup.java");
final String[] expected = {
"6: Found duplicate of 13 lines in " + innerDupPath + ", starting from line 22",
};
final File[] checkedFiles = new File[] {
new File(innerDupPath),
new File(getPath("duplicates/Shorty.java")),
};
verify(createChecker(checkConfig), checkedFiles, innerDupPath, expected);
}
public void testSmallMin() throws Exception
{
final DefaultConfiguration checkConfig = createCheckConfig(StrictDuplicateCodeCheck.class);
checkConfig.addAttribute("min", "3");
final String aPath = getPath("duplicates/A.java");
final String bPath = getPath("duplicates/B.java");
final String[] expected = {
// imports should not be marked because developer cannot avoid them
// same constant def should not be marked because order is important for this check
};
final File[] checkedFiles = new File[] {
new File(aPath),
new File(bPath),
};
verify(createChecker(checkConfig), checkedFiles, aPath, expected);
}
}