View Javadoc
1   /*
2    * Copyright (C) 2010-2014 Hamburg Sud and the contributors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.aludratest.codecheck.rule.pmd.page;
17  
18  import org.aludratest.codecheck.rule.pmd.AbstractUsageRestrictionRule;
19  import org.aludratest.codecheck.rule.pmd.ClassMatcher;
20  import org.aludratest.codecheck.rule.pmd.UsageRestrictionCheck;
21  import org.aludratest.service.gui.web.page.Page;
22  import org.aludratest.service.gui.web.page.PageHelper;
23  import org.aludratest.service.gui.web.page.PageUtility;
24  
25  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
26  
27  /**
28   * See <code>src/main/resources/pmd-rules-aludra.xml</code> or the project Site
29   * for rule description.
30   * 
31   * @author falbrech
32   * 
33   */
34  public class PageUtilityUsageRestriction extends AbstractUsageRestrictionRule {
35  
36      @Override
37      protected UsageRestrictionCheck createUsageRestrictionCheck() {
38          UsageRestrictionCheck pageHelperUsageCheck = new UsageRestrictionCheck(new ClassMatcher() {
39              @Override
40              public boolean matches(Class<?> clazz) {
41                  return isPageUtilityClass(clazz);
42              }
43          });
44  
45          pageHelperUsageCheck.addAllowedUserClass(Page.class);
46          pageHelperUsageCheck.addAllowedUserClass(PageHelper.class);
47          pageHelperUsageCheck.addAllowedUserClass(PageUtility.class);
48          return pageHelperUsageCheck;
49      }
50  
51      @Override
52      protected String getImportViolationMessage() {
53          return "PageHelper classes must only be used by Pages and other PageHelpers";
54      }
55  
56      @Override
57      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
58          if (!isPageHelperClass(node) && !isPageUtilityClass(node) && !isPageClass(node)) {
59              return super.visit(node, data);
60          }
61  
62          return null;
63      }
64  }