-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathClassesConsistencyTest.java
More file actions
173 lines (154 loc) · 6.76 KB
/
ClassesConsistencyTest.java
File metadata and controls
173 lines (154 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package fr.xephi.authme;
import ch.jalu.configme.properties.Property;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.data.captcha.CaptchaCodeStorage;
import fr.xephi.authme.datasource.AbstractSqlDataSource;
import fr.xephi.authme.datasource.Columns;
import fr.xephi.authme.datasource.columnshandler.DataSourceColumn;
import fr.xephi.authme.datasource.columnshandler.PlayerAuthColumn;
import fr.xephi.authme.datasource.mysqlextensions.MySqlExtension;
import fr.xephi.authme.initialization.HasCleanup;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.security.crypts.Whirlpool;
import fr.xephi.authme.util.expiring.ExpiringMap;
import fr.xephi.authme.util.expiring.ExpiringSet;
import fr.xephi.authme.util.expiring.TimedCounter;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Contains consistency tests across all AuthMe classes.
*/
class ClassesConsistencyTest {
/** Contains all production classes. */
private static final List<Class<?>> ALL_CLASSES =
new ClassCollector(TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT).collectClasses();
/** Expiring structure types. */
private static final Set<Class<?>> EXPIRING_STRUCTURES = ImmutableSet.of(
ExpiringSet.class, ExpiringMap.class, TimedCounter.class, CaptchaCodeStorage.class);
/** Immutable types, which are allowed to be used in non-private constants. */
private static final Set<Class<?>> IMMUTABLE_TYPES = ImmutableSet.of(
/* JDK */
int.class, long.class, float.class, String.class, File.class, Enum.class, collectionsUnmodifiableList(),
Charset.class,
/* AuthMe */
Property.class, RegistrationMethod.class, DataSourceColumn.class, PlayerAuthColumn.class,
/* Guava */
ImmutableMap.class, ImmutableList.class);
/** Classes excluded from the field visibility test. */
private static final Set<Class<?>> CLASSES_EXCLUDED_FROM_VISIBILITY_TEST = ImmutableSet.of(
Whirlpool.class, // not our implementation, so we don't touch it
MySqlExtension.class, // has immutable protected fields used by all children
AbstractSqlDataSource.class, // protected members for inheritance
Columns.class // uses non-static String constants, which is safe
);
/**
* Checks that there aren't two classes with the same name; this is confusing and should be avoided.
*/
@Test
void shouldNotHaveSameName() {
// given
Set<String> names = new HashSet<>();
// when / then
for (Class<?> clazz : ALL_CLASSES) {
if (!names.add(clazz.getSimpleName())) {
fail("Class with name '" + clazz.getSimpleName() + "' already encountered!");
}
}
}
/**
* Checks that fields of classes are either private or static final fields of an immutable type.
*/
@Test
void shouldHaveNonPrivateConstantsOnly() {
// given / when
Set<String> invalidFields = ALL_CLASSES.stream()
.filter(clz -> !CLASSES_EXCLUDED_FROM_VISIBILITY_TEST.contains(clz))
.map(Class::getDeclaredFields)
.flatMap(Arrays::stream)
.filter(f -> !f.getName().contains("$"))
.filter(f -> hasIllegalFieldVisibility(f))
.map(f -> formatField(f))
.collect(Collectors.toSet());
// then
if (!invalidFields.isEmpty()) {
fail("Found " + invalidFields.size() + " fields with non-private, mutable fields:\n- "
+ String.join("\n- ", invalidFields));
}
}
private static boolean hasIllegalFieldVisibility(Field field) {
final int modifiers = field.getModifiers();
if (Modifier.isPrivate(modifiers)) {
return false;
} else if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
return true;
}
// Field is non-private, static and final
Class<?> valueType;
if (Collection.class.isAssignableFrom(field.getType()) || Map.class.isAssignableFrom(field.getType())) {
// For collections/maps, need to check the actual type to ensure it's an unmodifiable implementation
Object value = ReflectionTestUtils.getFieldValue(field, null);
valueType = value.getClass();
} else {
valueType = field.getType();
}
// Field is static, final, and not private -> check that it is immutable type
return IMMUTABLE_TYPES.stream()
.noneMatch(immutableType -> immutableType.isAssignableFrom(valueType));
}
/**
* Prints out the field with its modifiers.
*
* @param field the field to format
* @return description of the field
*/
private static String formatField(Field field) {
String modifiersText = Modifier.toString(field.getModifiers());
return String.format("[%s] %s %s %s", field.getDeclaringClass().getSimpleName(), modifiersText.trim(),
field.getType().getSimpleName(), field.getName());
}
/**
* Checks that classes with expiring collections (such as {@link ExpiringMap}) implement the {@link HasCleanup}
* interface to regularly clean up expired data.
*/
@Test
void shouldImplementHasCleanup() {
// given / when / then
for (Class<?> clazz : ALL_CLASSES) {
if (hasExpiringCollectionAsField(clazz) && !EXPIRING_STRUCTURES.contains(clazz)) {
assertThat("Class '" + clazz.getSimpleName() + "' has expiring collections, should implement HasCleanup",
HasCleanup.class.isAssignableFrom(clazz), equalTo(true));
}
}
}
private static boolean hasExpiringCollectionAsField(Class<?> clazz) {
for (Field field : clazz.getDeclaredFields()) {
if (EXPIRING_STRUCTURES.stream().anyMatch(t -> t.isAssignableFrom(field.getType()))) {
return true;
}
}
return false;
}
/**
* @return the concrete class of the unmodifiable list as returned by {@link Collections#unmodifiableList(List)}.
*/
private static Class<?> collectionsUnmodifiableList() {
return Collections.unmodifiableList(new ArrayList<>()).getClass();
}
}