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.assertTrue;
8   import junit.framework.JUnit4TestAdapter;
9   import net.sourceforge.pmd.Rule;
10  import net.sourceforge.pmd.RuleContext;
11  import net.sourceforge.pmd.RuleViolation;
12  import net.sourceforge.pmd.RuleViolationComparator;
13  import net.sourceforge.pmd.lang.java.ast.DummyJavaNode;
14  import net.sourceforge.pmd.lang.java.rule.JavaRuleViolation;
15  import net.sourceforge.pmd.lang.rule.MockRule;
16  
17  import org.junit.Ignore;
18  import org.junit.Test;
19  
20  public class RuleViolationTest {
21  
22      @Ignore
23      @Test
24      public void testConstructor1() {
25          Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
26          RuleContext ctx = new RuleContext();
27          ctx.setSourceCodeFilename("filename");
28          DummyJavaNode s = new DummyJavaNode(1);
29          s.testingOnly__setBeginLine(2);
30          RuleViolation r = new JavaRuleViolation(rule, ctx, s, rule.getMessage());
31          assertEquals("object mismatch", rule, r.getRule());
32          assertEquals("line number is wrong", 2, r.getBeginLine());
33          assertEquals("filename is wrong", "filename", r.getFilename());
34      }
35  
36      @Ignore
37      @Test
38      public void testConstructor2() {
39          Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
40          RuleContext ctx = new RuleContext();
41          ctx.setSourceCodeFilename("filename");
42          DummyJavaNode s = new DummyJavaNode(1);
43          s.testingOnly__setBeginLine(2);
44          RuleViolation r = new JavaRuleViolation(rule, ctx, s, "description");
45          assertEquals("object mismatch", rule, r.getRule());
46          assertEquals("line number is wrong", 2, r.getBeginLine());
47          assertEquals("filename is wrong", "filename", r.getFilename());
48          assertEquals("description is wrong", "description", r.getDescription());
49      }
50  
51      @Ignore
52      @Test
53      public void testComparatorWithDifferentFilenames() {
54          Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
55          RuleViolationComparator comp = RuleViolationComparator.INSTANCE;
56          RuleContext ctx = new RuleContext();
57          ctx.setSourceCodeFilename("filename1");
58          DummyJavaNode s = new DummyJavaNode(1);
59          s.testingOnly__setBeginLine(10);
60          RuleViolation r1 = new JavaRuleViolation(rule, ctx, s, "description");
61          ctx.setSourceCodeFilename("filename2");
62          DummyJavaNode s1 = new DummyJavaNode(1);
63          s1.testingOnly__setBeginLine(10);
64          RuleViolation r2 = new JavaRuleViolation(rule, ctx, s1, "description");
65          assertEquals(-1, comp.compare(r1, r2));
66          assertEquals(1, comp.compare(r2, r1));
67      }
68  
69      @Ignore
70      @Test
71      public void testComparatorWithSameFileDifferentLines() {
72          Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
73          RuleViolationComparator comp = RuleViolationComparator.INSTANCE;
74          RuleContext ctx = new RuleContext();
75          ctx.setSourceCodeFilename("filename");
76          DummyJavaNode s = new DummyJavaNode(1);
77          s.testingOnly__setBeginLine(10);
78          DummyJavaNode s1 = new DummyJavaNode(1);
79          s1.testingOnly__setBeginLine(20);
80          RuleViolation r1 = new JavaRuleViolation(rule, ctx, s, "description");
81          RuleViolation r2 = new JavaRuleViolation(rule, ctx, s1, "description");
82          assertTrue(comp.compare(r1, r2) < 0);
83          assertTrue(comp.compare(r2, r1) > 0);
84      }
85  
86      @Ignore
87      @Test
88      public void testComparatorWithSameFileSameLines() {
89          Rule rule = new MockRule("name", "desc", "msg", "rulesetname");
90          RuleViolationComparator comp = RuleViolationComparator.INSTANCE;
91          RuleContext ctx = new RuleContext();
92          ctx.setSourceCodeFilename("filename");
93          DummyJavaNode s = new DummyJavaNode(1);
94          s.testingOnly__setBeginLine(10);
95          DummyJavaNode s1 = new DummyJavaNode(1);
96          s1.testingOnly__setBeginLine(10);
97          RuleViolation r1 = new JavaRuleViolation(rule, ctx, s, "description");
98          RuleViolation r2 = new JavaRuleViolation(rule, ctx, s1, "description");
99          assertEquals(1, comp.compare(r1, r2));
100         assertEquals(1, comp.compare(r2, r1));
101     }
102 
103     public static junit.framework.Test suite() {
104         return new JUnit4TestAdapter(RuleViolationTest.class);
105     }
106 }