View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.component;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  import javax.faces.component.UIComponent;
26  import java.lang.reflect.Method;
27  import java.lang.reflect.Modifier;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  public class MethodOverwritingOfGeneratedUIComponentsUnitTest extends AbstractGeneratedUIComponentsUnitTest {
33  
34    private static final List<String> IGNORED_METHODS
35        = Arrays.asList("getFamily", "saveState", "restoreState", "getEventNames", "getDefaultEventName");
36    private static final MethodOfComponentList IGNORED_METHODS_PER_COMPONENT = new MethodOfComponentList();
37  
38    static {
39      IGNORED_METHODS_PER_COMPONENT.add("isResizable", UIColumn.class);
40      IGNORED_METHODS_PER_COMPONENT.add("isResizable", UIColumnNode.class);
41      IGNORED_METHODS_PER_COMPONENT.add("isShowRootJunction", UISheet.class);
42      IGNORED_METHODS_PER_COMPONENT.add("isShowRootJunction", UITree.class);
43      IGNORED_METHODS_PER_COMPONENT.add("isPlain", UIOut.class);
44      IGNORED_METHODS_PER_COMPONENT.add("isPlain", UIForm.class);
45    }
46  
47    @Test
48    public void test() {
49  
50      for (final Class<? extends UIComponent> uiComponent : getUiComponents()) {
51        final Method[] methods = uiComponent.getMethods();
52        for (final Method method : methods) {
53  
54          if (!method.getDeclaringClass().equals(uiComponent)) {
55            // check only the method, that have an implementation in the generated class
56            continue;
57          }
58  
59          if (IGNORED_METHODS.contains(method.getName())) {
60            continue;
61          }
62  
63          if (IGNORED_METHODS_PER_COMPONENT.contains(method.getName(), uiComponent)) {
64            continue;
65          }
66  
67          for (Class<?> superClass = uiComponent.getSuperclass();
68               superClass != null;
69               superClass = superClass.getSuperclass()) {
70            final Method superMethod;
71            try {
72              superMethod = superClass.getMethod(method.getName(), method.getParameterTypes());
73            } catch (final NoSuchMethodException e) {
74              // only looking for super methods
75              continue;
76            }
77  
78            if (!Modifier.isAbstract(superMethod.getModifiers())) {
79              Assertions.fail(method.getName() + " of " + uiComponent.getName()
80                  + " hides a concrete super method (but is not in the ignore list).");
81              break;
82            }
83          }
84        }
85      }
86    }
87  
88    private static class MethodOfComponentList {
89  
90      private List<String> list = new ArrayList<>();
91  
92      public void add(final String method, final Class<? extends UIComponent> component) {
93        list.add(concatenate(method, component));
94      }
95  
96      public boolean contains(final String method, final Class<? extends UIComponent> component) {
97        return list.contains(concatenate(method, component));
98      }
99  
100     private String concatenate(final String method, final Class<? extends UIComponent> component) {
101       return method + '@' + component.getName();
102     }
103 
104   }
105 }