Exemple #1
0
class TestSafira(TestCase):
    def setUp(self):
        self.java = Java(get_config()['java_home'])
        self.maven = Maven(self.java, get_config()['maven_home'])
        self.maven.compile(calculator_project_dir(), 10)

    '''def test_run_safira(self):
        safira = Safira(self.java, calculator_target_dir(),
                        calculator_src_aor_1())

        results = safira.run()

        self.assertEqual(3, len(results.methods))
        self.assertTrue('br.ufal.ic.easy.operations.Sum.impacted(int, int)' in
                        results.methods)
        self.assertTrue('br.ufal.ic.easy.operations.Sum.execute(int, int)' in
                        results.methods)
        self.assertTrue('br.ufal.ic.easy.Calculator.sum(int, int)' in
                        results.methods)
        self.assertEqual(2, len(results.constructors))
        self.assertTrue('br.ufal.ic.easy.Calculator()' in
                        results.constructors)
        self.assertTrue('br.ufal.ic.easy.operations.Sum()' in
                        results.constructors)'''

    def test_run_safira_timeout(self):
        safira = Safira(self.java, calculator_target_dir(),
                        calculator_src_aor_1())

        with self.assertRaises(subprocess.TimeoutExpired):
            safira.run(timeout=0)
class TestRandoop(TestCase):
    def setUp(self):
        self.java_home = get_config()['java_home']
        self.maven_home = get_config()['maven_home']

        self.java = Java(self.java_home)
        self.maven = Maven(self.java, self.maven_home)

        self.maven.compile(calculator_project_dir(), 10)

    def test_generate(self):

        tests_src = os.path.join(calculator_project_dir(), 'randoop')

        randoop = Randoop(java=self.java,
                          classpath=os.path.join(calculator_target_dir(),
                                                 'classes'),
                          tests_src=tests_src,
                          sut_class='br.ufal.ic.easy.operations.Sum')
        randoop.parameters = ['--time-limit=1']

        (suite_name, suite_dir, suite_classes_dir,
         suite_classes) = randoop.generate()

        self.assertTrue(suite_name.startswith('randoop'))
        self.assertTrue(os.path.exists(suite_dir))

        self.assertTrue(len(get_java_files(suite_dir)) > 1)
        self.assertTrue(len(get_class_files(suite_classes_dir)) > 1)
        self.assertEquals(1, len(suite_classes))

        # shutil.rmtree(tests_src)

    def test_generate_with_impact_analysis(self):
        tests_src = os.path.join(calculator_project_dir(), 'randoop')
        classes_dir = os.path.join(calculator_target_dir(), 'classes')

        randoop = Randoop(java=self.java,
                          classpath=classes_dir,
                          tests_src=tests_src,
                          sut_class='br.ufal.ic.easy.operations.Sum')
        randoop.parameters = ['--time-limit=1']

        safira = Safira(self.java, classes_dir, calculator_src_aor_1())

        (suite_name, suite_dir, suite_classes_dir,
         suite_classes) = randoop.generate_with_impact_analysis(safira)

        self.assertTrue(suite_name.startswith('randoop'))
        self.assertTrue(os.path.exists(suite_dir))

        self.assertTrue(len(get_java_files(suite_dir)) > 1)
        self.assertTrue(len(get_class_files(suite_classes_dir)) > 1)
        self.assertEquals(1, len(suite_classes))

        shutil.rmtree(tests_src)

    def tearDown(self):
        calculator_clean_project()
Exemple #3
0
    def test_compile_in_maven_project_dir(self):
        project_dir = os.path.join(calculator_project_dir(), '..', 'example')

        calculator_clean_project()

        maven = Maven(java=self.java, maven_home=self.maven_home)
        maven.compile(project_dir, timeout=10)

        self.assertTrue(os.path.exists(os.path.join(project_dir, 'target')))

        calculator_clean_project()
