Search in sources :

Example 61 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class FunctionalMockStatement method fillWithNullRefs.

public void fillWithNullRefs() {
    for (int i = 0; i < parameters.size(); i++) {
        VariableReference ref = parameters.get(i);
        if (ref == null) {
            Class<?> expected = getExpectedParameterType(i);
            Object value = null;
            if (expected.isPrimitive()) {
                // can't fill a primitive with null
                if (expected.equals(Integer.TYPE)) {
                    value = 0;
                } else if (expected.equals(Float.TYPE)) {
                    value = 0f;
                } else if (expected.equals(Double.TYPE)) {
                    value = 0d;
                } else if (expected.equals(Long.TYPE)) {
                    value = 0L;
                } else if (expected.equals(Boolean.TYPE)) {
                    value = false;
                } else if (expected.equals(Short.TYPE)) {
                    value = Short.valueOf("0");
                } else if (expected.equals(Character.TYPE)) {
                    value = 'a';
                }
            }
            parameters.set(i, new ConstantValue(tc, new GenericClass(expected), value));
        }
    }
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

Example 62 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class ConstantValue method changeClassLoader.

@Override
public void changeClassLoader(ClassLoader loader) {
    super.changeClassLoader(loader);
    if (value instanceof Class<?>) {
        GenericClass genericClass = new GenericClass((Class<?>) value);
        genericClass.changeClassLoader(loader);
        value = genericClass.getRawClass();
    }
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) GenericClass(org.evosuite.utils.generic.GenericClass)

Example 63 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class MethodDescriptor method executeMatcher.

public Object executeMatcher(int i) throws IllegalArgumentException {
    if (i < 0 || i >= getNumberOfInputParameters()) {
        throw new IllegalArgumentException("Invalid index: " + i);
    }
    Type[] types = method.getParameterTypes();
    Type type = types[i];
    try {
        if (type.equals(Integer.TYPE) || type.equals(Integer.class)) {
            return Mockito.anyInt();
        } else if (type.equals(Long.TYPE) || type.equals(Long.class)) {
            return Mockito.anyLong();
        } else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class)) {
            return Mockito.anyBoolean();
        } else if (type.equals(Double.TYPE) || type.equals(Double.class)) {
            return Mockito.anyDouble();
        } else if (type.equals(Float.TYPE) || type.equals(Float.class)) {
            return Mockito.anyFloat();
        } else if (type.equals(Short.TYPE) || type.equals(Short.class)) {
            return Mockito.anyShort();
        } else if (type.equals(Character.TYPE) || type.equals(Character.class)) {
            return Mockito.anyChar();
        } else if (type.equals(String.class)) {
            return Mockito.anyString();
        } else if (TypeUtils.isAssignable(type, List.class)) {
            return Mockito.anyList();
        } else if (TypeUtils.isAssignable(type, Set.class)) {
            return Mockito.anySet();
        } else if (TypeUtils.isAssignable(type, Map.class)) {
            return Mockito.anyMap();
        } else if (TypeUtils.isAssignable(type, Collection.class)) {
            return Mockito.anyCollection();
        } else if (TypeUtils.isAssignable(type, Iterable.class)) {
            return Mockito.anyIterable();
        } else {
            GenericClass gc = new GenericClass(type);
            return Mockito.nullable(gc.getRawClass());
        }
    } catch (Exception e) {
        logger.error("Failed to executed Mockito matcher n{} of type {} in {}.{}: {}", i, type, className, methodName, e.getMessage());
        throw new EvosuiteError(e);
    }
}
Also used : Set(java.util.Set) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) Type(java.lang.reflect.Type) GenericClass(org.evosuite.utils.generic.GenericClass) Collection(java.util.Collection)

Example 64 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class Issre13SystemTest method testOnSpecificTest.

