Search in sources :

Example 86 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class PoolSystemTest method testPoolWithException.

@Test
public void testPoolWithException() throws IOException, NoSuchMethodException, SecurityException {
    File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
    String filename = f.getAbsolutePath();
    f.delete();
    System.out.println(filename);
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DependencyClassWithException.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.getTargetClassAndDontInitialise();
    TestCase test = new DefaultTestCase();
    VariableReference instance = test.addStatement(new ConstructorStatement(test, new GenericConstructor(DependencyClassWithException.class.getConstructors()[0], DependencyClassWithException.class), new ArrayList<VariableReference>()));
    VariableReference int42 = test.addStatement(new IntPrimitiveStatement(test, 42));
    GenericMethod foo = new GenericMethod(DependencyClassWithException.class.getMethod("foo", int.class), DependencyClassWithException.class);
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    TestSuiteChromosome best = new TestSuiteChromosome();
    best.addTest(test);
    ObjectPool pool = new ObjectPool();
    pool.addSequence(new GenericClass(DependencyClassWithException.class), test);
    pool.writePool(filename);
    System.out.println("EvolvedTestSuite:\n" + best);
    resetStaticVariables();
    setDefaultPropertiesForTestCases();
    targetClass = ClassDependingOnExceptionClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.P_OBJECT_POOL = 0.8;
    Properties.OBJECT_POOLS = filename;
    ObjectPoolManager.getInstance().initialisePool();
    // Properties.SEARCH_BUDGET = 50000;
    command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
    f = new File(filename);
    f.delete();
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) DependencyClassWithException(com.examples.with.different.packagename.pool.DependencyClassWithException) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) File(java.io.File) ObjectPool(org.evosuite.seeding.ObjectPool) ClassDependingOnExceptionClass(com.examples.with.different.packagename.pool.ClassDependingOnExceptionClass) Test(org.junit.Test)

Example 87 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestConstantInliner method testArrayIndexInlining.

