View Javadoc

    1   package net.sourceforge.pmd;
    2
    3   import static org.junit.Assert.assertEquals;
    4   import static org.junit.Assert.assertSame;
    5
    6   import java.util.ArrayList;
    7   import java.util.Arrays;
    8   import java.util.Collections;
    9   import java.util.List;
    10  import java.util.Random;
    11
    12  import junit.framework.JUnit4TestAdapter;
    13  import net.sourceforge.pmd.Rule;
    14  import net.sourceforge.pmd.RuleContext;
    15  import net.sourceforge.pmd.RuleViolation;
    16  import net.sourceforge.pmd.RuleViolationComparator;
    17  import net.sourceforge.pmd.lang.java.ast.DummyJavaNode;
    18  import net.sourceforge.pmd.lang.java.rule.JavaRuleViolation;
    19  import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
    20  import net.sourceforge.pmd.lang.rule.MockRule;
    21
    22  import org.junit.Test;
    23
    24  public class RuleViolationComparatorTest {
    25
    26      @Test
    27      public void testComparator() {
    28  	Rule rule1 = new MockRule("name1", "desc", "msg", "rulesetname1");
    29  	Rule rule2 = new MockRule("name2", "desc", "msg", "rulesetname2");
    30
    31  	// RuleViolations created in pre-sorted order
    32  	RuleViolation[] expectedOrder = new RuleViolation[12];
    33
    34  	int index = 0;
    35  	// Different begin line
    36  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 10, "desc1", 0, 20, 80);
    37  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file1", 20, "desc1", 0, 20, 80);
    38  	// Different description
    39  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc1", 0, 20, 80);
    40  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file2", 10, "desc2", 0, 20, 80);
    41  	// Different begin column
    42  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 0, 20, 80);
    43  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file3", 10, "desc1", 10, 20, 80);
    44  	// Different end line
    45  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 0, 20, 80);
    46  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file4", 10, "desc1", 0, 30, 80);
    47  	// Different end column
    48  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 0, 20, 80);
    49  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file5", 10, "desc1", 0, 20, 90);
    50  	// Different rule name
    51  	expectedOrder[index++] = createJavaRuleViolation(rule1, "file6", 10, "desc1", 0, 20, 80);
    52  	expectedOrder[index++] = createJavaRuleViolation(rule2, "file6", 10, "desc1", 0, 20, 80);
    53
    54  	// Randomize
    55  	List<RuleViolation> ruleViolations = new ArrayList<RuleViolation>(Arrays.asList(expectedOrder));
    56  	long seed = System.nanoTime();
    57  	Random random = new Random(seed);
    58  	Collections.shuffle(ruleViolations, random);
    59
    60  	// Sort
    61  	Collections.sort(ruleViolations, RuleViolationComparator.INSTANCE);
    62
    63  	// Check
    64  	int count = 0;
    65  	for (int i = 0; i < expectedOrder.length; i++) {
    66  	    count++;
    67  	    assertSame("Wrong RuleViolation " + i + ", usind seed: " + seed, expectedOrder[i], ruleViolations.get(i));
    68  	}
    69  	assertEquals("Missing assertion for every RuleViolation", expectedOrder.length, count);
    70      }
    71
    72      private RuleViolation createJavaRuleViolation(Rule rule, String fileName, int beginLine, String description,
    73  	    int beginColumn, int endLine, int endColumn) {
    74  	RuleContext ruleContext = new RuleContext();
    75  	ruleContext.setSourceCodeFilename(fileName);
    76  	DummyJavaNode simpleNode = new DummyJavaNode(1);
    77  	simpleNode.setScope(new SourceFileScope(fileName));
    78  	simpleNode.testingOnly__setBeginLine(beginLine);
    79  	simpleNode.testingOnly__setBeginColumn(beginColumn);
    80  	simpleNode.testingOnly__setEndLine(endLine);
    81  	simpleNode.testingOnly__setEndColumn(endColumn);
    82  	RuleViolation ruleViolation = new JavaRuleViolation(rule, ruleContext, simpleNode, description);
    83  	return ruleViolation;
    84      }
    85
    86      public static junit.framework.Test suite() {
    87  	return new JUnit4TestAdapter(RuleViolationComparatorTest.class);
    88      }
    89  }