You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lo...@apache.org on 2013/02/12 13:44:27 UTC

svn commit: r1445142 - /myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/component/MethodOverwritingOfGeneratedUIComponentsUnitTest.java

Author: lofwyr
Date: Tue Feb 12 12:44:27 2013
New Revision: 1445142

URL: http://svn.apache.org/r1445142
Log:
TOBAGO-1212: Using JSR 269: Pluggable Annotation Processing API
 - Test to check the  overwritten methods

Added:
    myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/component/MethodOverwritingOfGeneratedUIComponentsUnitTest.java

Added: myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/component/MethodOverwritingOfGeneratedUIComponentsUnitTest.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/component/MethodOverwritingOfGeneratedUIComponentsUnitTest.java?rev=1445142&view=auto
==============================================================================
--- myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/component/MethodOverwritingOfGeneratedUIComponentsUnitTest.java (added)
+++ myfaces/tobago/trunk/tobago-core/src/test/java/org/apache/myfaces/tobago/component/MethodOverwritingOfGeneratedUIComponentsUnitTest.java Tue Feb 12 12:44:27 2013
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.myfaces.tobago.component;
+
+import junit.framework.Assert;
+import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.faces.component.UIComponent;
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.List;
+
+public class MethodOverwritingOfGeneratedUIComponentsUnitTest {
+
+  private static final List<String> IGNORED_METHODS = Arrays.asList("getFamily", "saveState", "restoreState");
+  private static final List<String> IGNORED_COMPONENTS = Arrays.asList(
+      UIViewRoot.class.getSimpleName(),
+      UIExtensionPanel.class.getSimpleName(),
+      UIMenuSelectOne.class.getSimpleName(),
+      UIWizard.class.getSimpleName(), // TODO: check what is here to do. Wizard is not currently working.
+      UITreeData.class.getSimpleName()); // deprecated
+  private static final MethodOfComponentList IGNORED_METHODS_PER_COMPONENT = new MethodOfComponentList();
+
+  static {
+    IGNORED_METHODS_PER_COMPONENT.add("isResizable", UIColumn.class);
+    IGNORED_METHODS_PER_COMPONENT.add("isResizable", UIColumnNode.class);
+    IGNORED_METHODS_PER_COMPONENT.add("isShowRootJunction", UISheet.class);
+    IGNORED_METHODS_PER_COMPONENT.add("isShowRootJunction", UITree.class);
+  }
+
+  private List<Class<? extends UIComponent>> uiComponents;
+
+  @Before
+  public void setup() throws IOException, ClassNotFoundException {
+    uiComponents = findUIComponents();
+  }
+
+  @Test
+  public void test() {
+
+    for (Class<? extends UIComponent> uiComponent : uiComponents) {
+      final Method[] methods = uiComponent.getMethods();
+      for (Method method : methods) {
+
+        if (!method.getDeclaringClass().equals(uiComponent)) {
+          // check only the method, that have an implementation in the generated class
+          continue;
+        }
+
+        if (IGNORED_METHODS.contains(method.getName())) {
+          continue;
+        }
+
+        if (IGNORED_METHODS_PER_COMPONENT.contains(method.getName(), uiComponent)) {
+          continue;
+        }
+
+        for (Class<?> superClass = uiComponent.getSuperclass();
+             superClass != null;
+             superClass = superClass.getSuperclass()) {
+          Method superMethod;
+          try {
+            superMethod = superClass.getMethod(method.getName(), method.getParameterTypes());
+          } catch (NoSuchMethodException e) {
+            // only looking for super methods
+            continue;
+          }
+
+          if (!Modifier.isAbstract(superMethod.getModifiers())) {
+            Assert.fail(method.getName() + " of " + uiComponent.getName()
+                + " hides a concrete super method (but is not in the ignore list).");
+            break;
+          }
+        }
+      }
+    }
+  }
+
+  /**
+   * Find all classes in a package
+   */
+  private List<Class<? extends UIComponent>> findUIComponents() throws ClassNotFoundException, IOException {
+    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+    final String packageName = this.getClass().getPackage().getName();
+    final String path = packageName.replace('.', '/');
+    final Enumeration<URL> resources = classLoader.getResources(path);
+    final List<File> directories = new ArrayList<File>();
+    while (resources.hasMoreElements()) {
+      final URL resource = resources.nextElement();
+      directories.add(new File(resource.getFile()));
+    }
+    final ArrayList<Class<? extends UIComponent>> result = new ArrayList<Class<? extends UIComponent>>();
+    for (File directory : directories) {
+      final File[] files = directory.listFiles();
+      if (files != null) {
+        for (File file : files) {
+          String name = file.getName();
+          if (!StringUtils.endsWith(name, ".class")) {
+            continue;
+          }
+          final String nameWithoutSuffix = name.substring(0, name.length() - 6);
+          if (IGNORED_COMPONENTS.contains(nameWithoutSuffix)) {
+            continue;
+          }
+          final String className = packageName + '.' + nameWithoutSuffix;
+          final Class<? extends UIComponent> clazz = (Class<? extends UIComponent>) Class.forName(className);
+          if (UIComponent.class.isAssignableFrom(clazz)) {
+            result.add(clazz);
+          }
+        }
+      }
+    }
+    return result;
+  }
+
+  private static class MethodOfComponentList {
+
+    private List<String> list = new ArrayList<String>();
+
+    public void add(String method, Class<? extends UIComponent> component) {
+      list.add(concatenate(method, component));
+    }
+
+    public boolean contains(String method, Class<? extends UIComponent> component) {
+      return list.contains(concatenate(method, component));
+    }
+
+    private String concatenate(String method, Class<? extends UIComponent> component) {
+      return method + '@' + component.getName();
+    }
+
+  }
+}