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 junit.framework.Assert;
23  import org.apache.myfaces.tobago.internal.util.StringUtils;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import javax.faces.component.UIComponent;
28  import java.io.File;
29  import java.io.IOException;
30  import java.lang.reflect.Method;
31  import java.lang.reflect.Modifier;
32  import java.net.URL;
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.Enumeration;
36  import java.util.List;
37  
38  public class MethodOverwritingOfGeneratedUIComponentsUnitTest {
39  
40    private static final List<String> IGNORED_METHODS = Arrays.asList("getFamily", "saveState", "restoreState");
41    private static final List<String> IGNORED_COMPONENTS = Arrays.asList(
42        UIMenuSelectOne.class.getSimpleName());
43    private static final MethodOfComponentList IGNORED_METHODS_PER_COMPONENT = new MethodOfComponentList();
44  
45    static {
46      IGNORED_METHODS_PER_COMPONENT.add("isResizable", UIColumn.class);
47      IGNORED_METHODS_PER_COMPONENT.add("isResizable", UIColumnNode.class);
48      IGNORED_METHODS_PER_COMPONENT.add("isShowRootJunction", UISheet.class);
49      IGNORED_METHODS_PER_COMPONENT.add("isShowRootJunction", UITree.class);
50    }
51  
52    private List<Class<? extends UIComponent>> uiComponents;
53  
54    @Before
55    public void setup() throws IOException, ClassNotFoundException {
56      uiComponents = findUIComponents();
57    }
58  
59    @Test
60    public void test() {
61  
62      for (final Class<? extends UIComponent> uiComponent : uiComponents) {
63        final Method[] methods = uiComponent.getMethods();
64        for (final Method method : methods) {
65  
66          if (!method.getDeclaringClass().equals(uiComponent)) {
67            // check only the method, that have an implementation in the generated class
68            continue;
69          }
70  
71          if (IGNORED_METHODS.contains(method.getName())) {
72            continue;
73          }
74  
75          if (IGNORED_METHODS_PER_COMPONENT.contains(method.getName(), uiComponent)) {
76            continue;
77          }
78  
79          for (Class<?> superClass = uiComponent.getSuperclass();
80               superClass != null;
81               superClass = superClass.getSuperclass()) {
82            final Method superMethod;
83            try {
84              superMethod = superClass.getMethod(method.getName(), method.getParameterTypes());
85            } catch (final NoSuchMethodException e) {
86              // only looking for super methods
87              continue;
88            }
89  
90            if (!Modifier.isAbstract(superMethod.getModifiers())) {
91              Assert.fail(method.getName() + " of " + uiComponent.getName()
92                  + " hides a concrete super method (but is not in the ignore list).");
93              break;
94            }
95          }
96        }
97      }
98    }
99  
100   /**
101    * Find all classes in a package
102    */
103   private List<Class<? extends UIComponent>> findUIComponents() throws ClassNotFoundException, IOException {
104     final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
105     final String packageName = this.getClass().getPackage().getName();
106     final String path = packageName.replace('.', '/');
107     final Enumeration<URL> resources = classLoader.getResources(path);
108     final List<File> directories = new ArrayList<File>();
109     while (resources.hasMoreElements()) {
110       final URL resource = resources.nextElement();
111       directories.add(new File(resource.getFile()));
112     }
113     final ArrayList<Class<? extends UIComponent>> result = new ArrayList<Class<? extends UIComponent>>();
114     for (final File directory : directories) {
115       final File[] files = directory.listFiles();
116       if (files != null) {
117         for (final File file : files) {
118           final String name = file.getName();
119           if (!StringUtils.endsWith(name, ".class")) {
120             continue;
121           }
122           final String nameWithoutSuffix = name.substring(0, name.length() - 6);
123           if (IGNORED_COMPONENTS.contains(nameWithoutSuffix)) {
124             continue;
125           }
126           final String className = packageName + '.' + nameWithoutSuffix;
127           final Class<? extends UIComponent> clazz = (Class<? extends UIComponent>) Class.forName(className);
128           if (UIComponent.class.isAssignableFrom(clazz)) {
129             result.add(clazz);
130           }
131         }
132       }
133     }
134     return result;
135   }
136 
137   private static class MethodOfComponentList {
138 
139     private List<String> list = new ArrayList<String>();
140 
141     public void add(final String method, final Class<? extends UIComponent> component) {
142       list.add(concatenate(method, component));
143     }
144 
145     public boolean contains(final String method, final Class<? extends UIComponent> component) {
146       return list.contains(concatenate(method, component));
147     }
148 
149     private String concatenate(final String method, final Class<? extends UIComponent> component) {
150       return method + '@' + component.getName();
151     }
152 
153   }
154 }