Class 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 Detail

      • Support

        public Support()
    • Method Detail

      • 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
      • readSource

        public static String 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 String 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.
      • 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 -