示例#1
0
 def mk_and_cp_operator_mutant_dir(src_dir, subject_name, operator_dir_name,
                                   packages_dir):
     new_dirs = os.path.join(subdir_mutants, subject_name,
                             operator_dir_name)
     new_dirs_packages = os.path.join(new_dirs, packages_dir)
     src_dir = os.path.join(src_dir, operator_dir_name)
     # si existe el directorio, ya deberia contener los .class, entonces no copio los mutantes ni compilo
     if not os.path.exists(new_dirs):
         shutil.copytree(src_dir, new_dirs_packages)
         utils.compile_workdir(new_dirs, new_dirs, new_dirs,
                               original_code_dir)
示例#2
0
def setup_subjects(results_dir_name, original_code_dir, instrumented_code_dir,
                   name, evosuite_classes, class_name):
    bin_original_code_dir = get_subject_original_bin_dir(
        results_dir_name, name)
    bin_instrumented_code_dir = get_subject_instrumented_bin_dir(
        results_dir_name, name)
    if exist_subject(bin_original_code_dir, bin_instrumented_code_dir,
                     class_name):
        return
    utils.compile_workdir(original_code_dir, bin_original_code_dir,
                          evosuite_classes)
    utils.compile_workdir(instrumented_code_dir, bin_instrumented_code_dir,
                          evosuite_classes)
示例#3
0
def setup_subjects(results_dir_name, original_code_dir, instrumented_code_dir,
                   mining_code_dir, name, evosuite_classes, class_name,
                   extra_classpath):
    bin_original_code_dir = get_subject_original_bin_dir(
        results_dir_name, name)
    bin_instrumented_code_dir = get_subject_instrumented_bin_dir(
        results_dir_name, name)
    bin_mining_code_dir = get_subject_mining_bin_dir(results_dir_name, name)
    if not exist_subject(bin_original_code_dir, class_name):
        utils.compile_workdir(original_code_dir, bin_original_code_dir,
                              evosuite_classes, extra_classpath)
    if not exist_subject(bin_instrumented_code_dir, class_name):
        utils.compile_workdir(instrumented_code_dir, bin_instrumented_code_dir,
                              evosuite_classes, extra_classpath)
    if not exist_subject(bin_mining_code_dir, class_name):
        utils.compile_workdir(mining_code_dir, bin_mining_code_dir,
                              evosuite_classes, extra_classpath)
