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.Assert;
23  import org.junit.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    }
44  
45    @Test
46    public void test() {
47  
48      for (final Class<? extends UIComponent> uiComponent : getUiComponents()) {
49        final Method[] methods = uiComponent.getMethods();
50        for (final Method method : methods) {
51  
52          if (!method.getDeclaringClass().equals(uiComponent)) {
53            // check only the method, that have an implementation in the generated class
54            continue;
55          }
56  
57          if (IGNORED_METHODS.contains(method.getName())) {
58            continue;
59          }
60  
61          if (IGNORED_METHODS_PER_COMPONENT.contains(method.getName(), uiComponent)) {
62            continue;
63          }
64  
65          for (Class<?> superClass = uiComponent.getSuperclass();
66               superClass != null;
67               superClass = superClass.getSuperclass()) {
68            final Method superMethod;
69            try {
70              superMethod = superClass.getMethod(method.getName(), method.getParameterTypes());
71            } catch (final NoSuchMethodException e) {
72              // only looking for super methods
73              continue;
74            }
75  
76            if (!Modifier.isAbstract(superMethod.getModifiers())) {
77              Assert.fail(method.getName() + " of " + uiComponent.getName()
78                  + " hides a concrete super method (but is not in the ignore list).");
79              break;
80            }
81          }
82        }
83      }
84    }
85  
86    private static class MethodOfComponentList {
87  
88      private List<String> list = new ArrayList<String>();
89  
90      public void add(final String method, final Class<? extends UIComponent> component) {
91        list.add(concatenate(method, component));
92      }
93  
94      public boolean contains(final String method, final Class<? extends UIComponent> component) {
95        return list.contains(concatenate(method, component));
96      }
97  
98      private String concatenate(final String method, final Class<? extends UIComponent> component) {
99        return method + '@' + component.getName();
100     }
101 
102   }
103 }