From 31dad88a261c88c85c06503e909e82d8ddb17afd Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Tue, 31 Dec 2019 07:53:55 +0800 Subject: [PATCH] refactor: tbs behavior to jmethod --- core/context/count/count_app.go | 2 +- core/context/rcall/rcall_graph.go | 2 +- core/context/tbs/tbs_app.go | 30 ++++++++---------------------- core/domain/jannotation.go | 12 ++++++++++++ core/domain/jmethod.go | 12 +++++++++++- core/domain/jmethod_call.go | 10 +++++++++- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/core/context/count/count_app.go b/core/context/count/count_app.go index 7ade3c0..9658354 100644 --- a/core/context/count/count_app.go +++ b/core/context/count/count_app.go @@ -13,7 +13,7 @@ func BuildCallMap(parserDeps []domain.JClassNode) map[string]int { for _, clz := range parserDeps { for _, method := range clz.Methods { for _, call := range method.MethodCalls { - callMethod := call.BuilFullMethodName() + callMethod := call.BuildFullMethodName() if _, ok := projectMethods[callMethod]; ok { if callMap[callMethod] == 0 { callMap[callMethod] = 1 diff --git a/core/context/rcall/rcall_graph.go b/core/context/rcall/rcall_graph.go index 19cf2d2..8966ebf 100644 --- a/core/context/rcall/rcall_graph.go +++ b/core/context/rcall/rcall_graph.go @@ -47,7 +47,7 @@ func BuildRCallMethodMap(parserDeps []domain.JClassNode, projectMaps map[string] var caller = method.BuildFullMethodName(clz) for _, call := range method.MethodCalls { if call.Class != "" { - callee := call.BuilFullMethodName() + callee := call.BuildFullMethodName() if projectMaps[callee] < 1 { continue } diff --git a/core/context/tbs/tbs_app.go b/core/context/tbs/tbs_app.go index ca8dfd4..3dfcdbd 100644 --- a/core/context/tbs/tbs_app.go +++ b/core/context/tbs/tbs_app.go @@ -25,7 +25,7 @@ func (a TbsApp) AnalysisPath(deps []domain.JClassNode, identifiersMap map[string callMethodMap := buildCallMethodMap(deps) for _, clz := range deps { for _, method := range clz.Methods { - if !isTest(method) { + if !method.IsJunitTest() { continue } @@ -44,7 +44,7 @@ func (a TbsApp) AnalysisPath(deps []domain.JClassNode, identifiersMap map[string continue } - methodCallMap[getMethodCallFullPath(methodCall)] = append(methodCallMap[getMethodCallFullPath(methodCall)], methodCall) + methodCallMap[methodCall.BuildFullMethodName()] = append(methodCallMap[methodCall.BuildFullMethodName()], methodCall) checkRedundantPrintTest(clz.Path, methodCall, &results, &testType) checkSleepyTest(clz.Path, methodCall, method, &results, &testType) @@ -72,7 +72,7 @@ func buildCallMethodMap(deps []domain.JClassNode) map[string]domain.JMethod { var callMethodMap = make(map[string]domain.JMethod) for _, clz := range deps { for _, method := range clz.Methods { - callMethodMap[clz.Package+"."+clz.Class+"."+method.Name] = method + callMethodMap[method.BuildFullMethodName(clz)] = method } } return callMethodMap @@ -82,7 +82,7 @@ func updateMethodCallsForSelfCall(method domain.JMethod, clz domain.JClassNode, currentMethodCalls := method.MethodCalls for _, methodCall := range currentMethodCalls { if methodCall.Class == clz.Class { - jMethod := callMethodMap[getMethodCallFullPath(methodCall)] + jMethod := callMethodMap[methodCall.BuildFullMethodName()] if jMethod.Name != "" { currentMethodCalls = append(currentMethodCalls, jMethod.MethodCalls...) } @@ -119,16 +119,6 @@ func hasAssertion(methodName string) bool { return false } -func isTest(method domain.JMethod) bool { - var isTest = false - for _, annotation := range method.Annotations { - if annotation.QualifiedName == "Test" || annotation.QualifiedName == "Ignore" { - isTest = true - } - } - return isTest -} - func checkUnknownTest(clz domain.JClassNode, method domain.JMethod, results *[]TestBadSmell, testType *string) { *testType = "UnknownTest" tbs := *&TestBadSmell{ @@ -165,12 +155,8 @@ func checkDuplicateAssertTest(clz domain.JClassNode, results *[]TestBadSmell, me } } -func getMethodCallFullPath(methodCall domain.JMethodCall) string { - return methodCall.Package + "." + methodCall.Class + "." + methodCall.MethodName -} - func checkSleepyTest(path string, method domain.JMethodCall, jMethod domain.JMethod, results *[]TestBadSmell, testType *string) { - if method.MethodName == "sleep" && method.Class == "Thread" { + if method.IsThreadSleep() { *testType = "SleepyTest" tbs := *&TestBadSmell{ FileName: path, @@ -184,7 +170,7 @@ func checkSleepyTest(path string, method domain.JMethodCall, jMethod domain.JMet } func checkRedundantPrintTest(path string, mCall domain.JMethodCall, results *[]TestBadSmell, testType *string) { - if mCall.Class == "System.out" && (mCall.MethodName == "println" || mCall.MethodName == "printf" || mCall.MethodName == "print") { + if mCall.IsSystemOutput() { *testType = "RedundantPrintTest" tbs := *&TestBadSmell{ FileName: path, @@ -198,7 +184,7 @@ func checkRedundantPrintTest(path string, mCall domain.JMethodCall, results *[]T } func checkEmptyTest(path string, annotation domain.Annotation, results *[]TestBadSmell, method domain.JMethod, testType *string) { - if annotation.QualifiedName == "Test" { + if annotation.IsTest() { if len(method.MethodCalls) <= 1 { *testType = "EmptyTest" tbs := *&TestBadSmell{ @@ -214,7 +200,7 @@ func checkEmptyTest(path string, annotation domain.Annotation, results *[]TestBa } func checkIgnoreTest(clzPath string, annotation domain.Annotation, results *[]TestBadSmell, testType *string) { - if annotation.QualifiedName == "Ignore" { + if annotation.IsIgnoreTest() { *testType = "IgnoreTest" tbs := *&TestBadSmell{ FileName: clzPath, diff --git a/core/domain/jannotation.go b/core/domain/jannotation.go index ea1fe9b..16d81c4 100644 --- a/core/domain/jannotation.go +++ b/core/domain/jannotation.go @@ -19,4 +19,16 @@ func NewAnnotation() Annotation { func (n * Annotation) IsComponentOrRepository() bool { return n.QualifiedName == "Component" || n.QualifiedName == "Repository" +} + +func (n * Annotation) IsTest() bool { + return n.QualifiedName == "Test" +} + +func (n * Annotation) IsIgnoreTest() bool { + return n.QualifiedName == "Ignore" +} + +func (n * Annotation) IsIgnoreOrTest() bool { + return n.IsTest() || n.IsIgnoreTest() } \ No newline at end of file diff --git a/core/domain/jmethod.go b/core/domain/jmethod.go index 93693f4..54692a7 100644 --- a/core/domain/jmethod.go +++ b/core/domain/jmethod.go @@ -64,8 +64,18 @@ func (m *JMethod) GetAllCallString() []string { var calls []string for _, call := range m.MethodCalls { if call.Class != "" { - calls = append(calls, call.BuilFullMethodName()) + calls = append(calls, call.BuildFullMethodName()) } } return calls } + +func (m *JMethod) IsJunitTest() bool { + var isTest = false + for _, annotation := range m.Annotations { + if annotation.IsIgnoreOrTest() { + isTest = true + } + } + return isTest +} diff --git a/core/domain/jmethod_call.go b/core/domain/jmethod_call.go index 62a186a..cd1f79e 100644 --- a/core/domain/jmethod_call.go +++ b/core/domain/jmethod_call.go @@ -26,6 +26,14 @@ func NewJMethodCall() JMethodCall { } } -func (c *JMethodCall) BuilFullMethodName() string { +func (c *JMethodCall) BuildFullMethodName() string { return c.Package + "." + c.Class + "." + c.MethodName +} + +func (c *JMethodCall) IsSystemOutput() bool { + return c.Class == "System.out" && (c.MethodName == "println" || c.MethodName == "printf" || c.MethodName == "print") +} + +func (c *JMethodCall) IsThreadSleep() bool { + return c.MethodName == "sleep" && c.Class == "Thread" } \ No newline at end of file -- GitLab