示例#4
0
    def run(self):
        if self.method in [
                EpatestingMethod.ONLY_TESTGEN.value,
                EpatestingMethod.BOTH.value,
                EpatestingMethod.BOTH_WITHOUT_MUJAVA.value
        ]:
            print('GENERATING TESTS')
            code_dir = self.instrumented_code_dir if "epa".upper(
            ) in self.criterion.upper() else self.original_code_dir
            if "mining".upper() in self.criterion.upper(
            ) or "Compiler_" in self.name:  #hack for Compiler
                code_dir = self.mining_code_dir

            bin_code_dir = self.bin_instrumented_code_dir if "epa".upper(
            ) in self.criterion.upper() else self.bin_original_code_dir
            if "mining".upper() in self.criterion.upper(
            ) or "Compiler_" in self.name:  #hack for Compiler
                bin_code_dir = self.bin_mining_code_dir
            if len(self.extra_classpath) != 0:
                bin_code_dir += os.path.pathsep + self.extra_classpath

            # if exists testsuite in other bug_type, copy it!
            testsuite_exists = False
            curr_bug_type = self.bug_type
            try:
                lock.acquire()
                testsuite_exists = cp_testsuite_if_exists_in_other_results(
                    curr_bug_type, self.subdir_testgen,
                    self.generated_test_report_evosuite_dir, self.class_name,
                    self.criterion)
            except Exception as e:
                testsuite_exists = False
                print(
                    "error copying from other bug_type folder to {}. Error {}".
                    format(self.subdir_testgen, e))
            finally:
                lock.release()

            if (not testsuite_exists):
                if self.criterion == "randoop":
                    run_randoop(projectCP=bin_code_dir,
                                class_name=self.class_name,
                                randoop_jar_path=self.randoop_jar_path,
                                testdir=self.generated_test_dir,
                                search_budget=self.search_budget)
                else:
                    run_evosuite(
                        evosuite_jar_path=self.evosuite_jar_path,
                        strategy=self.strategy_value,
                        projectCP=bin_code_dir,
                        class_name=self.class_name,
                        criterion=self.criterion,
                        epa_path=self.epa_path,
                        inferred_epa_xml_path=self.inferred_epa_xml,
                        test_dir=self.generated_test_dir,
                        stopping_condition=self.stopping_condition,
                        search_budget=self.search_budget,
                        report_dir=self.generated_test_report_evosuite_dir)

            add_fails = False
            if (self.bug_type.upper() == BugType.ERRPROT.name):
                # If is run in errprot mode, then always remove asserts and specific exceptions
                self.assert_type = AssertType.NO_ASSERT_EXCEPTION.name
                #if("JDBCResultSet" in self.name):
                #add_fails= True;
            if self.assert_type.upper() in [
                    AssertType.NO_ASSERT.name,
                    AssertType.NO_ASSERT_EXCEPTION.name
            ]:
                if "randoop".upper() in self.criterion.upper():
                    test_dir = self.generated_test_dir
                    packages_dir = utils.get_package_dir(
                        self.class_name.split(".")[:-1])
                    test_dir_sub = os.path.join(test_dir, packages_dir)
                    for test_file_name in os.listdir(test_dir_sub):
                        test_file = os.path.join(test_dir_sub, test_file_name)
                        if not test_file.endswith(".java"):
                            continue
                        # ErrorTest files are generated by randoop. Contains error test. That fails in PIT
                        if "ErrorTest" in test_file:
                            continue
                        workaround_test(self.generated_test_dir,
                                        self.class_name, test_file_name,
                                        add_fails, self.assert_type)
                else:
                    test_file_name = self.class_name.split(
                        ".")[-1] + "_ESTest.java"
                    workaround_test(self.generated_test_dir, self.class_name,
                                    test_file_name, add_fails,
                                    self.assert_type)

            utils.compile_workdir(self.generated_test_dir,
                                  self.generated_test_dir, bin_code_dir,
                                  self.junit_jar, self.evosuite_classes,
                                  self.evosuite_runtime_jar_path,
                                  self.extra_classpath)

        criterion = get_alternative_criterion_names(self.criterion)

        if self.method in [
                EpatestingMethod.ONLY_METRICS.value,
                EpatestingMethod.BOTH.value,
                EpatestingMethod.BOTH_WITHOUT_MUJAVA.value,
                EpatestingMethod.ONLY_METRICS_WITHOUT_MUJAVA.value
        ]:
            print('GENERATING METRICS')
            packages_dir = utils.get_package_dir(
                self.class_name.split(".")[:-1])
            test_dir_sub = os.path.join(self.generated_test_dir, packages_dir)
            if not os.path.exists(test_dir_sub):
                print("not found test folder ! '{}'".format(test_dir_sub))
                exit(1)

            bin_code_dir = self.bin_instrumented_code_dir if "epa".upper(
            ) in self.criterion.upper() else self.bin_original_code_dir
            if "mining".upper() in self.criterion.upper(
            ) or "Compiler_" in self.name:  #hack for Compiler
                bin_code_dir = self.bin_mining_code_dir
            if len(self.extra_classpath) != 0:
                bin_code_dir += os.path.pathsep + self.extra_classpath

            ###to compile test suite
            #utils.compile_workdir(self.generated_test_dir, self.generated_test_dir, bin_code_dir, self.junit_jar, self.evosuite_classes, self.evosuite_runtime_jar_path, self.extra_classpath)

            #measure_evosuite(evosuite_jar_path=self.evosuite_jar_path, projectCP=self.bin_instrumented_code_dir, testCP=self.generated_test_dir, class_name=self.class_name, epa_path=self.epa_path, report_dir=self.generated_report_evosuite_dir, criterion="epatransition", inferred_epa_xml_path="", force_inferred_epa=False)
            #measure_evosuite(evosuite_jar_path=self.evosuite_jar_path, projectCP=self.bin_instrumented_code_dir, testCP=self.generated_test_dir, class_name=self.class_name, epa_path=self.epa_path, report_dir=self.generated_report_evosuite_dir, criterion="epaexception", inferred_epa_xml_path="", force_inferred_epa=False)
            #measure_evosuite(evosuite_jar_path=self.evosuite_jar_path, projectCP=self.bin_instrumented_code_dir, testCP=self.generated_test_dir, class_name=self.class_name, epa_path=self.epa_path, report_dir=self.generated_report_evosuite_dir, criterion="epaadjacentedges", inferred_epa_xml_path="", force_inferred_epa=False)
            # Hack to generate inferred epa for randoop. For other criteria it is generated in the generation process - only needed one true in force_inferred_epa
            force_inferred_epa_value = True if "randoop".upper(
            ) in self.criterion.upper() else False
            measure_evosuite(evosuite_jar_path=self.evosuite_jar_path,
                             projectCP=bin_code_dir,
                             testCP=self.generated_test_dir,
                             class_name=self.class_name,
                             epa_path=self.epa_path,
                             report_dir=self.generated_report_evosuite_dir,
                             criterion="epatransitionmining",
                             inferred_epa_xml_path=self.inferred_epa_xml,
                             force_inferred_epa=force_inferred_epa_value)
            measure_evosuite(evosuite_jar_path=self.evosuite_jar_path,
                             projectCP=self.bin_mining_code_dir,
                             testCP=self.generated_test_dir,
                             class_name=self.class_name,
                             epa_path=self.epa_path,
                             report_dir=self.generated_report_evosuite_dir,
                             criterion="epaexceptionmining",
                             inferred_epa_xml_path="",
                             force_inferred_epa=False)
            measure_evosuite(evosuite_jar_path=self.evosuite_jar_path,
                             projectCP=self.bin_mining_code_dir,
                             testCP=self.generated_test_dir,
                             class_name=self.class_name,
                             epa_path=self.epa_path,
                             report_dir=self.generated_report_evosuite_dir,
                             criterion="epaadjacentedgesmining",
                             inferred_epa_xml_path="",
                             force_inferred_epa=False)
            #if force_inferred_epa_value:
            #   return

            # Run Pitest to measure

            targetTests = "{}_ESTest".format(self.class_name)
            if "randoop".upper() in self.criterion.upper():
                targetTests = "{}.RegressionTest".format(
                    utils.get_package_name_from_qualifiedname(self.class_name))
            pitest_measure(
                self.generated_report_pitest_dir, self.class_name, targetTests,
                self.original_code_dir.replace("mining", "original"),
                self.generated_test_dir)
            #pitest_measure(self.generated_report_pitest_dir, self.class_name, self.original_code_dir, self.generated_test_dir, utils.get_package_dir(self.class_name.split(".")[0:-1]))

            #TODO: add strategy
            if self.method in [
                    EpatestingMethod.ONLY_METRICS.value,
                    EpatestingMethod.BOTH.value
            ]:
                mujava_measure(
                    self.bug_type, self.name, criterion, self.subdir_mutants,
                    self.error_prot_list, self.ignore_mutants_list,
                    self.bin_original_code_dir.replace("mining", "original"),
                    self.generated_test_dir, self.class_name, self.junit_jar,
                    self.hamcrest_jar_path, self.generated_report_mujava)

            # Resume the reports generated
            all_report_dir = os.path.join(self.subdir_metrics, 'all_reports')
            command_mkdir_report = 'mkdir {}'.format(all_report_dir)
            #utils.print_command(command_mkdir_report)
            if not os.path.exists(all_report_dir):
                os.makedirs(all_report_dir)

            copy_pitest_csv(self.name, self.generated_report_pitest_dir,
                            all_report_dir)

            statistics_csv = os.path.join(self.generated_report_evosuite_dir,
                                          "statistics.csv")
            copy_csv(statistics_csv, 'epacoverage_{}'.format(self.name),
                     all_report_dir)

            statistics_testgen_csv = ""
            if not self.criterion == "randoop":
                try:
                    statistics_testgen_csv = os.path.join(
                        self.generated_test_report_evosuite_dir,
                        "statistics.csv")
                    copy_csv(statistics_testgen_csv,
                             'statistics_testgen_{}'.format(self.name),
                             all_report_dir)
                except:
                    print(
                        "statistics_testgen_csv (generated by Evosuite) not found"
                    )
            mujava_csv = os.path.join(self.generated_report_mujava,
                                      "mujava_report.csv")
            if os.path.exists(mujava_csv):
                copy_csv(mujava_csv, 'mujava_{}'.format(self.name),
                         all_report_dir)
            else:
                print("Does not exists mujava file {}".format(mujava_csv))

            epacoverage_csv = os.path.join(
                all_report_dir, "epacoverage_{}.csv".format(self.name))
            if self.criterion != "randoop":
                statistics_testgen_csv = os.path.join(
                    all_report_dir,
                    "statistics_testgen_{}.csv".format(self.name))
            jacoco_csv = os.path.join(all_report_dir,
                                      "{}_jacoco.csv".format(self.name))
            mutations_csv = os.path.join(all_report_dir,
                                         "{}_mutations.csv".format(self.name))

            pit_mutants_histogram(self.strategy_name, self.bug_type, criterion,
                                  self.search_budget, self.stopping_condition,
                                  mutations_csv, self.generated_test_dir,
                                  self.generated_pitest_killer_test,
                                  self.runid)
            # For test suite LOC
            result_jncss_temp = get_file_path_jncss(
                self.strategy_name, self.class_name, self.generated_test_dir,
                self.results_dir_name, self.bug_type, self.stopping_condition,
                self.search_budget, criterion, self.runid,
                self.javancss_jar_path)
            # For covered exceptions goals
            testgen_log_file_path = os.path.join(self.subdir_testgen,
                                                 "testgen_out.txt")

            make_report_resume.resume(
                self.class_name, epacoverage_csv, statistics_testgen_csv,
                jacoco_csv, mutations_csv, self.resume_csv, self.runid,
                self.stopping_condition, self.search_budget, criterion,
                self.bug_type, self.strategy_name, mujava_csv,
                result_jncss_temp, testgen_log_file_path)

        if self.method in [EpatestingMethod.ONLY_PIT_MUTANTS_HISTOGRAM.value]:
            mutations_csv = get_mutation_csv_pit(
                self.generated_report_pitest_dir)
            pit_mutants_histogram(self.strategy_name, self.bug_type, criterion,
                                  self.search_budget, self.stopping_condition,
                                  mutations_csv, self.generated_test_dir,
                                  self.generated_pitest_killer_test,
                                  self.runid)

        # Hack (for old executions)
        if self.method in [
                EpatestingMethod.ONLY_TEST_SUITE_LOC_AND_EXCEPTION.value
        ]:
            #Para TS LOC
            result_jncss_temp = get_file_path_jncss(
                self.class_name, self.generated_test_dir,
                self.results_dir_name, self.bug_type, self.stopping_condition,
                self.search_budget, criterion, self.runid,
                self.javancss_jar_path)
            # Para obtener exceptions
            ####################
            testgen_log_file_path = os.path.join(self.subdir_testgen,
                                                 "testgen_out.txt")
            # Este archivo tiene la cantidad de goals cubiertos para cada criterio (-measureCoverage)
            all_report_dir = os.path.join(self.subdir_metrics, 'all_reports')
            epacoverage_csv = os.path.join(
                all_report_dir, "epacoverage_{}.csv".format(self.name))
            # Este archivo tiene la suma de goals cubiertos (incluyendo criterio exception)
            statistics_testgen_csv = os.path.join(
                all_report_dir, "statistics_testgen_{}.csv".format(self.name))

            utils.make_dirs_if_not_exist(self.subdir_metrics)
            make_report_resume.resume_test_suite_loc_and_exceptions(
                self.class_name, self.resume_csv, self.runid,
                self.stopping_condition, self.search_budget, criterion,
                self.bug_type, result_jncss_temp, testgen_log_file_path,
                epacoverage_csv, statistics_testgen_csv)