Search in sources :

Example 46 with TestCase

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);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 47 with TestCase

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);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) MethodCoverageTestFitness(org.evosuite.coverage.method.MethodCoverageTestFitness) ArrayList(java.util.ArrayList) OutputCoverageTestFitness(org.evosuite.coverage.io.output.OutputCoverageTestFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) OutputCoverageGoal(org.evosuite.coverage.io.output.OutputCoverageGoal) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 48 with TestCase

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());
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestCase(org.evosuite.testcase.TestCase) ArrayList(java.util.ArrayList) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 49 with TestCase

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();
        }
    }
}
Also used : TestCase(org.evosuite.testcase.TestCase)

Example 50 with TestCase

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);
    }
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) DoublePrimitiveStatement(org.evosuite.testcase.statements.numeric.DoublePrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Aggregations

TestCase (org.evosuite.testcase.TestCase)192 Test (org.junit.Test)119 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)90 ArrayList (java.util.ArrayList)67 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)45 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)43 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)39 VariableReference (org.evosuite.testcase.variable.VariableReference)26 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)25 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)25 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)24 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)17 TestChromosome (org.evosuite.testcase.TestChromosome)17 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)15 Ignore (org.junit.Ignore)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)14 GenericMethod (org.evosuite.utils.generic.GenericMethod)12 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)11 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)10