use of org.evosuite.utils.generic.GenericAccessibleObject in project evosuite by EvoSuite.
the class TestFactory method insertRandomCallOnEnvironment.
/**
* @param test
* @param lastValidPosition
* @return the position where the insertion happened, or a negative value otherwise
*/
public int insertRandomCallOnEnvironment(TestCase test, int lastValidPosition) {
int previousLength = test.size();
currentRecursion.clear();
List<GenericAccessibleObject<?>> shuffledOptions = TestCluster.getInstance().getRandomizedCallsToEnvironment();
if (shuffledOptions == null || shuffledOptions.isEmpty()) {
return -1;
}
// iterate (in random order) over all possible environment methods till we find one that can be inserted
for (GenericAccessibleObject<?> o : shuffledOptions) {
try {
int position = ConstraintVerifier.getAValidPositionForInsertion(o, test, lastValidPosition);
if (position < 0) {
// the given method/constructor cannot be added
continue;
}
if (o.isConstructor()) {
GenericConstructor c = (GenericConstructor) o;
addConstructor(test, c, position, 0);
return position;
} else if (o.isMethod()) {
GenericMethod m = (GenericMethod) o;
if (!m.isStatic()) {
VariableReference callee = null;
Type target = m.getOwnerType();
if (!test.hasObject(target, position)) {
callee = createObject(test, target, position, 0, null);
position += test.size() - previousLength;
previousLength = test.size();
} else {
callee = test.getRandomNonNullObject(target, position);
}
if (!TestUsageChecker.canUse(m.getMethod(), callee.getVariableClass())) {
logger.error("Cannot call method " + m + " with callee of type " + callee.getClassName());
}
addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position);
return position;
} else {
addMethod(test, m, position, 0);
return position;
}
} else {
throw new RuntimeException("Unrecognized type for environment: " + o);
}
} catch (ConstructionFailedException e) {
// TODO what to do here?
AtMostOnceLogger.warn(logger, "Failed environment insertion: " + e);
}
}
return -1;
}
use of org.evosuite.utils.generic.GenericAccessibleObject in project evosuite by EvoSuite.
the class ConstraintHelper method getExcludedMethods.
/**
* Retrieve all blacklisted methods in the test.
* This is based on the 'excludeOthers' constraint
*
* @param tc
* @return
* @throws IllegalArgumentException
*/
public static List<String[]> getExcludedMethods(TestCase tc) throws IllegalArgumentException {
Inputs.checkNull(tc);
List<String[]> list = new ArrayList<>();
for (int i = 0; i < tc.size(); i++) {
Statement st = tc.getStatement(i);
Constraints constraints = getConstraints(st);
if (constraints == null) {
continue;
}
GenericAccessibleObject ao = st.getAccessibleObject();
Class<?> declaringClass = ao.getDeclaringClass();
for (String excluded : constraints.excludeOthers()) {
String[] klassAndMethod = getClassAndMethod(excluded, declaringClass);
list.add(klassAndMethod);
}
}
return list;
}
use of org.evosuite.utils.generic.GenericAccessibleObject in project evosuite by EvoSuite.
the class TestFinalPrimitiveFieldIsNotAddedToCluster method test.
/**
* As RESET_STATIC_FINAL_FIELDS=true removes the <code>final</code> modifier
* of static fields in the target class, the purpose of this test case is to
* check that the TestClusterGenerator indeed does not include these fields.
*
* @throws ClassNotFoundException
* @throws RuntimeException
*/
@Test
public void test() throws ClassNotFoundException, RuntimeException {
Properties.TARGET_CLASS = FinalPrimitiveField.class.getCanonicalName();
Properties.RESET_STATIC_FINAL_FIELDS = true;
ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
InheritanceTree tree = DependencyAnalysis.getInheritanceTree();
TestClusterGenerator gen = new TestClusterGenerator(tree);
assertNotNull(gen);
TestCluster cluster = TestCluster.getInstance();
List<GenericAccessibleObject<?>> testCalls = cluster.getTestCalls();
assertEquals("Unexpected number of TestCalls", 2, testCalls.size());
}
use of org.evosuite.utils.generic.GenericAccessibleObject in project evosuite by EvoSuite.
the class TestFinalReferenceFieldIsNotAddedToCluster method test.
/**
* As RESET_STATIC_FINAL_FIELDS=true removes the <code>final</code> modifier
* of static fields in the target class, the purpose of this test case is to
* check that the TestClusterGenerator indeed does not include these fields.
*
* @throws ClassNotFoundException
* @throws RuntimeException
*/
@Test
public void test() throws ClassNotFoundException, RuntimeException {
Properties.TARGET_CLASS = FinalReferenceField.class.getCanonicalName();
Properties.RESET_STATIC_FINAL_FIELDS = true;
ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
InheritanceTree tree = DependencyAnalysis.getInheritanceTree();
TestClusterGenerator gen = new TestClusterGenerator(tree);
assertNotNull(gen);
TestCluster cluster = TestCluster.getInstance();
List<GenericAccessibleObject<?>> testCalls = cluster.getTestCalls();
assertEquals("Unexpected number of TestCalls", 2, testCalls.size());
}
use of org.evosuite.utils.generic.GenericAccessibleObject in project evosuite by EvoSuite.
the class Archive method ignoreMethodCall.
/**
* Informs {@link org.evosuite.setup.TestCluster} that a particular method of a particular class
* has been fully covered, and therefore no need to generate any solution to cover any of its
* targets.
*
* @param className name of the class which contains the method that has been fully covered and
* can be ignored
* @param methodName name of the method that has been fully covered and can be ignored
*/
protected void ignoreMethodCall(String className, String methodName) {
TestCluster cluster = TestCluster.getInstance();
List<GenericAccessibleObject<?>> calls = cluster.getTestCalls();
for (GenericAccessibleObject<?> call : calls) {
if (!call.getDeclaringClass().getName().equals(className)) {
continue;
}
if (call instanceof GenericMethod) {
GenericMethod genericMethod = (GenericMethod) call;
if (!methodName.startsWith(genericMethod.getName())) {
continue;
}
String desc = Type.getMethodDescriptor(genericMethod.getMethod());
if ((genericMethod.getName() + desc).equals(methodName)) {
logger.info("Removing method " + methodName + " from cluster");
cluster.removeTestCall(call);
logger.info("Testcalls left: " + cluster.getNumTestCalls());
}
} else if (call instanceof GenericConstructor) {
GenericConstructor genericConstructor = (GenericConstructor) call;
if (!methodName.startsWith("<init>")) {
continue;
}
String desc = Type.getConstructorDescriptor(genericConstructor.getConstructor());
if (("<init>" + desc).equals(methodName)) {
logger.info("Removing constructor " + methodName + " from cluster");
cluster.removeTestCall(call);
logger.info("Testcalls left: " + cluster.getNumTestCalls());
}
}
}
}
Aggregations