@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
    Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
    // force using DSE
    Properties.DSE_PROBABILITY = 1.0;
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    // String string0 = "baz5";
    VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
    // DseFoo dseFoo0 = new DseFoo();
    GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
    ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
    VariableReference fooVar = test.addStatement(fooConstructorStatement);
    Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
    GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
    // DseBar dseBar0 = new DseBar(string0);
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
    VariableReference callee = test.addStatement(constructorStatement);
    // dseBar0.coverMe(dseFoo0);
    Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
    test.addStatement(ms);
    System.out.println(test);
    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
    BranchCoverageMap.getInstance().searchStarted(null);
    assertEquals(4.0, fitness.getFitness(suite), 0.1F);
    suite.addTest(test);
    assertEquals(1.0, fitness.getFitness(suite), 0.1F);
    System.out.println("Test suite: " + suite);
    TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
    LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
    localObjective.addFitnessFunction(fitness);
    localSearch.doSearch(suite, localObjective);
    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
    BranchCoverageMap.getInstance().searchFinished(null);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) DseBar(com.examples.with.different.packagename.localsearch.DseBar) TestSuiteLocalSearch(org.evosuite.testsuite.localsearch.TestSuiteLocalSearch) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) GenericClass(org.evosuite.utils.generic.GenericClass) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DseFoo(com.examples.with.different.packagename.localsearch.DseFoo) Test(org.junit.Test)

Example 65 with GenericClass

use of org.evosuite.utils.generic.GenericClass in project evosuite by EvoSuite.

the class ContainsTraceObserver method visit.

/* (non-Javadoc)
	 * @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
	 */
/**
 * {@inheritDoc}
 */
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
    try {
        Object object = var.getObject(scope);
        if (object == null)
            return;
        if (statement instanceof AssignmentStatement)
            return;
        if (statement instanceof PrimitiveStatement<?>)
            return;
        // Only relevant for Collections
        if (!(object instanceof Collection))
            return;
        Collection collectionObject = (Collection) object;
        List<GenericClass> parameterClasses = var.getGenericClass().getParameterClasses();
        // Need to know exact type
        if (parameterClasses.size() != 1)
            return;
        java.lang.reflect.Type parameterType = parameterClasses.get(0).getType();
        ContainsTraceEntry entry = new ContainsTraceEntry(var);
        int position = statement.getPosition();
        Set<VariableReference> otherVariables = new LinkedHashSet<>();
        otherVariables.addAll(scope.getElements(parameterType));
        for (int i = 0; i <= statement.getPosition(); i++) {
            for (VariableReference candidateVar : currentTest.getStatement(i).getVariableReferences()) {
                if (candidateVar instanceof ConstantValue && candidateVar.isAssignableTo(parameterType)) {
                    otherVariables.add(candidateVar);
                }
            }
        }
        for (VariableReference other : otherVariables) {
            Object otherObject;
            if (other instanceof ConstantValue)
                otherObject = ((ConstantValue) other).getValue();
            else
                otherObject = other.getObject(scope);
            if (otherObject == null)
                // TODO: Don't do this?
                continue;
            int otherPos = other.getStPosition();
            if (otherPos > position)
                // Don't compare with variables that are not defined - may happen with primitives?
                continue;
            Statement otherStatement = currentTest.getStatement(otherPos);
            if (otherStatement instanceof MethodStatement) {
                if (((MethodStatement) otherStatement).getMethodName().equals("hashCode"))
                    // No comparison against hashCode, as the hashCode return value will not be in the test
                    continue;
            }
            try {
                logger.debug("Checking whether {} contains {} is: {}", var, other, collectionObject.contains(otherObject));
                entry.addEntry(other, collectionObject.contains(otherObject));
            } catch (Throwable t) {
                logger.debug("Exception during equals: " + t);
            // ignore?
            }
            if (object instanceof Comparable<?>) {
            // TODO
            }
        }
        trace.addEntry(statement.getPosition(), var, entry);
    } catch (CodeUnderTestException e) {
        logger.debug("", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) GenericClass(org.evosuite.utils.generic.GenericClass) Collection(java.util.Collection) ConstantValue(org.evosuite.testcase.variable.ConstantValue)

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