use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class Archive method hasFunctionalMocksForGenerableTypes.
private boolean hasFunctionalMocksForGenerableTypes(TestCase testCase) {
for (Statement statement : testCase) {
if (statement instanceof FunctionalMockStatement) {
FunctionalMockStatement fm = (FunctionalMockStatement) statement;
Class<?> target = fm.getTargetClass();
GenericClass gc = new GenericClass(target);
if (TestCluster.getInstance().hasGenerator(gc)) {
return true;
}
}
}
return false;
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class ClassPrimitiveStatement method readObject.
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
this.value = retval.getGenericClass().getRawClass();
List<GenericClass> newAssignableClasses = (List<GenericClass>) ois.readObject();
assignableClasses = new LinkedHashSet<Class<?>>();
for (GenericClass assignableClass : newAssignableClasses) {
assignableClasses.add(assignableClass.getRawClass());
}
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class FunctionalMockStatement method canBeFunctionalMocked.
public static boolean canBeFunctionalMocked(Type type) {
Class<?> rawClass = new GenericClass(type).getRawClass();
final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (Properties.hasTargetClassBeenLoaded() && GenericClass.isAssignable(targetClass, rawClass)) {
return false;
}
return canBeFunctionalMockedIncludingSUT(type);
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class FunctionalMockStatement method updateMockedMethods.
/**
* Based on most recent test execution, update the mocking configuration.
* After calling this method, it is <b>necessary</b> to provide the missing
* VariableReferences, if any, using addMissingInputs.
*
* @return a ordered, non-null list of types of missing new inputs that will need to be provided
*/
public List<Type> updateMockedMethods() throws ConstructionFailedException {
logger.debug("Executing updateMockedMethods. Parameter size: " + parameters.size());
List<Type> list = new ArrayList<>();
assert !super.parameters.contains(null);
assert mockedMethods.size() == methodParameters.size();
List<VariableReference> copy = new ArrayList<>(super.parameters);
assert copy.size() == super.parameters.size();
super.parameters.clear();
// important to remove all the no longer used calls
mockedMethods.clear();
Map<String, int[]> mpCopy = new LinkedHashMap<>();
List<MethodDescriptor> executed = listener.getCopyOfMethodDescriptors();
int mdIndex = 0;
for (MethodDescriptor md : executed) {
mockedMethods.add(md);
if (!md.shouldBeMocked() || md.getCounter() == 0) {
// void method or not called, so no parameter needed for it
mpCopy.put(md.getID(), null);
continue;
}
int added = 0;
logger.debug("Method called on mock object: " + md.getMethod());
// infer parameter mapping of current vars from previous execution, if any
int[] minMax = methodParameters.get(md.getID());
// total number of existing parameters
int existingParameters;
if (minMax == null) {
// before it was not called
minMax = new int[] { -1, -1 };
existingParameters = 0;
} else {
assert minMax[1] >= minMax[0] && minMax[0] >= 0;
assert minMax[1] < copy.size() : "Max=" + minMax[1] + " but n=" + copy.size();
existingParameters = 1 + (minMax[1] - minMax[0]);
}
assert existingParameters <= Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT;
// check if less calls
if (existingParameters > md.getCounter()) {
// now the method has been called less times,
// so remove the last calls, ie decrease counter
minMax[1] -= (existingParameters - md.getCounter());
}
if (existingParameters > 0) {
for (int i = minMax[0]; i <= minMax[1]; i++) {
// align super class data structure
super.parameters.add(copy.get(i));
added++;
}
}
// check if rather more calls
if (existingParameters < md.getCounter()) {
for (int i = existingParameters; i < md.getCounter() && i < Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT; i++) {
// Create a copy as the typemap is stored in the class during generic instantiation
// but we might want to have a different type for each call of the same method invocation
GenericClass calleeClass = new GenericClass(retval.getGenericClass());
Type returnType = md.getGenericMethodFor(calleeClass).getGeneratedType();
assert !returnType.equals(Void.TYPE);
logger.debug("Return type: " + returnType + " for retval " + retval.getGenericClass());
list.add(returnType);
// important place holder for following updates
super.parameters.add(null);
added++;
}
}
minMax[0] = mdIndex;
// max is inclusive
minMax[1] = (mdIndex + added - 1);
// max >= min
assert minMax[1] >= minMax[0] && minMax[0] >= 0;
assert super.parameters.size() == minMax[1] + 1;
mpCopy.put(md.getID(), minMax);
mdIndex += added;
}
methodParameters.clear();
methodParameters.putAll(mpCopy);
for (MethodDescriptor md : mockedMethods) {
if (!methodParameters.containsKey(md.getID())) {
methodParameters.put(md.getID(), null);
}
}
return list;
}
use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.
the class EnumPrimitiveStatement method writeObject.
private void writeObject(ObjectOutputStream oos) throws IOException {
GenericClass currentClass = new GenericClass(enumClass);
oos.writeObject(currentClass);
int pos = 0;
for (pos = 0; pos < constants.length; pos++) {
if (constants[pos].equals(value)) {
break;
}
}
oos.writeInt(pos);
}
Aggregations