632 lines
24 KiB
Java
632 lines
24 KiB
Java
import java.lang.reflect.Constructor;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Modifier;
|
|
|
|
import java.util.Objects;
|
|
import java.util.function.Function;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework;
|
|
import de.uni_due.s3.jack2.backend.checkers.tracingchecker.framework.TracingFramework.Test;
|
|
|
|
public class Tests {
|
|
|
|
private static final String TESTAT_NAME = "Testat 1A";
|
|
|
|
// Aufgabe 1 - 25 Punkte
|
|
|
|
@Test(name = "Aufgabe 1 - empty array - 5 points")
|
|
public void testA1EmptyArray() {
|
|
int[] arr = new int[0];
|
|
testCalculateMeanAbove(arr, 5, 0.0, 5);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - null array - 5 points")
|
|
public void testA1NullArray() {
|
|
int[] arr = null;
|
|
testCalculateMeanAbove(arr, 42, 0.0, 5);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - no elements above threshold - 5 points")
|
|
public void testA1NoElementsAboveThreshold() {
|
|
int[] arr = { -1000000000, 0, 1000, 1000000, 1000000000 };
|
|
testCalculateMeanAbove(arr, Integer.MAX_VALUE, 0.0, 5);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - all elements above threshold - 5 points")
|
|
public void testA1AllElementsAboveThreshold() {
|
|
int[] arr = { 10, 20, 30 };
|
|
testCalculateMeanAbove(arr, Integer.MIN_VALUE, 20.0, 5);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 1 - some elements above threshold - 5 points")
|
|
public void testA1SomeElementsAboveThreshold() {
|
|
int[] arr = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
|
|
testCalculateMeanAbove(arr, -2, 2.0, 5);
|
|
}
|
|
|
|
public void testCalculateMeanAbove(int[] arr, int threshold, double expected, int points) {
|
|
double result = Testat1A.calculateMeanAbove(arr, threshold);
|
|
if (result != expected) {
|
|
TracingFramework.printError(String.format("Expected %f for %s with threshold %d, but got %f",
|
|
expected, Arrays.toString(arr), threshold, result));
|
|
addPoints(0);
|
|
} else {
|
|
addPoints(points);
|
|
}
|
|
|
|
}
|
|
|
|
// Aufgabe 2 - 25 Punkte
|
|
|
|
@Test(name = "Aufgabe 2 - empty array - 2 points")
|
|
public void testA2EmptyArray() {
|
|
int[][] arr = new int[0][];
|
|
testCalculateRowMean(arr, new double[0], 2);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - null array - 2 points")
|
|
public void testA2NullArray() {
|
|
int[][] arr = null;
|
|
testCalculateRowMean(arr, new double[0], 2);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - some rows empty - 2 points")
|
|
public void testA2SomeRowsEmpty() {
|
|
int[][] arr = { {}, {}, { 1, 2, 3 }, {}, {}, { 4, 5, 6 } };
|
|
testCalculateRowMean(arr, new double[] { 0.0, 0.0, 2.0, 0.0, 0.0, 5.0 }, 2);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - some rows null - 2 points")
|
|
public void testA2SomeRowsNull() {
|
|
int[][] arr = { null, { 1, 2, 3 }, null, { 4, 5, 6 }, null, null };
|
|
testCalculateRowMean(arr, new double[] { 0.0, 2.0, 0.0, 5.0, 0.0, 0.0 }, 2);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - unequal row lengths - 3 points")
|
|
public void testA2UnequalRowLengths() {
|
|
int[][] arr = { { 1, 2, 3 }, { 4, 5 }, { 6 }, { -4, -3, -2, -1 }, { 7, 8, 9, 10 }, { -5, 0, 5 } };
|
|
testCalculateRowMean(arr, new double[] { 2.0, 4.5, 6.0, -2.5, 8.5, 0.0 }, 3);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - rectangular array - 7 points")
|
|
public void testA2RectangularArray() {
|
|
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { -30, -20, -10 } };
|
|
testCalculateRowMean(arr, new double[] { 2.0, 5.0, 8.0, -20.0 }, 7);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 2 - quadratic array - 7 points")
|
|
public void testA2QuadraticArray() {
|
|
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
|
|
testCalculateRowMean(arr, new double[] { 2.0, 5.0, 8.0 }, 7);
|
|
}
|
|
|
|
private void test2DArrayOperation(int[][] arr, double[] expected, int points, Function<int[][], double[]> func) {
|
|
double[] result = func.apply(arr);
|
|
if (Arrays.equals(result, expected)) {
|
|
addPoints(points);
|
|
} else {
|
|
TracingFramework.printError(String.format("Expected %s for %s, but got %s",
|
|
Arrays.toString(expected), Arrays.deepToString(arr), Arrays.toString(result)));
|
|
addPoints(0);
|
|
}
|
|
}
|
|
|
|
public void testCalculateRowMean(int[][] arr, double[] expected, int points) {
|
|
test2DArrayOperation(arr, expected, points, Testat1A::calculateRowMean);
|
|
}
|
|
|
|
// Aufgabe 3 - 50 Punkte
|
|
|
|
@Test(name = "Aufgabe 3a - private fields - 6 points")
|
|
public void testA3PrivateFields() {
|
|
int points = 6;
|
|
|
|
points -= testField(FridgeItem.class, "weight", double.class, 2);
|
|
points -= testField(FridgeItem.class, "name", String.class, 2);
|
|
points -= testField(FridgeItem.class, "color", String.class, 2);
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3b - FridgeItem constructor - 6 points")
|
|
public void testA3FridgeItemConstructor() {
|
|
int points = 6;
|
|
|
|
double weight = 42.314159;
|
|
String name = "Foo";
|
|
String color = "Red";
|
|
FridgeItem item = new FridgeItem(weight, name, color);
|
|
|
|
points -= checkConstructorField(FridgeItem.class, "weight", double.class, item, weight, 2);
|
|
points -= checkConstructorField(FridgeItem.class, "name", String.class, item, name, 2);
|
|
points -= checkConstructorField(FridgeItem.class, "color", String.class, item, color, 2);
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3c - weightful interface - 8 points")
|
|
public void testA3WeightfulInterface() {
|
|
int points = 8;
|
|
|
|
Class<?> clazz = FridgeItem.class;
|
|
if (!implementsInterface(clazz, Weightful.class)) {
|
|
points -= 4;
|
|
TracingFramework
|
|
.printError(String.format("Class '%s' does not implement interface '%s'. Deducting 4 points.",
|
|
clazz.getName(), Weightful.class.getName()));
|
|
}
|
|
Method method = findMethod(FridgeItem.class, "getWeight", double.class);
|
|
if (method == null) {
|
|
points -= 4;
|
|
TracingFramework
|
|
.printError(String.format("Method 'getWeight' not found in class '%s'. Deducting 4 points.",
|
|
clazz.getName()));
|
|
} else {
|
|
double weight = 42.314159;
|
|
FridgeItem item = new FridgeItem(weight, "Foo", "Red");
|
|
try {
|
|
double result = (double) method.invoke(item);
|
|
if (result != weight) {
|
|
TracingFramework.printError(String.format("Expected getWeight() to return %f, but got %f",
|
|
weight, result));
|
|
points = 0;
|
|
}
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
|
TracingFramework
|
|
.printError(String.format("Could not invoke getWeight() on item of class '%s'",
|
|
item.getClass().getName()));
|
|
points = 0;
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3d - FridgeItem toString - 10 points")
|
|
public void testA3FridgeItemToString() {
|
|
int points = 10;
|
|
|
|
if (!overridesMethod(FridgeItem.class, "toString")) {
|
|
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
|
|
FridgeItem.class.getName()));
|
|
points = 0;
|
|
} else {
|
|
FridgeItem item = new FridgeItem(42.314159, "Foo", "Red");
|
|
String expected = "Foo (Red 42.314159 g)";
|
|
points -= testToString(item, expected, 5);
|
|
|
|
FridgeItem item2 = new FridgeItem(100.0, "Milk", "White");
|
|
String expected2 = "Milk (White 100.0 g)";
|
|
points -= testToString(item2, expected2, 5);
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3e - Cheese extends FridgeItem - 5 points")
|
|
public void testA3CheeseExtendsFridgeItem() {
|
|
int points = 5;
|
|
|
|
if (!extendsClass(Cheese.class, FridgeItem.class)) {
|
|
TracingFramework.printError(String.format("Class '%s' does not extend class '%s'",
|
|
Cheese.class.getName(), FridgeItem.class.getName()));
|
|
points -= 5;
|
|
} else {
|
|
Cheese cheese = createInstance(Cheese.class, 42.0, "Test Cheese", "Yellow");
|
|
if (cheese == null) {
|
|
TracingFramework.printError(String.format("Could not create instance of class '%s'",
|
|
Cheese.class.getName()));
|
|
points = 0;
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3e - Yoghurt extends FridgeItem - 5 points")
|
|
public void testA3YoghurtExtendsFridgeItem() {
|
|
int points = 5;
|
|
|
|
if (!extendsClass(Yoghurt.class, FridgeItem.class)) {
|
|
TracingFramework.printError(String.format("Class '%s' does not extend class '%s'",
|
|
Yoghurt.class.getName(), FridgeItem.class.getName()));
|
|
points -= 5;
|
|
} else {
|
|
Yoghurt yoghurt = createInstance(Yoghurt.class, 42.0, "Test Yoghurt", "White");
|
|
if (yoghurt == null) {
|
|
TracingFramework.printError(String.format("could not create instance of class '%s'",
|
|
Yoghurt.class.getName()));
|
|
points = 0;
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3g - Cheese overrides toString - 5 points")
|
|
public void testA3CheeseOverridesToString() {
|
|
int points = 5;
|
|
|
|
if (!overridesMethod(Cheese.class, "toString")) {
|
|
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
|
|
Cheese.class.getName()));
|
|
points = 0;
|
|
} else {
|
|
Cheese cheese = createInstance(Cheese.class, 100.0, "Cheddar", "Yellow");
|
|
if (cheese == null) {
|
|
TracingFramework.printError(String.format("Could not create instance of class '%s'",
|
|
Cheese.class.getName()));
|
|
points = 0;
|
|
} else {
|
|
String expected = "Cheese: Cheddar (Yellow 100.0 g)";
|
|
points -= testToString(cheese, expected, 5);
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
@Test(name = "Aufgabe 3g - Yoghurt overrides toString - 5 points")
|
|
public void testA3YoghurtOverridesToString() {
|
|
int points = 5;
|
|
|
|
if (!overridesMethod(Yoghurt.class, "toString")) {
|
|
TracingFramework.printError(String.format("Class '%s' does not override method 'toString'",
|
|
Yoghurt.class.getName()));
|
|
points = 0;
|
|
} else {
|
|
Yoghurt yoghurt = createInstance(Yoghurt.class, 200.0, "Greek Yoghurt", "White");
|
|
if (yoghurt == null) {
|
|
TracingFramework.printError(String.format("Could not create instance of class '%s'",
|
|
Yoghurt.class.getName()));
|
|
points = 0;
|
|
} else {
|
|
String expected = "Yoghurt: Greek Yoghurt (White 200.0 g)";
|
|
points -= testToString(yoghurt, expected, 5);
|
|
}
|
|
}
|
|
|
|
addPoints(points);
|
|
}
|
|
|
|
/**
|
|
* Creates a new instance of the specified class with the given constructor
|
|
* arguments.
|
|
*
|
|
* @param <T> The type of the class to instantiate.
|
|
* @param clazz The class to instantiate.
|
|
* @param args The constructor arguments.
|
|
* @return A new instance of the specified class, or null if instantiation
|
|
* failed.
|
|
*/
|
|
public static <T> T createInstance(Class<T> clazz, Object... args) {
|
|
try {
|
|
Class<?>[] argTypes = Arrays.stream(args).map(Object::getClass).map(c -> {
|
|
if (c.getName().equals("java.lang.Integer")) {
|
|
return int.class;
|
|
} else if (c.getName().equals("java.lang.Double")) {
|
|
return double.class;
|
|
}
|
|
return c;
|
|
}).toArray(Class<?>[]::new);
|
|
Constructor<T> constructor = clazz.getDeclaredConstructor(argTypes);
|
|
constructor.setAccessible(true);
|
|
Object[] convertedArgs = Arrays.stream(args).map(arg -> {
|
|
if (arg instanceof Integer) {
|
|
return (int) arg;
|
|
} else if (arg instanceof Double) {
|
|
return (double) arg;
|
|
}
|
|
return arg;
|
|
}).toArray();
|
|
return constructor.newInstance(convertedArgs);
|
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
|
|
| InvocationTargetException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Finds a method with the given name, return type, and parameter types in the
|
|
* specified class or its superclasses.
|
|
*
|
|
* @param clazz The class to search in.
|
|
* @param methodName The name of the method to find.
|
|
* @param returnType The return type of the method.
|
|
* @param parameterTypes The parameter types of the method.
|
|
* @return The method if found, null otherwise.
|
|
*/
|
|
public static Method findMethod(Class<?> clazz, String methodName, Class<?> returnType,
|
|
Class<?>... parameterTypes) {
|
|
try {
|
|
Method method = clazz.getDeclaredMethod(methodName, parameterTypes);
|
|
if (method.getReturnType().equals(returnType)) {
|
|
return method;
|
|
}
|
|
} catch (NoSuchMethodException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Returns true if the class clazz extends the superclass superclass.
|
|
*
|
|
* @param clazz The class to check.
|
|
* @param superclass The superclass to check against.
|
|
* @return True if clazz extends superclass, false otherwise.
|
|
*/
|
|
public boolean extendsClass(Class<?> clazz, Class<?> superclass) {
|
|
return superclass.isAssignableFrom(clazz);
|
|
}
|
|
|
|
/**
|
|
* Returns the points to deduct for a test case that checks the
|
|
* toString() method.
|
|
*
|
|
* @param obj The object to test.
|
|
* @param expected The expected string representation.
|
|
* @param maxPoints The maximum points to deduct.
|
|
* @return The points to deduct. maxPoints if the toString() method returns
|
|
* completely different output,
|
|
* maxPoints / 2 if the output is similar but has different whitespace,
|
|
* otherwise 0.
|
|
*/
|
|
private static int testToString(Object obj, String expected, int maxPoints) {
|
|
String result = obj.toString();
|
|
if (result == null) {
|
|
TracingFramework.printError(String.format("toString() returned null for object of class '%s'",
|
|
obj.getClass().getName()));
|
|
return maxPoints;
|
|
} else if (!result.equals(expected)) {
|
|
if (result.replaceAll("\\s+", " ").trim().equals(expected.replaceAll("\\s+", " ").trim())) {
|
|
TracingFramework.printError(String.format(
|
|
"toString() returned '%s' for object of class '%s', but expected '%s' (ignoring whitespace)",
|
|
result, obj.getClass().getName(), expected));
|
|
return maxPoints / 2;
|
|
} else {
|
|
TracingFramework.printError(String.format("Expected toString() to return '%s', but got '%s'",
|
|
expected, result));
|
|
return maxPoints;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Returns whether the given class overrides the specified method.
|
|
*
|
|
* @param subclass The class to check.
|
|
* @param methodName The name of the method to find.
|
|
* @param parameterTypes The parameter types of the method to find.
|
|
* @return True if the class overrides the method, false otherwise.
|
|
*/
|
|
public static boolean overridesMethod(Class<?> subclass, String methodName, Class<?>... parameterTypes) {
|
|
try {
|
|
Method method = subclass.getDeclaredMethod(methodName, parameterTypes);
|
|
return method.getDeclaringClass().equals(subclass);
|
|
} catch (NoSuchMethodException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns the points to deduct for not setting the field in the constructor.
|
|
*
|
|
* @param <T> The type of the field.
|
|
* @param clazz The class that should contain the field.
|
|
* @param fieldName The name of the field.
|
|
* @param type The type of the field.
|
|
* @param instance The instance to check.
|
|
* @param expected The expected value.
|
|
* @param maxPoints The maximum points to deduct.
|
|
* @return The points to deduct.
|
|
*/
|
|
private <T> int checkConstructorField(Class<?> clazz, String fieldName, Class<T> type, Object instance, T expected,
|
|
int maxPoints) {
|
|
Field field = getField(clazz, fieldName, type);
|
|
if (field == null) {
|
|
TracingFramework.printError(String.format("Field '%s' of type %s not found in class '%s'", fieldName,
|
|
type.getName(), clazz.getName()));
|
|
return maxPoints;
|
|
}
|
|
|
|
T value = readField(field, type, instance);
|
|
if (!Objects.equals(value, expected)) {
|
|
TracingFramework.printError(String.format("Expected field '%s' to be %s, but got %s", fieldName,
|
|
expected, value));
|
|
return maxPoints;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Returns the points to deduct for a missing or incorrect field. Based on the
|
|
* type and the privacy of the field.
|
|
*
|
|
* @param clazz The class that should contain the field.
|
|
* @param fieldName The name of the field.
|
|
* @param type The type of the field.
|
|
* @param maxPoints The maximum points to deduct.
|
|
* @return If the field is missing maxPoints, if the field is present but not
|
|
* private maxPoints / 2, otherwise 0.
|
|
*/
|
|
private int testField(Class<?> clazz, String fieldName, Class<?> type, int maxPoints) {
|
|
Field field = getField(clazz, fieldName, type);
|
|
if (field == null) {
|
|
TracingFramework
|
|
.printError(String.format("Field '%s' of type %s not found in class '%s'", fieldName,
|
|
type.getName(), clazz.getName()));
|
|
return maxPoints;
|
|
} else if (!isPrivateField(field)) {
|
|
TracingFramework.printError(
|
|
String.format("Field '%s' in class '%s' is not private", fieldName, clazz.getName()));
|
|
return maxPoints / 2;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Returns the field with the given name and type from the specified class.
|
|
*
|
|
* @param clazz The class to check.
|
|
* @param fieldName The name of the field to find.
|
|
* @param type The type of the field to find.
|
|
* @return The field if found, null otherwise.
|
|
*/
|
|
public Field getField(Class<?> clazz, String fieldName, Class<?> type) {
|
|
try {
|
|
Field field = clazz.getDeclaredField(fieldName);
|
|
if (!field.getType().equals(type)) {
|
|
return null;
|
|
}
|
|
return field;
|
|
} catch (NoSuchFieldException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if the given field is private.
|
|
*
|
|
* @param field The field to check.
|
|
* @return True if the field is private, false otherwise.
|
|
*/
|
|
public boolean isPrivateField(Field field) {
|
|
return Modifier.isPrivate(field.getModifiers());
|
|
}
|
|
|
|
/**
|
|
* Reads the value of a field from an instance.
|
|
*
|
|
* @param <T> The type of the field.
|
|
* @param field The field to read.
|
|
* @param type The type of the field.
|
|
* @param instance The instance from which to read the field.
|
|
* @return The value of the field, or null if an error occurs.
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
public <T> T readField(Field field, Class<T> type, Object instance) {
|
|
try {
|
|
field.setAccessible(true);
|
|
Object obj = field.get(instance);
|
|
if (obj == null) {
|
|
return null;
|
|
|
|
}
|
|
|
|
if (type.isPrimitive()) {
|
|
if ((type == double.class && obj instanceof Double) ||
|
|
(type == int.class && obj instanceof Integer) ||
|
|
(type == boolean.class && obj instanceof Boolean) ||
|
|
(type == long.class && obj instanceof Long) ||
|
|
(type == float.class && obj instanceof Float) ||
|
|
(type == short.class && obj instanceof Short) ||
|
|
(type == byte.class && obj instanceof Byte) ||
|
|
(type == char.class && obj instanceof Character)) {
|
|
return (T) obj;
|
|
} else {
|
|
throw new ClassCastException("Cannot cast " + obj.getClass().getName() + " to " + type.getName());
|
|
}
|
|
}
|
|
|
|
return type.cast(obj);
|
|
} catch (IllegalAccessException | ClassCastException e) {
|
|
e.printStackTrace();
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns true if the given class implements the specified interface.
|
|
*
|
|
* @param clazz The class to check.
|
|
* @param interfaceClass The interface to check for.
|
|
* @return True if the class implements the interface, false otherwise.
|
|
*/
|
|
public static boolean implementsInterface(Class<?> clazz, Class<?> interfaceClass) {
|
|
return interfaceClass.isAssignableFrom(clazz);
|
|
}
|
|
|
|
/**
|
|
* The points of each test case.
|
|
*/
|
|
private List<Integer> testPoints = new ArrayList<>();
|
|
|
|
/**
|
|
* This method will be called by jack after all test have been executed to get
|
|
* the resulting points.
|
|
*
|
|
* @return The points resulting from the test.
|
|
*/
|
|
public int getResult() {
|
|
int sum = 0;
|
|
for (Integer p : testPoints) {
|
|
sum += p;
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
/**
|
|
* Adds points for a test case. This class assumes that the method is called
|
|
* <b>exactly</b> once per test case.
|
|
*
|
|
* @param value The points to add.
|
|
*/
|
|
protected void addPoints(int value) {
|
|
testPoints.add(value);
|
|
}
|
|
|
|
/**
|
|
* A utility method to execute any test driver class extending abstract test
|
|
* locally without going through Jack.
|
|
*
|
|
* @param test The test driver to execute.
|
|
* @throws Exception Thrown if something fails.
|
|
*/
|
|
public static void runAll() throws Exception {
|
|
Class<Tests> clazz = Tests.class;
|
|
Tests test = new Tests();
|
|
System.out.println(String.format("Running tests for: %s", TESTAT_NAME));
|
|
System.out.println();
|
|
for (Method method : clazz.getMethods()) {
|
|
if (method.isAnnotationPresent(Test.class)) {
|
|
System.out.println(String.format("Currently testing: %s", method.getName()));
|
|
int size = test.testPoints.size();
|
|
try {
|
|
method.invoke(test);
|
|
if (test.testPoints.size() != size + 1) {
|
|
System.err.println("ERROR: Test yields no or multiple results. Erroneous test.");
|
|
} else {
|
|
System.out.println(
|
|
String.format("PASS: Test passed with %d points!",
|
|
test.testPoints.get(test.testPoints.size() - 1)));
|
|
}
|
|
} catch (Exception e) {
|
|
System.err.println(String.format("ERROR: Test failed with exception: %s", e.getMessage()));
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println();
|
|
}
|
|
|
|
}
|
|
System.out.println();
|
|
System.out.println(String.format("Total points: %d", test.getResult()));
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
try {
|
|
runAll();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|