Verified Commit 9d78a8c1 authored by Daniel Mangold's avatar Daniel Mangold
Browse files

Moved assertions to own class

parent 0c423af4
package h05;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Map;
import static java.lang.reflect.Modifier.*;
import static java.lang.reflect.Modifier.ABSTRACT;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class Assertions {
/**
* Assert that the given member has (map value = {@code true}) or does not have
* (map value = {@code false}) the modifiers in {@code modifiers}. <br>
* Throws a {@link AssertionError} if the assertion fails
* <br><br>
* Calls {@link Assertions#assertHasModifiers(Member, Map, String)} with {@code null} as last
* parameter (default error message)
* @param member the member to check the modifiers of
* @param modifiers a map of modifiers (constants in {@link Modifier}) with a
* corresponding boolean value
*/
public static void assertHasModifiers(Member member, Map<Integer, Boolean> modifiers) {
assertHasModifiers(member, modifiers, null);
}
/**
* Assert that the given member has (map value = {@code true}) or does not have
* (map value = {@code false}) the modifiers in {@code modifiers}. <br>
* Throws a {@link AssertionError} if the assertion fails
* @param member the member to check the modifiers of
* @param modifiers a map of modifiers (constants in {@link Modifier}) with a
* corresponding boolean value
* @param msg the error message to pass to {@link AssertionError}
*/
public static void assertHasModifiers(Member member, Map<Integer, Boolean> modifiers, String msg) {
Class<Modifier> modifierClass = Modifier.class;
try {
for (Integer modifier : modifiers.keySet()) {
Method method;
String msgSuffix;
switch (modifier) {
case PUBLIC:
method = modifierClass.getDeclaredMethod("isPublic", int.class);
msgSuffix = "public";
break;
case PROTECTED:
method = modifierClass.getDeclaredMethod("isProtected", int.class);
msgSuffix = "protected";
break;
case PRIVATE:
method = modifierClass.getDeclaredMethod("isPrivate", int.class);
msgSuffix = "private";
break;
case STATIC:
method = modifierClass.getDeclaredMethod("isStatic", int.class);
msgSuffix = "static";
break;
case FINAL:
method = modifierClass.getDeclaredMethod("isFinal", int.class);
msgSuffix = "final";
break;
case ABSTRACT:
method = modifierClass.getDeclaredMethod("isAbstract", int.class);
msgSuffix = "abstract";
break;
default:
throw new IllegalArgumentException(String.valueOf(modifier));
}
if (modifiers.get(modifier))
assertTrue((Boolean) method.invoke(null, member.getModifiers()),
msg != null ? msg : member.getName() + " must be " + msgSuffix);
else
assertFalse((Boolean) method.invoke(null, member.getModifiers()),
msg != null ? msg : member.getName() + " must not be " + msgSuffix);
}
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
}
......@@ -7,6 +7,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import static h05.Assertions.*;
import static h05.Utils.*;
import static h05.Utils.TestType.Type.CLASS;
import static java.lang.reflect.Modifier.*;
......
......@@ -8,6 +8,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import static h05.Assertions.*;
import static h05.Utils.*;
import static h05.Utils.TestType.Type.CLASS;
import static java.lang.reflect.Modifier.*;
......
......@@ -19,6 +19,7 @@ import java.util.Iterator;
import java.util.Map;
import java.util.Stack;
import static h05.Assertions.*;
import static h05.Utils.*;
import static h05.Utils.TestType.Type.CLASS;
import static java.lang.reflect.Modifier.*;
......
......@@ -13,9 +13,6 @@ import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.*;
import static java.lang.reflect.Modifier.*;
import static org.junit.jupiter.api.Assertions.*;
public class Utils {
public static final long SEED = new Random().nextLong();
......@@ -169,85 +166,6 @@ public class Utils {
}
}
/**
* Assert that the given member has (map value = {@code true}) or does not have
* (map value = {@code false}) the modifiers in {@code modifiers}. <br>
* Throws a {@link AssertionError} if the assertion fails
* <br><br>
* Calls {@link Utils#assertHasModifiers(Member, Map, String)} with {@code null} as last
* parameter (default error message)
* @param member the member to check the modifiers of
* @param modifiers a map of modifiers (constants in {@link Modifier}) with a
* corresponding boolean value
*/
public static void assertHasModifiers(Member member, Map<Integer, Boolean> modifiers) {
assertHasModifiers(member, modifiers, null);
}
/**
* Assert that the given member has (map value = {@code true}) or does not have
* (map value = {@code false}) the modifiers in {@code modifiers}. <br>
* Throws a {@link AssertionError} if the assertion fails
* @param member the member to check the modifiers of
* @param modifiers a map of modifiers (constants in {@link Modifier}) with a
* corresponding boolean value
* @param msg the error message to pass to {@link AssertionError}
*/
public static void assertHasModifiers(Member member, Map<Integer, Boolean> modifiers, String msg) {
Class<Modifier> modifierClass = Modifier.class;
try {
for (Integer modifier : modifiers.keySet()) {
Method method;
String msgSuffix;
switch (modifier) {
case PUBLIC:
method = modifierClass.getDeclaredMethod("isPublic", int.class);
msgSuffix = "public";
break;
case PROTECTED:
method = modifierClass.getDeclaredMethod("isProtected", int.class);
msgSuffix = "protected";
break;
case PRIVATE:
method = modifierClass.getDeclaredMethod("isPrivate", int.class);
msgSuffix = "private";
break;
case STATIC:
method = modifierClass.getDeclaredMethod("isStatic", int.class);
msgSuffix = "static";
break;
case FINAL:
method = modifierClass.getDeclaredMethod("isFinal", int.class);
msgSuffix = "final";
break;
case ABSTRACT:
method = modifierClass.getDeclaredMethod("isAbstract", int.class);
msgSuffix = "abstract";
break;
default:
throw new IllegalArgumentException(String.valueOf(modifier));
}
if (modifiers.get(modifier))
assertTrue((Boolean) method.invoke(null, member.getModifiers()),
msg != null ? msg : member.getName() + " must be " + msgSuffix);
else
assertFalse((Boolean) method.invoke(null, member.getModifiers()),
msg != null ? msg : member.getName() + " must not be " + msgSuffix);
}
} catch (ReflectiveOperationException e) {
e.printStackTrace();
}
}
private static class Updater {
private static final String REPOSITORY_URL = "https://git.rwth-aachen.de/aud-tests/AuD-2021-H05-Student/-/raw/development/";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment