/** * {@inheritDoc} * * Generate a random chromosome */ @Override public TestChromosome getChromosome() { TestChromosome c = new TestChromosome(); c.setTestCase(getRandomTestCase(Properties.CHROMOSOME_LENGTH)); return c; }
/** * {@inheritDoc} * * Generate a random chromosome */ @Override public TestChromosome getChromosome() { TestChromosome c = new TestChromosome(); c.setTestCase(getRandomTestCase(Properties.CHROMOSOME_LENGTH)); return c; }
/** * Add a test to a test suite * * @param test * a {@link org.evosuite.testcase.TestCase} object. */ public TestChromosome addTest(TestCase test) { TestChromosome c = new TestChromosome(); c.setTestCase(test); addTest(c); return c; }
/** * {@inheritDoc} * * Generate a random chromosome */ @Override public TestChromosome getChromosome() { TestChromosome c = new TestChromosome(); c.setTestCase(getRandomTestCase(Properties.CHROMOSOME_LENGTH)); return c; }
/** * Helper function if this is used without a chromosome * * @param result * @return */ public boolean isCovered(ExecutionResult result) { TestChromosome chromosome = new TestChromosome(); chromosome.setTestCase(result.test); chromosome.setLastExecutionResult(result); chromosome.setChanged(false); return isCovered(chromosome, result); }
@Test public void testSerializationEmptyTest() throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); double fitness = 3.14; TestChromosome chromosome = new TestChromosome(); chromosome.setFitness(null, fitness); oos.writeObject(chromosome); byte [] baSerialized = baos.toByteArray(); ByteArrayInputStream bais = new ByteArrayInputStream(baSerialized); ObjectInputStream ois = new ObjectInputStream(bais); TestChromosome copy = (TestChromosome) ois.readObject(); Assert.assertEquals(chromosome.getFitness(), copy.getFitness(), 0.0); }
@Test public void testNoDirectInsertion() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); GenericMethod gm = new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("noDirect", null), ConstraintVerifierTest.class); //should not be possible to insert it int pos = ConstraintVerifier.getAValidPositionForInsertion(gm, tc.getTestCase(), 0); Assert.assertTrue(pos < 0); }
@Test public void compareBetterRank() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(0); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(1); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(); double result = comparator.compare(tch1, tch2); assertEquals(-1, result, 0.00001); }
@Test public void compareBetterRank_maximization() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(0); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(1); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(true); double result = comparator.compare(tch1, tch2); assertEquals(+1, result, 0.00001); }
@Test public void compareHigherRank_maximization() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(1); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(0); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(true); double result = comparator.compare(tch1, tch2); assertEquals(-1, result, 0.00001); }
@Test public void compareHigherRank() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(1); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(0); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(); double result = comparator.compare(tch1, tch2); assertEquals(+1, result, 0.00001); }
@Test public void compareEqual_maximization() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(0); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(0); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(); comparator.setMaximize(true); double result = comparator.compare(tch1, tch2); assertEquals(0, result, 0.00001); }
@Test public void testEvoSuiteClassExclude() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //shouldn't be able to instantiate EvoServletConfig directly factory.addConstructor(tc.getTestCase(), new GenericConstructor(EvoServletConfig.class.getConstructor(), EvoServletConfig.class), 0, 0); Assert.assertEquals(1, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); } }
@Test public void testBaseTest() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); factory.addConstructor(tc.getTestCase(), new GenericConstructor(Object.class.getConstructor(), Object.class), 0, 0); Assert.assertEquals(1, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void compareBetterDistance() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setRank(0); tch1.setDistance(1); TestChromosome tch2 = new TestChromosome(); tch2.setTestCase(new DefaultTestCase()); tch2.setRank(0); tch2.setDistance(0); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(); double result = comparator.compare(tch1, tch2); assertEquals(-1, result, 0.00001); }
@Test public void testInitializingBoundedVariable_wrong_noConstructor() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getAFakeServletInstance"), ConstraintVerifierTest.class), 0, 0); //initializing bounding variable method called on instance not generated with new factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0); Assert.assertEquals(2, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }
@Test public void testInitializingBoundedVariable_wrong_callingMethodsBeforeInit() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 1); //initializing bounding variable method cannot be called here after "foo" is called on the bounded variable factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }
@Test public void testNull() { BranchCoverageGoal goal = Mockito.mock(BranchCoverageGoal.class); BranchCoverageTestFitness fitness = new BranchCoverageTestFitness(goal); TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); tch1.setFitness(fitness, 0); PreferenceSortingComparator comparator = new PreferenceSortingComparator(fitness); double value = comparator.compare(tch1, null); assertEquals(-1, value, 0.0001); value = comparator.compare(null, tch1); assertEquals(+1, value, 0.0001); }
@Test public void testExcludeMethod() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //'reset' is a method that shouldn't be used in a test factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("reset"), EvoServletState.class), 0, 0); Assert.assertEquals(tc.getTestCase().toCode(), 1, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }
@Test public void testNull() { TestChromosome tch1 = new TestChromosome(); tch1.setTestCase(new DefaultTestCase()); RankAndCrowdingDistanceComparator comparator = new RankAndCrowdingDistanceComparator(); double value = comparator.compare(tch1, null); assertEquals(-1, value, 0.0001); value = comparator.compare(null, tch1); assertEquals(+1, value, 0.0001); } }