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