Class OperationModel


  • public class OperationModel
    extends Object
    OperationModel represents the information context from which tests are generated. The model includes:
    • classes under test,
    • operations of all classes,
    • any atomic code sequences derived from command-line arguments, and
    • the contracts or oracles used to generate tests.

    This class manages all information about generic classes internally, and instantiates any type variables in operations before returning them.

    • Field Detail

      • inputTypes

        private Set<Type> inputTypes
        The set of input types for this model.
      • coveredClassesGoal

        private final LinkedHashSet<Class<?>> coveredClassesGoal
        The set of classes used as goals in the covered-class test filter.
      • annotatedTestValues

        private Set<Sequence> annotatedTestValues
        Set of singleton sequences for values from TestValue annotated fields.
      • contracts

        private ContractSet contracts
        Set of object contracts used to generate tests.
      • operations

        private final Set<TypedOperation> operations
        Set of concrete operations extracted from classes.
      • omitMethods

        private List<Pattern> omitMethods
        For debugging only.
      • omitMethodsPredicate

        private OmitMethodsPredicate omitMethodsPredicate
        User-supplied predicate for methods that should not be used during test generation.
    • Constructor Detail

      • OperationModel

        private OperationModel()
        Create an empty model of test context.
    • Method Detail

      • createModel

        public static OperationModel createModel​(AccessibilityPredicate accessibility,
                                                 ReflectionPredicate reflectionPredicate,
                                                 List<Pattern> omitMethods,
                                                 Set<@ClassGetName String> classnames,
                                                 Set<@ClassGetName String> coveredClassesGoalNames,
                                                 ClassNameErrorHandler errorHandler,
                                                 List<String> literalsFileList,
                                                 SpecificationCollection operationSpecifications)
                                          throws SignatureParseException,
                                                 NoSuchMethodException
        Factory method to construct an operation model for a particular set of classes.
        Parameters:
        accessibility - the AccessibilityPredicate to test accessibility of classes and class members
        reflectionPredicate - the reflection predicate to determine which classes and class members are used
        omitMethods - the patterns for operations that should be omitted
        classnames - the names of classes under test
        coveredClassesGoalNames - the coverage goal: the names of classes to be tested by the covered class heuristic
        errorHandler - the handler for bad file name errors
        literalsFileList - the list of literals file names
        operationSpecifications - the collection of operation specifications
        Returns:
        the OperationModel constructed with the given arguments
        Throws:
        SignatureParseException - if a method signature is ill-formed
        NoSuchMethodException - if an attempt is made to load a non-existent method
      • createModel

        static OperationModel createModel​(AccessibilityPredicate accessibility,
                                          ReflectionPredicate reflectionPredicate,
                                          Set<@ClassGetName String> classnames,
                                          Set<@ClassGetName String> coveredClassnames,
                                          ClassNameErrorHandler errorHandler,
                                          List<String> literalsFileList)
                                   throws NoSuchMethodException,
                                          SignatureParseException
        Factory method to construct an operation model for a particular set of classes without an omit-methods list or behavior specifications.
        Parameters:
        accessibility - the AccessibilityPredicate to test accessibility of classes and class members
        reflectionPredicate - the reflection predicate to determine which classes and class members are used
        classnames - the names of classes under test
        coveredClassnames - the names of classes to be tested by exercised heuristic
        errorHandler - the handler for bad file name errors
        literalsFileList - the list of literals file names
        Returns:
        the operation model for the parameters
        Throws:
        SignatureParseException - if a method signature is ill-formed
        NoSuchMethodException - if an attempt is made to load a non-existent method
      • createModel

        public static OperationModel createModel​(AccessibilityPredicate accessibility,
                                                 ReflectionPredicate reflectionPredicate,
                                                 List<Pattern> omitMethods,
                                                 Set<@ClassGetName String> classnames,
                                                 Set<@ClassGetName String> coveredClassnames,
                                                 ClassNameErrorHandler errorHandler,
                                                 List<String> literalsFileList)
                                          throws NoSuchMethodException,
                                                 SignatureParseException
        Factory method to construct an operation model for a particular set of classes without behavior specifications.
        Parameters:
        accessibility - the AccessibilityPredicate to test accessibility of classes and class members
        reflectionPredicate - the reflection predicate to determine which classes and class members are used
        omitMethods - the patterns for operations that should be omitted
        classnames - the names of classes under test
        coveredClassnames - the names of classes to be tested by covered class heuristic
        errorHandler - the handler for bad file name errors
        literalsFileList - the list of literals file names
        Returns:
        the OperationModel constructed with the given arguments
        Throws:
        SignatureParseException - if a method signature is ill-formed
        NoSuchMethodException - if an attempt is made to load a non-existent method
      • addClassLiterals

        public void addClassLiterals​(ComponentManager compMgr,
                                     List<String> literalsFileList,
                                     GenInputsAbstract.ClassLiteralsMode literalsLevel)
        Adds literals to the component manager, by parsing any literals files specified by the user. Includes literals at different levels indicated by GenInputsAbstract.ClassLiteralsMode.
        Parameters:
        compMgr - the component manager
        literalsFileList - the list of literals file names
        literalsLevel - the level of literals to add
      • readOperations

        public static MultiMap<Type,​TypedClassOperation> readOperations​(@Nullable Path file)
                                                                       throws OperationParseException
        Given a file containing fully-qualified method signatures, returns the operations for them.
        Parameters:
        file - a file that contains method or constructor signatures, one per line. If null, this method returns an empty map.
        Returns:
        a map from each class type to its methods and constructors that were read from the file
        Throws:
        OperationParseException - if a method signature cannot be parsed
      • readOperations

        public static MultiMap<Type,​TypedClassOperation> readOperations​(@Nullable Path file,
                                                                              boolean ignoreParseError)
                                                                       throws OperationParseException
        Given a file containing fully-qualified method signatures, returns the operations for them.
        Parameters:
        file - a file that contains method or constructor signatures, one per line. If null, this method returns an empty map.
        ignoreParseError - if true, ignore parse errors (skip malformed signatures)
        Returns:
        a map from each class type to its methods and constructors that were read from the file
        Throws:
        OperationParseException - if a method signature cannot be parsed
      • readOperations

        private static MultiMap<Type,​TypedClassOperation> readOperations​(org.plumelib.util.EntryReader er,
                                                                               boolean ignoreParseError)
        Returns operations read from the given EntryReader, which contains fully-qualified method signatures.
        Parameters:
        er - the EntryReader to read from
        ignoreParseError - if true, ignore parse errors (skip malformed signatures)
        Returns:
        contents of the file, as a map from classes to operations
      • readOperations

        public static MultiMap<Type,​TypedClassOperation> readOperations​(InputStream is,
                                                                              String filename)
        Returns operations read from the given stream, which contains fully-qualified method signatures.
        Parameters:
        is - the stream from which to read
        filename - the file name to use in diagnostic messages
        Returns:
        contents of the file, as a map from classes to operations
      • readOperations

        public static MultiMap<Type,​TypedClassOperation> readOperations​(InputStream is,
                                                                              String filename,
                                                                              boolean ignoreParseError)
        Returns operations read from the given stream, which contains fully-qualified method signatures.
        Parameters:
        is - the stream from which to read
        filename - the file name to use in diagnostic messages
        ignoreParseError - if true, ignore parse errors (skip malformed signatures)
        Returns:
        contents of the file, as a map from classes to operations
      • getClassTypes

        public Set<ClassOrInterfaceType> getClassTypes()
        Returns the set of types for classes under test.
        Returns:
        the set of class types
      • getCoveredClassesGoal

        public Set<Class<?>> getCoveredClassesGoal()
        Returns the set of Class<?> objects that are the goals for the covered class heuristic.
        Returns:
        the set of covered classes
      • getInputTypes

        public Set<Type> getInputTypes()
        Returns the set of input types that occur as parameters in classes under test.
        Returns:
        the set of input types that occur in classes under test
        See Also:
        TypeExtractor
      • getOperations

        public List<TypedOperation> getOperations()
        Return the operations of this model as a list.
        Returns:
        the operations of this model
      • getContracts

        public ContractSet getContracts()
        Returns all ObjectContract objects for this run of Randoop. Includes Randoop defaults and CheckRep annotated methods.
        Returns:
        the list of contracts
      • getOmitMethodsPredicate

        public OmitMethodsPredicate getOmitMethodsPredicate()
        Returns the user-specified predicate for methods that should not be called.
        Returns:
        the user-specified predicate for methods that should not be called
      • getAnnotatedTestValues

        public Set<Sequence> getAnnotatedTestValues()
        Returns the set of singleton sequences for values from @TestValue annotated fields.
        Returns:
        sequences that get fields annotated with @TestValue
      • log

        public void log()
      • logOperations

        public void logOperations​(PrintStream out)
        Output the operations of this model to out, if logging is enabled.
        Parameters:
        out - the PrintStream on which to produce output
      • logOperations

        public void logOperations​(Writer out)
        Output the operations of this model, if logging is enabled.
        Parameters:
        out - the Writer on which to produce output
      • dumpModel

        public void dumpModel()
        Print a verbose representation of the model, if logging is enabled.
      • dumpModel

        public void dumpModel​(PrintStream out)
        Print a verbose representation of the model to out.
        Parameters:
        out - the PrintStream on which to produce output
      • dumpModel

        public void dumpModel​(Writer out)
        Print a verbose representation of the model to out.
        Parameters:
        out - the Writer on which to produce output
      • addClassTypes

        private void addClassTypes​(AccessibilityPredicate accessibility,
                                   ReflectionPredicate reflectionPredicate,
                                   Set<@ClassGetName String> classnames,
                                   Set<@ClassGetName String> coveredClassesGoalNames,
                                   ClassNameErrorHandler errorHandler,
                                   List<String> literalsFileList)
        Gathers class types to be used in a run of Randoop and adds them to this OperationModel. Specifically, collects types for classes-under-test, objects for covered-class heuristic, concrete input types, annotated test values, and literal values. It operates by converting from strings to Class objects. Also collects annotated test values, and class literal values used in test generation.
        Parameters:
        accessibility - the accessibility predicate
        reflectionPredicate - the predicate to determine which reflection objects are used
        classnames - the names of classes-under-test
        coveredClassesGoalNames - the names of classes used as goals in the covered-class heuristic
        errorHandler - the handler for bad class names
        literalsFileList - the list of literals file names
      • getClass

        private static @Nullable Class<?> getClass​(@ClassGetName String classname,
                                                   ClassNameErrorHandler errorHandler)
        Returns the class whose name is classname. A wrapper around Class.forName.
        Parameters:
        classname - the name of a class or primitive type
        errorHandler - is called if no such class exists
        Returns:
        the Class whose name is classname. May return null if errorHandler just warns on bad names.
      • getOperationsFromFile

        private List<TypedClassOperation> getOperationsFromFile​(Path methodSignatures_file,
                                                                AccessibilityPredicate accessibility,
                                                                ReflectionPredicate reflectionPredicate)
                                                         throws SignatureParseException
        Constructs an operation from every method signature in the given file.
        Parameters:
        methodSignatures_file - the file containing the signatures; if null, return the emply list
        accessibility - the accessibility predicate
        reflectionPredicate - the reflection predicate
        Returns:
        operations read from the file
        Throws:
        SignatureParseException - if any signature is syntactically invalid
      • addObjectConstructor

        private void addObjectConstructor()
        Creates and adds the Object class default constructor call to the concrete operations.