Exemplo n.º 1
0
    def read_targets_json(self, class_name, count=0):
        targets = []
        t = read_json(os.path.join(self.mutants_dir, 'plugin_targets.json'))

        for target in t:
            mutant = None

            for m in t[target]:
                if m['tid'] == int(target) and m['directory']:
                    mutant = m
                    break

            if mutant:
                targets.append({
                    'id':
                    count,
                    'ignore':
                    False,
                    'class':
                    class_name,
                    'method':
                    ''.join(mutant['methodSignature'].split('_')[1:]),
                    'type_method':
                    mutant['methodSignature'],
                    'line':
                    mutant['lineNumber'],
                    'column':
                    0,
                    'statement':
                    ''.join(mutant['transformation'].split('=>')[0]).strip(),
                    'statement_nodes':
                    '',
                    'context': [],
                    'context_full': [],
                    'method_ast': [],
                    'operand_nodes':
                    '',
                    'operator_kind':
                    '',
                    'operator':
                    mutant['expOperator'],
                    'directory':
                    os.path.join(
                        os.sep.join(mutant['directory'].split(os.sep)[:-2]),
                        target),
                    'oid':
                    int(target),
                    'target_class':
                    mutant['targetClass'],
                    'target_repr':
                    mutant['targetRepr'],
                    'children':
                    mutant['children'],
                    'prefix_operator':
                    mutant['prefixExpOperator'],
                    'mutants': []
                })
            count += 1
        return targets
Exemplo n.º 2
0
def _recover_targets(options):
    targets_file = os.path.join(options.mutants, 'targets.json')

    if os.path.exists(targets_file):
        targets = read_json(targets_file)
    else:
        targets = []

    return targets
Exemplo n.º 3
0
def _recover_files(options):
    save_status_file = os.path.join(options.mutants, 'save_status.json')

    if os.path.exists(save_status_file):
        status = read_json(save_status_file)
    else:
        status = {'files': [], 'targets': 0}

    return status
Exemplo n.º 4
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)
Exemplo n.º 5
0
def generate_test_suites(jdk,
                         classpath,
                         config_file,
                         sut_class,
                         output,
                         is_randoop_disabled,
                         is_evosuite_disabled,
                         project_dir,
                         suites_evosuite,
                         suites_randoop,
                         junit,
                         impacted_methods=None,
                         impacted_constructors=None):

    reuse_tests = os.path.join(output, '')
    saved_suites = {}

    if reuse_tests:
        if not os.path.exists(reuse_tests):
            os.makedirs(reuse_tests)
        saved_suites_file = os.path.join(reuse_tests, 'saved_suites.json')
        if os.path.exists(saved_suites_file):
            saved_suites = read_json(saved_suites_file)

    tests_dir = os.path.join(output, 'tests')
    test_suites = {}

    if sut_class in saved_suites.keys():
        valid_suites = []
        for s in saved_suites[sut_class]:
            if os.path.exists(s['source_dir']):
                valid_suites.append(s)
        if len(valid_suites) > 0:
            saved_suites[sut_class] = valid_suites
        else:
            del saved_suites[sut_class]

    if sut_class not in saved_suites.keys():
        saved_suites[sut_class] = []
        #print('Threading EvoSuite')
        thread_randoop = threading.Thread(
            target=generate_randoop_tests,
            args=(is_randoop_disabled, suites_randoop, jdk, classpath,
                  config_file, tests_dir, sut_class, project_dir, junit,
                  test_suites, saved_suites, impacted_methods,
                  impacted_constructors))
        thread_evosuite = threading.Thread(
            target=generate_evosuite_tests,
            args=(is_evosuite_disabled, suites_evosuite, jdk, classpath,
                  config_file, tests_dir, sut_class, junit, test_suites,
                  saved_suites, impacted_methods, impacted_constructors))

        thread_randoop.start()
        thread_evosuite.start()

        thread_randoop.join()
        thread_evosuite.join()

        #OLD sequential execution...
        #generate_randoop_tests(is_randoop_disabled, suites_randoop, jdk, classpath, config_file, tests_dir, sut_class, project_dir, junit, test_suites, saved_suites)
        #generate_evosuite_tests(is_evosuite_disabled, suites_evosuite, jdk, classpath, config_file, tests_dir, sut_class, junit, test_suites, saved_suites)
    else:
        for t in saved_suites[sut_class]:
            test_suites[t['tid']] = TestSuiteResult(
                tid=t['tid'],
                source_dir=t['source_dir'],
                classes_dir=t['classes_dir'],
                classes=t['classes'],
                prefix=t['prefix'])

    if reuse_tests:
        write_json(saved_suites, name='saved_suites', output_dir=reuse_tests)

    return test_suites
