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)
def write_state_csv(abstract, output_dir, filename): write_json(abstract, output_dir=output_dir, name=filename) with open(os.path.join(output_dir, filename + '.csv'), 'w') as csv: csv.write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}\n".format( '', 'kdm', '%', 'kim', '%', 'kds', '%', 'kis', '%', 'total')) for r in sort_state(abstract): r = r[0] csv.write("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}\n".format( r, abstract[r]['kdm'], _percent(abstract[r], 'kdm'), abstract[r]['kim'], _percent(abstract[r], 'kim'), abstract[r]['kds'], _percent(abstract[r], 'kds'), abstract[r]['kis'], _percent(abstract[r], 'kis'), abstract[r]['total'], )) csv.close()
def redundant_abstract(targets, output_dir=None): red = Mutant.is_redundant_in_targets_group_by_mutation(targets) not_red = Mutant.is_not_redundant_in_targets_group_by_mutation(targets) min = Mutant.belongs_to_minimal_in_targets_group_by_mutation(targets) not_min = Mutant.doesnt_belongs_to_minimal_in_targets_group_by_mutation( targets) abstract = {} total = _group_by_dict( Mutant.select(Mutant.mutation, fn.COUNT(Mutant.id)).where( (Mutant.target.in_(targets))).group_by(Mutant.mutation)) def _percent(d, key): return '{0:.2f}'.format(d[key] / d['total']) for r in total: abstract[r] = { 'redundant': red[r] if r in red.keys() else 0, 'not_redundant': not_red[r] if r in not_red.keys() else 0, 'dominant': min[r] if r in min.keys() else 0, 'subsumed': not_min[r] if r in not_min.keys() else 0, 'total': total[r] } if output_dir is not None: write_json(abstract, output_dir=output_dir, name='abstract') with open(os.path.join(output_dir, 'abstract.csv'), 'w') as csv: csv.write("{0},{1},{2},{3},{4},{5}\n".format( '', 'redundant', '%', 'subsumed', '%', 'total')) for r in abstract: csv.write("{0},{1},{2},{3},{4},{5}\n".format( r, abstract[r]['redundant'], _percent(abstract[r], 'redundant'), abstract[r]['subsumed'], _percent(abstract[r], 'subsumed'), abstract[r]['total'], )) csv.close() return abstract
def write_state_tex(abstract, output_dir, filename): write_json(abstract, output_dir=output_dir, name=filename) with open(os.path.join(output_dir, filename + '.tex'), 'w') as tex: tex.write('\\begin{tabular}{@{}lccccc@{}}\n') tex.write('\t\\toprule\n') tex.write('\t\\textbf{Mutante} & \\textbf{KDM} & \\textbf{KIM} ' '& \\textbf{KDS} & \\textbf{KIS} & \\textbf{Total} \\\\\n') tex.write('\t\\midrule\n') for r in sort_state(abstract): r = r[0] tex.write('\t{0: <17}& {1: <13}& {2: <13}& {3: <13}' '& {4: <13}& {5: < 15}\\\\\n' ''.format(r.replace('%', '\%'), _percent(abstract[r], 'kdm', False) + '\\%', _percent(abstract[r], 'kim', False) + '\\%', _percent(abstract[r], 'kds', False) + '\\%', _percent(abstract[r], 'kis', False) + '\\%', abstract[r]['total'])) tex.write('\t\\bottomrule\n') tex.write('\\end{tabular}\n') tex.close()
def run(self): jdk = JDK(self.options.java_home) classpath = Maven( jdk=jdk, maven_home=self.options.maven_home, no_compile=self.options.no_compile ).compile_project(self.options.source) junit = JUnit( jdk=jdk, sut_class=self.options.sut_class, classpath=classpath, source_dir=self.options.source ) #Execute impact analysis method_list, constructor_list, elapsed_time = SafiraImpactAnalysis( jdk=jdk, original=self.options.source, mutant=self.options.mutants, classpath=classpath ).run_impactanalysis() print('Impacted Methods:' + str(len(method_list))) print('Impacted Constructors:' + str(len(constructor_list))) test_suites = generate_test_suites( jdk=jdk, classpath=classpath, config_file=self.options.config_file, sut_class=self.options.sut_class, output=self.options.output, is_randoop_disabled=self.options.is_randoop_disabled, is_evosuite_disabled=self.options.is_evosuite_disabled, project_dir=self.options.source, suites_evosuite=self.options.suites_evosuite, suites_randoop=self.options.suites_randoop, junit=junit, impacted_methods=method_list, impacted_constructors=constructor_list ) mutants = equivalence_analysis( jdk=jdk, junit=junit, classpath=classpath, test_suites=test_suites, mutants=self.options.mutants, mutation_tool=self.options.mutation_tool, sut_class=self.options.sut_class, coverage_threshold=self.options.coverage_threshold, output=self.options.output, mutants_dir=self.options.mutants, using_target=self.using_target ) if mutants is not None: subsuming_mutants = subsuming( mutants, coverage_threshold=self.options.coverage_threshold ) if not self.options.is_minimal_testsuite_disabled: minimized, minimal_tests = minimize( mutants, coverage_threshold=self.options.coverage_threshold ) write_json(minimized, 'subsuming_minimal_tests', self.options.mutants) write_json(list(minimal_tests), 'minimal_tests', self.options.mutants) create_dmsg(mutants=subsuming_mutants, export_dir=self.options.output) mutants = subsuming( mutants, clean=False, coverage_threshold=self.options.coverage_threshold ) mutants_dict = [mutants[m].to_dict() for m in mutants] write_json(mutants_dict, 'mutants', self.options.mutants) write_json(subsuming_mutants, 'subsuming_mutants', self.options.mutants) return mutants_dict, subsuming_mutants return {}, {}
def _save_targets(options, targets): write_json(targets, 'targets', options.mutants)
def _save_files(options, files, targets, java_file): files['files'].append(java_file) files['targets'] += len(targets) write_json(files, 'save_status', options.mutants)
def run(self): jdk = JDK(self.options.java_home) classpath = Maven(jdk=jdk, maven_home=self.options.maven_home, no_compile=self.options.no_compile).compile_project( self.options.source) junit = JUnit(jdk=jdk, sut_class=self.options.sut_class, classpath=classpath, source_dir=self.options.source) test_suites = generate_test_suites( jdk=jdk, classpath=classpath, config_file=self.options.config_file, sut_class=self.options.sut_class, output=self.options.output, is_randoop_disabled=self.options.is_randoop_disabled, is_evosuite_disabled=self.options.is_evosuite_disabled, project_dir=self.options.source, suites_evosuite=self.options.suites_evosuite, suites_randoop=self.options.suites_randoop, junit=junit) mutants = equivalence_analysis( jdk=jdk, junit=junit, classpath=classpath, test_suites=test_suites, mutants=self.options.mutants, mutation_tool=self.options.mutation_tool, sut_class=self.options.sut_class, coverage_threshold=self.options.coverage_threshold, output=self.options.output, mutants_dir=self.options.mutants, using_target=self.using_target) if mutants is not None: subsuming_mutants = subsuming( mutants, coverage_threshold=self.options.coverage_threshold) if not self.options.is_minimal_testsuite_disabled: minimized, minimal_tests = minimize( mutants, coverage_threshold=self.options.coverage_threshold) write_json(minimized, 'subsuming_minimal_tests', self.options.mutants) write_json(list(minimal_tests), 'minimal_tests', self.options.mutants) create_dmsg(mutants=subsuming_mutants, export_dir=self.options.output) mutants = subsuming( mutants, clean=False, coverage_threshold=self.options.coverage_threshold) mutants_dict = [mutants[m].to_dict() for m in mutants] write_json(mutants_dict, 'mutants', self.options.mutants) write_json(subsuming_mutants, 'subsuming_mutants', self.options.mutants) return mutants_dict, subsuming_mutants return {}, {}
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
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