View Javadoc

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