/** {@inheritDoc} */ @Override public boolean isValid() { for (Statement s : statements) { assert (s.isValid()) : toCode(); } return true; }
/** * Method used to mutate an offspring. * * @param offspring * @param parent */ private void mutate(T offspring, T parent) { offspring.mutate(); TestChromosome tch = (TestChromosome) offspring; if (!offspring.isChanged()) { // if offspring is not changed, we try to mutate it once again offspring.mutate(); } if (!this.hasMethodCall(offspring)) { tch.setTestCase(((TestChromosome) parent).getTestCase().clone()); boolean changed = tch.mutationInsert(); if (changed) { for (Statement s : tch.getTestCase()) { s.isValid(); } } offspring.setChanged(changed); } this.notifyMutation(offspring); }
@Test public void testInnerCalls() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.InnerCallsTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.InnerCalls.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.assertNotNull(code); Assert.assertEquals(code, 4, carved.test.size()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } System.out.println(code); }
@Test public void testGenericParameter() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericTest.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("", 5, carved.test.size()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } String code = carved.toString(); String setLong = "HashSet<Long>"; Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong)); }
@Test public void testGenericClassArray() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperArrayTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.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()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } String code = carved.toString(); String setLong = "GenericObjectWrapper<Long[]>"; Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong)); }
@Test public void testInnerConstructor() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.InnerConstructorTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.InnerConstructor.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.assertNotNull(code); Assert.assertEquals(code, 2, carved.test.size()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } System.out.println(code); }
@Test public void testGenericClassList() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithListTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithList.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("", 10, carved.test.size()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } String code = carved.toString(); String setLong = "GenericObjectWrapperWithList<GenericObjectWrapperWithListTest.Foo>"; Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong)); }
@Test public void testGenericClassSequence() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperSequenceTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.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()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } String code = carved.toString(); String setLong = "GenericObjectWrapper<GenericObjectWrapperSequenceTest.Foo>"; Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong)); code = carved.toString(); setLong = "(Object)"; Assert.assertFalse("generated code contains object cast " + setLong + "\n" + code, code.contains(setLong)); }
@Test public void testGenericClassTwoParameter() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperTwoParameterTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperTwoParameter.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("", 8, carved.test.size()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } String code = carved.toString(); String setLong = "GenericObjectWrapperTwoParameter<String, String>"; Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong)); }
@Test public void testGenericClassSet() { Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperSetTest.class.getCanonicalName(); Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.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()); for (int i = 0; i < carved.test.size(); i++) { Statement stmt = carved.test.getStatement(i); boolean valid = stmt.isValid(); Assert.assertTrue("Invalid stmt at position " + i, valid); } String code = carved.toString(); String setLong = "GenericObjectWrapper<HashSet<Long>>"; Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong)); code = carved.toString(); setLong = "(Object)"; Assert.assertFalse("generated code contains object cast " + setLong + "\n" + code, code.contains(setLong)); }