Class Support

java.lang.Object
de.hsh.graja.modules.junit.api.Support

public class Support extends Object
This class provides supporting methods when writing JUnit tests for student submitted code. It heavily depends on JUnit.
  • Constructor Details

    • Support

      public Support()
  • Method Details

    • assertHasToStringMethod

      public static <Base> void assertHasToStringMethod(Class<? extends Base> clazz) throws AssertionError
      Tests, whether the method toString is implemented and has the correct return type. Will fail, if not.
      Type Parameters:
      Base - a generic type parameter describing a base class of the target class
      Parameters:
      clazz - the class that should implement toString
      Throws:
      AssertionError
    • assertImplementsRunnable

      public static <Base> void assertImplementsRunnable(Class<? extends Base> clazz) throws AssertionError
      Test, whether the clazz implements Runnable.
      Type Parameters:
      Base - a generic type parameter describing a base class of the target class
      Parameters:
      clazz - the class that should implement Runnable
      Throws:
      AssertionError
    • callMainAndReturnOutput

      public static String callMainAndReturnOutput(Class<?> clazz, String[] args)
      calls the main method of clazz and returns stdout output as string
      Parameters:
      clazz - the class whose main method gets called
      args - arguments passed to the main method
      Returns:
      stdout catched from the called main method
    • callMainAndReturnOutput

      public static String callMainAndReturnOutput(Class<?> clazz, String[] args, String systemInInput)
      calls the main method of clazz and returns stdout output as string
      Parameters:
      clazz - the class whose main method gets called
      args - arguments passed to the main method
      systemInInput - String with console input (null, if not needed)
      Returns:
      stdout catched from the called main method
    • assertAllAttributesArePrivateOrClassConstants

      public static <Base> void assertAllAttributesArePrivateOrClassConstants(Class<? extends Base> clazz)
      asserts that all attributes of clazz are private or class constants. A grader calling this method needs the permission java.lang.RuntimePermission "accessDeclaredMembers";
      Type Parameters:
      Base - generic type parameter specifying a base class of the target class
      Parameters:
      clazz - the target class
    • assertAllNonFinalAttributesAreNonStatic

      public static <Base> void assertAllNonFinalAttributesAreNonStatic(Class<? extends Base> clazz)
      asserts that all non-final attributes of clazz are instance attributes. A grader calling this method needs the permission java.lang.RuntimePermission "accessDeclaredMembers";
      Type Parameters:
      Base - generic type parameter specifying a base class of the target class
      Parameters:
      clazz - the target class
    • assertGettersDeliverValuesEqualToConstructorParams

      public static void assertGettersDeliverValuesEqualToConstructorParams(Class<?> clazz, String[] getterNames, Object[] constructorParams)
      This method calls the constructor of clazz passing the parameters constructorParams to it. Errors result in a JUnit fail call. After that this method calls all the getters specified in getterNames. Each getter return value is compared using equals to the constructor parameters. If there are differences, this method will call JUnit's fail method.
      Type Parameters:
      Base - generic type parameter specifying a base class of the target class
      Parameters:
      clazz - the target class
      getterNames - array of method names of get methods. Can be empty or null and must have the same length as constructorParams.
      constructorParams - array of actual constructor parameters. Can be empty or null .
    • assertOverrideAnnotation

      public static void assertOverrideAnnotation(Path workspace, String fullyQualifiedClassName, String methodName)

      This method loads the source and matches a regular expression in order to check, whether the @Override annotation is present at the specified method. Since @Override has retention policy source, reflecting the annotation at runtime is not possible.

      Caveat: This method will assume, that type names in the loaded source code will be written without comments in between, e. g. we assume something like java.lang.String, but not java./*comment*/lang.String. The latter is a valid type name as described in the JLS, but it won't be recognized as such by this method. Whitespaces between dots are supported: java . lang . String. Also, this method won't accept comments between the @Override annotation and any method modifiers, return type and the methodName.

      Parameters:
      workspace - the folder where this method will search for source files
      fullyQualifiedClassName - the class name
      methodName - the method name
    • assertOverrideAnnotation

      public static void assertOverrideAnnotation(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName, String methodName)

      This method loads the source and matches a regular expression in order to check, whether the @Override annotation is present at the specified method. Since @Override has retention policy source, reflecting the annotation at runtime is not possible.

      Caveat: This method will assume, that type names in the loaded source code will be written without comments in between, e. g. we assume something like java.lang.String, but not java./*comment*/lang.String. The latter is a valid type name as described in the JLS, but it won't be recognized as such by this method. Whitespaces between dots are supported: java . lang . String. Also, this method won't accept comments between the @Override annotation and any method modifiers, return type and the methodName.

      Parameters:
      workspace - the folder where this method will search for source files
      searchInSubdirs - a list of subfolders that this method will scan in the specified order
      fullyQualifiedClassName - the class name
      methodName - the method name
    • assertImplementsGenericInterface

      public static void assertImplementsGenericInterface(Path workspace, String fullyQualifiedClassName, String fullyQualifiedInterfaceName, String fullyQualifiedGenericTypeArgument)

      This method loads the source and matches a regular expression in order to check, whether the class fullyQualifiedClassName implements the generic interface specified by the parameters fullyQualifiedInterfaceName and fullyQualifiedGenericTypeArgument. This method will also return silently, if the source does not contain fully qualified but simple type names in the code.

      Caveat: This method will assume, that type names and the sequence implements typename<T> in the loaded source code will be written without comments in between, e. g. we assume something like implements Comparable<java.lang.String>, but not implements /*comment*/ Comparable<java./*comment*/lang.String /*comment*/ >. The latter is perfecty valid java code as described in the JLS, but it won't be recognized as such by this method. Whitespaces between dots and <> are supported.

      Parameters:
      workspace - the folder where this method will search for source files
      fullyQualifiedClassName - the target class
      fullyQualifiedInterfaceName - the interface that must be implemented
      fullyQualifiedGenericTypeArgument - the generic type parameter of the interface
    • assertImplementsGenericInterface

      public static void assertImplementsGenericInterface(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName, String fullyQualifiedInterfaceName, String fullyQualifiedGenericTypeArgument)

      This method loads the source and matches a regular expression in order to check, whether the class fullyQualifiedClassName implements the generic interface specified by the parameters fullyQualifiedInterfaceName and fullyQualifiedGenericTypeArgument. This method will also return silently, if the source does not contain fully qualified but simple type names in the code.

      Caveat: This method will assume, that type names and the sequence implements typename<T> in the loaded source code will be written without comments in between, e. g. we assume something like implements Comparable<java.lang.String>, but not implements /*comment*/ Comparable<java./*comment*/lang.String /*comment*/ >. The latter is perfecty valid java code as described in the JLS, but it won't be recognized as such by this method. Whitespaces between dots and <> are supported.

      Parameters:
      workspace - the folder where this method will search for source files
      searchInSubdirs - a list of subfolders that this method will scan in the specified order
      fullyQualifiedClassName - the target class
      fullyQualifiedInterfaceName - the interface that must be implemented
      fullyQualifiedGenericTypeArgument - the generic type parameter of the interface
    • getFileByClassName

      public static Path getFileByClassName(Path workspace, String fullyQualifiedClassName)
      This method returns the first java file that contains the given class name. It also returns a wrongly named source file (classname != filename). If no matching file was found, an AssertionError is thrown.
      Parameters:
      workspace -
      fullyQualifiedClassName -
      Returns:
    • getFileByClassName

      public static Path getFileByClassName(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName)
      This method returns the first java file that contains the given class name. It also returns a wrongly named source file (classname != filename). If no matching file was found, an AssertionError is thrown.
      Parameters:
      workspace -
      searchInSubdirs -
      fullyQualifiedClassName -
      Returns:
    • parseCompilationUnit

      public static com.github.javaparser.ast.CompilationUnit parseCompilationUnit(String sourceCode)
      Read with StaticJavaParser.
      Parameters:
      sourceCode -
      Returns:
    • readSource

      public static Support.SourceFilePathAndContent readSource(Path workspace, String fullyQualifiedClassName)
      This method loads the source code of a class. If no matching file was found, an AssertionError is thrown.
      Parameters:
      workspace - the folder where this method will search for source files
      fullyQualifiedClassName - the class name
    • readSource

      public static Support.SourceFilePathAndContent readSource(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName)
      This method loads the source code of a class. If no matching file was found, an AssertionError is thrown.
      Parameters:
      workspace - the folder where this method will search for source files
      searchInSubdirs - a list of subfolders that this method will scan in the specified order
      fullyQualifiedClassName - the class name
    • assertRegexInSource

      public static void assertRegexInSource(Path workspace, String fullyQualifiedClassName, String regex, String messageOnError)
      This method loads the source and matches a regular expression against the source (in DOTALL mode). If no match was found, the messageOnError is thrown as an AssertionError. If the file was not readable, a different AssertionError will arise.
      Parameters:
      workspace - the folder where this method will search for source files
      fullyQualifiedClassName - the class name
      regex - the regular expression
      messageOnError - the error message
    • assertRegexInSource

      public static void assertRegexInSource(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName, String regex, String messageOnError)
      This method loads the source and matches a regular expression against the source (in DOTALL mode). If no match was found, the messageOnError is thrown as an AssertionError. If the file was not readable, a different AssertionError will arise.
      Parameters:
      workspace - the folder where this method will search for source files
      searchInSubdirs - a list of subfolders that this method will scan in the specified order
      fullyQualifiedClassName - the class name
      regex - the regular expression
      messageOnError - the error message
    • assertSuperClass

      public static void assertSuperClass(Class<?> clazz, String expectedSuperclassFqn)
      This method fails, if clazz does not extend expectedSuperclassFqn
      Parameters:
      clazz - the target class
      expectedSuperclassFqn - fully qualified name of expected super class
    • assertSuperInterface

      public static void assertSuperInterface(Class<?> clazz, String expectedInterfaceFqn)
      This method fails, if clazz is a class that does not implement expectedInterfaceFqn, or if clazz is an interface that does not extend expectedInterfaceFqn.
      Parameters:
      clazz - a class or interface
      expectedInterfaceFqn - fully qualified name of expected interface
    • getField

      public static Field getField(Class<?> declaringClass, Class<?> fieldType)
      Searches a field of type fieldType in the declaringClass (exact match required, no superclasses searched). Fails, if not found.
      Parameters:
      declaringClass - the declaring class
      fieldType - the field type
      Returns:
      the field.
    • posOnFailAndReturn

      public static <T, E extends Throwable> T posOnFailAndReturn(Support.Callable<T,E> r, ASTPos pos) throws E
      Executes the callable. If it fails with an AssertionError, the AssertionError will be wrapped into a PositionAssertionError including the given position so the error will be displayed in the code oriented feedback.
      Parameters:
      r -
      pos -
      Throws:
      E extends Throwable
    • posOnFailAndReturn

      public static <T, E extends Throwable> T posOnFailAndReturn(Support.Callable<T,E> r, Supplier<ASTPos> pos) throws E
      Executes the callable. If it fails with an AssertionError, the AssertionError will be wrapped into a PositionAssertionError including the given position so the error will be displayed in the code oriented feedback.
      Parameters:
      r -
      pos -
      Throws:
      E extends Throwable
    • posOnFail

      public static void posOnFail(Runnable r, ASTPos pos)
      Executes the runnable. If it fails with an AssertionError, the AssertionError will be wrapped into a PositionAssertionError including the given position so the error will be displayed in the code oriented feedback.
      Parameters:
      r -
      pos -
    • posOnFail

      public static void posOnFail(Runnable r, Supplier<ASTPos> pos)
      Executes the runnable. If it fails with an AssertionError, the AssertionError will be wrapped into a PositionAssertionError including the given position so the error will be displayed in the code oriented feedback.
      Parameters:
      r -
      pos -
    • assertTrue

      public static void assertTrue(String msg, boolean condition, ASTPos posOnFail)
      Asserts the condition being true. If it is not true, a PositionAssertionError including the given position is thrown.
      Parameters:
      msg -
      condition -
      posOnFail -
    • assertFalse

      public static void assertFalse(String msg, boolean condition, ASTPos posOnFail)
      Asserts the condition being false. If it is not false, a PositionAssertionError including the given position is thrown.
      Parameters:
      msg -
      condition -
      posOnFail -
    • assertEquals

      public static void assertEquals(String msg, int expected, int observed, ASTPos posOnFail)
      Asserts the observed value being equals to expected. If not, a PositionAssertionError including the given position is thrown.
      Parameters:
      msg -
      expected -
      observed -
      posOnFail -
    • assertEquals

      public static void assertEquals(String msg, double expected, double observed, double delta, ASTPos posOnFail)
      Asserts the observed value being equals to expected. If not, a PositionAssertionError including the given position is thrown.
      Parameters:
      msg -
      expected -
      observed -
      delta -
      posOnFail -
    • assertEquals

      public static void assertEquals(String msg, String expected, String observed, ASTPos posOnFail)
      Asserts the observed value being equals to expected. If not, a PositionAssertionError including the given position is thrown.
      Parameters:
      msg -
      expected -
      observed -
      posOnFail -
    • fail

      public static void fail(String msg, ASTPos posOnFail)
      Fails with a PositionAssertionError including the given position is thrown.
      Parameters:
      msg -
      posOnFail -
    • hintOnFail

      public static void hintOnFail(Runnable r, String hint)
      Executes the runnable. If it fails with an AssertionError, the AssertionError will be wrapped into a CommentAssertionError including the given hint.
      Parameters:
      r -
      hint -
    • getClassForName

      public static Class<?> getClassForName(String className)
      See Also:
    • create

      public static <T> T create(Class<T> returnType, Object... params)
      See Also:
    • createCatchAny

      public static <T> T createCatchAny(Class<T> returnType, Object... params)
      Like {@link #create(Class, Object...), but wraps any throwable inside a PositionAssertionError.
    • create

      public static Object create(String className, Object... params)
      See Also:
    • createEx

      public static <T> T createEx(Class<T> returnType, Object... params) throws Exception
      Throws:
      Exception
      See Also:
    • createEx

      public static Object createEx(String className, Object... params) throws Exception
      Throws:
      Exception
      See Also:
    • getMatchingConstructor

      public static Constructor<?> getMatchingConstructor(Class<?> c, Class<?>... params)
    • getMatchingConstructor

      public static Constructor<?> getMatchingConstructor(Class<?> c, int mandatoryModifiers, int disallowedModifiers, Class<?>... params)
      See Also:
    • invoke

      public static <T> T invoke(Object receiver, Class<T> returnType, String methodName, Object... params)
      See Also:
    • invokeCatchAny

      public static <T> T invokeCatchAny(Object receiver, Class<T> returnType, String methodName, Object... params)
      Like {@link #invoke(Object, Class, String, Object...), but wraps any throwable inside a PositionAssertionError.
    • invoke

      public static Object invoke(Object receiver, Method method, Object... params)
      See Also:
    • invokeCatchAny

      public static Object invokeCatchAny(Object receiver, Method method, Object... params)
      Like {@link #invoke(Object, Method, Object...), but wraps any throwable inside a PositionAssertionError.
    • invoke

      public static void invoke(Object receiver, String methodName, Object... params)
      See Also:
    • invokeCatchAny

      public static void invokeCatchAny(Object receiver, String methodName, Object... params)
      Like {@link #invoke(Object, String, Object...), but wraps any throwable inside a PositionAssertionError.
    • invokeEx

      public static <T> T invokeEx(Object receiver, Class<T> returnType, String methodName, Object... params) throws Exception
      Throws:
      Exception
      See Also:
    • invokeEx

      public static Object invokeEx(Object receiver, Method method, Object... params) throws Exception
      Throws:
      Exception
      See Also:
    • invokeEx

      public static void invokeEx(Object receiver, String methodName, Object... params) throws Exception
      Throws:
      Exception
      See Also:
    • invokeStatic

      public static <T> T invokeStatic(Class<?> targetClass, Class<T> returnType, String methodName, Object... params)
      See Also:
    • invokeStaticCatchAny

      public static <T> T invokeStaticCatchAny(Class<?> targetClass, Class<T> returnType, String methodName, Object... params)
      Like {@link #invokeStatic(Class, Class, String, Object...), but wraps any throwable inside a PositionAssertionError.
    • getMatchingMethod

      public static Method getMatchingMethod(Class<?> c, String name, Class<?>... params)
      See Also:
    • getMatchingMethod

      public static Method getMatchingMethod(Class<?> c, String name, int mandatoryModifiers, int disallowedModifiers, Class<?>... params)
      See Also:
    • getMethod

      public static Method getMethod(Class<?> c, String name, Class<?>... params)
      See Also:
    • get

      public static <T> T get(Object receiver, Class<T> type, String fieldName)
      See Also:
    • get

      public static <T> T get(Class<?> receiverClass, Object receiver, Class<T> type, String fieldName)
      See Also:
    • getField

      public static Field getField(Class<?> receiverClass, Object receiver, Class<?> type, String fieldName)
      See Also:
    • setField

      public static void setField(Class<?> receiverClass, Object receiver, String fieldName, Object value, Class<?> fieldType)
      See Also:
    • set

      public static void set(Class<?> receiverClass, Object receiver, String fieldName, Object value)
      See Also:
    • set

      public static void set(Object receiver, String fieldName, Object value)
      See Also:
    • set

      public static void set(Object receiver, String fieldName, Object value, Class<?> fieldType)
      See Also:
    • setInt

      public static void setInt(Class<?> receiverClass, Object receiver, String fieldName, int value)
      See Also: