Search in sources :

Example 81 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestTestSuiteMinimizer method minimizeSuiteFullCoverage.

@Test
public void minimizeSuiteFullCoverage() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
    Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    testFactory.addConstructor(test, gc, 0, 0);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    for (int i = 0; i < 10; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
        VariableReference vr = test.addStatement(ips, i + 1);
    }
    ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    testFactory.addMethod(test, method, 11, 0);
    parameters = new ArrayList<VariableReference>();
    for (int i = 12; i < 15; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
        VariableReference vr = test.addStatement(ips, i);
    }
    ct = new ConstructorStatement(test, gc, parameters);
    testFactory.addMethod(test, method, 15, 0);
    assertEquals(16, test.size());
    TestSuiteChromosome tsc = new TestSuiteChromosome();
    tsc.addTest(test);
    TestSuiteFitnessFunction ff = new BranchCoverageSuiteFitness();
    double previous_fitness = ff.getFitness(tsc);
    tsc.setFitness(ff, previous_fitness);
    assertEquals(previous_fitness, 0.0, 0.0);
    TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory());
    minimizer.minimize(tsc, false);
    assertEquals(1, tsc.getTests().size());
    assertEquals(5, tsc.getTests().get(0).size());
    // assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nint int1 = 28241;\nflagExample1_0.testMe(int1);\nflagExample1_0.testMe(int0);\n"));
    double fitness = ff.getFitness(tsc);
    assertEquals(previous_fitness, fitness, 0.0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) ArrayList(java.util.ArrayList) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) FlagExample1(com.examples.with.different.packagename.FlagExample1) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) GenericClass(org.evosuite.utils.generic.GenericClass) Test(org.junit.Test)

Example 82 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestTestSuiteMinimizer method minimizeSuiteFullCoverageWithTwoFitnessFunctions.

@Test
public void minimizeSuiteFullCoverageWithTwoFitnessFunctions() throws ClassNotFoundException, NoSuchFieldException, SecurityException, ConstructionFailedException, NoSuchMethodException {
    Properties.TARGET_CLASS = FlagExample1.class.getCanonicalName();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    testFactory.addConstructor(test, gc, 0, 0);
    List<VariableReference> parameters = new ArrayList<VariableReference>();
    for (int i = 0; i < 10; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, 28234 + i);
        VariableReference vr = test.addStatement(ips, i + 1);
    }
    ConstructorStatement ct = new ConstructorStatement(test, gc, parameters);
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class });
    GenericMethod method = new GenericMethod(m, sut);
    testFactory.addMethod(test, method, 11, 0);
    parameters = new ArrayList<VariableReference>();
    for (int i = 12; i < 15; i++) {
        IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i);
        VariableReference vr = test.addStatement(ips, i);
    }
    ct = new ConstructorStatement(test, gc, parameters);
    testFactory.addMethod(test, method, 15, 0);
    assertEquals(16, test.size());
    TestSuiteChromosome tsc = new TestSuiteChromosome();
    tsc.addTest(test);
    TestSuiteFitnessFunction branch = new BranchCoverageSuiteFitness();
    double previous_branch_fitness = branch.getFitness(tsc);
    tsc.setFitness(branch, previous_branch_fitness);
    assertEquals(previous_branch_fitness, 0.0, 0.0);
    TestSuiteFitnessFunction defuse = new DefUseCoverageSuiteFitness();
    double previous_defuse_fitness = defuse.getFitness(tsc);
    tsc.setFitness(defuse, previous_defuse_fitness);
    assertEquals(previous_defuse_fitness, 0.0, 0.0);
    List<TestFitnessFactory<? extends TestFitnessFunction>> factories = new ArrayList<TestFitnessFactory<? extends TestFitnessFunction>>();
    factories.add(new BranchCoverageFactory());
    factories.add(new DefUseCoverageFactory());
    TestSuiteMinimizer minimizer = new TestSuiteMinimizer(factories);
    minimizer.minimize(tsc, false);
    assertEquals(1, tsc.getTests().size());
    assertEquals(5, tsc.getTests().get(0).size());
    // assertTrue(tsc.getTests().get(0).toCode().equals("FlagExample1 flagExample1_0 = new FlagExample1();\nint int0 = 28234;\nint int1 = 28241;\nflagExample1_0.testMe(int1);\nflagExample1_0.testMe(int0);\n"));
    double branch_fitness = branch.getFitness(tsc);
    assertEquals(previous_branch_fitness, branch_fitness, 0.0);
    double defuse_fitness = defuse.getFitness(tsc);
    assertEquals(previous_defuse_fitness, defuse_fitness, 0.0);
}
Also used : DefUseCoverageSuiteFitness(org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) BranchCoverageFactory(org.evosuite.coverage.branch.BranchCoverageFactory) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) DefUseCoverageFactory(org.evosuite.coverage.dataflow.DefUseCoverageFactory) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) FlagExample1(com.examples.with.different.packagename.FlagExample1) Test(org.junit.Test)

