/** * {@inheritDoc} * * The size of a chromosome is the length of its test case */ @Override public int size() { return test.size(); }
@Override public boolean isMaximumLengthReached() { return testCase.size() > Properties.CHROMOSOME_LENGTH; }
public boolean isInitialized(int index) { return isInitialized(index, testCase.size()); }
private int getLastMutatableStatement() { ExecutionResult result = getLastExecutionResult(); if (result != null && !result.noThrownExceptions()) { int pos = result.getFirstPositionOfThrownException(); // It may happen that pos > size() after statements have been deleted if (pos >= test.size()) return test.size() - 1; else return pos; } else { return test.size() - 1; } }
private boolean isRelevant(Statement s, TestCase t) { // Always allow assertions on the last statement if (s.getPosition() == (t.size() - 1)) return true; // Allow assertions after method calls on the UUT if (s instanceof MethodStatement) { MethodStatement ms = (MethodStatement) s; String declaringClass = ms.getMethod().getDeclaringClass().getName(); while (declaringClass.contains("$")) declaringClass = declaringClass.substring(0, declaringClass.indexOf("$")); if (declaringClass.equals(Properties.TARGET_CLASS) || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && declaringClass.startsWith(Properties.TARGET_CLASS_PREFIX))) return true; } return false; }
public void inferTypes(TestCase test) { this.test = test; logger.debug("Inferring generic types"); // calculateExactTypes(); for (int i = test.size() - 1; i >= 0; i--) { Statement statement = test.getStatement(i); if (statement instanceof ConstructorStatement) { determineExactType((ConstructorStatement) statement); } } logger.debug("Resulting test: "+test.toCode()); }
/** * Simply check if there is any generator that gives us a SUT instance * * @param test * @return */ private boolean doesTestHaveSUTInstance(TestCase test) { return test.hasObject(Properties.getInitializedTargetClass(), test.size()); }
public static int countNumberOfNewInstances(TestCase test, Class<?> klass) throws IllegalArgumentException{ Inputs.checkNull(test,klass); int counter = 0; for(int i=0; i<test.size(); i++){ Statement st = test.getStatement(i); if(st instanceof ConstructorStatement){ ConstructorStatement cs = (ConstructorStatement) st; if(klass.isAssignableFrom(cs.getConstructor().getDeclaringClass())){ counter++; } } } return counter; }
private void chopException(TestCase test, ExecutionResult result) { if (!result.noThrownExceptions()) { // No code including or after an exception should be in the pool Integer pos = result.getFirstPositionOfThrownException(); if (pos != null) { test.chop(pos); } else { test.chop(test.size() - 1); } } }
private void removeAssertionsAfterException(List<ExecutionResult> results) { for(ExecutionResult result : results) { if(result.noThrownExceptions()) continue; int exceptionPosition = result.getFirstPositionOfThrownException(); // TODO: Not clear how that can happen... if(result.test.size() > exceptionPosition) result.test.getStatement(exceptionPosition).removeAssertions(); } }
/** {@inheritDoc} */ @Override public void addAssertions(TestCase other) { for (int i = 0; i < statements.size() && i < other.size(); i++) { for (Assertion a : other.getStatement(i).getAssertions()) { if (!statements.get(i).getAssertions().contains(a)) if (a != null) statements.get(i).getAssertions().add(a.clone(this)); } } }
/** {@inheritDoc} */ @Override public boolean isPrefix(TestCase t) { if (statements.size() > t.size()) return false; for (int i = 0; i < statements.size(); i++) { if (!statements.get(i).same(t.getStatement(i))) { return false; } } return true; }
/** {@inheritDoc} */ @Override public void addAssertions(TestCase test) { ExecutionResult result = runTest(test); for (OutputTrace<?> trace : result.getTraces()) { trace.getAllAssertions(test); } for (int i = 0; i < test.size(); i++) { Statement s = test.getStatement(i); if (!isRelevant(s, test)) s.removeAssertions(); } }
@Test public void testSimpleTest() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.SimpleTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.Simple.class.getCanonicalName(); Properties.SEED_MUTATIONS = 1; Properties.SEED_CLONE = 1; JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory( null); TestChromosome carved = factory.getChromosome(); Assert.assertNotNull(carved); Assert.assertEquals("Should be: constructor, method, 2 variables, method, 1 variable, method", 7, carved.test.size()); }
@Test public void testObjectWrapperSequence() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.ObjectWrapperSequenceTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.ObjectWrapper.class.getCanonicalName(); Properties.SEED_MUTATIONS = 1; Properties.SEED_CLONE = 1; JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory( null); Assert.assertTrue(factory.hasCarvedTestCases()); TestChromosome carved = factory.getChromosome(); Assert.assertNotNull(carved); Assert.assertEquals("", 6, carved.test.size()); }
@Test public void testObjectSetWrapper() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.ObjectWrapperSetTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.ObjectWrapper.class.getCanonicalName(); Properties.SEED_MUTATIONS = 1; Properties.SEED_CLONE = 1; JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory( null); Assert.assertTrue(factory.hasCarvedTestCases()); TestChromosome carved = factory.getChromosome(); Assert.assertNotNull(carved); Assert.assertEquals("", 13, carved.test.size()); }
@Test public void testObjectWrapperArray() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.ObjectWrapperArrayTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.ObjectWrapper.class.getCanonicalName(); Properties.SEED_MUTATIONS = 1; Properties.SEED_CLONE = 1; JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory( null); Assert.assertTrue(factory.hasCarvedTestCases()); TestChromosome carved = factory.getChromosome(); Assert.assertNotNull(carved); Assert.assertEquals("", 13, carved.test.size()); }
@Test public void testPersonExample() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.TestPerson.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.Person.class.getCanonicalName(); Properties.SEED_MUTATIONS = 1; Properties.SEED_CLONE = 1; JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory( null); Assert.assertTrue(factory.hasCarvedTestCases()); TestChromosome carved = factory.getChromosome(); Assert.assertNotNull(carved); String code = carved.toString(); Assert.assertEquals(code, 3, carved.test.size()); }
@Test public void testJavaAgent() { Properties.SELECTED_JUNIT = PersonWithJavaAgentSystemTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.Person.class.getCanonicalName(); Properties.SEED_MUTATIONS = 1; Properties.SEED_CLONE = 1; JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory( null); Assert.assertTrue(factory.hasCarvedTestCases()); TestChromosome carved = factory.getChromosome(); Assert.assertNotNull(carved); String code = carved.toString(); Assert.assertEquals(code, 3, carved.test.size()); }
@Ignore @Test public void test4() { TestSuiteChromosome suite = generateSuite(ExampleStaticVoidSetterClass.class); Assert.assertTrue(suite.size() > 0); for (TestCase test : suite.getTests()) { if (test.size() > 1) Assert.assertTrue("Test has no assertions: " + test.toCode(), test.hasAssertions()); } }