Simplify exclusion of enum properties in SettingsConsistencyTest

- Allow enum values to be absent if they are deprecated
This commit is contained in:
ljacqu 2017-03-24 23:23:40 +01:00
parent a2d62ea6d9
commit b7901c6b59

View File

@ -5,14 +5,11 @@ import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.EnumProperty; import ch.jalu.configme.properties.EnumProperty;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import fr.xephi.authme.ClassCollector; import fr.xephi.authme.ClassCollector;
import fr.xephi.authme.ReflectionTestUtils; import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever; import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
import fr.xephi.authme.settings.properties.DatabaseSettings;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -42,15 +39,10 @@ public class SettingsConsistencyTest {
private static final int MAX_COMMENT_LENGTH = 90; private static final int MAX_COMMENT_LENGTH = 90;
/** /**
* Exclusions for the enum in comments check. Use {@link Exclude#ALL} * Properties to exclude from the enum check.
* to skip an entire property from being checked.
*/ */
private static final Multimap<Property<?>, Enum<?>> EXCLUDED_ENUMS = private static final Set<Property<?>> EXCLUDED_ENUM_PROPERTIES =
ImmutableSetMultimap.<Property<?>, Enum<?>>builder() ImmutableSet.of(SecuritySettings.PASSWORD_HASH, SecuritySettings.LEGACY_HASHES);
.put(DatabaseSettings.BACKEND, DataSourceType.FILE)
.put(SecuritySettings.PASSWORD_HASH, Exclude.ALL)
.put(SecuritySettings.LEGACY_HASHES, Exclude.ALL)
.build();
private static ConfigurationData configurationData; private static ConfigurationData configurationData;
@ -165,10 +157,10 @@ public class SettingsConsistencyTest {
for (Property<?> property : configurationData.getProperties()) { for (Property<?> property : configurationData.getProperties()) {
// when // when
Class<? extends Enum<?>> enumClass = getEnumClass(property); Class<? extends Enum<?>> enumClass = getEnumClass(property);
if (enumClass != null) { if (enumClass != null && !EXCLUDED_ENUM_PROPERTIES.contains(property)) {
String comments = String.join("\n", configurationData.getCommentsForSection(property.getPath())); String comments = String.join("\n", configurationData.getCommentsForSection(property.getPath()));
Arrays.stream(enumClass.getEnumConstants()) Arrays.stream(enumClass.getEnumConstants())
.filter(e -> !comments.contains(e.name()) && !isExcluded(property, e)) .filter(e -> !comments.contains(e.name()) && !isDeprecated(e))
.findFirst() .findFirst()
.ifPresent(e -> invalidEnumProperties.put(property, e)); .ifPresent(e -> invalidEnumProperties.put(property, e));
} }
@ -199,16 +191,12 @@ public class SettingsConsistencyTest {
return null; return null;
} }
private static boolean isExcluded(Property<?> property, Enum<?> enumValue) { private static boolean isDeprecated(Enum<?> enumValue) {
return EXCLUDED_ENUMS.get(property).contains(Exclude.ALL) try {
|| EXCLUDED_ENUMS.get(property).contains(enumValue); return enumValue.getDeclaringClass().getField(enumValue.name()).isAnnotationPresent(Deprecated.class);
} } catch (NoSuchFieldException e) {
throw new IllegalStateException("Could not fetch field for enum '" + enumValue
/** + "' in " + enumValue.getDeclaringClass());
* Dummy enum to specify in the exclusion that all enum values }
* should be skipped. See its usages.
*/
private enum Exclude {
ALL
} }
} }