Search in sources :

Example 6 with InstrumentingClassLoader

use of org.evosuite.instrumentation.InstrumentingClassLoader 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 7 with InstrumentingClassLoader

use of org.evosuite.instrumentation.InstrumentingClassLoader in project evosuite by EvoSuite.

the class ProjectAnalyzer method analyze.

/**
 * Analyze the classes in the given target
 * @return
 */
public ProjectStaticData analyze() {
    ProjectStaticData data = new ProjectStaticData();
    if (Properties.CTG_SCHEDULE.equals(AvailableSchedule.HISTORY)) {
        data.initializeLocalHistory();
    }
    for (String className : getCutsToAnalyze()) {
        Class<?> theClass = null;
        int numberOfBranches = -1;
        boolean hasCode = false;
        Properties.TARGET_CLASS = className;
        InstrumentingClassLoader instrumenting = new InstrumentingClassLoader();
        BranchPool.getInstance(instrumenting).reset();
        try {
            /*
				 * to access number of branches, we need to use
				 * instrumenting class loader. But loading a class would
				 * execute its static code, and so we need to 
				 * use a security manager. 
				 */
            Sandbox.goingToExecuteUnsafeCodeOnSameThread();
            instrumenting.loadClass(className);
            numberOfBranches = BranchPool.getInstance(instrumenting).getBranchCounter();
            hasCode = (numberOfBranches > 0) || (BranchPool.getInstance(instrumenting).getBranchlessMethods().size() > 0);
            /*
				 * just to avoid possible issues with instrumenting classloader
				 */
            theClass = ClassLoader.getSystemClassLoader().loadClass(className);
        // TODO kind
        // if(theClass.isInterface()){
        // kind = ClassKind.INTERFACE;
        // } else if(theClass.is  Modifier.isAbstract( someClass.getModifiers() );
        } catch (Exception e) {
            logger.warn("Cannot handle " + className + " due to: " + e.getClass() + " " + e.getMessage());
            continue;
        } finally {
            Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
            BranchPool.getInstance(instrumenting).reset();
            Properties.TARGET_CLASS = "";
        }
        ClassInfo ci = new ClassInfo(theClass, numberOfBranches, hasCode);
        data.addNewClass(ci);
        if (Properties.CTG_SCHEDULE == AvailableSchedule.HISTORY) {
            ci.setChanged(data.hasChanged(theClass.getCanonicalName() + ".java"));
            ci.isToTest(data.isToTest(theClass.getCanonicalName(), HistorySchedule.COMMIT_IMPROVEMENT));
        }
    }
    return data;
}
Also used : InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) ClassInfo(org.evosuite.continuous.project.ProjectStaticData.ClassInfo)

Example 8 with InstrumentingClassLoader

use of org.evosuite.instrumentation.InstrumentingClassLoader in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_differentPackage_instrumentation_public.

/*
     * This test fails when Mockito is instrumented (it would pass if org.mockito. is excluded from instrumentation.
     * However, in the packaged version, Mockito is shaded and thus isn't actually instrumented. I don't have a good
     * solution to fix this test, hence I've marked it as ignored. (Gordon, 9.2.2018)
     */
@Test
public void testPackageLevel_differentPackage_instrumentation_public() throws Exception {
    TestCase tc = new DefaultTestCase();
    RuntimeInstrumentation.setAvoidInstrumentingShadedClasses(true);
    ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
    InstrumentingClassLoader loader = new InstrumentingClassLoader();
    Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePublicLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
    tc.addStatement(mockStmt);
    execute(tc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 9 with InstrumentingClassLoader

use of org.evosuite.instrumentation.InstrumentingClassLoader in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_differentPackage_instrumentation_package.

@Test
public void testPackageLevel_differentPackage_instrumentation_package() throws Exception {
    TestCase tc = new DefaultTestCase();
    ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
    InstrumentingClassLoader loader = new InstrumentingClassLoader();
    Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 10 with InstrumentingClassLoader

use of org.evosuite.instrumentation.InstrumentingClassLoader in project evosuite by EvoSuite.

the class MockJOptionPaneShowInternalOptionDialogTest method testShowInternalInputDialogs.

@Test
public void testShowInternalInputDialogs() throws Exception {
    TestSuiteChromosome suite = new TestSuiteChromosome();
    InstrumentingClassLoader cl = new InstrumentingClassLoader();
    TestCase t1 = buildTestCase0(cl);
    suite.addTest(t1);
    BranchCoverageSuiteFitness ff = new BranchCoverageSuiteFitness(cl);
    ff.getFitness(suite);
    Set<TestFitnessFunction> coveredGoals = suite.getCoveredGoals();
    Assert.assertEquals(2, coveredGoals.size());
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Aggregations

InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)26 Test (org.junit.Test)23 TestCase (org.evosuite.testcase.TestCase)15 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)13 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)12 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)11 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)5 VariableReference (org.evosuite.testcase.variable.VariableReference)4 GenericClass (org.evosuite.utils.generic.GenericClass)3 ExtendingFile (com.examples.with.different.packagename.mock.java.io.ExtendingFile)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 NonInstrumentingClassLoader (org.evosuite.instrumentation.NonInstrumentingClassLoader)2 MockFile (org.evosuite.runtime.mock.java.io.MockFile)2 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)2 CBranchExample (com.examples.with.different.packagename.cbranch.CBranchExample)1 MemoryCheck (com.examples.with.different.packagename.mock.java.lang.MemoryCheck)1 TypeToken (com.googlecode.gentyref.TypeToken)1 File (java.io.File)1 Assertion (org.evosuite.assertion.Assertion)1