Minor - fix some small todo's

This commit is contained in:
ljacqu
2017-04-23 11:02:43 +02:00
parent 2c31ccc020
commit d0dbc14358
10 changed files with 124 additions and 82 deletions
@@ -0,0 +1,84 @@
package fr.xephi.authme;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static org.junit.Assert.fail;
/**
* Matcher which checks with reflection that all fields have the same value.
* This matcher considers all non-static fields until the Object parent.
*/
public final class IsEqualByReflectionMatcher<T> extends TypeSafeMatcher<T> {
private final T expected;
private IsEqualByReflectionMatcher(T expected) {
this.expected = expected;
}
/**
* Creates a matcher that checks if all fields are the same as on the {@code expected} object.
*
* @param expected the object to match
* @param <T> the object's type
* @return the matcher for the expected object
*/
public static <T> Matcher<T> isEqualTo(T expected) {
return new IsEqualByReflectionMatcher<>(expected);
}
@Override
protected boolean matchesSafely(T item) {
return assertAreFieldsEqual(item);
}
@Override
public void describeTo(Description description) {
description.appendText("parameters " + expected);
}
private boolean assertAreFieldsEqual(T item) {
if (expected.getClass() != item.getClass()) {
fail("Classes don't match, got " + expected.getClass().getSimpleName()
+ " and " + item.getClass().getSimpleName());
return false;
}
List<Field> fieldsToCheck = getAllFields(expected);
for (Field field : fieldsToCheck) {
Object lhsValue = ReflectionTestUtils.getFieldValue(field, expected);
Object rhsValue = ReflectionTestUtils.getFieldValue(field, item);
if (!Objects.equals(lhsValue, rhsValue)) {
fail("Field '" + field.getName() + "' does not have same value: '"
+ lhsValue + "' vs. '" + rhsValue + "'");
return false;
}
}
return true;
}
private static List<Field> getAllFields(Object object) {
List<Field> fields = new ArrayList<>();
Class<?> currentClass = object.getClass();
while (currentClass != null) {
for (Field f : currentClass.getDeclaredFields()) {
if (!Modifier.isStatic(f.getModifiers())) {
fields.add(f);
}
}
if (currentClass == Object.class) {
break;
}
currentClass = currentClass.getSuperclass();
}
return fields;
}
}
@@ -1,6 +1,5 @@
package fr.xephi.authme.command.executable.register;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.mail.EmailService;
import fr.xephi.authme.message.MessageKey;
@@ -10,7 +9,6 @@ import fr.xephi.authme.process.register.RegistrationType;
import fr.xephi.authme.process.register.executors.EmailRegisterParams;
import fr.xephi.authme.process.register.executors.PasswordRegisterParams;
import fr.xephi.authme.process.register.executors.RegistrationMethod;
import fr.xephi.authme.process.register.executors.RegistrationParameters;
import fr.xephi.authme.process.register.executors.TwoFactorRegisterParams;
import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.service.CommonService;
@@ -20,9 +18,6 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -31,16 +26,11 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import static fr.xephi.authme.IsEqualByReflectionMatcher.isEqualTo;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
@@ -306,56 +296,4 @@ public class RegisterCommandTest {
verify(management).performRegister(eq(RegistrationMethod.PASSWORD_REGISTRATION),
argThat(isEqualTo(PasswordRegisterParams.of(player, "myPass", null))));
}
// TODO ljacqu 20170317: Document and extract as util
private static <P extends RegistrationParameters> Matcher<P> isEqualTo(P expected) {
return new TypeSafeMatcher<P>() {
@Override
protected boolean matchesSafely(RegistrationParameters item) {
assertAreParamsEqual(expected, item);
return true;
}
@Override
public void describeTo(Description description) {
description.appendText("parameters " + expected);
}
};
}
private static void assertAreParamsEqual(RegistrationParameters lhs, RegistrationParameters rhs) {
if (lhs.getClass() != rhs.getClass()) {
fail("Params classes don't match, got " + lhs.getClass().getSimpleName()
+ " and " + rhs.getClass().getSimpleName());
}
List<Field> fieldsToCheck = getFields(lhs);
for (Field field : fieldsToCheck) {
Object lhsValue = ReflectionTestUtils.getFieldValue(field, lhs);
Object rhsValue = ReflectionTestUtils.getFieldValue(field, rhs);
if (!Objects.equals(lhsValue, rhsValue)) {
fail("Field '" + field.getName() + "' does not have same value: '"
+ lhsValue + "' vs. '" + rhsValue + "'");
}
}
}
private static List<Field> getFields(RegistrationParameters params) {
List<Field> fields = new ArrayList<>();
Class<?> currentClass = params.getClass();
while (currentClass != null) {
for (Field f : currentClass.getDeclaredFields()) {
if (!Modifier.isStatic(f.getModifiers())) {
fields.add(f);
}
}
if (currentClass == RegistrationParameters.class) {
break;
}
currentClass = currentClass.getSuperclass();
}
return fields;
}
}
@@ -11,7 +11,6 @@ import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.ProtectionSettings;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -82,7 +81,6 @@ public class AntiBotServiceTest {
}
@Test
@Ignore // TODO ljacqu 20161030: Fix test
public void shouldActivateAntibot() {
// given - listening antibot
runSyncDelayedTaskWithDelay(bukkitService);