Exemple #4
0
class Project_dependecies:
    def __init__(self, config, path_local_project, path_local_module_analysis,
                 project_name):
        self.config = config

        self.sut_class = None
        self.sut_method = None
        self.dRegCp = None  # base
        self.classes_dir = None  # left
        self.mergeDir = None  # merge

        self.java = Java(self.config['java_home'])
        self.maven = Maven(self.java, self.config['maven_home'])
        self.tests_dst = self.create_directory_test_destination()
        #self.tests_dst = self.config["tests_dst"]
        self.project = GitProject(path_local_project,
                                  path_local_module_analysis, project_name)
        self.projects_folder = self.config["projects_folder"]
        self.path_hash_csv = self.config["path_hash_csv"]
        self.path_output_csv = self.config["path_output_csv"]

    def create_directory_test_destination(self):
        path_directory = os.getcwd().replace("/nimrod/proj",
                                             "/") + 'output-test-dest'
        if (os.path.isdir(path_directory) == False):
            os.mkdir(path_directory)
        return path_directory

    def compile_commits(self, scenario):
        java_file = self.find_java_files(
            self.project.get_path_local_project(),
            scenario.merge_scenario.get_sut_class())
        data = [(scenario.merge_scenario.get_base_hash(), "base"),
                (scenario.merge_scenario.get_left_hash(), "left"),
                (scenario.merge_scenario.get_right_hash(), "right"),
                (scenario.merge_scenario.get_merge_hash(), "merge")]
        self.sut_class = scenario.merge_scenario.get_sut_class()
        self.sut_method = scenario.merge_scenario.get_sut_method()
        for hash in data:
            try:
                self.project.checkout_on_commit(".")
                self.project.checkout_on_commit(hash[0])
                self.project.checkout_on_commit(".")
                self.set_method_public(java_file)
                #self.add_default_constructor(java_file)

                self.maven.compile(self.project.get_path_local_project(),
                                   120,
                                   clean=True,
                                   install=True)
                self.maven.save_dependencies(
                    self.project.get_path_local_project())
                dst = self.projects_folder + self.project.get_project_name(
                ) + "/" + data[3][0] + "/" + hash[1]
                if os.path.exists(dst):
                    shutil.rmtree(dst)

                shutil.copytree(self.project.get_path_local_module_analysis(),
                                dst)
            except:
                print("The commit " + str(hash) + " was not compilable")

    @staticmethod
    def find_java_files(dir_path, sut_class):
        # dir_path = self.project.get_path_local_project()
        # dir_path = os.path.dirname(os.path.realpath(__file__))
        class_name = sut_class.split('.')[-1]
        for root, dirs, files in os.walk(dir_path):
            for file in files:
                if file.endswith('.java') and ("Test" not in str(file)) and (
                        class_name in file):
                    return root + '/' + str(file)

    def generate_dependencies_path(self, scenario, commit_type):

        project_folder = self.projects_folder + self.project.get_project_name(
        ) + "/" + scenario.merge_scenario.get_merge_hash() + "/"
        dependencies = [(x[0], x[2])
                        for x in os.walk(project_folder + commit_type +
                                         "/target/dependency/")]
        dep_path = dependencies[0][0]
        final_path = ""
        print(dep_path)
        for dependency in dependencies[0][1]:
            final_path = final_path + dep_path + dependency + ":"

        final_path = dep_path + ":" + final_path + project_folder + commit_type + "/target/classes/"

        return final_path

    @staticmethod
    def set_method_public(file):

        for line in fileinput.input(file, inplace=1):
            if re.search(
                    r'(protected|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])',
                    line):
                print(line.replace("protected", "public").rstrip())
            elif re.search(
                    r'(private|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])',
                    line):
                print(line.replace("private", "public").rstrip())
            else:
                print(line.rstrip())

    @staticmethod
    def add_default_constructor(file):
        for line in fileinput.input(file, inplace=1):
            if re.search(
                    "(((|public|final|abstract|private|static|protected)(\\s+))?(class)(\\s+)(\\w+)(<.*>)?(\\s+extends\\s+\\w+)?(<.*>)?(\\s+implements\\s+)?(.*)?(<.*>)?(\\s*))\\{$",
                    line):
                print(line.rstrip() +
                      "\npublic Ball(){}\n")  # ajust this later

            elif re.search(".*?private final.*", line):
                print(line.replace("private final", "private").rstrip())
            else:
                print(line.rstrip())
Exemple #5
0
 def test_compile_no_maven_project_dir(self):
     maven = Maven(java=self.java, maven_home=self.maven_home)
     with self.assertRaises(subprocess.CalledProcessError):
         maven.compile('.', timeout=10)
