Exemplo n.º 1
0
    def test_linker(self):
        self.create_codebase()
        self.create_filters()
        self.create_documentation()
        self.create_channel()
        self.create_documentation2()
        self.create_channel2()
        self.parse_snippets()
        link_code(self.pname, 'core', self.release, 'javaclass', 'd',
                self.release)
        link_code(self.pname, 'core', self.release, 'javaclass', 's',
                None)
        link_code(self.pname, 'core', self.release, 'javapostclass', '',
                None)
        link_code(self.pname, 'core', self.release, 'javamethod', 'd',
                self.release)
        link_code(self.pname, 'core', self.release, 'javamethod', 's',
                None)
        link_code(self.pname, 'core', self.release, 'javafield', 'd',
                self.release)
        link_code(self.pname, 'core', self.release, 'javafield', 's',
                None)
        link_code(self.pname, 'core', self.release, 'javageneric', 'd',
                self.release)
        link_code(self.pname, 'core', self.release, 'javageneric', 's',
                None)

        code_ref1 = self.code_refs[0]
        code_ref1 = SingleCodeReference.objects.get(pk=code_ref1.pk)
        self.assertEqual(
                'p1.Annotation1',
                code_ref1.release_links.all()[0].first_link.code_element.fqn)

        snippet2 = self.code_snippets[1]
        snippet2 = CodeSnippet.objects.get(pk=snippet2.pk)
        ann_ref = None
        for code_ref in snippet2.single_code_references.all():
            if code_ref.kind_hint.pk == self.ann_kind.pk:
                ann_ref = code_ref
                break

        self.assertEqual(
                'Annotation1',
                ann_ref.release_links.all()[0]
                    .first_link.code_element.simple_name)

        # Test class insensitive comparison
        code_ref3 = self.code_refs[2]
        code_ref3 = SingleCodeReference.objects.get(pk=code_ref3.pk)
        self.assertEqual(
                'Clazz1',
                code_ref3.release_links.all()[0]
                .first_link.code_element.simple_name)

        # Test enum in class.
        code_ref4 = self.code_refs[3]
        code_ref4 = SingleCodeReference.objects.get(pk=code_ref4.pk)
        self.assertEqual(
                'Enum1',
                code_ref4.release_links.all()[0]
                .first_link.code_element.simple_name)

        # Test reclassification
        code_ref5 = self.code_refs[4]
        code_ref5 = SingleCodeReference.objects.get(pk=code_ref5.pk)
        self.assertEqual(
                'unknown',
                code_ref5.kind_hint.kind)

        # Test post-processing
        code_ref2 = self.code_refs[1]
        code_ref2 = SingleCodeReference.objects.get(pk=code_ref2.pk)
        self.assertEqual('highest_frequency',
                code_ref2.release_links.all()[0].first_link.rationale)

        self.assertEqual('heuristic_depth',
                code_ref3.release_links.all()[0].first_link.rationale)
        self.assertEqual('p1.Clazz1',
                code_ref3.release_links.all()[0]
                .first_link.code_element.fqn)
        self.assertEqual('heuristic_depth',
                code_ref4.release_links.all()[0].first_link.rationale)

        # Test type custom filter (single ref)
        code_ref6 = self.code_refs[5]
        code_ref6 = SingleCodeReference.objects.get(pk=code_ref6.pk)
        # Not reclassified because custom filtered!
        self.assertEqual(
                'class',
                code_ref6.kind_hint.kind)
        type_log = DEBUG_LOG[code_ref6.pk][0]
        self.assertEqual(0, type_log['final size'])
        self.assertTrue(type_log['custom filtered'])

        # Test type custom filter (compound ref)
        code_ref7 = self.code_refs[6]
        code_ref7 = SingleCodeReference.objects.get(pk=code_ref7.pk)
        self.assertEqual(
                'RecodocClient',
                code_ref7.release_links.all()[0]
                .first_link.code_element.simple_name)

        # Test List in snippet
        custom_filtered = []
        for code_ref in snippet2.single_code_references.all():
            if code_ref.content.find('List') > -1:
                if (len(DEBUG_LOG[code_ref.pk]) > 0):
                    custom_filtered.append(
                            DEBUG_LOG[code_ref.pk][0]['custom filtered'])
        self.assertEqual(1, len(custom_filtered))
        for customf in custom_filtered:
            self.assertTrue(customf)

        # Test RecodocClient in snippet
        custom_filtered = []
        for code_ref in snippet2.single_code_references.all():
            if code_ref.content.find('RecodocClient') > -1 and\
                code_ref.kind_hint.kind == 'class':
                if (len(DEBUG_LOG[code_ref.pk]) > 0):
                    custom_filtered.append(
                            DEBUG_LOG[code_ref.pk][0]['custom filtered'])
        self.assertEqual(2, len(custom_filtered))
        for customf in custom_filtered:
            self.assertFalse(customf)

        # Test method param number filter
        code_ref8 = self.code_refs[7]
        code_ref8 = SingleCodeReference.objects.get(pk=code_ref8.pk)
        self.assertEqual('callMethod1',
                code_ref8.release_links.all()[0]
                .first_link.code_element.simple_name)
        self.assertEqual(1,
                code_ref8.release_links.all()[0]
                .first_link.code_element.parameters().count())
        method_log = DEBUG_LOG[code_ref8.pk][0]
        self.assertTrue(method_log['ParameterNumberFilter'][0])

        # Test method param type filter
        code_ref9 = self.code_refs[8]
        code_ref9 = SingleCodeReference.objects.get(pk=code_ref9.pk)
        self.assertEqual('String',
                code_ref9.release_links.all()[0]
                .first_link.code_element.parameters()[0].type_simple_name)
        method_log = DEBUG_LOG[code_ref9.pk][0]
        self.assertTrue(method_log['ParameterTypeFilter'][0])

        # Test method param type package filter
        code_ref10 = self.code_refs[9]
        code_ref10 = SingleCodeReference.objects.get(pk=code_ref10.pk)
        self.assertEqual('p3.RecodocClient',
                code_ref10.release_links.all()[0]
                .first_link.code_element.parameters()[0].type_fqn)
        method_log = DEBUG_LOG[code_ref10.pk][0]
        self.assertTrue(method_log['ParameterTypeFilter'][0])

        # Test method object filter
        code_ref11 = self.code_refs[10]
        code_ref11 = SingleCodeReference.objects.get(pk=code_ref11.pk)
        method_log = DEBUG_LOG[code_ref11.pk][0]
        self.assertTrue(method_log['ObjectMethodsFilter'][0])
        self.assertEqual(0, method_log['final size'])

        # Test method custom filter
        code_ref13 = self.code_refs[12]
        code_ref13 = SingleCodeReference.objects.get(pk=code_ref13.pk)
        method_log = DEBUG_LOG[code_ref13.pk][0]
        self.assertTrue(method_log['custom filtered'])
        self.assertEqual(0, method_log['final size'])

        # Test filter decorator
        code_ref14 = self.code_refs[13]
        code_ref14 = SingleCodeReference.objects.get(pk=code_ref14.pk)
        method_log = DEBUG_LOG[code_ref14.pk][0]
        self.assertFalse(method_log['ParameterTypeFilter'][0])
        self.assertEqual(0, method_log['final size'])

        # Test name similarity
        code_ref16 = self.code_refs[15]
        code_ref16 = SingleCodeReference.objects.get(pk=code_ref16.pk)
        method_log = DEBUG_LOG[code_ref16.pk][0]
        self.assertTrue(method_log['ContextNameSimilarityFilter'][0])
        self.assertEqual('p3.RecodocClient',
                code_ref16.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        # Test immediate context filter
        code_ref22 = self.code_refs[21]
        code_ref22 = SingleCodeReference.objects.get(pk=code_ref22.pk)
        method_log = DEBUG_LOG[code_ref22.pk][0]
        self.assertTrue(method_log['ImmediateContextFilter'][0])
        self.assertEqual('p3.GeneralClient',
                code_ref22.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        code_ref24 = self.code_refs[23]
        code_ref24 = SingleCodeReference.objects.get(pk=code_ref24.pk)
        method_log = DEBUG_LOG[code_ref24.pk][0]
        self.assertTrue(method_log['ImmediateContextHierarchyFilter'][0])
        self.assertEqual('p3.GeneralClient',
                code_ref24.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        # Test global filter
        code_ref25 = self.code_refs[24]
        code_ref25 = SingleCodeReference.objects.get(pk=code_ref25.pk)
        method_log = DEBUG_LOG[code_ref25.pk][0]
        self.assertTrue(method_log['globContextFilter'][0])
        self.assertEqual('p3.RecodocClient',
                code_ref25.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        # Test snippet filter
        snippet2 = self.code_snippets[1]
        snippet2 = CodeSnippet.objects.get(pk=snippet2.pk)
        method_ref = None
        for code_ref in snippet2.single_code_references.all():
            if code_ref.kind_hint.pk == self.method_kind.pk and\
                    code_ref.content.find('callMethod10') > -1:
                method_ref = code_ref
                break
        method_log = DEBUG_LOG[method_ref.pk][0]
        self.assertTrue(method_log['snipContextFilter'][0])
        self.assertEqual('p3.RecodocClient',
                method_ref.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        # Test return type
        code_ref18 = self.code_refs[17]
        code_ref18 = SingleCodeReference.objects.get(pk=code_ref18.pk)
        method_log = DEBUG_LOG[code_ref18.pk][0]
        self.assertTrue(method_log['locReturnContextFilter'][0])
        self.assertEqual('p3.RecodocClient2',
                code_ref18.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        # Test return type hierarchy
        code_ref19 = self.code_refs[18]
        code_ref19 = SingleCodeReference.objects.get(pk=code_ref19.pk)
        method_log = DEBUG_LOG[code_ref19.pk][0]
        self.assertTrue(method_log['locReturnContextFilterHierarchy'][0])
        self.assertEqual('p3.RecodocClient2Parent',
                code_ref19.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, method_log['final size'])

        # Test global filter
        code_ref26 = self.code_refs[25]
        code_ref26 = SingleCodeReference.objects.get(pk=code_ref26.pk)
        method_log = DEBUG_LOG[code_ref26.pk][0]
        self.assertTrue(method_log['AbstractTypeFilter'][0])
        self.assertEqual('p3.RecodocClient',
                code_ref26.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(2, method_log['final size'])

        code_ref27 = self.code_refs[26]
        code_ref27 = SingleCodeReference.objects.get(pk=code_ref27.pk)
        method_log = DEBUG_LOG[code_ref27.pk][0]
        self.assertTrue(method_log['StrictFilter'][0])
        self.assertEqual(0, method_log['final size'])

        # Test annotation field
        #snippet1 = self.code_snippets[0]
        #snippet1 = CodeSnippet.objects.get(pk=snippet1.pk)
        #field_ref = None
        #for code_ref in snippet1.single_code_references.all():
            #if code_ref.kind_hint.pk == self.ann_field_kind.pk and\
                    #code_ref.content.find('foobarbaz') > -1:
                #field_ref = code_ref
                #break
        #field_log = DEBUG_LOG[field_ref.pk][0]
        #self.assertEqual('p1.Annotation1',
                #method_ref.release_links.all()[0]
                #.first_link.code_element.containers.all()[0].fqn)
        #self.assertEqual(1, field_log['final size'])

        # Test enum value
        code_ref28 = self.code_refs[27]
        code_ref28 = SingleCodeReference.objects.get(pk=code_ref28.pk)
        field_log = DEBUG_LOG[code_ref28.pk][0]
        self.assertTrue(field_log['globContextFilter'][0])
        self.assertEqual(1, field_log['original size'])
        self.assertEqual('p1.Enum1',
                code_ref28.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, field_log['final size'])

        # Test field
        code_ref29 = self.code_refs[28]
        code_ref29 = SingleCodeReference.objects.get(pk=code_ref29.pk)
        field_log = DEBUG_LOG[code_ref29.pk][0]
        self.assertTrue(field_log['globContextFilter'][0])
        self.assertEqual('p3.RecodocClient',
                code_ref29.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, field_log['final size'])

        # Test generic linker
        code_ref30 = self.code_refs[29]
        code_ref30 = SingleCodeReference.objects.get(pk=code_ref30.pk)
        field_log = DEBUG_LOG[code_ref30.pk][1]
        self.assertTrue(field_log['globContextFilter'][0])
        self.assertEqual('p3.RecodocClient',
                code_ref30.release_links.all()[0]
                .first_link.code_element.containers.all()[0].fqn)
        self.assertEqual(1, field_log['final size'])
        self.assertEqual('javageneric', field_log['linker'])
Exemplo n.º 2
0
    def test_context(self):
        self.create_codebase()
        self.create_filters()
        self.create_documentation()
        self.create_channel()
        self.create_documentation2()
        self.parse_snippets()
        link_code(self.pname, 'core', self.release, 'javaclass', 'd',
                self.release)
        link_code(self.pname, 'core', self.release, 'javaclass', 's',
                None)
        link_code(self.pname, 'core', self.release, 'javapostclass', '',
                None)

        code_ref20 = self.code_refs[19]
        code_ref20 = SingleCodeReference.objects.get(pk=code_ref20.pk)

        print('DEBUG CONTEXT')
        print(code_ref20.content)

        # Local Context
        context_types = ctx.get_context_types(
                code_ref20.local_object_id,
                code_ref20.source,
                ctx.local_filter,
                self.codebase,
                ctx.LOCAL)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertTrue('p1.ChildClazz' in fqn)
        self.assertEqual(1, len(fqn))

        context_types = ctx.get_context_types_hierarchy(
                code_ref20.local_object_id,
                code_ref20.source,
                ctx.local_filter,
                self.codebase,
                ctx.LOCAL)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertTrue('p1.InterfaceClazz' in fqn)
        self.assertEqual(3, len(fqn))

        # Mid Context
        context_types = ctx.get_context_types(
                code_ref20.mid_object_id,
                code_ref20.source,
                ctx.mid_filter,
                self.codebase,
                ctx.MIDDLE)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(3, len(fqn))
        self.assertTrue('p1.ChildClazz' in fqn)
        self.assertTrue('p1.Annotation1' in fqn)
        self.assertTrue('p1.Annotation2' in fqn)

        context_types = ctx.get_context_types_hierarchy(
                code_ref20.mid_object_id,
                code_ref20.source,
                ctx.mid_filter,
                self.codebase,
                ctx.MIDDLE)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(5, len(fqn))
        self.assertTrue('p1.InterfaceClazz' in fqn)

        # Global Context
        context_types = ctx.get_context_types(
                code_ref20.global_object_id,
                code_ref20.source,
                ctx.global_filter,
                self.codebase,
                ctx.GLOBAL)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(3, len(fqn))
        self.assertTrue('p1.Annotation2' in fqn)

        context_types = ctx.get_context_types_hierarchy(
                code_ref20.global_object_id,
                code_ref20.source,
                ctx.global_filter,
                self.codebase,
                ctx.GLOBAL)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(5, len(fqn))
        self.assertTrue('p1.InterfaceClazz' in fqn)

        # Snippet
        snippet1 = self.code_snippets[0]
        context_types = ctx.get_context_types(
                snippet1.pk,
                snippet1.source,
                ctx.snippet_filter,
                self.codebase,
                ctx.SNIPPET)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(2, len(fqn))
        self.assertTrue('p3.RecodocClient' in fqn)

        context_types = ctx.get_context_types_hierarchy(
                snippet1.pk,
                snippet1.source,
                ctx.snippet_filter,
                self.codebase,
                ctx.SNIPPET)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(5, len(fqn))
        self.assertTrue('p3.IGeneralClient' in fqn)

        # Return Type
        link_code(self.pname, 'core', self.release, 'javamethod', 'd',
                self.release)
        link_code(self.pname, 'core', self.release, 'javamethod', 's',
                None)
        code_ref14 = self.code_refs[13]
        code_ref14 = SingleCodeReference.objects.get(pk=code_ref14.pk)
        context_types = ctx.get_context_return_types(
                code_ref14.local_object_id,
                code_ref14.source,
                ctx.local_filter,
                self.codebase,
                ctx.LOCAL)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(1, len(fqn))
        self.assertTrue('p3.RecodocClient2' in fqn)

        context_types = ctx.get_context_return_types_hierarchy(
                code_ref14.local_object_id,
                code_ref14.source,
                ctx.local_filter,
                self.codebase,
                ctx.LOCAL)
        fqn = [context_type.fqn for context_type in context_types]
        print(fqn)
        self.assertEqual(2, len(fqn))
        self.assertTrue('p3.RecodocClient2Parent' in fqn)