Search in sources :

Example 6 with GenericAccessibleObject

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;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) CaptureType(com.googlecode.gentyref.CaptureType) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 7 with GenericAccessibleObject

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;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) Constraints(org.evosuite.runtime.annotation.Constraints) ArrayList(java.util.ArrayList)

Example 8 with GenericAccessibleObject

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());
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) InheritanceTree(org.evosuite.setup.InheritanceTree) TestCluster(org.evosuite.setup.TestCluster) TestClusterGenerator(org.evosuite.setup.TestClusterGenerator) FinalPrimitiveField(com.examples.with.different.packagename.clinit.FinalPrimitiveField) Test(org.junit.Test)

Example 9 with GenericAccessibleObject

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());
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) FinalReferenceField(com.examples.with.different.packagename.clinit.FinalReferenceField) InheritanceTree(org.evosuite.setup.InheritanceTree) TestCluster(org.evosuite.setup.TestCluster) TestClusterGenerator(org.evosuite.setup.TestClusterGenerator) Test(org.junit.Test)

Example 10 with GenericAccessibleObject

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());
            }
        }
    }
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) TestCluster(org.evosuite.setup.TestCluster) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Aggregations

GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)20 GenericClass (org.evosuite.utils.generic.GenericClass)14 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)8 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)7 CaptureType (com.googlecode.gentyref.CaptureType)3 Constructor (java.lang.reflect.Constructor)3 ArrayList (java.util.ArrayList)3 Entry (java.util.Map.Entry)3 Collectors (java.util.stream.Collectors)3 Properties (org.evosuite.Properties)3 TestGenerationContext (org.evosuite.TestGenerationContext)3 AtMostOnceLogger (org.evosuite.runtime.util.AtMostOnceLogger)3 Inputs (org.evosuite.runtime.util.Inputs)3 CastClassManager (org.evosuite.seeding.CastClassManager)3 TestCluster (org.evosuite.setup.TestCluster)3 InstanceOnlyOnce (org.evosuite.testcase.jee.InstanceOnlyOnce)3 Method (java.lang.reflect.Method)2 Type (java.lang.reflect.Type)2 java.util (java.util)2