use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testAll_once.
@Test
public void testAll_once() throws Exception {
TestCase tc = new DefaultTestCase();
VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("all_once", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
Assert.assertEquals(0, mockStmt.getNumParameters());
// execute first time with default mock
Scope scope = execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(7, types.size());
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testArray.
@Test
public void testArray() throws Exception {
TestCase tc = new DefaultTestCase();
/*
String s = "...";
String[] array = new String[1];
array[0] = s;
Foo foo = mock(Foo.class);
getFirstInArray(foo);
*/
final String MOCKED_VALUE = "Hello 42!!!";
VariableReference aString = tc.addStatement(new StringPrimitiveStatement(tc, MOCKED_VALUE));
ArrayReference mockedArray = (ArrayReference) tc.addStatement(new ArrayStatement(tc, String[].class, 1));
ArrayIndex arrayIndex = new ArrayIndex(tc, mockedArray, 0);
AssignmentStatement stmt = new AssignmentStatement(tc, arrayIndex, aString);
tc.addStatement(stmt);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, Foo.class, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("getFirstInArray", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
// if not executed, should be no way to tell if needs new inputs
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
Assert.assertEquals(0, mockStmt.getNumParameters());
// execute first time with default mock
Scope scope = execute(tc);
Object obj = scope.getObject(result);
// default mock value should be null for objects/arrays
Assert.assertNull(obj);
// after execution, there should be one variable to provide
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(1, types.size());
Assert.assertEquals(String[].class, types.get(0));
// add int variable to list of mock expected returns
mockStmt.addMissingInputs(Arrays.asList(mockedArray));
Assert.assertEquals(1, mockStmt.getNumParameters());
Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedArray));
// re-execute with initialized mock
scope = execute(tc);
String val = (String) scope.getObject(result);
Assert.assertEquals(MOCKED_VALUE, val);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testLimit.
@Test
public void testLimit() throws Exception {
TestCase tc = new DefaultTestCase();
final int LIMIT_5 = 5;
Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = LIMIT_5;
final int LOOP_0 = 0, LOOP_3 = 3, LOOP_5 = 5, LOOP_7 = 7;
IntPrimitiveStatement x = new IntPrimitiveStatement(tc, LOOP_3);
VariableReference loop = tc.addStatement(x);
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, true));
VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("limit", Foo.class, int.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock, loop)));
// execute first time with default mock
execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(LOOP_3, types.size());
for (Type t : types) {
Assert.assertEquals(boolean.class, t);
}
// add the 3 missing values
mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef, boolRef));
// before re-executing, change loops to the limit
x.setValue(LOOP_5);
execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(LOOP_5 - LOOP_3, types.size());
for (Type t : types) {
Assert.assertEquals(boolean.class, t);
}
// add the 2 missing values
mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef));
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// before re-executing 3rd time, change loops above the limit
x.setValue(LOOP_7);
execute(tc);
// no update should be required
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// decrease, but to the limit, so still no required change
x.setValue(LOOP_5);
execute(tc);
// no update should be required
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// further decrease, but now we need to remove parameters
x.setValue(LOOP_3);
execute(tc);
// do update
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
// but no new types to add
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_3, mockStmt.getNumParameters());
// remove all
x.setValue(LOOP_0);
execute(tc);
// do update
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
// but no new types to add
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_0, mockStmt.getNumParameters());
}
use of org.evosuite.testcase.TestCase 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.testcase.TestCase in project evosuite by EvoSuite.
the class ReadWriteSystemPropertiesSystemTest method testReadLineSeparator.
@Test
public void testReadLineSeparator() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ReadTimezone.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.SANDBOX = true;
Properties.REPLACE_CALLS = true;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
double cov = best.getCoverage();
Assert.assertEquals("Non-optimal coverage: ", 1d, cov, 0.001);
// now check the JUnit generation
List<TestCase> list = best.getTests();
int n = list.size();
Assert.assertTrue(n > 0);
// needed because it gets pulled down after the search
TestCaseExecutor.initExecutor();
try {
Sandbox.initializeSecurityManagerForSUT();
JUnitAnalyzer.removeTestsThatDoNotCompile(list);
} finally {
Sandbox.resetDefaultSecurityManager();
}
Assert.assertEquals(n, list.size());
TestGenerationResult tgr = TestGenerationResultBuilder.buildSuccessResult();
String code = tgr.getTestSuiteCode();
Assert.assertTrue("Test code:\n" + code, code.contains("user.timezone"));
/*
* This is tricky. The property 'debug' is read, but it does not exist.
* Ideally, we should still have in the test case a call to be sure the variable
* is set to null. But that would lead to a lot of problems :( eg cases
* in which we end up in reading hundreds of thousands variables that do not exist
*/
Assert.assertTrue("Test code:\n" + code, !code.contains("debug"));
}
Aggregations