Exemplo n.º 6
0
def generate_test_suites(jdk, classpath, config_file, sut_class, output,
                         is_randoop_disabled, is_evosuite_disabled,
                         project_dir, suites_evosuite, suites_randoop, junit):

    reuse_tests = os.path.join(output, '..')
    saved_suites = {}

    if reuse_tests:
        if not os.path.exists(reuse_tests):
            os.makedirs(reuse_tests)
        saved_suites_file = os.path.join(reuse_tests, 'saved_suites.json')
        if os.path.exists(saved_suites_file):
            saved_suites = read_json(saved_suites_file)

    tests_dir = os.path.join(output, 'tests')
    test_suites = {}

    if sut_class in saved_suites.keys():
        valid_suites = []
        for s in saved_suites[sut_class]:
            if os.path.exists(s['source_dir']):
                valid_suites.append(s)
        if len(valid_suites) > 0:
            saved_suites[sut_class] = valid_suites
        else:
            del saved_suites[sut_class]

    if sut_class not in saved_suites.keys():
        saved_suites[sut_class] = []

        if not is_randoop_disabled:
            for i in range(suites_randoop):
                test_suite_name = '{0}_{1}'.format('randoop', i + 1)
                randoop = Randoop(jdk,
                                  classpath,
                                  config_file,
                                  tests_dir,
                                  sut_class,
                                  project_dir,
                                  test_suite_name=test_suite_name)
                source_dir, classes_dir, classes = randoop.generate()

                test_suite = TestSuiteResult(tid=test_suite_name,
                                             source_dir=source_dir,
                                             classes_dir=classes_dir,
                                             classes=classes,
                                             prefix='{0}_{1}'.format(
                                                 'RAN', i + 1))

                checked_test_suites = junit.run_test_suites(
                    {test_suite_name: test_suite}, classpath)

                if (not checked_test_suites[test_suite_name].maybe_in_loop
                        and not checked_test_suites[test_suite_name].fail
                        and checked_test_suites[test_suite_name].is_valid):
                    test_suites[test_suite_name] = test_suite

                    saved_suites[sut_class].append({
                        'tid':
                        test_suite_name,
                        'source_dir':
                        source_dir,
                        'classes_dir':
                        classes_dir,
                        'classes':
                        classes,
                        'prefix':
                        '{0}_{1}'.format('RAN', i + 1)
                    })
                else:
                    print(
                        '# ERROR: invalid suite. FAIL: {0}, LOOP: {1}, '
                        'FAILED TESTES: {2}'.format(
                            checked_test_suites[test_suite_name].fail,
                            checked_test_suites[test_suite_name].maybe_in_loop,
                            checked_test_suites[test_suite_name].fail_tests))

        if not is_evosuite_disabled:
            for i in range(suites_evosuite):
                test_suite_name = '{0}_{1}'.format('evosuite', i + 1)
                evosuite = Evosuite(jdk,
                                    classpath,
                                    config_file,
                                    tests_dir,
                                    sut_class,
                                    test_suite_name=test_suite_name)
                source_dir, classes_dir, classes = evosuite.generate()

                test_suite = TestSuiteResult(tid=test_suite_name,
                                             source_dir=source_dir,
                                             classes_dir=classes_dir,
                                             classes=classes,
                                             prefix='{0}_{1}'.format(
                                                 'EVO', i + 1))

                checked_test_suites = junit.run_test_suites(
                    {test_suite_name: test_suite}, classpath)

                if (not checked_test_suites[test_suite_name].maybe_in_loop
                        and not checked_test_suites[test_suite_name].fail
                        and checked_test_suites[test_suite_name].is_valid):
                    test_suites[test_suite_name] = test_suite

                    saved_suites[sut_class].append({
                        'tid':
                        test_suite_name,
                        'source_dir':
                        source_dir,
                        'classes_dir':
                        classes_dir,
                        'classes':
                        classes,
                        'prefix':
                        '{0}_{1}'.format('EVO', i + 1)
                    })
                else:
                    print(
                        '# ERROR: invalid suite. FAIL: {0}, LOOP: {1}, '
                        'FAILED TESTES: {2}'.format(
                            checked_test_suites[test_suite_name].fail,
                            checked_test_suites[test_suite_name].maybe_in_loop,
                            checked_test_suites[test_suite_name].fail_tests))
    else:
        for t in saved_suites[sut_class]:
            test_suites[t['tid']] = TestSuiteResult(
                tid=t['tid'],
                source_dir=t['source_dir'],
                classes_dir=t['classes_dir'],
                classes=t['classes'],
                prefix=t['prefix'])

    if reuse_tests:
        write_json(saved_suites, name='saved_suites', output_dir=reuse_tests)

    return test_suites