Beispiel #1
0
def main():
    options = to_options_gen(arg_parser_gen())

    jdk = JDK(options.java_home)

    classes_dir = _maven_work(options, jdk)

    _create_mutants_dir(options)

    tool = MuJava(options.mutants, jdk=jdk, classpath=classes_dir)

    state = _recover_state(options)
    db = _initialize_db(options)
    targets = state[0]
    analysed_files = state[1]

    count = 1
    files = get_java_files(options.java_src)

    for i, file in enumerate(sort_files(files)):
        print('PROCESSING {0} {1}/{2}'.format(file, i + 1, len(files)))
        if file not in analysed_files['files']:
            t = tool.generate(classes_dir, options.java_src, file,
                              len(targets))
            print('\ttargets found: {0}'.format(len(t)))
            targets += t
            for target in t:
                target['mutants'] = _run_hunor(options, target)

            _persist_targets(db, t)
            _save_state(options, state, t, file)

            count += 1
Beispiel #2
0
def main():
    options = to_options_gen(arg_parser_gen())

    _create_mutants_dir(options)
    tool = HunorPlugin(options)
    state = _recover_state(options)
    db = _initialize_db(options)
    targets = state[0]
    analysed_files = state[1]

    files = get_java_files(options.java_src)

    for i, file in enumerate(sort_files(files)):
        print('PROCESSING {0} {1}/{2}'.format(file, i + 1, len(files)))
        if file not in analysed_files['files']:
            t = tool.generate(file, len(targets))
            print('\ttargets found: {0}'.format(len(t)))
            targets += t
            for target in t:
                print('-' * (23 + len(target['directory'])))
                print('| RUNNING HUNOR FOR: {0} |'.format(target['directory']))
                print('-' * (23 + len(target['directory'])))
                target['mutants'] = _run_hunor(options, target)

            _persist_targets(db, t)
            _save_state(options, state, t, file)
Beispiel #3
0
def main():
    source_dir = PROJECT_DIR
    files = get_java_files(source_dir)
    targets = []

    for file in files:
        targets += get_targets(source_dir, file, len(targets))

    write_config_json(targets)
Beispiel #4
0
    def _get_java_files(self):
        ordered_files = []

        for file in sorted(get_java_files(self.suite_dir)):
            if '_scaffolding' in file:
                ordered_files.insert(0, file)
            else:
                ordered_files.append(file)

        return ordered_files
Beispiel #5
0
    def _compile(self):
        os.mkdir(self.tests_classes)

        classpath = generate_classpath([self.classpath, self.tests_src, JUNIT,
                                        HAMCREST])

        for java_test_file in sorted(get_java_files(self.tests_src)):
            self.jdk.run_javac(os.path.join(self.tests_src, java_test_file),
                               5 * 60, self.tests_src, '-classpath', classpath,
                               '-d', self.tests_classes)

        return self._test_classes()
Beispiel #6
0
def main():
    options = to_options_gen(arg_parser_gen())

    _create_mutants_dir(options)
    tool = HunorPlugin(options)
    state = _recover_state(options)
    db = _initialize_db(options)
    targets = state[0]
    analysed_files = state[1]

    files = get_java_files(options.java_src)

    targets_count = {}
    class_count = {}

    if os.path.exists('targets_count.json'):
        targets_count = read_json('targets_count.json')

    if os.path.exists('class_count.json'):
        class_count = read_json('class_count.json')

    for i, file in enumerate(sort_files(files)):
        print('PROCESSING {0} {1}/{2}'.format(file, i + 1, len(files)))
        if file not in analysed_files['files']:
            t = tool.generate(file, len(targets))
            print('\ttargets found: {0}'.format(len(t)))
            targets += t
            for target in t:
                if target['target_repr'] not in targets_count.keys():
                    targets_count[target['target_repr']] = 0

                targets_count[target['target_repr']] += 1

                if target['class'] not in class_count.keys():
                    class_count[target['class']] = 0

                class_count[target['class']] += 1

            write_json(targets_count, 'targets_count')
            write_json(class_count, 'class_count')
            _persist_targets(db, t)
            _save_state(options, state, t, file)
Beispiel #7
0
def main():
    options = to_options_gen(arg_parser_gen())
    configure_logger()
    project_dir = os.path.abspath(
        os.sep.join(config(options.config_file)['source']))

    logger.debug(project_dir)

    java = JavaFactory.get_instance()
    maven = MavenFactory.get_instance()

    build = maven.compile(project_dir, clean=True)

    junit = JUnit(java=java, classpath=build.classes_dir)

    _create_mutants_dir(options)

    options.is_enable_reduce = False
    tool = HunorPlugin(copy.deepcopy(options))

    options.is_enable_reduce = True
    tool_reduced = HunorPlugin(copy.deepcopy(options))

    state = _recover_state(options)
    targets = state[0]
    analysed_files = state[1]

    files = get_java_files(options.java_src)

    for i, file in enumerate(sort_files(files)):
        logger.info('EVALUATING {0} {1}/{2}'.format(file, i + 1, len(files)))
        if file not in analysed_files['files'] and include(project_dir, file):
            t = tool.generate(file, len(targets))
            t_r = tool_reduced.generate(file, len(targets))

            logger.info('\ttargets found: {0}, in reduced: {1}'.format(
                len(t), len(t_r)))
            targets += t
            count = 1
            for target in t_r:
                if not include(project_dir, file, target):
                    logger.info("skipping target {0}.".format(target['oid']))
                    continue

                log = '| RUNNING FOR: {0} {1}/{2} {3} (#{4})|'.format(
                    target['directory'], count, len(t_r),
                    target['target_repr'], target['oid'])
                count += 1
                mutants_dir = os.path.join(options.mutants + '_reduced',
                                           target['directory'])
                logger.info('-' * len(log))
                logger.info(log)
                logger.info('-' * len(log))

                mutation_tool = MuJava(mutants_dir)
                mutants = mutation_tool.read_log()
                tce = TCE(options, build, target)

                suites = []
                count_assertions = 0
                has_error = False
                for mutant in mutants:
                    if not has_error:
                        mutant = mutants[mutant]
                        evosuite = Evosuite(
                            java=java,
                            classpath=os.path.join(build.classes_dir),
                            tests_src=os.path.join(mutants_dir, 'suites'),
                            sut_class=target['class'],
                            params=['-Dsearch_budget=60'])

                        suite = evosuite.generate_differential(mutant.path)

                        logger.debug(
                            'Test suite created to %s mutation with'
                            ' %i assertions.', mutant.mutation,
                            suite.tests_with_assertion)

                        r = junit.exec_suite_with_mutant(
                            suite, target['class'], mutant)

                        if not JUnit.check_pass(r):
                            count_assertions += suite.tests_with_assertion
                            suites.append(suite)
                        else:
                            logger.info(
                                'The suite not kill the mutant, %s '
                                'EvosuiteR fail. :(', mutant.mutation)
                            has_error = True

                if not has_error:
                    result = True

                    logger.debug('Testing all %i suites in original program',
                                 len(suites))
                    for suite in suites:
                        result = (result and JUnit.check_pass(
                            junit.exec_suite(suite, target['class']))
                                  and suite.tests_with_assertion > 0)

                    if result:
                        logger.debug('All tests created and not failing in '
                                     'original program.')
                    else:
                        logger.warning(
                            'Any suite is empty or failing in original '
                            'program.')

                    mutants_full_dir = os.path.join(options.mutants,
                                                    target['directory'])

                    all_mutants = MuJava(mutants_full_dir).read_log()
                    equivalent_mutants = set()
                    total_mutants = len(all_mutants)

                    if (result and mutants and len(suites) == len(mutants)):

                        killed_mutants = set()
                        not_killed_mutants = set()

                        for mutant in all_mutants:
                            mutant = all_mutants[mutant]
                            if not os.path.exists(mutant.path):
                                total_mutants -= 1
                                continue

                            result = junit.exec_suites_with_mutant(
                                suites, target['class'], mutant)

                            if not JUnit.check_pass(result):
                                logger.debug(
                                    'Mutation %s (%s) was killed by %i tests.',
                                    mutant.mutation, mutant.id,
                                    result.fail_tests)
                                killed_mutants.add(mutant)
                            else:
                                logger.debug(
                                    'Mutation %s (%s) was not killed.',
                                    mutant.mutation, mutant.id)

                                if tce.run(mutant):
                                    equivalent_mutants.add(mutant)
                                    logger.debug('\tBut it is equivalent.')
                                not_killed_mutants.add(mutant)

                        not_equivalent_mutants = (total_mutants -
                                                  len(equivalent_mutants))

                        percent = (len(killed_mutants) /
                                   not_equivalent_mutants)
                        logger.info(
                            '%i mutants of %i were killed by tests. (%.2f)',
                            len(killed_mutants), not_equivalent_mutants,
                            percent),

                        headers = [
                            'id', 'target', 'mutants_in_minimal',
                            'test_suites', 'assertions', 'mutants',
                            'killed_mutants', 'not_equivalent_mutants', '%',
                            'differential_success', 'minimal', 'survive',
                            'killed', 'equivalent', 'class', 'method', 'line',
                            'column', 'statement', 'operator'
                        ]

                        write_to_csv(headers, [
                            str(target['id']), target['target_repr'],
                            str(len(mutants)),
                            str(len(suites)),
                            str(count_assertions),
                            str(total_mutants),
                            str(len(killed_mutants)),
                            str(not_equivalent_mutants),
                            str(percent),
                            str(len(suites) == len(mutants)), ','.join([
                                mutants[m].mutation for m in mutants
                            ]), ','.join([
                                m.mutation
                                for m in not_killed_mutants.difference(
                                    equivalent_mutants)
                            ]), ','.join([m.mutation for m in killed_mutants]),
                            ','.join([m.mutation for m in equivalent_mutants
                                      ]), target['class'], target['method'],
                            str(target['line']),
                            str(target['column']), target['statement'],
                            target['operator']
                        ],
                                     output_dir=options.mutants)

                    if mutants and len(suites) < len(mutants):
                        headers = [
                            'id', 'class', 'method', 'line', 'column',
                            'statement', 'operator'
                        ]
                        write_to_csv(headers, [
                            str(target['id']), target['class'],
                            target['method'],
                            str(target['line']),
                            str(target['column']), target['statement'],
                            target['operator']
                        ],
                                     output_dir=options.mutants,
                                     filename='not_tested.csv')

                _save_state(options, state, t_r, file)
