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