Package randoop.main

Class GenTests


public class GenTests extends GenInputsAbstract
Test generation.
  • Field Details

    • NO_OPERATIONS_TO_TEST

      private static final String NO_OPERATIONS_TO_TEST
      See Also:
    • command

      private static final String command
      See Also:
    • pitch

      private static final String pitch
      See Also:
    • commandGrammar

      private static final String commandGrammar
      See Also:
    • where

      private static final String where
      See Also:
    • summary

      private static final String summary
      See Also:
    • input

      private static final String input
      See Also:
    • output

      private static final String output
      See Also:
    • example

      private static final String example
      See Also:
    • notes

      private static final List<String> notes
      Explanations printed to the user.
    • TEST_METHOD_NAME_PREFIX

      public static final @Identifier String TEST_METHOD_NAME_PREFIX
      The prefix for test method names.
      See Also:
    • afterAllFixtureBody

      private com.github.javaparser.ast.stmt.BlockStmt afterAllFixtureBody
    • afterEachFixtureBody

      private com.github.javaparser.ast.stmt.BlockStmt afterEachFixtureBody
    • beforeAllFixtureBody

      private com.github.javaparser.ast.stmt.BlockStmt beforeAllFixtureBody
    • beforeEachFixtureBody

      private com.github.javaparser.ast.stmt.BlockStmt beforeEachFixtureBody
    • options

      private static org.plumelib.options.Options options
    • sequenceCompileFailureCount

      private int sequenceCompileFailureCount
      The count of sequences that failed to compile.
    • POSSIBLY_FLAKY_PREFIX

      public static final String POSSIBLY_FLAKY_PREFIX
      Is output to the user before each possibly flaky method.
      See Also:
    • fileSystemCache

      private Map<URI,FileSystem> fileSystemCache
      A cache used by getResourceDirectoryPath(java.lang.String), to prevent FileSystemAlreadyExistsException.
  • Constructor Details

    • GenTests

      public GenTests()
      GenTests constructor that uses default messages.
  • Method Details

    • handle

      public boolean handle(String[] args)
      Specified by:
      handle in class CommandHandler
    • readSideEffectFreeMethods

      public static MultiMap<Type,TypedClassOperation> readSideEffectFreeMethods()
      Read side-effect-free methods from the default JDK side-effect-free method list, and from a user-provided method list if provided.
      Returns:
      a map from a Type to a set of side-effect-free methods that take that type as their only argument
    • processAndOutputFlakyMethods

      private void processAndOutputFlakyMethods(List<ExecutableSequence> flakySequences, List<ExecutableSequence> sequences, MultiMap<Type,TypedClassOperation> sideEffectFreeMethodsByType, OmitMethodsPredicate omitMethodsPredicate, AccessibilityPredicate accessibilityPredicate)
      Outputs names of suspected flaky methods by using the tf-idf metric (Term Frequency - Inverse Document Frequency), which is:
      (number of flaky tests M occurs in) / (number of total tests M occurs in)
      Parameters:
      flakySequences - the flaky test sequences
      sequences - all the sequences (flaky and non-flaky)
      sideEffectFreeMethodsByType - side-effect-free methods to use in assertions
      omitMethodsPredicate - the user-supplied predicate for which methods should not be used during test generation
      accessibilityPredicate - accessibility predicate for side-effect-free methods
    • testNamesToSequences

      private List<ExecutableSequence> testNamesToSequences(Iterable<String> testNames, List<ExecutableSequence> sequences)
      Given a collection of test names of the form "test005", returns the corresponding elements from the given list.
      Parameters:
      testNames - names of the form "test005"
      sequences - test sequences (error or regression), numbered sequentially
      Returns:
      the sequences corresponding to the test names
    • countSequencesPerOperation

      private Map<TypedClassOperation,Integer> countSequencesPerOperation(List<ExecutableSequence> sequences, MultiMap<Type,TypedClassOperation> assertableSideEffectFreeMethods)
      Counts the number of sequences each operation occurs in.
      Parameters:
      sequences - a list of sequences
      assertableSideEffectFreeMethods - a map from a type to all its side-effect-free methods that can be used in assertions
      Returns:
      a map from operation to the number of sequences in which the operation occurs at least once
    • getOperationsInSequence

      private Set<TypedClassOperation> getOperationsInSequence(ExecutableSequence es)
      Constructs a set of method-call operations appearing in an Executable Sequence. Non-method-call operations are excluded.
      Parameters:
      es - an ExecutableSequence
      Returns:
      the set of method call operations in es
    • convertClasspathToAbsolute

      private String convertClasspathToAbsolute(String classpath)
      Convert each element of the given classpath from a relative to an absolute path.
      Parameters:
      classpath - the classpath to replace
      Returns:
      a version of classpath with relative paths replaced by absolute paths
    • writeTestFiles

      private void writeTestFiles(JUnitCreator junitCreator, List<ExecutableSequence> testSequences, CodeWriter codeWriter, String classNamePrefix, String testKind)
      Creates the test classes for the test sequences using the JUnitCreator and then writes the files using the CodeWriter. Writes the test suite if GenInputsAbstract.junit_reflection_allowed is true, or the test driver, otherwise.

      Class names are numbered with classNamePrefix as the prefix. The package for tests is GenInputsAbstract.junit_package_name.

      Parameters:
      junitCreator - the JUnitCreator to create the test class source
      testSequences - a list of ExecutableSequence objects for test methods
      codeWriter - the CodeWriter to output the test classes
      classNamePrefix - the prefix for the class name
      testKind - a String indicating the kind of tests for logging and error messages
    • getFixtureCode

      private boolean getFixtureCode()
      Returns:
      true if all fixtures were read without error, false, otherwise
    • readPatterns

      private List<Pattern> readPatterns(Path file)
      Returns patterns read from the given user-provided file.
      Parameters:
      file - the file to read from, may be null (in which case this returns an empty list)
      Returns:
      contents of the file, as a list of Patterns
    • readPatternsFromResource

      private List<Pattern> readPatternsFromResource(String filename)
      Returns patterns read from the given resource.
      Parameters:
      filename - the resource from which to read
      Returns:
      contents of the resource, as a list of Patterns
    • readPatterns

      private List<Pattern> readPatterns(InputStream is, String filename)
      Returns patterns read from the given stream.
      Parameters:
      is - the stream from which to read
      filename - the file name to use in diagnostic messages
      Returns:
      contents of the file, as a list of Patterns
    • readPatterns

      private List<Pattern> readPatterns(org.plumelib.util.EntryReader er)
      Returns patterns read from the given EntryReader.
      Parameters:
      er - the EntryReader to read from
      Returns:
      contents of the file, as a list of Patterns
    • createPatternsFromSignatures

      private List<Pattern> createPatternsFromSignatures(List<String> signatures)
      Creates a list of signature strings (see RawSignature.toString() to a list of Pattern.
      Parameters:
      signatures - the list of signature strings
      Returns:
      the list of patterns for the signature strings
    • signatureToPattern

      private static Pattern signatureToPattern(String signatureString)
      Converts a signature string (see RawSignature.toString() to a Pattern that matches that string.
      Parameters:
      signatureString - the string representation of a signature
      Returns:
      the pattern to match signatureString
    • printSequenceExceptionError

      private void printSequenceExceptionError(AbstractGenerator explorer, SequenceExceptionError e)
      Prints information about a SequenceExceptionError that indicates a flaky test has been found. Prints information to help user identify source of flakiness, including exception, statement that threw the exception, the full sequence where exception was thrown, and the input subsequence.
      Parameters:
      explorer - the test generator
      e - the sequence exception
    • createTestOutputPredicate

      public Predicate<ExecutableSequence> createTestOutputPredicate(Set<Sequence> excludeSet, Set<Class<?>> coveredClasses, Pattern includePattern)
      Builds the test predicate that determines whether a particular sequence will be included in the output based on command-line arguments. A true result means the test is a candidate for output.
      Parameters:
      excludeSet - the set of sequences to exclude
      coveredClasses - the list of classes to test for coverage
      includePattern - the pattern for method name inclusion
      Returns:
      the predicate
    • createTestCheckGenerator

      public static TestCheckGenerator createTestCheckGenerator(AccessibilityPredicate accessibility, ContractSet contracts, MultiMap<Type,TypedClassOperation> sideEffectFreeMethodsByType, OmitMethodsPredicate omitMethodsPredicate)
      Creates the test check generator for this run based on the command-line arguments. The goal of the generator is to produce all appropriate checks for each sequence it is applied to.

      The generator always contains validity and contract checks. If regression tests are to be generated, it also contains the regression checks generator.

      Parameters:
      accessibility - the accessibility predicate
      contracts - the contract checks
      sideEffectFreeMethodsByType - the map from types to side-effect-free methods
      omitMethodsPredicate - the user-supplied predicate for which methods should not be used during test generation
      Returns:
      the TestCheckGenerator that reflects command line arguments
    • usage

      private static void usage(String format, Object... args)
      Print message, then print usage information, then exit.
      Parameters:
      format - the string format
      args - the arguments
    • getFileText

      private static List<String> getFileText(String filename)
      Return the text of the given file, as a list of lines. Returns null if the filename argument is null. Terminates execution if the filename file cannot be read.
      Parameters:
      filename - the file to read
      Returns:
      the contents of filename, as a list of strings
    • getJDKSpecificationFiles

      private Collection<? extends Path> getJDKSpecificationFiles()
      Returns the list of JDK specification files from the specifications/jdk resources directory in the Randoop jar file.
      Returns:
      the list of JDK specification files
      Throws:
      RandoopBug - if there is an error locating the specification files
    • getResourceDirectoryPath

      private Path getResourceDirectoryPath(String resourceDirectory)
      Returns the path for the resource directory in the jar file.
      Parameters:
      resourceDirectory - the resource directory relative to the root of the jar file, should start with "/"
      Returns:
      the Path for the resource directory
      Throws:
      RandoopBug - if an error occurs when locating the directory
    • incrementSequenceCompileFailureCount

      public void incrementSequenceCompileFailureCount()
      Increments the count of sequence compilation failures.