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.assertFalse;
    8   import static org.junit.Assert.assertTrue;
    9
    10  import java.util.HashMap;
    11  import java.util.Map;
    12
    13  import net.sourceforge.pmd.PropertyDescriptor;
    14  import net.sourceforge.pmd.Report;
    15  import net.sourceforge.pmd.RuleContext;
    16  import net.sourceforge.pmd.RulePriority;
    17  import net.sourceforge.pmd.RuleViolation;
    18  import net.sourceforge.pmd.lang.Language;
    19  import net.sourceforge.pmd.lang.java.ast.DummyJavaNode;
    20  import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
    21  import net.sourceforge.pmd.lang.java.rule.JavaRuleViolation;
    22  import net.sourceforge.pmd.lang.java.symboltable.SourceFileScope;
    23  import net.sourceforge.pmd.lang.rule.properties.IntegerProperty;
    24  import net.sourceforge.pmd.lang.rule.properties.StringProperty;
    25
    26  import org.junit.Test;
    27  public class AbstractRuleTest {
    28
    29      private static class MyRule extends AbstractJavaRule {
    30      	private static final StringProperty pd = new StringProperty("foo", "foo property", "x", 1.0f);
    31
    32      	private static final StringProperty xpath = new StringProperty("xpath", "xpath property", "", 2.0f);
    33
    34          public MyRule() {
    35              definePropertyDescriptor(pd);
    36              definePropertyDescriptor(xpath);
    37              setName("MyRule");
    38              setMessage("my rule msg");
    39              setPriority(RulePriority.MEDIUM);
    40              setProperty(pd, "value");
    41          }
    42      }
    43
    44      private static class MyOtherRule extends AbstractJavaRule {
    45      	private static final PropertyDescriptor pd = new StringProperty("foo", "foo property", "x", 1.0f);
    46
    47  		public MyOtherRule() {
    48  	            definePropertyDescriptor(pd);
    49              setName("MyOtherRule");
    50              setMessage("my other rule");
    51              setPriority(RulePriority.MEDIUM);
    52              setProperty(pd, "value");
    53          }
    54      }
    55
    56      @Test
    57      public void testCreateRV() {
    58          MyRule r = new MyRule();
    59          r.setRuleSetName("foo");
    60          RuleContext ctx = new RuleContext();
    61          ctx.setSourceCodeFilename("filename");
    62          DummyJavaNode s = new DummyJavaNode(1);
    63          s.testingOnly__setBeginColumn(5);
    64          s.testingOnly__setBeginLine(5);
    65          s.setScope(new SourceFileScope("foo"));
    66          RuleViolation rv = new JavaRuleViolation(r, ctx, s, r.getMessage());
    67          assertEquals("Line number mismatch!", 5, rv.getBeginLine());
    68          assertEquals("Filename mismatch!", "filename", rv.getFilename());
    69          assertEquals("Rule object mismatch!", r, rv.getRule());
    70          assertEquals("Rule msg mismatch!", "my rule msg", rv.getDescription());
    71          assertEquals("RuleSet name mismatch!", "foo", rv.getRule().getRuleSetName());
    72      }
    73
    74      @Test
    75      public void testCreateRV2() {
    76          MyRule r = new MyRule();
    77          RuleContext ctx = new RuleContext();
    78          ctx.setSourceCodeFilename("filename");
    79          DummyJavaNode s = new DummyJavaNode(1);
    80          s.testingOnly__setBeginColumn(5);
    81          s.testingOnly__setBeginLine(5);
    82          s.setScope(new SourceFileScope("foo"));
    83          RuleViolation rv = new JavaRuleViolation(r, ctx, s, "specificdescription");
    84          assertEquals("Line number mismatch!", 5, rv.getBeginLine());
    85          assertEquals("Filename mismatch!", "filename", rv.getFilename());
    86          assertEquals("Rule object mismatch!", r, rv.getRule());
    87          assertEquals("Rule description mismatch!", "specificdescription", rv.getDescription());
    88      }
    89
    90      @Test
    91      public void testRuleWithVariableInMessage() {
    92          MyRule r = new MyRule();
    93          r.definePropertyDescriptor(new IntegerProperty("testInt", "description", 0, 100, 10, 0));
    94          r.setMessage("Message ${packageName} ${className} ${methodName} ${variableName} ${testInt} ${noSuchProperty}");
    95          RuleContext ctx = new RuleContext();
    96          ctx.setLanguageVersion(Language.JAVA.getDefaultVersion());
    97          ctx.setReport(new Report());
    98          ctx.setSourceCodeFilename("filename");
    99          DummyJavaNode s = new DummyJavaNode(1);
    100         s.testingOnly__setBeginColumn(5);
    101         s.testingOnly__setBeginLine(5);
    102         s.setImage("TestImage");
    103         s.setScope(new SourceFileScope("foo"));
    104         r.addViolation(ctx, s);
    105         RuleViolation rv = ctx.getReport().getViolationTree().iterator().next();
    106         assertEquals("Message foo    10 ${noSuchProperty}", rv.getDescription());
    107     }
    108
    109     @Test
    110     public void testRuleSuppress() {
    111         MyRule r = new MyRule();
    112         RuleContext ctx = new RuleContext();
    113         Map<Integer, String> m = new HashMap<Integer, String>();
    114         m.put(Integer.valueOf(5), "");
    115         ctx.setReport(new Report());
    116         ctx.getReport().suppress(m);
    117         ctx.setSourceCodeFilename("filename");
    118         DummyJavaNode n = new DummyJavaNode(1);
    119         n.testingOnly__setBeginColumn(5);
    120         n.testingOnly__setBeginLine(5);
    121         n.setScope(new SourceFileScope("foo"));
    122         RuleViolation rv = new JavaRuleViolation(r, ctx, n, "specificdescription");
    123         ctx.getReport().addRuleViolation(rv);
    124         assertTrue(ctx.getReport().isEmpty());
    125     }
    126
    127     @Test
    128     public void testEquals1() {
    129         MyRule r = new MyRule();
    130         assertFalse("A rule is never equals to null!", r.equals(null));
    131     }
    132
    133     @Test
    134     public void testEquals2() {
    135         MyRule r = new MyRule();
    136         assertEquals("A rule must be equals to itself", r, r);
    137     }
    138
    139     @Test
    140     public void testEquals3() {
    141         MyRule r1 = new MyRule();
    142         MyRule r2 = new MyRule();
    143         assertEquals("Two instances of the same rule are equal", r1, r2);
    144         assertEquals("Hashcode for two instances of the same rule must be equal", r1.hashCode(), r2.hashCode());
    145     }
    146
    147     @Test
    148     public void testEquals4() {
    149         MyRule myRule = new MyRule();
    150         assertFalse("A rule cannot be equal to an object of another class", myRule.equals("MyRule"));
    151     }
    152
    153     @Test
    154     public void testEquals5() {
    155         MyRule myRule = new MyRule();
    156         MyOtherRule myOtherRule = new MyOtherRule();
    157         assertFalse("Two rules from different classes cannot be equal", myRule.equals(myOtherRule));
    158     }
    159
    160     @Test
    161     public void testEquals6() {
    162         MyRule r1 = new MyRule();
    163         MyRule r2 = new MyRule();
    164         r2.setName("MyRule2");
    165         assertFalse("Rules with different names cannot be equal", r1.equals(r2));
    166     }
    167
    168     @Test
    169     public void testEquals7() {
    170         MyRule r1 = new MyRule();
    171         MyRule r2 = new MyRule();
    172         r2.setPriority(RulePriority.HIGH);
    173         assertFalse("Rules with different priority levels cannot be equal", r1.equals(r2));
    174     }
    175
    176     @Test
    177     public void testEquals8() {
    178         MyRule r1 = new MyRule();
    179         r1.setProperty(MyRule.xpath, "something");
    180         MyRule r2 = new MyRule();
    181         r2.setProperty(MyRule.xpath, "something else");
    182         assertFalse("Rules with different properties values cannot be equal", r1.equals(r2));
    183     }
    184
    185     @Test
    186     public void testEquals9() {
    187         MyRule r1 = new MyRule();
    188         MyRule r2 = new MyRule();
    189         r2.setProperty(MyRule.xpath, "something else");
    190         assertFalse("Rules with different properties cannot be equal", r1.equals(r2));
    191     }
    192
    193     @Test
    194     public void testEquals10() {
    195         MyRule r1 = new MyRule();
    196         MyRule r2 = new MyRule();
    197         r2.setMessage("another message");
    198         assertEquals("Rules with different messages are still equal", r1, r2);
    199         assertEquals("Rules that are equal must have the an equal hashcode", r1.hashCode(), r2.hashCode());
    200     }
    201
    202
    203     public static junit.framework.Test suite() {
    204         return new junit.framework.JUnit4TestAdapter(AbstractRuleTest.class);
    205     }
    206 }