/** * Provided so that subtypes of this factory type can modify the returned * TestCase * * @return a {@link org.evosuite.testcase.TestCase} object. */ protected TestCase getNewTestCase() { return new DefaultTestCase(); }
public VariableReference appendIntPrimitive(int intValue) { IntPrimitiveStatement primitiveStmt = new IntPrimitiveStatement(tc, intValue); tc.addStatement(primitiveStmt); return primitiveStmt.getReturnValue(); }
/** {@inheritDoc} */ @Override public VariableReference getRandomNonNullObject(Type type, int position) throws ConstructionFailedException { Inputs.checkNull(type); List<VariableReference> variables = getObjects(type, position); Iterator<VariableReference> iterator = variables.iterator(); while (iterator.hasNext()) { VariableReference ref = iterator.next(); if (ref instanceof NullReference || (this.getStatement(ref.getStPosition()) instanceof FunctionalMockStatement) ) { iterator.remove(); } } if (variables.isEmpty()) throw new ConstructionFailedException("Found no variables of type " + type + " at position " + position); return Randomness.choice(variables); }
/** {@inheritDoc} */ @Override public int sizeWithAssertions() { return this.size() + this.getAssertions().size(); }
@Test public void testIntegerDependency() throws ConstructionFailedException, NoSuchMethodException, SecurityException { TestFactory testFactory = TestFactory.getInstance(); GenericMethod method = new GenericMethod( FactoryExample.class.getMethod("testByte", byte.class, byte.class), FactoryExample.class); DefaultTestCase test = new DefaultTestCase(); Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0; testFactory.addMethod(test, method, 0, 0); String code = test.toCode(); System.out.println(code); assertEquals(4, test.size()); assertTrue(code.contains("factoryExample0.testByte(byte0, byte1)")); }
@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(); test2.addStatement(new IntPrimitiveStatement(test2, 2)); // Need to add statements to change hashCode 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); }
System.out.println(genericMethod.getGeneratedClass().toString()); DefaultTestCase test = new DefaultTestCase(); MethodStatement ms1 = new MethodStatement(test, genericCreatorMethod, (VariableReference) null, new ArrayList<VariableReference>()); test.addStatement(ms1); test.addStatement(ps1); test.addStatement(ms2); ms2.addAssertion(assertion); String code = test.toCode(); Properties.CRITERION[0] = Criterion.MUTATION; DefaultTestCase testCopy = test.clone(); testCopy.changeClassLoader(loader); String code2 = testCopy.toCode(); Assert.assertEquals(code, test.toCode()); testCopy.removeAssertion(assertion); Assert.assertEquals(code, test.toCode()); test.removeAssertions(); System.out.println(test.toCode());
/** {@inheritDoc} */ @Override public String toString() { return toCode(); } }
@Test public void testGetCandidatesForReuse() throws ClassNotFoundException, NoSuchFieldException, ConstructionFailedException, NoSuchMethodException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); Properties.PRIMITIVE_REUSE_PROBABILITY = 1.0; Properties.OBJECT_REUSE_PROBABILITY = 1.0; DefaultTestCase test = new DefaultTestCase(); GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut); VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0); test.addStatement(new CharPrimitiveStatement(test,'-')); GenericMethod method = new GenericMethod(sut.getMethod("testInt", int.class), sut); testFactory.addMethodFor(test, var1, method, 2); MethodStatement stmt = (MethodStatement)test.getStatement(test.size()-1); VariableReference var = stmt.getParameterReferences().get(0); assertNotEquals("Char should not be passed as Integer", var.getType(), char.class); assertEquals("Incorrect test size", 4, test.size()); } }
@Test public void testDownCastUnnecessaryForInspectorAssertion() throws NoSuchMethodException { TestCaseBuilder builder = new TestCaseBuilder(); VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor()); VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo")); num0.setType(ConcreteSubclass.class); // This would be set during execution DefaultTestCase test = builder.getDefaultTestCase(); Inspector inspector = new Inspector(ConcreteSubclass.class, ConcreteSubclass.class.getMethod("getFoo")); InspectorAssertion assertion = new InspectorAssertion(inspector, test.getStatement(num0.getStPosition()), num0, true); test.getStatement(num0.getStPosition()).addAssertion(assertion); test.removeDownCasts(); System.out.println(test); assertEquals(AbstractSuperclass.class, test.getStatement(1).getReturnClass()); }
@Test public void testFieldReferenceNeedsDowncast() throws NoSuchMethodException, NoSuchFieldException { TestCaseBuilder builder = new TestCaseBuilder(); VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor()); VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getAbstractFoo")); num0.setType(ConcreteSubclass.class); // This would be set during execution VariableReference bool0 = builder.appendBooleanPrimitive(true); DefaultTestCase test = builder.getDefaultTestCase(); FieldReference fr = new FieldReference(test, new GenericField(ConcreteSubclass.class.getField("fieldInConcreteClass"), ConcreteSubclass.class), num0); AssignmentStatement statement = new AssignmentStatement(test, fr, bool0); test.addStatement(statement); test.removeDownCasts(); System.out.println(test); FieldReference fr2 = (FieldReference)test.getStatement(3).getReturnValue(); assertEquals(ConcreteSubclass.class, fr2.getSource().getVariableClass()); }
@Test public void minimizeSuiteOnlyWithVariables() { DefaultTestCase test = new DefaultTestCase(); for (int i = 0; i < 10; i++) { IntPrimitiveStatement ips = new IntPrimitiveStatement(test, i); test.addStatement(ips); } assertEquals(10, 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(0.0, previous_fitness, 0.0); TestSuiteMinimizer minimizer = new TestSuiteMinimizer(new BranchCoverageFactory()); minimizer.minimize(tsc, false); assertEquals(0, tsc.getTests().size()); double fitness = ff.getFitness(tsc); assertEquals(previous_fitness, fitness, 0.0); }
public void constructPopulation(){ Properties.POPULATION = 16; TestSuiteChromosome[] testSuiteChromObjects = new TestSuiteChromosome[Properties.POPULATION]; for(int i=0; i<testSuiteChromObjects.length; i++){ testSuiteChromObjects[i] = new TestSuiteChromosome(); DefaultTestCase testcase = new DefaultTestCase(); StringPrimitiveStatement stringStatement = new StringPrimitiveStatement(testcase, "any string statatement for TS"+i); testcase.addStatement(stringStatement); testSuiteChromObjects[i].addTest(testcase); population.add(testSuiteChromObjects[i]); } }
@Test public void testClone(){ DefaultTestCase tc = new DefaultTestCase(); DefaultTestCase clone = (DefaultTestCase) tc.clone(); Assert.assertTrue(tc.statements != clone.statements); } }
public void addException(Throwable exception) { int currentPos = this.tc.size()-1; if (currentPos<0) throw new IllegalStateException("Cannot add exception to empty test case"); if (exceptions.containsKey(currentPos)) { throw new IllegalStateException("Statement already contains an exception!"); } exceptions.put(currentPos, exception); } }
public void changeClassLoader(ClassLoader classLoader) { ((DefaultTestCase) test).changeClassLoader(classLoader); contract.changeClassLoader(classLoader); this.statement = this.test.getStatement(statement.getPosition()); for (int i = 0; i < variables.size(); i++) { variables.set(i, variables.get(i).clone(test)); } }
/** {@inheritDoc} */ @Override public VariableReference getReturnValue(int position) { return getStatement(position).getReturnValue(); }
VM.getInstance().prepareConcolicExecution(); defaultTestCase.getChangedClassLoader(); defaultTestCase.changeClassLoader(classLoader); SymbolicObserver symbolicExecObserver = new SymbolicObserver(env);
private void removeUnnecessaryDownCasts(List<ExecutionResult> results) { for(ExecutionResult result : results) { if(result.test instanceof DefaultTestCase) { ((DefaultTestCase)result.test).removeDownCasts(); } } }