Exemple #6
0
class TestUtils(TestCase):

    def setUp(self):
        self.java_home = get_config()['java_home']
        self.maven_home = get_config()['maven_home']

        self.java = Java(self.java_home)
        self.maven = Maven(self.java, self.maven_home)

        self.maven.compile(calculator_project_dir(), 10)

    def test_get_files(self):
        classes = get_files(calculator_target_dir())

        self.assertEqual(8, len(classes))
        self.assertTrue(('maven-status/maven-compiler-plugin/compile/'
                         + 'default-compile/inputFiles.lst') in classes)
        self.assertTrue(('maven-status/maven-compiler-plugin/compile/'
                         + 'default-compile/createdFiles.lst') in classes)
        self.assertTrue('classes/br/ufal/ic/easy/Calculator.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Sum.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Operation.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Div.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Sub.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Mul.class'
                        in classes)

    def test_get_class_files(self):
        classes = get_class_files(calculator_target_dir())

        self.assertEqual(6, len(classes))
        self.assertTrue('classes/br/ufal/ic/easy/Calculator.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Sum.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Operation.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Div.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Sub.class'
                        in classes)
        self.assertTrue('classes/br/ufal/ic/easy/operations/Mul.class'
                        in classes)

    def test_get_java_files(self):
        classes = get_java_files(os.path.join(calculator_project_dir(), 'src',
                                              'main', 'java'))

        self.assertEqual(6, len(classes))
        self.assertTrue('br/ufal/ic/easy/Calculator.java'
                        in classes)
        self.assertTrue('br/ufal/ic/easy/operations/Sum.java'
                        in classes)
        self.assertTrue('br/ufal/ic/easy/operations/Div.java'
                        in classes)
        self.assertTrue('br/ufal/ic/easy/operations/Operation.java'
                        in classes)
        self.assertTrue('br/ufal/ic/easy/operations/Sub.java'
                        in classes)
        self.assertTrue('br/ufal/ic/easy/operations/Mul.java'
                        in classes)

    def test_generate_classpath(self):
        path = generate_classpath(['a', 'b', 'c'])
        path_split = path.split(os.pathsep)
        self.assertEqual('a', path_split[0])
        self.assertEqual('b', path_split[1])
        self.assertEqual('c', path_split[2])

    def test_generate_classpath_with_invalid_paths(self):
        path = generate_classpath(['a', None, 'b', '', 'c'])
        path_split = path.split(os.pathsep)
        self.assertEqual('a', path_split[0])
        self.assertEqual('b', path_split[1])
        self.assertEqual('c', path_split[2])

    def test_dir_to_package(self):
        directory = os.path.join('org', 'apache', 'commons', 'math')
        package = dir_to_package(directory)

        self.assertEqual('org.apache.commons.math', package)

    def tearDown(self):
        calculator_clean_project()
class TestEvosuite(TestCase):
    def setUp(self):
        self.java_home = get_config()['java_home']
        self.maven_home = get_config()['maven_home']

        self.java = Java(self.java_home)
        self.maven = Maven(self.java, self.maven_home)

        self.maven.compile(calculator_project_dir(), 10)

    def test_generate(self):

        tests_src = os.path.join(calculator_project_dir(), 'evosuite')

        evosuite = Evosuite(java=self.java,
                            classpath=os.path.join(calculator_target_dir(),
                                                   'classes'),
                            tests_src=tests_src,
                            sut_class='br.ufal.ic.easy.operations.Sum',
                            params=['-Dsearch_budget=1'])

        (suite_name, suite_dir, suite_classes_dir,
         suite_classes) = evosuite.generate()

        self.assertTrue(suite_name.startswith('evosuite'))
        self.assertTrue(os.path.exists(suite_dir))

        self.assertTrue(len(get_java_files(suite_dir)) > 1)
        self.assertTrue(len(get_class_files(suite_classes_dir)) > 1)
        self.assertEquals(1, len(suite_classes))

        shutil.rmtree(tests_src)

    def test_generate_differential(self):

        tests_src = os.path.join(calculator_project_dir(), 'evosuite')

        evosuite = Evosuite(java=self.java,
                            classpath=os.path.join(calculator_target_dir(),
                                                   'classes'),
                            tests_src=tests_src,
                            sut_class='br.ufal.ic.easy.operations.Sum',
                            params=['-Dsearch_budget=1'])

        mutants = MuJava(java=self.java,
                         mutants_dir=calculator_mutants_dir()).read_log()

        (suite_name, suite_dir, suite_classes_dir,
         suite_classes) = evosuite.generate_differential(mutants[1].dir)

        self.assertTrue(suite_name.startswith('evosuite'))
        self.assertTrue(os.path.exists(suite_dir))

        self.assertTrue(len(get_java_files(suite_dir)) > 1)
        self.assertTrue(len(get_class_files(suite_classes_dir)) > 1)
        self.assertEquals(1, len(suite_classes))

        shutil.rmtree(tests_src)

    def tearDown(self):
        calculator_clean_project()