Package de.hsh.graja.modules.junit.api
Class GraderContext
java.lang.Object
de.hsh.graja.modules.junit.api.GraderContext
- All Implemented Interfaces:
CommentTarget
This is the context of your JUnit grader for a single assignment.
The class connects to Graja functionality through two classes:
- There is a
JunitBroker
object that serves requests from assignment graders. The broker is either called directly or through delegating methods in this base class. - The class
Support
has helper methods for various tasks
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addComment
(Content comment) this method is meant to be used from test classes for commenting in test methods.Class<?>
getClassForName
(String fullyQualifiedClassname) Graders must call this method to load classes from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain.this method is meant to be used from test classes.This method is meant to be called by test classes to get the workspace path where the current student submission under test is processed.getCVVp()
Class<?>
getInterfaceForName
(String fullyQualifiedInterfacename) Graders must call this method to load interfaces from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain.Class<?>
getPublicClassForName
(String fullyQualifiedClassname) Graders must call this method to load classes from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain.Class<?>
getPublicInterfaceForName
(String fullyQualifiedInterfacename) Graders must call this method to load interfaces from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain.<Base> Class<? extends Base>
getPublicSubclassForName
(String fullyQualifiedClassname, Class<Base> baseClass) Returns the class as a subclass of Base.this method is meant to be used from test classes.<Base> Class<? extends Base>
getSubclassForName
(String fullyQualifiedClassname, Class<Base> baseClass) Returns the class as a subclass of Base.Class<?>
readClassIntoGraderProtectionDomain
(String code, String fullyQualifiedClassname, Path workspace, StringBuilder errorLog) Sometimes a test class wants to compile code that is generated on the fly.Class<?>
readClassIntoSubmissionProtectionDomain
(String code, String fullyQualifiedClassname, Path workspace, StringBuilder errorLog) Sometimes a test class wants to compile code that is generated on the fly.If in mock mode this will return a new random with the same initial seed and same state.
Useful when multiple tests require a random and the test ordering is non-deterministic.regressionTestableRandom
(long seed) If in mock mode this will return a new random with the same initial seed and same state.
Useful when multiple tests require a random and the test ordering is non-deterministic.void
setMethodName
(String name) void
setTestClass
(Class<?> testClass) void
this method is meant to be used from test classes for commenting in test methods.
-
Constructor Details
-
GraderContext
public GraderContext()
-
-
Method Details
-
setMethodName
-
getMethodName
-
setTestClass
-
getCurrentTestMethod
- Returns:
- returns the currently executing test method
-
getCurrentWorkspacePath
This method is meant to be called by test classes to get the workspace path where the current student submission under test is processed.- Returns:
- the workspace path
-
getSecurityDebuggingInfo
this method is meant to be used from test classes. it returns a string containing information valuable when debugging security issues.- Parameters:
e
- an exception or error- Returns:
- an informational text
-
getCurrentStackTrace
this method is meant to be used from test classes. it returns a string containing information valuable when debugging.- Returns:
- an informational text with the stacktrace
-
getClassForName
Graders must call this method to load classes from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain. If the class cannot be found, this method fails with a org.junit.Assert.fail.- Parameters:
fullyQualifiedClassname
- fully qualified name of the class to be loaded- Returns:
- the loaded class object
-
getInterfaceForName
Graders must call this method to load interfaces from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain. If the interface cannot be found, this method fails with a org.junit.Assert.fail.- Parameters:
fullyQualifiedInterfacename
- fully qualified name of the interface to be loaded- Returns:
- the loaded class object
-
getPublicClassForName
Graders must call this method to load classes from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain. If the class cannot be found or is not public, this method fails with a org.junit.Assert.fail.- Parameters:
fullyQualifiedClassname
- fully qualified name of the class to be loaded- Returns:
- the loaded class object
-
getPublicInterfaceForName
Graders must call this method to load interfaces from student submissions, since this method guarantees that student code is loaded into the appropriate protection domain. If the interface cannot be found or is not public, this method fails with a org.junit.Assert.fail.- Parameters:
fullyQualifiedInterfacename
- fully qualified name of the interface to be loaded- Returns:
- the loaded class object
-
getSubclassForName
public <Base> Class<? extends Base> getSubclassForName(String fullyQualifiedClassname, Class<Base> baseClass) Returns the class as a subclass of Base. fails, if the class is not found or it is not a subclass of Base. Subclass here means including Base itself.- Parameters:
fullyQualifiedClassname
- fully qualified name of the class to be loadedbaseClass
- the base class- Returns:
- the loaded class object
-
getPublicSubclassForName
public <Base> Class<? extends Base> getPublicSubclassForName(String fullyQualifiedClassname, Class<Base> baseClass) Returns the class as a subclass of Base. fails, if the class is not found or it is not public or it is not a subclass of Base. Subclass here means including Base itself.- Parameters:
fullyQualifiedClassname
- fully qualified name of the class to be loadedbaseClass
- the base class- Returns:
- the loaded class object
-
readClassIntoGraderProtectionDomain
public Class<?> readClassIntoGraderProtectionDomain(String code, String fullyQualifiedClassname, Path workspace, StringBuilder errorLog) throws IOException, ClassNotFoundException Sometimes a test class wants to compile code that is generated on the fly. This method compiles that generated code and loads the resulting bytecode using the class loader that loaded this.getClass(). For an elaborate example seeCompileString
.- Parameters:
code
- program source code that must specify a single classfullyQualifiedClassname
- the name of the classworkspace
- path to where to put the temporary source fileerrorLog
- out parameter- Returns:
- the compiled byte code
- Throws:
IOException
- in case of I/O errors when generating / reading filesClassNotFoundException
- if the source code does not specify a single class
-
readClassIntoSubmissionProtectionDomain
public Class<?> readClassIntoSubmissionProtectionDomain(String code, String fullyQualifiedClassname, Path workspace, StringBuilder errorLog) throws IOException, ClassNotFoundException Sometimes a test class wants to compile code that is generated on the fly. This method compiles that generated code and loads the resulting bytecode using the class loader that loaded this.getClass(). For an elaborate example seeCompileString
.- Parameters:
code
- program source code that must specify a single classfullyQualifiedClassname
- the name of the classworkspace
- path to where to put the temporary source fileerrorLog
- out parameter- Returns:
- the compiled byte code
- Throws:
IOException
- in case of I/O errors when generating / reading filesClassNotFoundException
- if the source code does not specify a single class
-
getCVVp
- Returns:
- the variation resolution, if there is any; null otherwise.
-
addComment
this method is meant to be used from test classes for commenting in test methods.- Specified by:
addComment
in interfaceCommentTarget
- Parameters:
comment
- a comment object that will be added to the list of comments for the executing test method
-
useDefaultCodeOrientedFeedbackPos
this method is meant to be used from test classes for commenting in test methods.- Specified by:
useDefaultCodeOrientedFeedbackPos
in interfaceCommentTarget
- Parameters:
pos
- a default AST position that will be used for code oriented feedback if the current test method fails.
-
regressionTestableRandom
If in mock mode this will return a new random with the same initial seed and same state.
Useful when multiple tests require a random and the test ordering is non-deterministic.- Returns:
- in mock mode a new random with the same seed, else a randomly seeded random.
-
regressionTestableRandom
If in mock mode this will return a new random with the same initial seed and same state.
Useful when multiple tests require a random and the test ordering is non-deterministic.- Parameters:
seed
- seed to use in a production environment (i.e. everywhere else than in a regression test)- Returns:
- in mock mode a new random with the same seed, else a random with the given seed.
-