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 Summary
Constructors Constructor Description Support()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <Base> void
assertAllAttributesArePrivateOrClassConstants(Class<? extends Base> clazz)
asserts that all attributes ofclazz
are private or class constants.static <Base> void
assertAllNonFinalAttributesAreNonStatic(Class<? extends Base> clazz)
asserts that all non-final attributes ofclazz
are instance attributes.static void
assertGettersDeliverValuesEqualToConstructorParams(Class<?> clazz, String[] getterNames, Object[] constructorParams)
This method calls the constructor ofclazz
passing the parametersconstructorParams
to it.static <Base> void
assertHasToStringMethod(Class<? extends Base> clazz)
Tests, whether the methodtoString
is implemented and has the correct return type.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 classfullyQualifiedClassName
implements the generic interface specified by the parametersfullyQualifiedInterfaceName
andfullyQualifiedGenericTypeArgument
.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 classfullyQualifiedClassName
implements the generic interface specified by the parametersfullyQualifiedInterfaceName
andfullyQualifiedGenericTypeArgument
.static <Base> void
assertImplementsRunnable(Class<? extends Base> clazz)
Test, whether the clazz implementsRunnable
.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.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.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).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).static void
assertSuperClass(Class<?> clazz, String expectedSuperclassFqn)
This method fails, ifclazz
does not extendexpectedSuperclassFqn
static void
assertSuperInterface(Class<?> clazz, String expectedInterfaceFqn)
This method fails, ifclazz
is a class that does not implementexpectedInterfaceFqn
, or ifclazz
is an interface that does not extendexpectedInterfaceFqn
.static String
callMainAndReturnOutput(Class<?> clazz, String[] args)
calls the main method ofclazz
and returns stdout output as stringstatic String
callMainAndReturnOutput(Class<?> clazz, String[] args, String systemInInput)
calls the main method ofclazz
and returns stdout output as stringstatic Field
getField(Class<?> declaringClass, Class<?> fieldType)
Searches a field of typefieldType
in thedeclaringClass
(exact match required, no superclasses searched).static void
hintOnFail(Runnable r, String hint)
Executes the runnable.static String
readSource(Path workspace, String fullyQualifiedClassName)
This method loads the source code of a class.static String
readSource(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName)
This method loads the source code of a class.
-
-
-
Method Detail
-
assertHasToStringMethod
public static <Base> void assertHasToStringMethod(Class<? extends Base> clazz) throws AssertionError
Tests, whether the methodtoString
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 implementtoString
- Throws:
AssertionError
-
assertImplementsRunnable
public static <Base> void assertImplementsRunnable(Class<? extends Base> clazz) throws AssertionError
Test, whether the clazz implementsRunnable
.- Type Parameters:
Base
- a generic type parameter describing a base class of the target class- Parameters:
clazz
- the class that should implementRunnable
- Throws:
AssertionError
-
callMainAndReturnOutput
public static String callMainAndReturnOutput(Class<?> clazz, String[] args)
calls the main method ofclazz
and returns stdout output as string- Parameters:
clazz
- the class whose main method gets calledargs
- 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 ofclazz
and returns stdout output as string- Parameters:
clazz
- the class whose main method gets calledargs
- arguments passed to the main methodsystemInInput
- 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 ofclazz
are private or class constants. A grader calling this method needs the permissionjava.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 ofclazz
are instance attributes. A grader calling this method needs the permissionjava.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 ofclazz
passing the parametersconstructorParams
to it. Errors result in a JUnit fail call. After that this method calls all the getters specified ingetterNames
. Each getter return value is compared usingequals
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 classgetterNames
- array of method names of get methods. Can be empty or null and must have the same length asconstructorParams
.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 notjava./*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 themethodName
.- Parameters:
workspace
- the folder where this method will search for source filesfullyQualifiedClassName
- the class namemethodName
- 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 notjava./*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 themethodName
.- Parameters:
workspace
- the folder where this method will search for source filessearchInSubdirs
- a list of subfolders that this method will scan in the specified orderfullyQualifiedClassName
- the class namemethodName
- 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 parametersfullyQualifiedInterfaceName
andfullyQualifiedGenericTypeArgument
. 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 likeimplements Comparable<java.lang.String>
, but notimplements /*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 filesfullyQualifiedClassName
- the target classfullyQualifiedInterfaceName
- the interface that must be implementedfullyQualifiedGenericTypeArgument
- 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 parametersfullyQualifiedInterfaceName
andfullyQualifiedGenericTypeArgument
. 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 likeimplements Comparable<java.lang.String>
, but notimplements /*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 filessearchInSubdirs
- a list of subfolders that this method will scan in the specified orderfullyQualifiedClassName
- the target classfullyQualifiedInterfaceName
- the interface that must be implementedfullyQualifiedGenericTypeArgument
- 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, anAssertionError
is thrown.- Parameters:
workspace
- the folder where this method will search for source filesfullyQualifiedClassName
- 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, anAssertionError
is thrown.- Parameters:
workspace
- the folder where this method will search for source filessearchInSubdirs
- a list of subfolders that this method will scan in the specified orderfullyQualifiedClassName
- 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, themessageOnError
is thrown as anAssertionError
. If the file was not readable, a differentAssertionError
will arise.- Parameters:
workspace
- the folder where this method will search for source filesfullyQualifiedClassName
- the class nameregex
- the regular expressionmessageOnError
- 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, themessageOnError
is thrown as anAssertionError
. If the file was not readable, a differentAssertionError
will arise.- Parameters:
workspace
- the folder where this method will search for source filessearchInSubdirs
- a list of subfolders that this method will scan in the specified orderfullyQualifiedClassName
- the class nameregex
- the regular expressionmessageOnError
- the error message
-
assertSuperClass
public static void assertSuperClass(Class<?> clazz, String expectedSuperclassFqn)
This method fails, ifclazz
does not extendexpectedSuperclassFqn
- Parameters:
clazz
- the target classexpectedSuperclassFqn
- fully qualified name of expected super class
-
assertSuperInterface
public static void assertSuperInterface(Class<?> clazz, String expectedInterfaceFqn)
This method fails, ifclazz
is a class that does not implementexpectedInterfaceFqn
, or ifclazz
is an interface that does not extendexpectedInterfaceFqn
.- Parameters:
clazz
- a class or interfaceexpectedInterfaceFqn
- fully qualified name of expected interface
-
getField
public static Field getField(Class<?> declaringClass, Class<?> fieldType)
Searches a field of typefieldType
in thedeclaringClass
(exact match required, no superclasses searched). Fails, if not found.- Parameters:
declaringClass
- the declaring classfieldType
- the field type- Returns:
- the field.
-
-