View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql;
5   
6   import java.util.Arrays;
7   
8   import net.sourceforge.pmd.RuleContext;
9   import net.sourceforge.pmd.lang.Language;
10  import net.sourceforge.pmd.lang.LanguageVersion;
11  import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
12  import net.sourceforge.pmd.lang.rule.XPathRule;
13  
14  import org.junit.Assert;
15  import org.junit.Before;
16  import org.junit.Test;
17  
18  /**
19   * Tests to use XPath rules with PLSQL.
20   */
21  public class PLSQLXPathRuleTest extends AbstractPLSQLParserTst {
22  
23      private ASTInput node = parsePLSQL("create or replace\n" + 
24              "package pkg_xpath_problem\n" + 
25              "AS\n" + 
26              "    PROCEDURE pkg_minimal\n" + 
27              "    IS\n" + 
28              "        a_variable VARCHAR2(1);\n" + 
29              "    BEGIN \n" + 
30              "        --PRAGMA INLINE(output,'YES');\n" + 
31              "        a_variable := 'Y' ;\n" + 
32              "    END ;\n" + 
33              "end pkg_xpath_problem;\n" + 
34              "/\n" + 
35              "");
36  
37      private RuleContext ctx = new RuleContext();
38  
39      @Before
40      public void setup() {
41          ctx.setLanguageVersion(LanguageVersion.PLSQL);
42      }
43  
44      /**
45       * See https://sourceforge.net/p/pmd/bugs/1166/
46       */
47      @Test
48      public void testXPathRule1() {
49          XPathRule rule = createRule("1.0");
50  
51          rule.apply(Arrays.asList(node), ctx);
52          Assert.assertEquals(2, ctx.getReport().treeSize());
53      }
54  
55      /**
56       * See https://sourceforge.net/p/pmd/bugs/1166/
57       */
58      @Test
59      public void testXPathRule1Compatibility() {
60          XPathRule rule = createRule("1.0 compatibility");
61  
62          rule.apply(Arrays.asList(node), ctx);
63          Assert.assertEquals(2, ctx.getReport().treeSize());
64      }
65  
66      /**
67       * See https://sourceforge.net/p/pmd/bugs/1166/
68       */
69      @Test
70      public void testXPathRule2() {
71          XPathRule rule = createRule("2.0");
72  
73          rule.apply(Arrays.asList(node), ctx);
74          Assert.assertEquals(2, ctx.getReport().treeSize());
75      }
76  
77      private XPathRule createRule(String version) {
78          XPathRule rule = new XPathRule("//PrimaryExpression");
79          rule.setLanguage(Language.PLSQL);
80          rule.setVersion(version);
81          rule.setMessage("Test Violation");
82          return rule;
83      }
84  
85  }