1 package org.opentrafficsim.road.gtu.perception;
2
3 import static org.junit.Assert.fail;
4
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Method;
7 import java.lang.reflect.Modifier;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.LinkedHashSet;
11 import java.util.List;
12 import java.util.Set;
13
14 import org.junit.Test;
15 import org.opentrafficsim.base.TimeStampedObject;
16 import org.opentrafficsim.core.gtu.perception.AbstractPerceptionCategory;
17 import org.opentrafficsim.road.ClassList;
18
19
20
21
22
23
24
25
26
27
28
29
30 public class VerifyPerceptionCategoryMethods
31 {
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 @Test
65 public final void perceptionCategoryTest()
66 {
67
68 Collection<Class<?>> classList = ClassList.classList("org.opentrafficsim", true);
69 for (Class<?> c : classList)
70 {
71 if (AbstractPerceptionCategory.class.isAssignableFrom(c) && !Modifier.isAbstract(c.getModifiers()))
72 {
73 Set<String> fieldsDone = new LinkedHashSet<>();
74 List<String> fieldNames = new ArrayList<>();
75 List<String> methodNames = new ArrayList<>();
76 List<Class<?>> methodReturnTypes = new ArrayList<>();
77 for (Field field : c.getDeclaredFields())
78 {
79 fieldNames.add(field.getName());
80 }
81 for (Method method : c.getMethods())
82 {
83 methodNames.add(method.getName());
84 methodReturnTypes.add(method.getReturnType());
85 }
86 for (Method method : c.getDeclaredMethods())
87 {
88 if (Modifier.isPrivate(method.getModifiers()))
89 {
90 continue;
91 }
92 String name = method.getName();
93 String field = null;
94 boolean isBoolean = false;
95 if (name.startsWith("is") && name.endsWith("TimeStamped"))
96 {
97 field = name.substring(2, name.length() - 11);
98 isBoolean = true;
99 }
100 else if (name.startsWith("is"))
101 {
102 field = name.substring(2);
103 isBoolean = true;
104 }
105 else if (name.startsWith("getTimeStamped"))
106 {
107 field = name.substring(14);
108 }
109 else if (name.startsWith("get"))
110 {
111 field = name.substring(3);
112 }
113
114 if (field != null)
115 {
116 String fieldDown = field.substring(0, 1).toLowerCase() + field.substring(1);
117 if (!fieldsDone.contains(fieldDown))
118 {
119 String fieldUp = field.substring(0, 1).toUpperCase() + field.substring(1);
120 if (isBoolean)
121 {
122 testGetter(c, fieldNames, methodNames, methodReturnTypes, fieldDown, "is" + fieldUp,
123 "is" + fieldUp + "TimeStamped", "update" + fieldUp);
124 }
125 else
126 {
127 testGetter(c, fieldNames, methodNames, methodReturnTypes, fieldDown, "get" + fieldUp,
128 "getTimeStamped" + fieldUp, "update" + fieldUp);
129 }
130 fieldsDone.add(fieldDown);
131 }
132 }
133
134 }
135 }
136 }
137 }
138
139
140
141
142
143
144
145
146
147
148
149 @SuppressWarnings("checkstyle:parameternumber")
150 private void testGetter(final Class<?> c, final List<String> fieldNames, final List<String> methodNames,
151 final List<Class<?>> methodReturnTypes, final String field, final String getter, final String timeStampedGetter,
152 final String updater)
153 {
154 boolean fieldFound = false;
155 int i = 0;
156 while (!fieldFound && i < fieldNames.size())
157 {
158 fieldFound = fieldNames.get(i).startsWith(field);
159 i++;
160 }
161 if (!fieldFound)
162 {
163
164 Field[] fields = c.getDeclaredFields();
165 i = 0;
166 while (!fieldFound && i < fields.length)
167 {
168 if (AbstractPerceptionCategory.class.isAssignableFrom(fields[i].getType()))
169 {
170
171 Field[] wrappedFields = fields[i].getType().getDeclaredFields();
172 int j = 0;
173 while (!fieldFound && j < wrappedFields.length)
174 {
175 fieldFound = wrappedFields[j].getName().startsWith(field);
176 j++;
177 }
178 }
179 i++;
180 }
181 }
182 if (!fieldFound)
183 {
184
185
186 }
187 if (methodNames.contains(getter))
188 {
189 if (getter.startsWith("is") && !methodReturnTypes.get(methodNames.indexOf(getter)).equals(boolean.class))
190 {
191 fail("Class " + c + "'s method '" + getter + "' does not return a boolean.");
192 }
193 else if (methodReturnTypes.get(methodNames.indexOf(getter)).equals(void.class))
194 {
195 fail("Class " + c + "'s method '" + getter + "' does not return anything.");
196 }
197
198 }
199 else
200 {
201 fail("Class " + c + " does not contain a method '" + getter + "'.");
202 }
203 if (methodNames.contains(timeStampedGetter))
204 {
205 if (!methodReturnTypes.get(methodNames.indexOf(timeStampedGetter)).equals(TimeStampedObject.class))
206 {
207 fail("Class " + c + "'s method '" + timeStampedGetter + "' does not return a TimeStampedObject.");
208 }
209
210
211 }
212 else
213 {
214
215
216
217 }
218 if (!methodNames.contains(updater))
219 {
220
221
222
223 }
224 }
225
226
227
228
229 public static void main(final String[] args)
230 {
231 VerifyPerceptionCategoryMethods t = new VerifyPerceptionCategoryMethods();
232 t.perceptionCategoryTest();
233 }
234
235 }