/** * <p> * minimize * </p> * * @param test a {@link org.evosuite.testcase.TestChromosome} object. * @param objective a {@link org.evosuite.testcase.TestFitnessFunction} object. */ public void minimize(TestChromosome test, TestFitnessFunction objective) { this.objective = new TestMinimization(objective, test); test.test.accept(this); }
/** {@inheritDoc} */ @Override public String getTestString(int id, TestCase test, Map<Integer, Throwable> exceptions, TestCodeVisitor visitor) { visitor.setExceptions(exceptions); test.accept(visitor); visitor.clearExceptions(); return visitor.getCode(); }
/** {@inheritDoc} */ @Override public String getTestString(int id, TestCase test, Map<Integer, Throwable> exceptions, TestCodeVisitor visitor) { visitor.setExceptions(exceptions); test.accept(visitor); visitor.clearExceptions(); return visitor.getCode(); } }
visitor.clearExceptions(); visitor.setExceptions(result.exposeExceptionMapping()); result.test.accept(visitor); imports.addAll(visitor.getImports()); accessedClasses.addAll(result.test.getAccessedClasses());
/** * <p> * minimize * </p> * * @param suite a {@link org.evosuite.testsuite.TestSuiteChromosome} object. * @param objective a {@link org.evosuite.testsuite.TestSuiteFitnessFunction} * object. */ public void minimize(TestSuiteChromosome suite, TestSuiteFitnessFunction objective) { int i = 0; objective.getFitness(suite); // Ensure all tests have an execution result cached for (TestChromosome test : suite.getTestChromosomes()) { this.objective = new SuiteMinimization(objective, suite, i); test.test.accept(this); i++; } }
@Test public void testInnerClassAbstractEnum() throws NoSuchMethodException, ConstructionFailedException { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(AbstractEnumUser.class.getDeclaredConstructor(), AbstractEnumUser.class), 0, 0); EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, AbstractEnumInInnerClass.AnEnum.class); primitiveStatement.setValue(AbstractEnumInInnerClass.AnEnum.FOO); VariableReference enumObject = tc.addStatement(primitiveStatement); Method m = AbstractEnumUser.class.getDeclaredMethod("foo", AbstractEnumInInnerClass.AnEnum.class); GenericMethod gm = new GenericMethod(m, AbstractEnumUser.class); MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject)); tc.addStatement(ms); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception String code = visitor.getCode(); System.out.println(code); assertFalse(code.contains("= AbstractEnumInInnerClass.AnEnum.1.FOO")); assertTrue(code.contains("= AbstractEnumInInnerClass.AnEnum.FOO")); } }
@Test public void testInnerClassEnum() throws Throwable { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(EnumUser.class.getDeclaredConstructor(), EnumUser.class), 0, 0); EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, EnumInInnerClass.AnEnum.class); primitiveStatement.setValue(EnumInInnerClass.AnEnum.FOO); VariableReference enumObject = tc.addStatement(primitiveStatement); Method m = EnumUser.class.getDeclaredMethod("foo", EnumInInnerClass.AnEnum.class); GenericMethod gm = new GenericMethod(m, EnumUser.class); MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject)); tc.addStatement(ms); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception String code = visitor.getCode(); assertTrue(code.contains("= EnumInInnerClass.AnEnum.FOO")); }
@Test public void testClashingImportNames() throws NoSuchMethodException, ConstructionFailedException { TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class.getDeclaredConstructor(), com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class), 0, 0); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class.getDeclaredConstructor(), com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class), 1, 0); TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); System.out.println(visitor.getCode()); Set<Class<?>> imports = visitor.getImports(); // Imported assertTrue(imports.contains(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class)); // Not imported as the fully qualified name is used assertFalse(imports.contains(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class)); assertEquals("ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class)); assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class)); }
@Test public void testGenerics_methodWithExtends() throws NoSuchMethodException, ConstructionFailedException { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); VariableReference genericClass = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(ClassWithGeneric.class.getDeclaredConstructor(), ClassWithGeneric.class), 1, 0); Method m = ClassWithGeneric.class.getDeclaredMethod("hello", Servlet.class); GenericMethod gm = new GenericMethod(m, ClassWithGeneric.class); TestFactory.getInstance().addMethodFor(tc, genericClass, gm, 2); //Check if generic types were correctly analyzed/inferred Type[] types = gm.getParameterTypes(); assertEquals(1, types.length); //only 1 input Type type = types[0]; Assert.assertNotNull(type); TypeVariable<?> tv = (TypeVariable<?>) type; assertEquals(1, tv.getBounds().length); Class<?> upper = (Class<?>) tv.getBounds()[0]; assertEquals(Servlet.class, upper); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception }
@Test public void testClashingImportNamesSubClasses() throws NoSuchMethodException, ConstructionFailedException { TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class.getDeclaredConstructor(), com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class), 0, 0); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class.getDeclaredConstructor(), com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class), 1, 0); TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); System.out.println(visitor.getCode()); Set<Class<?>> imports = visitor.getImports(); // Imported assertTrue(imports.contains(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class)); // Not imported as the fully qualified name is used assertFalse(imports.contains(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class)); assertEquals("ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class)); assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class)); assertEquals("ExampleWithInnerClass.Foo", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class)); assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class)); }
@Test public void testGenerics_staticMethod() throws NoSuchMethodException, ConstructionFailedException { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(Object.class.getDeclaredConstructor(), Object.class), 0, 0); Method m = TestCodeVisitorTest.class.getDeclaredMethod("bar", Object.class); GenericMethod gm = new GenericMethod(m, TestCodeVisitorTest.class); TestFactory.getInstance().addMethod(tc, gm, 1, 0); //Check if generic types were correctly analyzed/inferred Type[] types = gm.getParameterTypes(); assertEquals(1, types.length); //only 1 input Type type = types[0]; Assert.assertNotNull(type); WildcardTypeImpl wt = (WildcardTypeImpl) type; assertEquals(0, wt.getLowerBounds().length); assertEquals(1, wt.getUpperBounds().length); Class<?> upper = (Class<?>) wt.getUpperBounds()[0]; assertEquals(Object.class, upper); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception System.out.println(visitor.getCode()); }
@Test public void testGenerics_staticMethodWithExtends() throws NoSuchMethodException, ConstructionFailedException { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); Method m = TestCodeVisitorTest.class.getDeclaredMethod("foo", Servlet.class); GenericMethod gm = new GenericMethod(m, TestCodeVisitorTest.class); TestFactory.getInstance().addMethod(tc, gm, 1, 0); //Check if generic types were correctly analyzed/inferred Type[] types = gm.getParameterTypes(); assertEquals(1, types.length); //only 1 input Type type = types[0]; Assert.assertNotNull(type); WildcardTypeImpl wt = (WildcardTypeImpl) type; assertEquals(0, wt.getLowerBounds().length); assertEquals(1, wt.getUpperBounds().length); Class<?> upper = (Class<?>) wt.getUpperBounds()[0]; assertEquals(Object.class, upper); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception }