Ejemplo n.º 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_class_files(options.java_src, ext='.java')

    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
Ejemplo n.º 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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
 def get_instance(cls):
     if not cls.maven:
         options = to_options_gen(arg_parser_gen())
         cls.maven = Maven(java=JavaFactory.get_instance(),
                           maven_home=options.maven_home)
     return cls.maven
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
0
 def get_instance(cls):
     if not cls.java:
         options = to_options_gen(arg_parser_gen())
         cls.java = Java(java_home=options.java_home)
     return cls.java