use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testConstructors.
@Test
public void testConstructors() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
// method goal
TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
// exception goal
TestFitnessFunction goal2 = new ExceptionCoverageTestFitness("FooClass", "<init>(LFooClass;)V", ArrayIndexOutOfBoundsException.class, ExceptionCoverageTestFitness.ExceptionType.IMPLICIT);
DefaultTestCase test1 = new DefaultTestCase();
test1.addCoveredGoal(goal1);
DefaultTestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 2));
test2.addCoveredGoal(goal1);
test2.addCoveredGoal(goal2);
ArrayList<TestCase> testCases = new ArrayList<>();
testCases.add(test1);
testCases.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
String nameTest1 = naming.getName(test1);
String nameTest2 = naming.getName(test2);
assertEquals("Generated test name differs from expected", "testCreatesFooClass", nameTest1);
assertEquals("Generated test name differs from expected", "testFailsToCreateFooClassThrowsArrayIndexOutOfBoundsException", nameTest2);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestMethodNamingComplexExamples method testOverloadedMethods.
@Test
public void testOverloadedMethods() throws NoSuchMethodException, ConstructionFailedException, ClassNotFoundException {
// Method goals
TestFitnessFunction goal1 = new MethodCoverageTestFitness("FooClass", "<init>(LFooClass;)V");
TestFitnessFunction goal2 = new MethodCoverageTestFitness("FooClass", "values([B)[B");
TestFitnessFunction goal3 = new MethodCoverageTestFitness("FooClass", "values([I)[B");
// Output goals for method values
TestFitnessFunction goal4 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([B)", Type.getType("[B"), ARRAY_EMPTY));
TestFitnessFunction goal5 = new OutputCoverageTestFitness(new OutputCoverageGoal("FooClass", "values([I)", Type.getType("[B"), ARRAY_NONEMPTY));
DefaultTestCase test1 = new DefaultTestCase();
test1.addCoveredGoal(goal1);
test1.addCoveredGoal(goal2);
test1.addCoveredGoal(goal4);
DefaultTestCase test2 = new DefaultTestCase();
// Need to add statements to change hashCode
test2.addStatement(new IntPrimitiveStatement(test2, 2));
test2.addCoveredGoal(goal1);
test2.addCoveredGoal(goal3);
test2.addCoveredGoal(goal5);
ArrayList<TestCase> testCases = new ArrayList<>();
testCases.add(test1);
testCases.add(test2);
CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(testCases);
String nameTest1 = naming.getName(test1);
String nameTest2 = naming.getName(test2);
assertEquals("Generated test name differs from expected", "testValuesTakingByteArray", nameTest1);
assertEquals("Generated test name differs from expected", "testValuesTakingIntArray", nameTest2);
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class JUnitAnalyzerTest method testSandboxIssue.
@Test
public void testSandboxIssue() throws Exception {
// First, get a TestCase from a carved JUnit
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.sandbox.OpenStreamInATryCatch_FakeTestToCarve.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.sandbox.OpenStreamInATryCatch.class.getCanonicalName();
Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.BRANCH };
Properties.SEED_MUTATIONS = 0;
Properties.SEED_CLONE = 1;
Properties.VIRTUAL_FS = false;
Properties.SANDBOX = true;
// needed for setLoggingForJUnit
Properties.ENABLE_ASSERTS_FOR_EVOSUITE = true;
Properties.TEST_SCAFFOLDING = false;
// FIXME
Sandbox.initializeSecurityManagerForSUT();
// file should never be created
Assert.assertFalse(file.exists());
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
TestChromosome carved = factory.getChromosome();
/*
* FIXME: issue with carver
*/
Files.deleteIfExists(file.toPath());
Assert.assertFalse(file.exists());
Assert.assertNotNull(carved);
TestCase test = carved.getTestCase();
Assert.assertEquals("Should be: constructor, 1 variable and 1 method", 3, test.size());
// Now that we have a test case, we check its execution after
// recompiling it to JUnit, and see if sandbox kicks in
List<TestCase> list = new ArrayList<TestCase>();
list.add(test);
Assert.assertFalse(file.exists());
// NOTE: following order of checks reflects what is done
// in EvoSuite after the search is finished
System.out.println("\n COMPILATION CHECK \n");
// first try to compile (which implies execution)
JUnitAnalyzer.removeTestsThatDoNotCompile(list);
Assert.assertEquals(1, list.size());
Assert.assertFalse(file.exists());
System.out.println("\n FIRST STABILITY CHECK \n");
// try once
JUnitAnalyzer.handleTestsThatAreUnstable(list);
Assert.assertEquals(1, list.size());
Assert.assertFalse(file.exists());
System.out.println("\n SECOND STABILITY CHECK \n");
// try again
JUnitAnalyzer.handleTestsThatAreUnstable(list);
Assert.assertEquals(1, list.size());
Assert.assertFalse(file.exists());
System.out.println("\n FINAL VERIFICATION \n");
JUnitAnalyzer.verifyCompilationAndExecution(list);
Assert.assertEquals(1, list.size());
Assert.assertFalse(file.exists());
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestStabilityChecker method checkStability.
public static boolean checkStability(List<TestCase> list) {
int n = list.size();
boolean previousRunOnSeparateProcess = Properties.JUNIT_CHECK_ON_SEPARATE_PROCESS;
Properties.JUNIT_CHECK_ON_SEPARATE_PROCESS = false;
// needed because it gets pulled down after the search
TestCaseExecutor.initExecutor();
// Before starting search, let's activate the sandbox
if (Properties.SANDBOX) {
Sandbox.initializeSecurityManagerForSUT();
}
for (TestCase tc : list) {
if (tc.isUnstable()) {
return false;
}
}
try {
JUnitAnalyzer.removeTestsThatDoNotCompile(list);
if (n != list.size()) {
return false;
}
JUnitAnalyzer.handleTestsThatAreUnstable(list);
if (n != list.size()) {
return false;
}
for (TestCase tc : list) {
if (tc.isUnstable()) {
return false;
}
}
return true;
} finally {
Properties.JUNIT_CHECK_ON_SEPARATE_PROCESS = previousRunOnSeparateProcess;
if (Properties.SANDBOX) {
Sandbox.resetDefaultSecurityManager();
}
}
}
use of org.evosuite.testcase.TestCase in project evosuite by EvoSuite.
the class TestDoubleFloatComparison method testDelta.
@Test
public void testDelta() {
TestCase test = new DefaultTestCase();
DoublePrimitiveStatement statement1 = new DoublePrimitiveStatement(test);
DoublePrimitiveStatement statement2 = new DoublePrimitiveStatement(test);
double d1 = statement1.getValue();
double d2 = statement2.getValue();
int val = BooleanHelper.doubleSubG(d1, d2);
assertEquals(val > 0, d1 > d2);
assertEquals(val < 0, d1 < d2);
assertEquals(val == 0, d1 == d2);
for (int i = 0; i < 100; i++) {
statement1.delta();
statement2.delta();
d1 = statement1.getValue();
d2 = statement2.getValue();
val = BooleanHelper.doubleSubG(d1, d2);
assertEquals(val > 0, d1 > d2);
assertEquals(val < 0, d1 < d2);
assertEquals(val == 0, d1 == d2);
}
for (int i = 0; i < 100; i++) {
statement1.randomize();
statement2.randomize();
d1 = statement1.getValue();
d2 = statement2.getValue();
val = BooleanHelper.doubleSubG(d1, d2);
assertEquals(val > 0, d1 > d2);
assertEquals(val < 0, d1 < d2);
assertEquals(val == 0, d1 == d2);
}
}
Aggregations