Package randoop.reflection
Class OperationModel
- java.lang.Object
-
- randoop.reflection.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 Summary
Fields Modifier and Type Field Description private Set<Sequence>
annotatedTestValues
Set of singleton sequences for values from TestValue annotated fields.private MultiMap<ClassOrInterfaceType,Sequence>
classLiteralMap
Map for singleton sequences of literals extracted from classes.private Set<ClassOrInterfaceType>
classTypes
The set of class declaration types for this model.private ContractSet
contracts
Set of object contracts used to generate tests.private LinkedHashSet<Class<?>>
coveredClassesGoal
The set of classes used as goals in the covered-class test filter.private Set<Type>
inputTypes
The set of input types for this model.private List<Pattern>
omitMethods
For debugging only.private OmitMethodsPredicate
omitMethodsPredicate
User-supplied predicate for methods that should not be used during test generation.private Set<TypedOperation>
operations
Set of concrete operations extracted from classes.
-
Constructor Summary
Constructors Modifier Constructor Description private
OperationModel()
Create an empty model of test context.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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 thisOperationModel
.private void
addObjectConstructor()
Creates and adds the Object class default constructor call to the concrete operations.private void
addOperationsFromClasses(AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate, SpecificationCollection operationSpecifications)
Adds operations to thisOperationModel
from all of the classes ofclassTypes
.static OperationModel
createModel(AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate, List<Pattern> omitMethods, Set<@ClassGetName String> classnames, Set<@ClassGetName String> coveredClassnames, ClassNameErrorHandler errorHandler, List<String> literalsFileList)
Factory method to construct an operation model for a particular set of classes without behavior specifications.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)
Factory method to construct an operation model for a particular set of classes.(package private) static OperationModel
createModel(AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate, Set<@ClassGetName String> classnames, Set<@ClassGetName String> coveredClassnames, ClassNameErrorHandler errorHandler, List<String> literalsFileList)
Factory method to construct an operation model for a particular set of classes without an omit-methods list or behavior specifications.void
dumpModel()
Print a verbose representation of the model, if logging is enabled.void
dumpModel(PrintStream out)
Print a verbose representation of the model toout
.void
dumpModel(Writer out)
Print a verbose representation of the model toout
.Set<Sequence>
getAnnotatedTestValues()
Returns the set of singleton sequences for values from@TestValue
annotated fields.private static @Nullable Class<?>
getClass(@ClassGetName String classname, ClassNameErrorHandler errorHandler)
Returns the class whose name isclassname
.Set<ClassOrInterfaceType>
getClassTypes()
Returns the set of types for classes under test.ContractSet
getContracts()
Returns allObjectContract
objects for this run of Randoop.Set<Class<?>>
getCoveredClassesGoal()
Returns the set ofClass<?>
objects that are the goals for the covered class heuristic.Set<Type>
getInputTypes()
Returns the set of input types that occur as parameters in classes under test.OmitMethodsPredicate
getOmitMethodsPredicate()
Returns the user-specified predicate for methods that should not be called.List<TypedOperation>
getOperations()
Return the operations of this model as a list.private List<TypedClassOperation>
getOperationsFromFile(Path methodSignatures_file, AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate)
Constructs an operation from every method signature in the given file.void
log()
void
logOperations(PrintStream out)
Output the operations of this model toout
, if logging is enabled.void
logOperations(Writer out)
Output the operations of this model, if logging is enabled.static MultiMap<Type,TypedClassOperation>
readOperations(@Nullable Path file)
Given a file containing fully-qualified method signatures, returns the operations for them.static MultiMap<Type,TypedClassOperation>
readOperations(@Nullable Path file, boolean ignoreParseError)
Given a file containing fully-qualified method signatures, returns the operations for them.static MultiMap<Type,TypedClassOperation>
readOperations(InputStream is, String filename)
Returns operations read from the given stream, which contains fully-qualified method signatures.static MultiMap<Type,TypedClassOperation>
readOperations(InputStream is, String filename, boolean ignoreParseError)
Returns operations read from the given stream, which contains fully-qualified method signatures.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.static TypedClassOperation
signatureToOperation(String signature, AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate)
Given a signature, returns the method or constructor it represents.
-
-
-
Field Detail
-
classTypes
private Set<ClassOrInterfaceType> classTypes
The set of class declaration types for this model.
-
coveredClassesGoal
private final LinkedHashSet<Class<?>> coveredClassesGoal
The set of classes used as goals in the covered-class test filter.
-
classLiteralMap
private MultiMap<ClassOrInterfaceType,Sequence> classLiteralMap
Map for singleton sequences of literals extracted from classes.
-
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.
-
omitMethodsPredicate
private OmitMethodsPredicate omitMethodsPredicate
User-supplied predicate for methods that should not be used during test generation.
-
-
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
- theAccessibilityPredicate
to test accessibility of classes and class membersreflectionPredicate
- the reflection predicate to determine which classes and class members are usedomitMethods
- the patterns for operations that should be omittedclassnames
- the names of classes under testcoveredClassesGoalNames
- the coverage goal: the names of classes to be tested by the covered class heuristicerrorHandler
- the handler for bad file name errorsliteralsFileList
- the list of literals file namesoperationSpecifications
- the collection of operation specifications- Returns:
- the
OperationModel
constructed with the given arguments - Throws:
SignatureParseException
- if a method signature is ill-formedNoSuchMethodException
- 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
- theAccessibilityPredicate
to test accessibility of classes and class membersreflectionPredicate
- the reflection predicate to determine which classes and class members are usedclassnames
- the names of classes under testcoveredClassnames
- the names of classes to be tested by exercised heuristicerrorHandler
- the handler for bad file name errorsliteralsFileList
- the list of literals file names- Returns:
- the operation model for the parameters
- Throws:
SignatureParseException
- if a method signature is ill-formedNoSuchMethodException
- 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
- theAccessibilityPredicate
to test accessibility of classes and class membersreflectionPredicate
- the reflection predicate to determine which classes and class members are usedomitMethods
- the patterns for operations that should be omittedclassnames
- the names of classes under testcoveredClassnames
- the names of classes to be tested by covered class heuristicerrorHandler
- the handler for bad file name errorsliteralsFileList
- the list of literals file names- Returns:
- the
OperationModel
constructed with the given arguments - Throws:
SignatureParseException
- if a method signature is ill-formedNoSuchMethodException
- 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 byGenInputsAbstract.ClassLiteralsMode
.- Parameters:
compMgr
- the component managerliteralsFileList
- the list of literals file namesliteralsLevel
- 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 fromignoreParseError
- 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 readfilename
- 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 readfilename
- the file name to use in diagnostic messagesignoreParseError
- 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 ofClass<?>
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 allObjectContract
objects for this run of Randoop. Includes Randoop defaults andCheckRep
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 toout
, 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 toout
.- Parameters:
out
- the PrintStream on which to produce output
-
dumpModel
public void dumpModel(Writer out)
Print a verbose representation of the model toout
.- 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 thisOperationModel
. 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 toClass
objects. Also collects annotated test values, and class literal values used in test generation.- Parameters:
accessibility
- the accessibility predicatereflectionPredicate
- the predicate to determine which reflection objects are usedclassnames
- the names of classes-under-testcoveredClassesGoalNames
- the names of classes used as goals in the covered-class heuristicerrorHandler
- the handler for bad class namesliteralsFileList
- the list of literals file names
-
getClass
private static @Nullable Class<?> getClass(@ClassGetName String classname, ClassNameErrorHandler errorHandler)
Returns the class whose name isclassname
. A wrapper around Class.forName.- Parameters:
classname
- the name of a class or primitive typeerrorHandler
- is called if no such class exists- Returns:
- the Class whose name is
classname
. May return null iferrorHandler
just warns on bad names.
-
addOperationsFromClasses
private void addOperationsFromClasses(AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate, SpecificationCollection operationSpecifications)
Adds operations to thisOperationModel
from all of the classes ofclassTypes
.- Parameters:
accessibility
- the accessibility predicatereflectionPredicate
- the reflection predicateoperationSpecifications
- the collection ofOperationSpecification
-
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 listaccessibility
- the accessibility predicatereflectionPredicate
- the reflection predicate- Returns:
- operations read from the file
- Throws:
SignatureParseException
- if any signature is syntactically invalid
-
signatureToOperation
public static TypedClassOperation signatureToOperation(String signature, AccessibilityPredicate accessibility, ReflectionPredicate reflectionPredicate) throws SignatureParseException, FailedPredicateException
Given a signature, returns the method or constructor it represents.- Parameters:
signature
- the operation's signature, in Randoop's formataccessibility
- the accessibility predicatereflectionPredicate
- the reflection predicate- Returns:
- the method or constructor that the signature represents
- Throws:
FailedPredicateException
- if the accessibility or reflection predicate returns false on the class or the method or constructorSignatureParseException
- if the signature cannot be parsed
-
addObjectConstructor
private void addObjectConstructor()
Creates and adds the Object class default constructor call to the concrete operations.
-
-