Verified Commit aa3b0d8b authored by Daniel Mangold's avatar Daniel Mangold
Browse files

Added tests for ListItem and MyTreeNode

parent 073519c3
package h05;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import static h05.Utils.*;
import static h05.Utils.TestType.Type.CLASS;
import static java.lang.reflect.Modifier.*;
import static org.junit.jupiter.api.Assertions.*;
@TestType(CLASS)
public class ListItemTest {
public static Class<?> listItemClass;
public static Constructor<?> constructor;
public static Field key, next;
@BeforeAll
public static void checkClass() {
if (CLASS_CORRECT.containsKey(ListItemTest.class))
return;
listItemClass = getClassForName("h05.ListItem");
// is generic
assertEquals(1, listItemClass.getTypeParameters().length, "ListItem must be generic");
assertEquals("T", listItemClass.getTypeParameters()[0].getName(), "Type parameter is not named 'T'");
// is not abstract
assertFalse(isAbstract(listItemClass.getModifiers()), "ListItem must not be abstract");
// constructors
try {
constructor = listItemClass.getDeclaredConstructor();
} catch (NoSuchMethodException e) {
fail("ListItem must have a parameterless constructor", e);
}
// fields
try {
key = listItemClass.getDeclaredField("key");
next = listItemClass.getDeclaredField("next");
} catch (NoSuchFieldException e) {
fail("ListItem is missing one or more required fields", e);
}
key.setAccessible(true);
next.setAccessible(true);
assertHasModifiers(key, Map.of(PUBLIC, true));
assertEquals("T", key.getGenericType().getTypeName(), "Type of field \"key\" is incorrect");
assertHasModifiers(next, Map.of(PUBLIC, true));
assertEquals("h05.ListItem<T>", next.getGenericType().getTypeName(), "Type of field \"next\" is incorrect");
CLASS_CORRECT.put(ListItemTest.class, true);
}
@Test
public void classDefinitionCorrect() {}
}
package h05;
import h05.Utils.TestType;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import static h05.Utils.*;
import static h05.Utils.TestType.Type.CLASS;
import static java.lang.reflect.Modifier.*;
import static org.junit.jupiter.api.Assertions.*;
@TestType(CLASS)
public class MyTreeNodeTest {
public static Class<?> myTreeNodeClass;
public static Constructor<?> constructor;
public static Field nodeID, successors;
private static final String CLASS_NAME = "h05.MyTreeNode";
@BeforeAll
public static void checkClass() {
if (CLASS_CORRECT.containsKey(MyTreeNodeTest.class))
return;
requireTest(ListItemTest.class);
myTreeNodeClass = getClassForName(CLASS_NAME);
// is public
assertTrue(isPublic(myTreeNodeClass.getModifiers()));
// is not generic
assertEquals(0, myTreeNodeClass.getTypeParameters().length, CLASS_NAME + " must not be generic");
// is not abstract
assertFalse(isAbstract(myTreeNodeClass.getModifiers()), CLASS_NAME + " must not be abstract");
// constructors
try {
constructor = myTreeNodeClass.getDeclaredConstructor(long.class);
} catch (NoSuchMethodException e) {
fail(CLASS_NAME + " is missing a required constructor", e);
}
assertHasModifiers(constructor, Map.of(PUBLIC, true), "Constructor of " + CLASS_NAME + " must be public");
// fields
try {
nodeID = myTreeNodeClass.getDeclaredField("nodeID");
successors = myTreeNodeClass.getDeclaredField("successors");
} catch (NoSuchFieldException e) {
fail(CLASS_NAME + " is missing one or more required fields", e);
}
assertHasModifiers(nodeID, Map.of(PUBLIC, true, STATIC, false, FINAL, true));
assertEquals(long.class, nodeID.getType(), "nodeID must be of type long");
assertHasModifiers(successors, Map.of(PUBLIC, true, STATIC, false, FINAL, false));
assertEquals("h05.ListItem<h05.MyTreeNode>", successors.getGenericType().getTypeName(),
"successors must be of type ListItem<MyTreeNode>");
CLASS_CORRECT.put(MyTreeNodeTest.class, true);
}
@Test
public void testFields() throws ReflectiveOperationException {
Object instance = constructor.newInstance(0);
assertEquals(0L, nodeID.get(instance), "Field nodeID has wrong value after instantiation");
assertNull(successors.get(instance), "Field successors has wrong value after instantiation");
}
}
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