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.apache.myfaces.tobago.internal.util.StringUtils;
23  import org.junit.jupiter.api.BeforeEach;
24  
25  import javax.faces.component.UIComponent;
26  import java.io.File;
27  import java.io.IOException;
28  import java.net.URL;
29  import java.util.ArrayList;
30  import java.util.Enumeration;
31  import java.util.List;
32  
33  public class AbstractGeneratedUIComponentsUnitTest {
34  
35    private List<Class<? extends UIComponent>> uiComponents;
36  
37    protected List<Class<? extends UIComponent>> getUiComponents() {
38      return uiComponents;
39    }
40  
41    @BeforeEach
42    public void setup() throws IOException, ClassNotFoundException {
43      uiComponents = findUIComponents();
44    }
45  
46    /**
47     * Find all classes in a package
48     */
49    private List<Class<? extends UIComponent>> findUIComponents() throws ClassNotFoundException, IOException {
50      final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
51      final String packageName = this.getClass().getPackage().getName();
52      final String path = packageName.replace('.', '/');
53      final Enumeration<URL> resources = classLoader.getResources(path);
54      final List<File> directories = new ArrayList<>();
55      while (resources.hasMoreElements()) {
56        final URL resource = resources.nextElement();
57        directories.add(new File(resource.getFile()));
58      }
59      final ArrayList<Class<? extends UIComponent>> result = new ArrayList<>();
60      for (final File directory : directories) {
61        final File[] files = directory.listFiles();
62        if (files != null) {
63          for (final File file : files) {
64            final String name = file.getName();
65            if (!StringUtils.endsWith(name, ".class")) {
66              continue;
67            }
68            final String nameWithoutSuffix = name.substring(0, name.length() - 6);
69            final String className = packageName + '.' + nameWithoutSuffix;
70            final Class<? extends UIComponent> clazz = (Class<? extends UIComponent>) Class.forName(className);
71            if (UIComponent.class.isAssignableFrom(clazz)) {
72              result.add(clazz);
73            }
74          }
75        }
76      }
77      return result;
78    }
79  
80  }