Search in sources :

Example 11 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class TestClassInitialization method buildLoadTargetClassTestCase.

private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
    DefaultTestCase test = new DefaultTestCase();
    StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
    VariableReference string0 = test.addStatement(stmt0);
    try {
        Method currentThreadMethod = Thread.class.getMethod("currentThread");
        Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
        VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
        Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
        Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
        VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
        Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
        Statement loadClassStmt = new MethodStatement(test, new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar, Collections.singletonList(string0));
        test.addStatement(loadClassStmt);
        return test;
    } catch (NoSuchMethodException | SecurityException e) {
        throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 12 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testClassLoaderChange.

@Test
public void testClassLoaderChange() throws NoSuchMethodException, SecurityException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassTwoParameters.class;
    Method creatorMethod = targetClass.getMethod("create", new Class<?>[] {});
    Method targetMethod = targetClass.getMethod("get", new Class<?>[] { Object.class });
    Method inspectorMethod = targetClass.getMethod("testMe", new Class<?>[] {});
    Constructor<?> intConst = Integer.class.getConstructor(new Class<?>[] { int.class });
    GenericClass listOfInteger = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassTwoParameters<Integer, Integer>>() {
    }.getType());
    GenericMethod genericCreatorMethod = new GenericMethod(creatorMethod, targetClass).getGenericInstantiationFromReturnValue(listOfInteger);
    System.out.println(genericCreatorMethod.getGeneratedClass().toString());
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass).copyWithNewOwner(genericCreatorMethod.getGeneratedClass());
    System.out.println(genericMethod.getGeneratedClass().toString());
    DefaultTestCase test = new DefaultTestCase();
    MethodStatement ms1 = new MethodStatement(test, genericCreatorMethod, (VariableReference) null, new ArrayList<VariableReference>());
    test.addStatement(ms1);
    IntPrimitiveStatement ps1 = (IntPrimitiveStatement) PrimitiveStatement.getPrimitiveStatement(test, int.class);
    test.addStatement(ps1);
    GenericConstructor intConstructor = new GenericConstructor(intConst, Integer.class);
    List<VariableReference> constParam = new ArrayList<VariableReference>();
    constParam.add(ps1.getReturnValue());
    ConstructorStatement cs1 = new ConstructorStatement(test, intConstructor, constParam);
    // test.addStatement(cs1);
    List<VariableReference> callParam = new ArrayList<VariableReference>();
    callParam.add(ps1.getReturnValue());
    MethodStatement ms2 = new MethodStatement(test, genericMethod, ms1.getReturnValue(), callParam);
    test.addStatement(ms2);
    Inspector inspector = new Inspector(targetClass, inspectorMethod);
    Assertion assertion = new InspectorAssertion(inspector, ms2, ms1.getReturnValue(), 0);
    ms2.addAssertion(assertion);
    String code = test.toCode();
    ClassLoader loader = new InstrumentingClassLoader();
    Properties.TARGET_CLASS = targetClass.getCanonicalName();
    Properties.CRITERION = new Criterion[1];
    Properties.CRITERION[0] = Criterion.MUTATION;
    DefaultTestCase testCopy = test.clone();
    testCopy.changeClassLoader(loader);
    String code2 = testCopy.toCode();
    Assert.assertEquals(code, code2);
    Assert.assertEquals(code, test.toCode());
    testCopy.removeAssertion(assertion);
    Assert.assertEquals(code, test.toCode());
    // test.removeAssertion(assertion);
    test.removeAssertions();
    System.out.println(test.toCode());
}
Also used : InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) Inspector(org.evosuite.assertion.Inspector) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 13 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.

@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
    Class<?> sut = ClassWithField.class;
    DefaultTestCase tc = new DefaultTestCase();
    // ClassWithField classWithField0 = new ClassWithField();
    GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
    ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
    VariableReference obj = tc.addStatement(constructorStatement);
    // classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
    FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
    Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
    GenericMethod gm = new GenericMethod(m, sut);
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    // classWithField0.BOOLEAN_FIELD = false;
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
    tc.addStatement(new AssignmentStatement(tc, field, boolRef));
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    Properties.TARGET_CLASS = sut.getCanonicalName();
    Properties.JUNIT_TESTS = true;
    TestSuiteChromosome testSuite = new TestSuiteChromosome();
    testSuite.addTest(tc);
    FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
    assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
    assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 14 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.

the class DeleteMutationSystemTest method getTwoIntTest.

private TestCase getTwoIntTest(int x, int y, int x1, int y1) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
    test.addStatement(new IntPrimitiveStatement(test, x));
    test.addStatement(new IntPrimitiveStatement(test, y));
    VariableReference wrongIntVar1 = test.addStatement(new IntPrimitiveStatement(test, x1));
    VariableReference wrongIntVar2 = test.addStatement(new IntPrimitiveStatement(test, y1));
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { wrongIntVar1, wrongIntVar2 }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

Example 15 with MethodStatement

use of org.evosuite.testcase.statements.MethodStatement 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)

Aggregations

MethodStatement (org.evosuite.testcase.statements.MethodStatement)54 VariableReference (org.evosuite.testcase.variable.VariableReference)40 GenericMethod (org.evosuite.utils.generic.GenericMethod)29 Method (java.lang.reflect.Method)25 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)22 Statement (org.evosuite.testcase.statements.Statement)19 GenericClass (org.evosuite.utils.generic.GenericClass)15 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 TestCase (org.evosuite.testcase.TestCase)7 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)5 FieldStatement (org.evosuite.testcase.statements.FieldStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 HashSet (java.util.HashSet)4 TestFactory (org.evosuite.testcase.TestFactory)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)4 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)4