Class GraderContext

java.lang.Object
de.hsh.graja.modules.junit.api.GraderContext
All Implemented Interfaces:
CommentTarget

public class GraderContext extends Object implements 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 Details

    • GraderContext

      public GraderContext()
  • Method Details

    • setMethodName

      public void setMethodName(String name)
    • getMethodName

      public String getMethodName()
    • setTestClass

      public void setTestClass(Class<?> testClass)
    • getCurrentTestMethod

      public String getCurrentTestMethod()
      Returns:
      returns the currently executing test method
    • getCurrentWorkspacePath

      public Path 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

      public String getSecurityDebuggingInfo(Throwable e)
      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

      public String 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

      public 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. 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

      public 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. 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

      public 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. 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

      public 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. 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 loaded
      baseClass - 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 loaded
      baseClass - 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 see CompileString.
      Parameters:
      code - program source code that must specify a single class
      fullyQualifiedClassname - the name of the class
      workspace - path to where to put the temporary source file
      errorLog - out parameter
      Returns:
      the compiled byte code
      Throws:
      IOException - in case of I/O errors when generating / reading files
      ClassNotFoundException - 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 see CompileString.
      Parameters:
      code - program source code that must specify a single class
      fullyQualifiedClassname - the name of the class
      workspace - path to where to put the temporary source file
      errorLog - out parameter
      Returns:
      the compiled byte code
      Throws:
      IOException - in case of I/O errors when generating / reading files
      ClassNotFoundException - if the source code does not specify a single class
    • getCVVp

      public CVVp getCVVp()
      Returns:
      the variation resolution, if there is any; null otherwise.
    • addComment

      public void addComment(Content comment)
      this method is meant to be used from test classes for commenting in test methods.
      Specified by:
      addComment in interface CommentTarget
      Parameters:
      comment - a comment object that will be added to the list of comments for the executing test method
    • useDefaultCodeOrientedFeedbackPos

      public void useDefaultCodeOrientedFeedbackPos(ASTPos pos)
      this method is meant to be used from test classes for commenting in test methods.
      Specified by:
      useDefaultCodeOrientedFeedbackPos in interface CommentTarget
      Parameters:
      pos - a default AST position that will be used for code oriented feedback if the current test method fails.
    • regressionTestableRandom

      public 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.
      Returns:
      in mock mode a new random with the same seed, else a randomly seeded random.
    • regressionTestableRandom

      public Random 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.
      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.