Beispiel #8
0
 def _get_java_files(self):
     return sorted(get_java_files(self.suite_dir))
Beispiel #9
0
def equivalence_analysis(jdk,
                         junit,
                         classpath,
                         test_suites,
                         mutants,
                         mutation_tool,
                         sut_class,
                         coverage_threshold,
                         output,
                         mutants_dir,
                         using_target=False):

    if mutation_tool == 'pit':
        mutation_tool = Pit(mutants, sut_class)
    elif mutation_tool == 'major':
        mutation_tool = Major(mutants)
    elif mutation_tool == 'mujava':
        mutation_tool = MuJava(mutants)

    mutants = mutation_tool.read_log()

    with open(os.path.join(output, 'equivalents.csv'), 'w') as f:
        f.write('id,maybe_equivalent,not_equivalent,coverage\n')
        f.close()

    original_dir = os.path.join(mutants_dir, 'ORIGINAL')
    ori_coverage = 0
    ori_tests_total = 0
    line_coverage = 0

    if using_target and len(list(mutants.keys())) > 0:
        line_coverage = mutants[list(mutants.keys())[0]].line_number

    if os.path.exists(original_dir):
        ori_test_suites = junit.run_test_suites(test_suites, original_dir,
                                                line_coverage)
        for t in ori_test_suites:
            test_suites[t].elapsed_time = ori_test_suites[t].elapsed_time
            ori_coverage += ori_test_suites[t].coverage
            ori_tests_total += ori_test_suites[t].tests_total
            if ori_test_suites[t].tests_total == 0:
                test_suites[t].is_valid = False

    coverage_threshold = float(coverage_threshold)
    if coverage_threshold < 1:
        coverage_threshold = math.ceil(coverage_threshold * ori_tests_total)
    else:
        coverage_threshold = math.ceil(coverage_threshold)

    if using_target and ori_coverage < coverage_threshold:
        print('WARNING: Not enough coverage for this target.' +
              ' coverage: {0}, test cases: {1}'.format(ori_coverage,
                                                       ori_tests_total))
        return None

    print('RUNNING TEST SUITES FOR ALL MUTANTS...')
    begin = datetime.now()
    for i, m in enumerate(mutants):
        mutant_begin = datetime.now()
        mutant = mutants[m]
        print('\tmutant: {0}... {1}/{2}'.format(mutant, i + 1, len(mutants)))

        if os.path.exists(mutant.path):
            compile_success = True
            for java_file in get_java_files(mutant.path):
                compile_success = compile_success and jdk.run_javac(
                    java_file, 60, mutant.path, "-classpath", classpath)

            if compile_success:
                mutant.result.test_suites = junit.run_test_suites(
                    test_suites, mutant.path, mutant.line_number, original_dir)
                coverage = 0
                fail = False
                maybe_in_loop = False
                coverage_log = []
                tests_total = 0
                fail_tests_total = 0
                fail_tests = set()

                for r in mutant.result.test_suites:
                    coverage += mutant.result.test_suites[r].coverage
                    fail = fail or mutant.result.test_suites[r].fail
                    maybe_in_loop = (maybe_in_loop
                                     or mutant.result.test_suites[r].fail)
                    tests_total += mutant.result.test_suites[r].tests_total
                    fail_tests_total += (
                        mutant.result.test_suites[r].fail_tests_total)
                    fail_tests = fail_tests.union(
                        mutant.result.test_suites[r].fail_tests)

                    coverage_log.append('{0}: {1}'.format(
                        r, mutant.result.test_suites[r].coverage))

                print('\t\tcoverage: {0}/{4} ({1}) tests fail: {2}/{3}'.format(
                    coverage, ', '.join(coverage_log), fail_tests_total,
                    tests_total, coverage_threshold))

                if tests_total > 0 or maybe_in_loop:
                    with open(os.path.join(output, 'equivalents.csv'),
                              'a') as f:
                        if coverage >= coverage_threshold and not fail:
                            print('\t\t +++ THIS MUTANT MAY BE EQUIVALENT!')
                            mutant.maybe_equivalent = True
                            f.write('{0},{1},{2},{3}\n'.format(
                                mutant.id, 'x', '', coverage))
                        elif fail:
                            print('\t\t --- THIS MUTANT IS NOT EQUIVALENT!')
                            f.write('{0},{1},{2},{3}\n'.format(
                                mutant.id, '', 'x', coverage))
                        else:
                            f.write('{0},{1},{2},{3}\n'.format(
                                mutant.id, '', '', coverage))
                        f.close()
                else:
                    mutant.is_invalid = True
            else:
                print('\t\tWARNING: mutant not compile: {0}'.format(
                    mutant.path))
                mutant.is_invalid = True
        else:
            print('\t\tWARNING: mutant directory not found: {0}'.format(
                mutant.path))
            mutant.is_invalid = True

        print('\t********************************* {0}'.format(datetime.now() -
                                                               mutant_begin))
    print('############ END ANALYSIS ########### {0}'.format(datetime.now() -
                                                             begin))

    return mutants
