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_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
Beispiel #2
0
    def test_javac_timeout(self):
        JAVA_HOME = 'java_home'

        jdk = JDK(JAVA_HOME)

        subprocess.check_call = MagicMock(
            side_effect=subprocess.TimeoutExpired('', 0))

        self.assertFalse(jdk.run_javac('file.java', 10, None))
Beispiel #3
0
    def test_javac_process_error(self):
        JAVA_HOME = 'java_home'

        jdk = JDK(JAVA_HOME)

        subprocess.check_call = MagicMock(
            side_effect=subprocess.CalledProcessError(-1, ''))

        self.assertFalse(jdk.run_javac('file.java', 10, None))
Beispiel #4
0
    def test_javac_with_args(self):
        JAVA_HOME = 'java_home'

        jdk = JDK(JAVA_HOME)

        subprocess.check_call = MagicMock()

        self.assertTrue(jdk.run_javac('file.java', 10, None, '-a', 'b'))

        subprocess.check_call.assert_called_once_with(
            [os.path.join(JAVA_HOME, 'bin', 'javac'), '-a', 'b', 'file.java'],
            stdout=subprocess.DEVNULL,
            timeout=10,
            cwd=None,
            stderr=subprocess.DEVNULL)
Beispiel #5
0
    def test_wrong_java_home(self):
        WRONG_JAVA_HOME = 'wrong_java_home'

        os.environ['JAVA_HOME'] = WRONG_JAVA_HOME

        subprocess.check_call = MagicMock(side_effect=OSError)

        with self.assertRaises(SystemExit):
            JDK('')

        subprocess.check_call.assert_called_once_with(
            [os.path.join(WRONG_JAVA_HOME, 'bin', 'javac'), '-version'],
            stdout=subprocess.DEVNULL,
            timeout=10,
            cwd=None,
            stderr=subprocess.DEVNULL)
Beispiel #6
0
    def test_java_path(self):
        JAVA_HOME = 'java_home'

        subprocess.check_call = MagicMock()

        jdk = JDK(JAVA_HOME)

        subprocess.check_call.assert_called_once_with(
            [os.path.join(JAVA_HOME, 'bin', 'javac'), '-version'],
            stdout=subprocess.DEVNULL,
            timeout=10,
            cwd=None,
            stderr=subprocess.DEVNULL)

        self.assertEqual(os.path.join(JAVA_HOME, 'jre', 'bin', 'java'),
                         jdk.java)
        self.assertEqual(os.path.join(JAVA_HOME, 'bin', 'javac'), jdk.javac)
Beispiel #7
0
    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 {}, {}
Beispiel #8
0
    def test_check_javac_no_java_home(self):
        del os.environ['JAVA_HOME']

        with self.assertRaises(SystemExit):
            JDK('')
Beispiel #9
0
    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 {}, {}