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());
}
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;
}
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);
}
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);
}
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());
}
Aggregations