@Test
public void testArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(Object.class.getConstructor(), Object.class), new ArrayList<VariableReference>());
    VariableReference objectVar = test.addStatement(cs);
    ArrayStatement as = new ArrayStatement(test, Object[].class, 3);
    test.addStatement(as);
    ArrayReference arrayVar = as.getArrayReference();
    ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
    ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
    ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
    test.addStatement(new AssignmentStatement(test, ai0, objectVar));
    test.addStatement(new AssignmentStatement(test, ai1, objectVar));
    test.addStatement(new AssignmentStatement(test, ai2, objectVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(ObjectParameter.class.getConstructor(), ObjectParameter.class), new ArrayList<VariableReference>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    parameters = new ArrayList<VariableReference>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    parameters = new ArrayList<VariableReference>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(ObjectParameter.class.getMethods()[0], ObjectParameter.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    assertFalse(code.contains("objectParameter0.testMe(objectArray0"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) ObjectParameter(com.examples.with.different.packagename.ObjectParameter) Test(org.junit.Test)

Example 88 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestConstantInliner method testNumericArrayIndexInlining.

@Test
public void testNumericArrayIndexInlining() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    PrimitiveStatement<?> primitiveStatement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
    VariableReference intVar = test.addStatement(primitiveStatement);
    ArrayStatement as = new ArrayStatement(test, int[].class, 3);
    test.addStatement(as);
    ArrayReference arrayVar = as.getArrayReference();
    ArrayIndex ai0 = new ArrayIndex(test, arrayVar, 0);
    ArrayIndex ai1 = new ArrayIndex(test, arrayVar, 1);
    ArrayIndex ai2 = new ArrayIndex(test, arrayVar, 2);
    test.addStatement(new AssignmentStatement(test, ai0, intVar));
    test.addStatement(new AssignmentStatement(test, ai1, intVar));
    test.addStatement(new AssignmentStatement(test, ai2, intVar));
    ConstructorStatement sutCS = new ConstructorStatement(test, new GenericConstructor(TrivialInt.class.getConstructor(), TrivialInt.class), new ArrayList<>());
    VariableReference sut = test.addStatement(sutCS);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(ai0);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai1);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    parameters = new ArrayList<>();
    parameters.add(ai2);
    test.addStatement(new MethodStatement(test, new GenericMethod(TrivialInt.class.getMethods()[0], TrivialInt.class), sut, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    System.out.println(test.toCode());
    assertFalse(code.contains("trivialInt0.testMe(int"));
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) TrivialInt(com.examples.with.different.packagename.TrivialInt) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Test(org.junit.Test)

Example 89 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestConstantInliner method testStringEndingWithClass.

@Test
public void testStringEndingWithClass() throws NoSuchMethodException, SecurityException {
    DefaultTestCase test = new DefaultTestCase();
    ConstructorStatement cs = new ConstructorStatement(test, new GenericConstructor(StringConstantInliningExample.class.getConstructor(), StringConstantInliningExample.class), new ArrayList<VariableReference>());
    VariableReference objectVar = test.addStatement(cs);
    StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(test, "test.class");
    VariableReference stringParam = test.addStatement(stringStatement);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    parameters.add(stringParam);
    test.addStatement(new MethodStatement(test, new GenericMethod(StringConstantInliningExample.class.getMethods()[0], StringConstantInliningExample.class), objectVar, parameters));
    System.out.println(test.toCode());
    ConstantInliner inliner = new ConstantInliner();
    inliner.inline(test);
    String code = test.toCode();
    System.out.println(code);
    assertFalse(code.contains("foo(test.class)"));
    assertTrue(code.contains("foo(\"test.class\")"));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) StringConstantInliningExample(com.examples.with.different.packagename.StringConstantInliningExample) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 90 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class ObjectFields method getObjectVariables.

public Map<Integer, Map<String, Object>> getObjectVariables(Collection<VariableReference> vrefs) {
    List<Map<Integer, Map<String, Object>>> ov = new ArrayList<Map<Integer, Map<String, Object>>>();
    Map<Integer, Map<String, Object>> variable_field = new HashMap<Integer, Map<String, Object>>();
    Map<Integer, Map<String, Map<String, Object>>> variable_ref_field = new HashMap<Integer, Map<String, Map<String, Object>>>();
    Map<String, Object> field_value = new HashMap<String, Object>();
    // Collection<Field> fields = getAllFields(c);
    List<Object> values = new ArrayList<Object>();
    for (VariableReference vref : vrefs) {
        Object scope_object = scope.getObject(vref);
        if (scope_object == null)
            continue;
        String vref_class = vref.getClassName();
        // logger.warn(x);
        int vref_string = vref.getStPosition();
        Map<String, Object> objectMap = getObjectMap(scope_object);
        // Map<String,Map<String,Object>> vrefObjectMap = new
        // HashMap<String,Map<String,Object>>();
        // vrefObjectMap.put(vref_class, objectMap);
        variable_field.put(vref_string, objectMap);
    // variable_ref_field.put(vref_string, vrefObjectMap);
    /*
			 * 
			 * Class<?> so_class = scope_object.getClass();
			 * 
			 * int vref_string = vref.getStPosition();
			 * 
			 * if (ClassUtils.isPrimitiveOrWrapper(scope_object.getClass())) {
			 * Map<String, Object> all_vars = new HashMap<String, Object>();
			 * variable_field.put(vref_string, (Map<String, Object>) (all_vars
			 * .put("fake_var", scope_object))); } else { Map<String, Object>
			 * all_vars = getAllVars(scope_object, 0,"");
			 * variable_field.put(vref_string, all_vars); }
			 */
    // boolean is_prim = ClassUtils.isPrimitiveOrWrapper(so_class);
    // assert is_prim != false : so_class.toString();
    /*
			 * logger.warn("scope_ob: " + scope_object.toString() + ", so_class"
			 * + so_class.toString() + ", vref:" + vref_string +
			 * ",primitve? "+is_prim);
			 */
    }
    return variable_field;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14