1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }