Search in sources :

Example 66 with GenericClass

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;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) Statement(org.evosuite.testcase.statements.Statement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Example 67 with GenericClass

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());
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) ArrayList(java.util.ArrayList) List(java.util.List) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 68 with GenericClass

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);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Example 69 with GenericClass

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;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) MethodDescriptor(org.evosuite.testcase.fm.MethodDescriptor) Type(java.lang.reflect.Type) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 70 with GenericClass

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);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass)

Aggregations

GenericClass (org.evosuite.utils.generic.GenericClass)144 Test (org.junit.Test)64 GenericMethod (org.evosuite.utils.generic.GenericMethod)40 Method (java.lang.reflect.Method)36 VariableReference (org.evosuite.testcase.variable.VariableReference)25 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)24 WildcardType (java.lang.reflect.WildcardType)22 TypeToken (com.googlecode.gentyref.TypeToken)20 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)17 Type (java.lang.reflect.Type)16 ArrayList (java.util.ArrayList)16 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)16 MethodStatement (org.evosuite.testcase.statements.MethodStatement)15 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)15 ParameterizedType (java.lang.reflect.ParameterizedType)14 WildcardTypeImpl (org.evosuite.utils.generic.WildcardTypeImpl)10 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)9 AnnotatedType (java.lang.reflect.AnnotatedType)8 LinkedList (java.util.LinkedList)8 List (java.util.List)7