Class Support
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Support.Callable<T,
E extends Throwable> static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <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
assertEquals
(String msg, double expected, double observed, double delta, ASTPos posOnFail) Asserts the observed value being equals to expected.static void
assertEquals
(String msg, int expected, int observed, ASTPos posOnFail) Asserts the observed value being equals to expected.static void
assertEquals
(String msg, String expected, String observed, ASTPos posOnFail) Asserts the observed value being equals to expected.static void
assertFalse
(String msg, boolean condition, ASTPos posOnFail) Asserts the condition being false.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 void
assertTrue
(String msg, boolean condition, ASTPos posOnFail) Asserts the condition being true.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 <T> T
static Object
static <T> T
createCatchAny
(Class<T> returnType, Object... params) Like {@link #create(Class, Object...), but wraps any throwable inside aPositionAssertionError
.static <T> T
static Object
static void
Fails with a PositionAssertionError including the given position is thrown.static <T> T
static <T> T
static Class<?>
getClassForName
(String className) static Field
Searches a field of typefieldType
in thedeclaringClass
(exact match required, no superclasses searched).static Field
static Path
getFileByClassName
(Path workspace, String fullyQualifiedClassName) This method returns the first java file that contains the given class name.static Path
getFileByClassName
(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName) This method returns the first java file that contains the given class name.static Constructor<?>
getMatchingConstructor
(Class<?> c, int mandatoryModifiers, int disallowedModifiers, Class<?>... params) static Constructor<?>
getMatchingConstructor
(Class<?> c, Class<?>... params) static Method
getMatchingMethod
(Class<?> c, String name, int mandatoryModifiers, int disallowedModifiers, Class<?>... params) static Method
getMatchingMethod
(Class<?> c, String name, Class<?>... params) static Method
static void
hintOnFail
(Runnable r, String hint) Executes the runnable.static <T> T
static Object
static void
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 aPositionAssertionError
.static Object
invokeCatchAny
(Object receiver, Method method, Object... params) Like {@link #invoke(Object, Method, Object...), but wraps any throwable inside aPositionAssertionError
.static void
invokeCatchAny
(Object receiver, String methodName, Object... params) Like {@link #invoke(Object, String, Object...), but wraps any throwable inside aPositionAssertionError
.static <T> T
static Object
static void
static <T> T
invokeStatic
(Class<?> targetClass, Class<T> returnType, String methodName, Object... params) 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 aPositionAssertionError
.static com.github.javaparser.ast.CompilationUnit
parseCompilationUnit
(String sourceCode) Read withStaticJavaParser
.static void
Executes the runnable.static void
Executes the runnable.static <T,
E extends Throwable>
TposOnFailAndReturn
(Support.Callable<T, E> r, ASTPos pos) Executes the callable.static <T,
E extends Throwable>
TposOnFailAndReturn
(Support.Callable<T, E> r, Supplier<ASTPos> pos) Executes the callable.readSource
(Path workspace, String fullyQualifiedClassName) This method loads the source code of a class.readSource
(Path workspace, String[] searchInSubdirs, String fullyQualifiedClassName) This method loads the source code of a class.static void
static void
static void
static void
setField
(Class<?> receiverClass, Object receiver, String fieldName, Object value, Class<?> fieldType) static void
-
Constructor Details
-
Support
public Support()
-
-
Method Details
-
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
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
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
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
-
getFileByClassName
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, anAssertionError
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, anAssertionError
is thrown.- Parameters:
workspace
-searchInSubdirs
-fullyQualifiedClassName
-- Returns:
-
parseCompilationUnit
Read withStaticJavaParser
.- 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, anAssertionError
is thrown.- Parameters:
workspace
- the folder where this method will search for source filesfullyQualifiedClassName
- 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, 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
This method fails, ifclazz
does not extendexpectedSuperclassFqn
- Parameters:
clazz
- the target classexpectedSuperclassFqn
- fully qualified name of expected super class
-
assertSuperInterface
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
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.
-
posOnFailAndReturn
public static <T,E extends Throwable> T posOnFailAndReturn(Support.Callable<T, E> r, ASTPos pos) throws EExecutes 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 EExecutes 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
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
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
Asserts the condition being true. If it is not true, a PositionAssertionError including the given position is thrown.- Parameters:
msg
-condition
-posOnFail
-
-
assertFalse
Asserts the condition being false. If it is not false, a PositionAssertionError including the given position is thrown.- Parameters:
msg
-condition
-posOnFail
-
-
assertEquals
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
Asserts the observed value being equals to expected. If not, a PositionAssertionError including the given position is thrown.- Parameters:
msg
-expected
-observed
-posOnFail
-
-
fail
Fails with a PositionAssertionError including the given position is thrown.- Parameters:
msg
-posOnFail
-
-
hintOnFail
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
-
create
-
createCatchAny
Like {@link #create(Class, Object...), but wraps any throwable inside aPositionAssertionError
. -
create
-
createEx
- Throws:
Exception
- See Also:
-
createEx
- Throws:
Exception
- See Also:
-
getMatchingConstructor
-
getMatchingConstructor
public static Constructor<?> getMatchingConstructor(Class<?> c, int mandatoryModifiers, int disallowedModifiers, Class<?>... params) -
invoke
-
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 aPositionAssertionError
. -
invoke
-
invokeCatchAny
Like {@link #invoke(Object, Method, Object...), but wraps any throwable inside aPositionAssertionError
. -
invoke
-
invokeCatchAny
Like {@link #invoke(Object, String, Object...), but wraps any throwable inside aPositionAssertionError
. -
invokeEx
public static <T> T invokeEx(Object receiver, Class<T> returnType, String methodName, Object... params) throws Exception - Throws:
Exception
- See Also:
-
invokeEx
- Throws:
Exception
- See Also:
-
invokeEx
- Throws:
Exception
- See Also:
-
invokeStatic
-
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 aPositionAssertionError
. -
getMatchingMethod
-
getMatchingMethod
-
getMethod
-
get
-
get
-
getField
-
setField
-
set
-
set
-
set
-
setInt
-