Class CodeJavaOrKotlinGenerator
java.lang.Object
org.evomaster.client.java.controller.problem.rpc.CodeJavaOrKotlinGenerator
a set of util for generating instance creation and assertion with java in tests
in order to make endpoint invocation
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddComment(List<String> codes, String comment, int indent) static StringcastToType(String typeName, String objCode, boolean isJava) cast object to a typestatic StringcodeBlockEnd(boolean isJava) static StringcodeBlockStart(boolean isJava) static Stringstatic StringfieldAccess(String obj, String filedName, boolean isJava, boolean objIsNullable, boolean isNonNullAsserted) static StringgetGetClientMethod(String controllerVarName, String interfaceName) process the code to get RPC clientstatic StringgetIndent(int indent) static StringgetStatementLast(boolean isJava) handle last of statement for Java or kotlingetStringListWithIndent(List<String> codes, int indent) static StringhandleClassNameWithGeneric(String fullName, List<String> genericTypes) representation of class with genericstatic StringhandleEscapeCharInString(String orgValue, boolean isJava) handle escape char in string in java https://docs.oracle.com/javase/tutorial/java/data/characters.htmlstatic StringhandleVariableName(String original) some variable name might contain invalid symbol such as ., then need to reformat themstatic StringjunitAssertEquals(String expectedValue, String variableName, boolean isJava) static StringjunitAssertNull(String variableName, boolean isJava) static StringjunitAssertNumbersMatch(String expectedValue, String variableName, boolean isJava) junit does not support equal assertions for double type, then employ numbersMatch herestatic StringmethodInvocation(String obj, String methodName, String params, boolean isJava, boolean objIsNullable, boolean isNotNullAsserted) static Stringstatic StringnewBuilderProto3(String fullName, String varBuilderName, boolean isJava) static Stringstatic Stringstatic StringnewObjectConsParams(String fullName, String params, boolean isJava) process new Object(p1, p2)static Stringstatic StringoneLineInstance(boolean isDeclaration, boolean doesIncludeName, String fullName, String varName, String value, boolean isJava, boolean isNullable) create an instance with one line eg, fullName varName = value;static StringoneLineInstance(boolean isDeclaration, boolean doesIncludeName, String fullName, String varName, String value, Boolean isPrimitive, boolean isJava, boolean isNullable) create an instance with one line eg, fullName varName = value;static StringoneLineSetterInstance(String setterMethodName, String fullName, String varName, String value, boolean isJava, boolean isNullable) set instance with setter eg, varName.setterMethodName((fullName)value)randomNInt(int max, int n) select n int from a range [0, max) Note that this is only used for deciding a number of data in collection to be asserted then it should not have any side-effect to randomness of searchstatic StringsetInstance(boolean includeVarName, String varName, String instance, boolean isJava) process [varName] = [instance]; or [instance];static StringsetInstance(String varName, String instance, boolean isJava) process [varName] = [instance]static StringsetInstanceObject(String fullName, String varName, boolean isJava) process [varName] = new Object()static StringtypeNameOfArrayOrCollection(String collectionType, boolean isArray, String genericType, boolean isJava) static StringwithLengthInAssertion(String variableName, boolean isJava, boolean isNullable) static StringwithSizeInAssertion(String variableName, boolean isJava, boolean isNullable)
-
Constructor Details
-
CodeJavaOrKotlinGenerator
public CodeJavaOrKotlinGenerator()
-
-
Method Details
-
handleClassNameWithGeneric
representation of class with generic -
handleEscapeCharInString
handle escape char in string in java https://docs.oracle.com/javase/tutorial/java/data/characters.html- Parameters:
orgValue- is an original stringisJava-- Returns:
- string with handled escape char
-
typeNameOfArrayOrCollection
-
enumValue
- Parameters:
enumTypeName- the enum nameitemName- the item name of the enum- Returns:
- a string which could retrieve the item of a Enum type, eg, Gender.Female
-
oneLineInstance
public static String oneLineInstance(boolean isDeclaration, boolean doesIncludeName, String fullName, String varName, String value, boolean isJava, boolean isNullable) create an instance with one line eg, fullName varName = value;- Parameters:
isDeclaration- whether the instance is also for declarationdoesIncludeName- whether to include variable namefullName- is the full name of the variablevarName- is the variable namevalue- is string to create the instanceisJava-isNullable-- Returns:
- a string which could create the instance
-
oneLineInstance
public static String oneLineInstance(boolean isDeclaration, boolean doesIncludeName, String fullName, String varName, String value, Boolean isPrimitive, boolean isJava, boolean isNullable) create an instance with one line eg, fullName varName = value;- Parameters:
isDeclaration- whether the instance is also for declarationdoesIncludeName- whether to include variable namefullName- is the full name of the variablevarName- is the variable namevalue- is string to create the instanceisPrimitive- indicates whether it is primitive typeisJava-isNullable-- Returns:
- a string which could create the instance
-
oneLineSetterInstance
public static String oneLineSetterInstance(String setterMethodName, String fullName, String varName, String value, boolean isJava, boolean isNullable) set instance with setter eg, varName.setterMethodName((fullName)value)- Parameters:
setterMethodName- is the setter method namefullName- is full name of the instancevarName- is variable namevalue- of the instanceisJava-isNullable-- Returns:
- a string which set instance
-
setInstanceObject
process [varName] = new Object()- Parameters:
fullName- specifies the full name of the classvarName- specifies the variable nameisJava-- Returns:
- code to set value with new object which has default constructor
-
getStatementLast
handle last of statement for Java or kotlin- Returns:
- eg, a string which appends ; at last for Java
-
newBuilderProto3
- Parameters:
fullName- is the full name of the dtovarBuilderName- is the variable name for the builderisJava-- Returns:
- code to instantiate builder for proto3 dto
-
setInstance
process [varName] = [instance]- Parameters:
varName- specifies the variable nameinstance- specifies the instanceisJava-- Returns:
- code to set value
-
setInstance
public static String setInstance(boolean includeVarName, String varName, String instance, boolean isJava) process [varName] = [instance]; or [instance];- Parameters:
includeVarName- specifies whether to set instance to variablevarName- specifies the variable nameinstance- specifies the instanceisJava-- Returns:
- code to set value
-
newObjectConsParams
process new Object(p1, p2)- Parameters:
fullName- is a full name of the type of objectparams- is a string which could represent a list of params divided with ,isJava-- Returns:
- code to new object with params constructor
-
newArray
- Parameters:
fullName- is full name of the typelength- is length of array to newisJava-- Returns:
- a string which could create a new array, eg, new String[5]
-
newSet
- Returns:
- a string which could create a new HashSet
-
newMap
- Returns:
- a string which could create a new HashMap
-
newList
- Returns:
- a string which could create a new ArrayList
-
getIndent
- Parameters:
indent- is a number of indent (here we use space)- Returns:
- a string which contains [indent] space
-
getStringListWithIndent
- Parameters:
codes- a list of codes, could be a blockindent- is a number of indent for the codes- Returns:
- a list of indented codes
-
addCode
- Parameters:
codes- is a list of current codecode- is one line code to be appended to the [codes]indent- is a number of indent for the [code]- Returns:
- a list of codes appended with the [code]
-
codeBlockStart
-
codeBlockEnd
-
addComment
- Parameters:
codes- is a list of current codecomment- is one line commentindent- is a number of indent for the [code]- Returns:
- a list of codes with comment
-
castToType
cast object to a type- Parameters:
typeName- to castobjCode- is the code representing object to castisJava-- Returns:
- a java code which casts obj to a type
-
getGetClientMethod
process the code to get RPC client- Parameters:
controllerVarName- specifies the controller variable nameinterfaceName- specifies the interface name to get its corresponding client- Returns:
- code which enables getting RPC client
-
methodInvocation
public static String methodInvocation(String obj, String methodName, String params, boolean isJava, boolean objIsNullable, boolean isNotNullAsserted) - Parameters:
obj- specifies an object which owns the method. it is nullablemethodName- specifies a name of the methodparams- specifies a list of paramsisJava-objIsNullable-isNotNullAsserted-- Returns:
- code to invoke the method
-
fieldAccess
-
junitAssertEquals
- Parameters:
expectedValue- is the excepted valuevariableName- is the variable name to be checkedisJava-- Returns:
- an assertion for equal with junit
-
junitAssertNull
- Parameters:
variableName- is the variable name to be checkedisJava-- Returns:
- a null assertion with junit
-
withSizeInAssertion
- Parameters:
variableName- is the variable which has size() methodisJava-isNullable-- Returns:
- a string to get size() of the variable
-
withLengthInAssertion
- Parameters:
variableName- is the variable which has length fieldisJava-isNullable-- Returns:
- a string to get length of the variable
-
junitAssertNumbersMatch
public static String junitAssertNumbersMatch(String expectedValue, String variableName, boolean isJava) junit does not support equal assertions for double type, then employ numbersMatch here- Parameters:
expectedValue- is the excepted valuevariableName- is the variable name to be checkedisJava-- Returns:
- a True assertion with numbersMatch method.
-
handleVariableName
some variable name might contain invalid symbol such as ., then need to reformat them- Parameters:
original- is the original name of the variable- Returns:
- a reformatted variable name
-
randomNInt
select n int from a range [0, max) Note that this is only used for deciding a number of data in collection to be asserted then it should not have any side-effect to randomness of search- Parameters:
max-n-- Returns:
-