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