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.uimap;
17  
18  import org.aludratest.codecheck.rule.pmd.AbstractAludraTestRule;
19  import org.aludratest.dict.ActionWordLibrary;
20  import org.aludratest.service.gui.web.page.PageHelper;
21  import org.aludratest.service.gui.web.page.PageUtility;
22  import org.aludratest.testcase.AludraTestCase;
23  
24  import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
25  import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
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 UIMapHelperImportRestriction extends AbstractAludraTestRule {
35      
36      private static final Class<?>[] FORBIDDEN_IMPORT_PARENTS = { PageHelper.class, PageUtility.class, AludraTestCase.class,
37              ActionWordLibrary.class };
38  
39      @Override
40      public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
41          if (!isUIMapHelperClass(node) && !isUIMapUtilityClass(node)) {
42              return null;
43          }
44  
45          // check all imports
46          for (ASTImportDeclaration impNode : getImports(node)) {
47              checkImport(impNode, data);
48          }
49  
50          return null;
51      }
52  
53      private void checkImport(ASTImportDeclaration importNode, Object data) {
54          Class<?> importClass = importNode.getType();
55  
56          for (Class<?> clazz : FORBIDDEN_IMPORT_PARENTS) {
57              if (clazz.isAssignableFrom(importClass)) {
58                  addViolationWithMessage(data, importNode, "Illegal import for a UIMapHelper or UIMapUtility class");
59                  return;
60              }
61          }
62      }
63  
64  }