Example 83 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericMethodFromReturnValueWithSubclass.

@Test
public void testGenericMethodFromReturnValueWithSubclass() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassWithGenericMethodAndSubclass.class;
    Method targetMethod = targetClass.getMethod("wrap", new Class<?>[] { Object.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    GenericClass generatedType = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassWithGenericMethodAndSubclass.Foo<String>>() {
    }.getType());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(generatedType);
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getParameterTypes().get(0), String.class);
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 84 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericRawParameter.

@Test
public void testGenericRawParameter() throws SecurityException, NoSuchMethodException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericParameters8.class;
    Method targetMethod = targetClass.getMethod("testMe", new Class<?>[] { List.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    System.out.println(genericMethod.toString());
    System.out.println(genericMethod.getOwnerClass().toString());
    System.out.println(genericMethod.getGeneratedClass().toString());
    Assert.assertFalse(genericMethod.getOwnerClass().hasTypeVariables());
    List<GenericClass> parameters = genericMethod.getParameterClasses();
    Assert.assertTrue(parameters.get(0).hasTypeVariables());
    Assert.assertFalse(parameters.get(0).hasWildcardTypes());
    Assert.assertFalse(genericMethod.getGeneratedClass().hasWildcardTypes());
/*
		GenericMethod instantiatedMethod = genericMethod.getGenericInstantiation();
		parameters = instantiatedMethod.getParameterClasses();
		System.out.println(instantiatedMethod.toString());
		System.out.println(instantiatedMethod.getOwnerClass().toString());
		System.out.println(instantiatedMethod.getGeneratedClass().toString());
		System.out.println(parameters.toString());
		Assert.assertFalse(parameters.get(0).hasTypeVariables());
		Assert.assertFalse(parameters.get(0).hasWildcardTypes());
		Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardTypes());
		*/
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 85 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericClassWithGenericMethodAndSubclass.

@Test
public void testGenericClassWithGenericMethodAndSubclass() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassWithGenericMethodAndSubclass.class;
    Method targetMethod = targetClass.getMethod("wrap", new Class<?>[] { Object.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    Assert.assertTrue(genericMethod.getOwnerClass().hasTypeVariables());
    System.out.println(genericMethod.toString());
    System.out.println(genericMethod.getOwnerClass().toString());
    System.out.println(genericMethod.getGeneratedClass().toString());
    List<GenericClass> parameters = genericMethod.getParameterClasses();
    Assert.assertFalse(parameters.get(0).hasTypeVariables());
    Assert.assertTrue(parameters.get(0).hasWildcardTypes());
    Assert.assertTrue(genericMethod.getGeneratedClass().hasWildcardTypes());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiation();
    parameters = instantiatedMethod.getParameterClasses();
    Assert.assertFalse(parameters.get(0).hasTypeVariables());
    Assert.assertFalse(parameters.get(0).hasWildcardTypes());
    Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardTypes());
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Aggregations

GenericMethod (org.evosuite.utils.generic.GenericMethod)110 Test (org.junit.Test)68 VariableReference (org.evosuite.testcase.variable.VariableReference)64 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)60 Method (java.lang.reflect.Method)57 GenericClass (org.evosuite.utils.generic.GenericClass)40 MethodStatement (org.evosuite.testcase.statements.MethodStatement)29 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 ArrayList (java.util.ArrayList)18 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)15 TestCase (org.evosuite.testcase.TestCase)12 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)11 Injector (org.evosuite.runtime.javaee.injection.Injector)10 Type (java.lang.reflect.Type)9 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)8 EvoServletState (org.evosuite.runtime.javaee.javax.servlet.EvoServletState)8 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)8 GenericField (org.evosuite.utils.generic.GenericField)8 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)7