Beispiel #10
0
def main():
    options = to_options_gen(arg_parser_gen())
    configure_logger()

    conf = config(options.config_file)
    project_dir = os.path.abspath(os.sep.join(conf['source']))

    logger.debug(project_dir)

    maven = MavenFactory.get_instance()

    maven.compile(project_dir, clean=True)
    start = time.time()
    maven.test(project_dir)

    logger.info("First test execution elapsed time: {0}s".format(
        int(time.time() - start)))

    elapsed_time = 0

    for i in range(5):
        start = time.time()
        maven.test(project_dir)
        actual = int(time.time() - start)
        logger.info("\t#{0} Test elapsed time: {1}s".format(i, actual))
        elapsed_time = max(elapsed_time, actual)

    logger.info("Test elapsed time: {0}s".format(elapsed_time))

    _create_mutants_dir(options)

    files = get_java_files(options.java_src)

    include = []

    if 'include' in conf:
        include = conf['include']

    with open('time_result.csv', 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=';')
        writer.writerow(['file',
                         'full_set_gen_time',
                         'full_set_testing_time',
                         'full_set_mutants',
                         'full_set_killed',
                         'full_set_survived',
                         'full_set_stillborn',
                         'full_set_valid',
                         'full_set_score',
                         'reduced_set_gen_time',
                         'reduced_set_mutation_time',
                         'reduced_set_mutants',
                         'reduced_set_killed',
                         'reduced_set_survived',
                         'reduced_set_stillborn',
                         'reduced_set_valid',
                         'reduced_set_score'
                         ])

    for i, file in enumerate(sort_files(files)):
        logger.info("Checking if {0} is included. {1}/{2}".format(file, i, len(files)))
        if file in include:
            try:
                full_set_result = _generate_mutants(file, options, False,
                                                    elapsed_time)
                redu_set_result = _generate_mutants(file, options, True,
                                                    elapsed_time)

                _write_to_csv(file, full_set_result, redu_set_result)
            except Exception as e:
                logger.error("Don't stop me now!", e)