From e9c7f90e8e2bbc271580a8e751a955d0f3efe744 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Sat, 28 Dec 2019 23:46:10 +0800 Subject: [PATCH] feat: make first map --- ...ulette.java => AssertionRouletteTest.java} | 2 +- ...ava => ConstructorInitializationTest.java} | 2 +- ...teAssert.java => DuplicateAssertTest.java} | 2 +- ...ysteryGuest.java => MysteryGuestTest.java} | 2 +- ...dantPrint.java => RedundantPrintTest.java} | 2 +- cmd/tbs.go | 32 +++++++++++--- core/adapter/helper.go | 17 +++++++ core/domain/tbs/tbs_app.go | 44 +++++++++++++++++-- core/support/file_analysis_helper.go | 26 ++++++++++- 9 files changed, 112 insertions(+), 17 deletions(-) rename _fixtures/tbs/code/{AssertionRoulette.java => AssertionRouletteTest.java} (93%) rename _fixtures/tbs/code/{ConstructorInitialization.java => ConstructorInitializationTest.java} (89%) rename _fixtures/tbs/code/{DuplicateAssert.java => DuplicateAssertTest.java} (94%) rename _fixtures/tbs/code/{MysteryGuest.java => MysteryGuestTest.java} (93%) rename _fixtures/tbs/code/{RedundantPrint.java => RedundantPrintTest.java} (84%) diff --git a/_fixtures/tbs/code/AssertionRoulette.java b/_fixtures/tbs/code/AssertionRouletteTest.java similarity index 93% rename from _fixtures/tbs/code/AssertionRoulette.java rename to _fixtures/tbs/code/AssertionRouletteTest.java index dcc9318..fbdfb45 100644 --- a/_fixtures/tbs/code/AssertionRoulette.java +++ b/_fixtures/tbs/code/AssertionRouletteTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; -public class AssertionRoulette { +public class AssertionRouletteTest { @Test public void testCloneNonBareRepoFromLocalTestServer() throws Exception { Calculate calculate = new Calculate(); diff --git a/_fixtures/tbs/code/ConstructorInitialization.java b/_fixtures/tbs/code/ConstructorInitializationTest.java similarity index 89% rename from _fixtures/tbs/code/ConstructorInitialization.java rename to _fixtures/tbs/code/ConstructorInitializationTest.java index a902e53..c98fb2e 100644 --- a/_fixtures/tbs/code/ConstructorInitialization.java +++ b/_fixtures/tbs/code/ConstructorInitializationTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; -public class ConstructorInitialization { +public class ConstructorInitializationTest { @Before public void init() throws Exception { diff --git a/_fixtures/tbs/code/DuplicateAssert.java b/_fixtures/tbs/code/DuplicateAssertTest.java similarity index 94% rename from _fixtures/tbs/code/DuplicateAssert.java rename to _fixtures/tbs/code/DuplicateAssertTest.java index 8077b87..50055d0 100644 --- a/_fixtures/tbs/code/DuplicateAssert.java +++ b/_fixtures/tbs/code/DuplicateAssertTest.java @@ -4,7 +4,7 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; -public class DuplicateAssert { +public class DuplicateAssertTest { @Test public void testXmlSanitizer() { boolean valid = XmlSanitizer.isValid("Fritzbox"); diff --git a/_fixtures/tbs/code/MysteryGuest.java b/_fixtures/tbs/code/MysteryGuestTest.java similarity index 93% rename from _fixtures/tbs/code/MysteryGuest.java rename to _fixtures/tbs/code/MysteryGuestTest.java index ffbac2c..b773ddb 100644 --- a/_fixtures/tbs/code/MysteryGuest.java +++ b/_fixtures/tbs/code/MysteryGuestTest.java @@ -6,7 +6,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -public class MysteryGuest { +public class MysteryGuestTest { @Test public void testPersistence() throws Exception { try (FileOutputStream out = new FileOutputStream("people.bin");) { diff --git a/_fixtures/tbs/code/RedundantPrint.java b/_fixtures/tbs/code/RedundantPrintTest.java similarity index 84% rename from _fixtures/tbs/code/RedundantPrint.java rename to _fixtures/tbs/code/RedundantPrintTest.java index 88ef5d1..1b7da1e 100644 --- a/_fixtures/tbs/code/RedundantPrint.java +++ b/_fixtures/tbs/code/RedundantPrintTest.java @@ -2,7 +2,7 @@ package tbs; import org.junit.Test; -public class RedundantPrint { +public class RedundantPrintTest { @Test public void testTransform10mNEUAndBack() { String result = "a, b, c"; diff --git a/cmd/tbs.go b/cmd/tbs.go index 4c5535e..cc2a761 100644 --- a/cmd/tbs.go +++ b/cmd/tbs.go @@ -1,15 +1,17 @@ package cmd import ( - "github.com/phodal/coca/cmd/cmd_util" - "github.com/phodal/coca/config" + "encoding/json" "github.com/phodal/coca/core/adapter" + "github.com/phodal/coca/core/adapter/call" "github.com/phodal/coca/core/domain/tbs" + "github.com/phodal/coca/core/models" + "github.com/phodal/coca/core/support" "github.com/spf13/cobra" ) type TbsCmdConfig struct { - DependencePath string + Path string } var ( @@ -21,17 +23,33 @@ var tbsCmd = &cobra.Command{ Short: "test bad smell", Long: ``, Run: func(cmd *cobra.Command, args []string) { - identifiers = adapter.LoadIdentify(apiCmdConfig.DependencePath) + files := support.GetJavaTestFiles(tbsCmdConfig.Path) + var identifiers []models.JIdentifier + + identifiers = adapter.LoadTestIdentify(files) identifiersMap = adapter.BuildIdentifierMap(identifiers) - parsedDeps := cmd_util.GetDepsFromJson(tbsCmdConfig.DependencePath) + var classes []string = nil + for _, node := range identifiers { + classes = append(classes, node.Package+"."+node.ClassName) + } + + analysisApp := call.NewJavaCallApp() + classNodes := analysisApp.AnalysisFiles(identifiers, files, classes) + + nodeContent, _ := json.MarshalIndent(classNodes, "", "\t") + support.WriteToCocaFile("tdeps.json", string(nodeContent)) + app := tbs.NewTbsApp() - app.AnalysisPath(parsedDeps, identifiersMap) + result := app.AnalysisPath(classNodes, identifiersMap) + + resultContent, _ := json.MarshalIndent(result, "", "\t") + support.WriteToCocaFile("tbs.json", string(resultContent)) }, } func init() { rootCmd.AddCommand(tbsCmd) - tbsCmd.PersistentFlags().StringVarP(&tbsCmdConfig.DependencePath, "dependence", "d", config.CocaConfig.ReporterPath+"/deps.json", "get dependence file") + tbsCmd.PersistentFlags().StringVarP(&tbsCmdConfig.Path, "path", "p", ".", "example -p core/main") } diff --git a/core/adapter/helper.go b/core/adapter/helper.go index e0df1bb..1741186 100644 --- a/core/adapter/helper.go +++ b/core/adapter/helper.go @@ -34,6 +34,23 @@ func LoadIdentify(importPath string) []models.JIdentifier { return *&identifiers } +func LoadTestIdentify(files []string) []models.JIdentifier { + var identifiers []models.JIdentifier + + apiContent := support.ReadCocaFile("tidentify.json") + if apiContent == nil { + identifierApp := new(identifier.JavaIdentifierApp) + ident := identifierApp.AnalysisFiles(files) + + identModel, _ := json.MarshalIndent(ident, "", "\t") + support.WriteToCocaFile("tidentify.json", string(identModel)) + + return *&ident + } + _ = json.Unmarshal(apiContent, &identifiers) + + return *&identifiers +} func BuildDIMap(identifiers []models.JIdentifier, identifierMap map[string]models.JIdentifier) map[string]string { var diMap = make(map[string]string) diff --git a/core/domain/tbs/tbs_app.go b/core/domain/tbs/tbs_app.go index a44b3bb..0079328 100644 --- a/core/domain/tbs/tbs_app.go +++ b/core/domain/tbs/tbs_app.go @@ -1,16 +1,54 @@ package tbs -import "github.com/phodal/coca/core/models" +import ( + "github.com/phodal/coca/core/models" +) type TbsApp struct { - } func NewTbsApp() *TbsApp { return &TbsApp{} } -func (a TbsApp) AnalysisPath(deps []models.JClassNode, identifiersMap map[string]models.JIdentifier) { +type TestBadSmell struct { + FileName string + Type string + Description string + Line int +} + +func (a TbsApp) AnalysisPath(deps []models.JClassNode, identifiersMap map[string]models.JIdentifier) []TestBadSmell { + var results []TestBadSmell = nil + var identMethodMap = make(map[string]models.JMethod) + for key, clzMap := range identifiersMap { + for _, method := range clzMap.Methods { + identMethodMap[key + "." + method.Name] = method + } + } + for _, clz := range deps { + // TODO refactoring identify & annotation + for _, method := range clz.Methods { + fullName := clz.Package + "." + clz.Class + "." + method.Name + checkIgnoreTest(clz, identMethodMap[fullName], &results) + } + } + + return results } +func checkIgnoreTest(clz models.JClassNode, method models.JMethod, results *[]TestBadSmell) { + for _, annotation := range method.Annotations { + if annotation.QualifiedName == "Ignore" { + tbs := *&TestBadSmell{ + FileName: clz.Path, + Type: "IgnoreTest", + Description: "", + Line: 0, + } + + *results = append(*results, tbs) + } + } +} diff --git a/core/support/file_analysis_helper.go b/core/support/file_analysis_helper.go index 8b5cd21..e6452f9 100644 --- a/core/support/file_analysis_helper.go +++ b/core/support/file_analysis_helper.go @@ -24,7 +24,29 @@ func GetJavaFiles(codeDir string) []string { } } - if strings.HasSuffix(path, ".java") && !strings.Contains(path, "Test.java")&& !strings.Contains(path, "Tests.java"){ + if strings.HasSuffix(path, ".java") && !strings.Contains(path, "Test.java") && !strings.Contains(path, "Tests.java") { + files = append(files, path) + } + return nil + }) + return files +} + +func GetJavaTestFiles(codeDir string) []string { + files := make([]string, 0) + gitIgnore, err := ignore.CompileIgnoreFile(".gitignore") + if err != nil { + fmt.Println(err) + } + + _ = filepath.Walk(codeDir, func(path string, fi os.FileInfo, err error) error { + if gitIgnore != nil { + if gitIgnore.MatchesPath(path) { + return nil + } + } + + if strings.Contains(path, "Test.java") || strings.Contains(path, "Tests.java") { files = append(files, path) } return nil @@ -35,7 +57,7 @@ func GetJavaFiles(codeDir string) []string { func ProcessFile(path string) *JavaParser { is, _ := antlr.NewFileStream(path) lexer := NewJavaLexer(is) - stream := antlr.NewCommonTokenStream(lexer, 0); + stream := antlr.NewCommonTokenStream(lexer, 0) parser := NewJavaParser(stream) return parser } -- GitLab