Class Support
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceSupport.Callable<T,E extends Throwable> static class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <Base> voidassertAllAttributesArePrivateOrClassConstants(Class<? extends Base> clazz) asserts that all attributes ofclazzare private or class constants.static <Base> voidassertAllNonFinalAttributesAreNonStatic(Class<? extends Base> clazz) asserts that all non-final attributes ofclazzare instance attributes.static voidassertEquals(String msg, double expected, double observed, double delta, ASTPos posOnFail) Asserts the observed value being equals to expected.static voidassertEquals(String msg, int expected, int observed, ASTPos posOnFail) Asserts the observed value being equals to expected.static voidassertEquals(String msg, String expected, String observed, ASTPos posOnFail) Asserts the observed value being equals to expected.static voidassertFalse(String msg, boolean condition, ASTPos posOnFail) Asserts the condition being false.static voidassertGettersDeliverValuesEqualToConstructorParams(Class<?> clazz, String[] getterNames, Object[] constructorParams) This method calls the constructor ofclazzpassing the parametersconstructorParamsto it.static <Base> voidassertHasToStringMethod(Class<? extends Base> clazz) Tests, whether the methodtoStringis implemented and has the correct return type.static voidassertImplementsGenericInterface(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 classfullyQualifiedClassNameimplements the generic interface specified by the parametersfullyQualifiedInterfaceNameandfullyQualifiedGenericTypeArgument.static voidassertImplementsGenericInterface(Path workspace, String fullyQualifiedClassName, String fullyQualifiedInterfaceName, String fullyQualifiedGenericTypeArgument) This method loads the source and matches a regular expression in order to check, whether the classfullyQualifiedClassNameimplements the generic interface specified by the parametersfullyQualifiedInterfaceNameandfullyQualifiedGenericTypeArgument.static <Base> voidassertImplementsRunnable(Class<? extends Base> clazz) Test, whether the clazz implementsRunnable.static voidassertOverrideAnnotation(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 voidassertOverrideAnnotation(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 voidassertRegexInSource(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 voidassertRegexInSource(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 voidassertSuperClass(Class<?> clazz, String expectedSuperclassFqn) This method fails, ifclazzdoes not extendexpectedSuperclassFqnstatic voidassertSuperInterface(Class<?> clazz, String expectedInterfaceFqn) This method fails, ifclazzis a class that does not implementexpectedInterfaceFqn, or ifclazzis an interface that does not extendexpectedInterfaceFqn.static voidassertTrue(String msg, boolean condition, ASTPos posOnFail) Asserts the condition being true.static StringcallMainAndReturnOutput(Class<?> clazz, String[] args) calls the main method ofclazzand returns stdout output as stringstatic StringcallMainAndReturnOutput(Class<?> clazz, String[] args, String systemInInput) calls the main method ofclazzand returns stdout output as stringstatic <T> Tstatic Objectstatic <T> TcreateCatchAny(Class<T> returnType, Object... params) Like {@link #create(Class, Object...), but wraps any throwable inside aPositionAssertionError.static <T> Tstatic Objectstatic voidFails with a PositionAssertionError including the given position is thrown.static <T> Tstatic <T> Tstatic Class<?>getClassForName(String className) static FieldSearches a field of typefieldTypein thedeclaringClass(exact match required, no superclasses searched).static Fieldstatic PathgetFileByClassName(Path workspace, String fullyQualifiedClassName) This method returns the first java file that contains the given class name.static PathgetFileByClassName(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 MethodgetMatchingMethod(Class<?> c, String name, int mandatoryModifiers, int disallowedModifiers, Class<?>... params) static MethodgetMatchingMethod(Class<?> c, String name, Class<?>... params) static Methodstatic voidhintOnFail(Runnable r, String hint) Executes the runnable.static <T> Tstatic Objectstatic voidstatic <T> TinvokeCatchAny(Object receiver, Class<T> returnType, String methodName, Object... params) Like {@link #invoke(Object, Class, String, Object...), but wraps any throwable inside aPositionAssertionError.static ObjectinvokeCatchAny(Object receiver, Method method, Object... params) Like {@link #invoke(Object, Method, Object...), but wraps any throwable inside aPositionAssertionError.static voidinvokeCatchAny(Object receiver, String methodName, Object... params) Like {@link #invoke(Object, String, Object...), but wraps any throwable inside aPositionAssertionError.static <T> Tstatic Objectstatic voidstatic <T> TinvokeStatic(Class<?> targetClass, Class<T> returnType, String methodName, Object... params) static <T> TinvokeStaticCatchAny(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.CompilationUnitparseCompilationUnit(String sourceCode) Read withStaticJavaParser.static voidExecutes the runnable.static voidExecutes 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 voidstatic voidstatic voidstatic voidsetField(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 methodtoStringis 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 ofclazzand 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 ofclazzand 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 ofclazzare 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 ofclazzare 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 ofclazzpassing the parametersconstructorParamsto it. Errors result in a JUnit fail call. After that this method calls all the getters specified ingetterNames. Each getter return value is compared usingequalsto 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
fullyQualifiedClassNameimplements the generic interface specified by the parametersfullyQualifiedInterfaceNameandfullyQualifiedGenericTypeArgument. 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
fullyQualifiedClassNameimplements the generic interface specified by the parametersfullyQualifiedInterfaceNameandfullyQualifiedGenericTypeArgument. 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, anAssertionErroris 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, anAssertionErroris 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, anAssertionErroris 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, anAssertionErroris 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, themessageOnErroris thrown as anAssertionError. If the file was not readable, a differentAssertionErrorwill 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, themessageOnErroris thrown as anAssertionError. If the file was not readable, a differentAssertionErrorwill 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, ifclazzdoes not extendexpectedSuperclassFqn- Parameters:
clazz- the target classexpectedSuperclassFqn- fully qualified name of expected super class
-
assertSuperInterface
This method fails, ifclazzis a class that does not implementexpectedInterfaceFqn, or ifclazzis an interface that does not extendexpectedInterfaceFqn.- Parameters:
clazz- a class or interfaceexpectedInterfaceFqn- fully qualified name of expected interface
-
getField
Searches a field of typefieldTypein 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
-