From f41d4f823402f13e99237aa4cb35ce52cea06809 Mon Sep 17 00:00:00 2001 From: tsantalis Date: Thu, 27 Jun 2024 07:15:19 -0400 Subject: [PATCH] Updated CodeElementLocator tests to work without name --- .../org/codetracker/element/Attribute.java | 19 ++- .../java/org/codetracker/element/Method.java | 25 +++- .../util/AbstractCodeElementLocator.java | 108 ++++++++++++++---- .../codetracker/util/CodeElementLocator.java | 52 +-------- .../CodeElementLocatorWithLocalFiles.java | 56 +-------- .../util/CodeElementLocatorTest.java | 75 +++++++----- .../CodeElementLocatorWithLocalFilesTest.java | 45 ++++++-- 7 files changed, 208 insertions(+), 172 deletions(-) diff --git a/src/main/java/org/codetracker/element/Attribute.java b/src/main/java/org/codetracker/element/Attribute.java index 9ed2e1ed8f6..537057106c7 100644 --- a/src/main/java/org/codetracker/element/Attribute.java +++ b/src/main/java/org/codetracker/element/Attribute.java @@ -4,6 +4,7 @@ import gr.uom.java.xmi.UMLAnonymousClass; import gr.uom.java.xmi.UMLAttribute; import gr.uom.java.xmi.UMLOperation; +import gr.uom.java.xmi.LocationInfo.CodeElementType; import gr.uom.java.xmi.decomposition.AbstractCodeFragment; import gr.uom.java.xmi.decomposition.CompositeStatementObject; import gr.uom.java.xmi.decomposition.LambdaExpressionObject; @@ -14,6 +15,8 @@ import static org.codetracker.util.Util.annotationsToString; import static org.codetracker.util.Util.getPath; +import java.util.LinkedHashSet; +import java.util.Set; import java.util.function.Predicate; public class Attribute extends BaseCodeElement { @@ -56,12 +59,18 @@ public Block findBlockWithoutName(Predicate equalOperator) { } } } + Set matches = new LinkedHashSet(); for (CompositeStatementObject composite : operation.getBody().getCompositeStatement().getInnerNodes()) { Block block = Block.of(composite, this); if (block != null && equalOperator.test(block)) { - return block; + matches.add(block); } } + for (Block match : matches) { + if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) { + return match; + } + } } } } @@ -76,12 +85,18 @@ public Block findBlockWithoutName(Predicate equalOperator) { } } } + Set matches = new LinkedHashSet(); for (CompositeStatementObject composite : lambda.getBody().getCompositeStatement().getInnerNodes()) { Block block = Block.of(composite, this); if (block != null && equalOperator.test(block)) { - return block; + matches.add(block); } } + for (Block match : matches) { + if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) { + return match; + } + } } } return null; diff --git a/src/main/java/org/codetracker/element/Method.java b/src/main/java/org/codetracker/element/Method.java index 7ad27c8c17d..55fdf9fdd14 100644 --- a/src/main/java/org/codetracker/element/Method.java +++ b/src/main/java/org/codetracker/element/Method.java @@ -1,6 +1,7 @@ package org.codetracker.element; import gr.uom.java.xmi.*; +import gr.uom.java.xmi.LocationInfo.CodeElementType; import gr.uom.java.xmi.decomposition.AbstractCodeFragment; import gr.uom.java.xmi.decomposition.AbstractStatement; import gr.uom.java.xmi.decomposition.CompositeStatementObject; @@ -118,12 +119,18 @@ public Block findBlockWithoutName(Predicate equalOperator) { } } } + Set matches = new LinkedHashSet(); for (CompositeStatementObject composite : umlOperation.getBody().getCompositeStatement().getInnerNodes()) { Block block = Block.of(composite, this); if (block != null && equalOperator.test(block)) { - return block; + matches.add(block); } } + for (Block match : matches) { + if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) { + return match; + } + } } for (UMLAnonymousClass anonymousClass : umlOperation.getAnonymousClassList()) { for (UMLOperation operation : anonymousClass.getOperations()) { @@ -137,12 +144,18 @@ public Block findBlockWithoutName(Predicate equalOperator) { } } } + Set matches = new LinkedHashSet(); for (CompositeStatementObject composite : operation.getBody().getCompositeStatement().getInnerNodes()) { Block block = Block.of(composite, this); if (block != null && equalOperator.test(block)) { - return block; + matches.add(block); } } + for (Block match : matches) { + if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) { + return match; + } + } } } } @@ -157,12 +170,18 @@ public Block findBlockWithoutName(Predicate equalOperator) { } } } + Set matches = new LinkedHashSet(); for (CompositeStatementObject composite : lambda.getBody().getCompositeStatement().getInnerNodes()) { Block block = Block.of(composite, this); if (block != null && equalOperator.test(block)) { - return block; + matches.add(block); } } + for (Block match : matches) { + if (!match.getLocation().getCodeElementType().equals(CodeElementType.BLOCK)) { + return match; + } + } } } return null; diff --git a/src/main/java/org/codetracker/util/AbstractCodeElementLocator.java b/src/main/java/org/codetracker/util/AbstractCodeElementLocator.java index d187e561f00..a058d0e42a8 100644 --- a/src/main/java/org/codetracker/util/AbstractCodeElementLocator.java +++ b/src/main/java/org/codetracker/util/AbstractCodeElementLocator.java @@ -4,6 +4,7 @@ import java.util.function.Predicate; import org.codetracker.api.CodeElement; +import org.codetracker.api.CodeElementNotFoundException; import org.codetracker.api.Version; import org.codetracker.element.Attribute; import org.codetracker.element.Block; @@ -36,15 +37,17 @@ public AbstractCodeElementLocator(String commitId, String filePath, int lineNumb public abstract CodeElement locate() throws Exception; - protected static Method getMethod(UMLModel umlModel, Version version, Predicate predicate) { + protected static Method getMethod(UMLModel umlModel, Version version, String filePath, Predicate predicate) { if (umlModel != null) for (UMLClass umlClass : umlModel.getClassList()) { - for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) { - Method method = getMethod(version, predicate, anonymousClass.getOperations()); - if (method != null) return method; - } - Method method = getMethod(version, predicate, umlClass.getOperations()); - if (method != null) return method; + if (umlClass.getSourceFile().equals(filePath)) { + for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) { + Method method = getMethod(version, predicate, anonymousClass.getOperations()); + if (method != null) return method; + } + Method method = getMethod(version, predicate, umlClass.getOperations()); + if (method != null) return method; + } } return null; } @@ -100,19 +103,21 @@ private static Method getMethod(Version version, Predicate predicate, Li return null; } - protected static Attribute getAttribute(UMLModel umlModel, Version version, Predicate predicate) { + protected static Attribute getAttribute(UMLModel umlModel, Version version, String filePath, Predicate predicate) { if (umlModel != null) for (UMLClass umlClass : umlModel.getClassList()) { - for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) { - Attribute attribute = getAttribute(version, predicate, anonymousClass.getAttributes()); - if (attribute != null) return attribute; - attribute = getAttribute(version, predicate, anonymousClass.getEnumConstants()); - if (attribute != null) return attribute; - } - Attribute attribute = getAttribute(version, predicate, umlClass.getAttributes()); - if (attribute != null) return attribute; - attribute = getAttribute(version, predicate, umlClass.getEnumConstants()); - if (attribute != null) return attribute; + if (umlClass.getSourceFile().equals(filePath)) { + for (UMLAnonymousClass anonymousClass : umlClass.getAnonymousClassList()) { + Attribute attribute = getAttribute(version, predicate, anonymousClass.getAttributes()); + if (attribute != null) return attribute; + attribute = getAttribute(version, predicate, anonymousClass.getEnumConstants()); + if (attribute != null) return attribute; + } + Attribute attribute = getAttribute(version, predicate, umlClass.getAttributes()); + if (attribute != null) return attribute; + attribute = getAttribute(version, predicate, umlClass.getEnumConstants()); + if (attribute != null) return attribute; + } } return null; } @@ -126,13 +131,72 @@ private static Attribute getAttribute(Version version, Predicate pred return null; } - protected static Class getClass(UMLModel umlModel, Version version, Predicate predicate) { + protected static Class getClass(UMLModel umlModel, Version version, String filePath, Predicate predicate) { if (umlModel != null) for (UMLClass umlClass : umlModel.getClassList()) { - Class clazz = Class.of(umlClass, version); - if (predicate.test(clazz)) - return clazz; + if (umlClass.getSourceFile().equals(filePath)) { + Class clazz = Class.of(umlClass, version); + if (predicate.test(clazz)) + return clazz; + } } return null; } + + protected CodeElement locateWithName(Version version, UMLModel umlModel) throws CodeElementNotFoundException { + Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithName); + if (clazz != null) { + return clazz; + } + Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithName); + if (attribute != null) { + return attribute; + } + Method method = getMethod(umlModel, version, filePath, this::methodPredicateWithName); + if (method != null) { + return method; + } + else { + method = getMethod(umlModel, version, filePath, this::methodPredicateWithoutName); + if (method != null) { + Variable variable = method.findVariable(this::variablePredicate); + if (variable != null) { + return variable; + } + Block block = method.findBlock(this::blockPredicate); + if (block != null) { + return block; + } + } + } + throw new CodeElementNotFoundException(filePath, name, lineNumber); + } + + protected CodeElement locateWithoutName(Version version, UMLModel umlModel) throws CodeElementNotFoundException { + Method method = getMethod(umlModel, version, filePath, this::methodPredicateWithoutName); + if (method != null) { + Block block = method.findBlockWithoutName(this::blockPredicate); + if (block != null) { + return block; + } + Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName); + if (attribute != null) { + return attribute; + } + return method; + } + Attribute attribute = getAttribute(umlModel, version, filePath, this::attributePredicateWithoutName); + if (attribute != null) { + Block block = attribute.findBlockWithoutName(this::blockPredicate); + if (block != null) { + return block; + } + return attribute; + } + Class clazz = getClass(umlModel, version, filePath, this::classPredicateWithoutName); + if (clazz != null) { + return clazz; + } + throw new CodeElementNotFoundException(filePath, name, lineNumber); + } } \ No newline at end of file diff --git a/src/main/java/org/codetracker/util/CodeElementLocator.java b/src/main/java/org/codetracker/util/CodeElementLocator.java index 0717cc00ead..63b39286446 100644 --- a/src/main/java/org/codetracker/util/CodeElementLocator.java +++ b/src/main/java/org/codetracker/util/CodeElementLocator.java @@ -2,10 +2,7 @@ import gr.uom.java.xmi.UMLModel; import org.codetracker.api.CodeElement; -import org.codetracker.api.CodeElementNotFoundException; import org.codetracker.api.Version; -import org.codetracker.element.*; -import org.codetracker.element.Class; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; @@ -35,58 +32,13 @@ public CodeElement locate() throws Exception { } Version version = gitRepository.getVersion(commitId); UMLModel umlModel = getUMLModel(repository, commitId, Collections.singleton(filePath)); - Class clazz = getClass(umlModel, version, this::classPredicateWithName); - if (clazz != null) { - return clazz; - } - Attribute attribute = getAttribute(umlModel, version, this::attributePredicateWithName); - if (attribute != null) { - return attribute; - } - Method method = getMethod(umlModel, version, this::methodPredicateWithName); - if (method != null) { - return method; - } - else { - method = getMethod(umlModel, version, this::methodPredicateWithoutName); - if (method != null) { - Variable variable = method.findVariable(this::variablePredicate); - if (variable != null) { - return variable; - } - Block block = method.findBlock(this::blockPredicate); - if (block != null) { - return block; - } - } - } - throw new CodeElementNotFoundException(filePath, name, lineNumber); + return locateWithName(version, umlModel); } private CodeElement locateWithoutName() throws Exception { Version version = gitRepository.getVersion(commitId); UMLModel umlModel = getUMLModel(repository, commitId, Collections.singleton(filePath)); - Method method = getMethod(umlModel, version, this::methodPredicateWithoutName); - if (method != null) { - Block block = method.findBlockWithoutName(this::blockPredicate); - if (block != null) { - return block; - } - return method; - } - Attribute attribute = getAttribute(umlModel, version, this::attributePredicateWithoutName); - if (attribute != null) { - Block block = attribute.findBlockWithoutName(this::blockPredicate); - if (block != null) { - return block; - } - return attribute; - } - Class clazz = getClass(umlModel, version, this::classPredicateWithoutName); - if (clazz != null) { - return clazz; - } - throw new CodeElementNotFoundException(filePath, name, lineNumber); + return locateWithoutName(version, umlModel); } private static UMLModel getUMLModel(Repository repository, String commitId, Set fileNames) throws Exception { diff --git a/src/main/java/org/codetracker/util/CodeElementLocatorWithLocalFiles.java b/src/main/java/org/codetracker/util/CodeElementLocatorWithLocalFiles.java index 1a050e0eed8..72f64276c2d 100644 --- a/src/main/java/org/codetracker/util/CodeElementLocatorWithLocalFiles.java +++ b/src/main/java/org/codetracker/util/CodeElementLocatorWithLocalFiles.java @@ -7,15 +7,10 @@ import java.util.concurrent.ConcurrentHashMap; import gr.uom.java.xmi.UMLModel; + import org.codetracker.VersionImpl; import org.codetracker.api.CodeElement; -import org.codetracker.api.CodeElementNotFoundException; import org.codetracker.api.Version; -import org.codetracker.element.Attribute; -import org.codetracker.element.Block; -import org.codetracker.element.Class; -import org.codetracker.element.Method; -import org.codetracker.element.Variable; import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl; import org.refactoringminer.rm1.GitHistoryRefactoringMinerImpl.ChangedFileInfo; @@ -88,57 +83,12 @@ public CodeElement locate() throws Exception { } Version version = new VersionImpl(commitId, 0, 0, ""); UMLModel umlModel = currentUMLModel; - Class clazz = getClass(umlModel, version, this::classPredicateWithName); - if (clazz != null) { - return clazz; - } - Attribute attribute = getAttribute(umlModel, version, this::attributePredicateWithName); - if (attribute != null) { - return attribute; - } - Method method = getMethod(umlModel, version, this::methodPredicateWithName); - if (method != null) { - return method; - } - else { - method = getMethod(umlModel, version, this::methodPredicateWithoutName); - if (method != null) { - Variable variable = method.findVariable(this::variablePredicate); - if (variable != null) { - return variable; - } - Block block = method.findBlock(this::blockPredicate); - if (block != null) { - return block; - } - } - } - throw new CodeElementNotFoundException(filePath, name, lineNumber); + return locateWithName(version, umlModel); } private CodeElement locateWithoutName() throws Exception { Version version = new VersionImpl(commitId, 0, 0, ""); UMLModel umlModel = currentUMLModel; - Method method = getMethod(umlModel, version, this::methodPredicateWithoutName); - if (method != null) { - Block block = method.findBlockWithoutName(this::blockPredicate); - if (block != null) { - return block; - } - return method; - } - Attribute attribute = getAttribute(umlModel, version, this::attributePredicateWithoutName); - if (attribute != null) { - Block block = attribute.findBlockWithoutName(this::blockPredicate); - if (block != null) { - return block; - } - return attribute; - } - Class clazz = getClass(umlModel, version, this::classPredicateWithoutName); - if (clazz != null) { - return clazz; - } - throw new CodeElementNotFoundException(filePath, name, lineNumber); + return locateWithoutName(version, umlModel); } } diff --git a/src/test/java/org/codetracker/util/CodeElementLocatorTest.java b/src/test/java/org/codetracker/util/CodeElementLocatorTest.java index 90833676326..c8f605c584c 100644 --- a/src/test/java/org/codetracker/util/CodeElementLocatorTest.java +++ b/src/test/java/org/codetracker/util/CodeElementLocatorTest.java @@ -12,6 +12,8 @@ import org.refactoringminer.api.GitService; import org.refactoringminer.util.GitServiceImpl; +import gr.uom.java.xmi.LocationInfo.CodeElementType; + @Disabled public class CodeElementLocatorTest { private final static String FOLDER_TO_CLONE = "tmp/"; @@ -25,10 +27,11 @@ public void testMethodLocator() throws Exception { final int lineNumber = 384; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Method.class); + assertEquals(((Method)codeElement).getUmlOperation().getName(), name); } } @@ -121,10 +124,11 @@ public void testAttributeLocator() throws Exception { final int lineNumber = 93; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } } @@ -137,10 +141,11 @@ public void testClassLocator() throws Exception { final int lineNumber = 67; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), org.codetracker.element.Class.class); + assertEquals(((org.codetracker.element.Class)codeElement).getUmlClass().getNonQualifiedName(), name); } } @@ -153,10 +158,11 @@ public void testInnerEnumLocator() throws Exception { final int lineNumber = 58; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), org.codetracker.element.Class.class); + assertEquals(((org.codetracker.element.Class)codeElement).getUmlClass().getNonQualifiedName(), name); } } @@ -169,10 +175,11 @@ public void testInnerClassLocator() throws Exception { final int lineNumber = 568; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), org.codetracker.element.Class.class); + assertEquals(((org.codetracker.element.Class)codeElement).getUmlClass().getNonQualifiedName(), name); } } @@ -185,10 +192,11 @@ public void testInnerClassAttributeLocator() throws Exception { final int lineNumber = 586; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } } @@ -201,10 +209,11 @@ public void testInnerClassMethodLocator() throws Exception { final int lineNumber = 703; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Method.class); + assertEquals(((Method)codeElement).getUmlOperation().getName(), name); } } @@ -281,10 +290,11 @@ public void testInnerClassEnumConstantLocator() throws Exception { final int lineNumber = 63; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } } @@ -297,10 +307,11 @@ public void testAnonymousClassAttributeLocator() throws Exception { final int lineNumber = 104; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } } @@ -313,10 +324,11 @@ public void testAnonymousClassMethodLocator() throws Exception { final int lineNumber = 107; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Method.class); + assertEquals(((Method)codeElement).getUmlOperation().getName(), name); } } @@ -348,6 +360,7 @@ public void testStatementLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.EXPRESSION_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -357,31 +370,31 @@ public void testForLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "for"; final int lineNumber = 387; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.ENHANCED_FOR_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @Test - public void testNesterForLocator() throws Exception { + public void testNestedForLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "for"; final int lineNumber = 391; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.ENHANCED_FOR_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -391,14 +404,14 @@ public void testNestedIfLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "if"; final int lineNumber = 389; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -408,14 +421,14 @@ public void testIfLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "if"; final int lineNumber = 396; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -425,14 +438,14 @@ public void testIfLocator2() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Main.java"; final String commitId = "b1b49751d38af0bf2476aea1f4595283615ab7de"; - final String name = "if"; final int lineNumber = 231; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -442,14 +455,14 @@ public void testTryLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "try"; final int lineNumber = 317; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.TRY_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -459,14 +472,14 @@ public void testIfInsideCatchLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "if"; final int lineNumber = 331; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -476,14 +489,14 @@ public void testForInsideTryLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "for"; final int lineNumber = 319; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.ENHANCED_FOR_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -493,14 +506,14 @@ public void testElseIfLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "if"; final int lineNumber = 471; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } @@ -510,14 +523,14 @@ public void testIfInsideAnonymousLocator() throws Exception { GitService gitService = new GitServiceImpl(); final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/gui/TreeTable.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; - final String name = "if"; final int lineNumber = 119; try (Repository repository = gitService.cloneIfNotExists(FOLDER_TO_CLONE + "checkstyle\\checkstyle", "https://github.com/checkstyle/checkstyle.git")){ - CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, name, lineNumber); + CodeElementLocator locator = new CodeElementLocator(repository, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } } diff --git a/src/test/java/org/codetracker/util/CodeElementLocatorWithLocalFilesTest.java b/src/test/java/org/codetracker/util/CodeElementLocatorWithLocalFilesTest.java index ab796fbd9ba..085534eec37 100644 --- a/src/test/java/org/codetracker/util/CodeElementLocatorWithLocalFilesTest.java +++ b/src/test/java/org/codetracker/util/CodeElementLocatorWithLocalFilesTest.java @@ -8,6 +8,8 @@ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; +import gr.uom.java.xmi.LocationInfo.CodeElementType; + public class CodeElementLocatorWithLocalFilesTest { @Test @@ -17,10 +19,11 @@ public void testMethodLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "fireErrors"; final int lineNumber = 384; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Method.class); + assertEquals(((Method)codeElement).getUmlOperation().getName(), name); } @Test @@ -95,10 +98,11 @@ public void testAttributeLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "classLoader"; final int lineNumber = 93; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } @Test @@ -108,10 +112,11 @@ public void testClassLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "Checker"; final int lineNumber = 67; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), org.codetracker.element.Class.class); + assertEquals(((org.codetracker.element.Class)codeElement).getUmlClass().getNonQualifiedName(), name); } @Test @@ -121,10 +126,11 @@ public void testInnerEnumLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "IgnoredModulesOptions"; final int lineNumber = 58; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), org.codetracker.element.Class.class); + assertEquals(((org.codetracker.element.Class)codeElement).getUmlClass().getNonQualifiedName(), name); } @Test @@ -134,10 +140,11 @@ public void testInnerClassLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "InternalLoader"; final int lineNumber = 568; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), org.codetracker.element.Class.class); + assertEquals(((org.codetracker.element.Class)codeElement).getUmlClass().getNonQualifiedName(), name); } @Test @@ -147,10 +154,11 @@ public void testInnerClassAttributeLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "METADATA"; final int lineNumber = 586; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } @Test @@ -160,10 +168,11 @@ public void testInnerClassMethodLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "containsAttribute"; final int lineNumber = 703; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Method.class); + assertEquals(((Method)codeElement).getUmlOperation().getName(), name); } @Test @@ -225,10 +234,11 @@ public void testInnerClassEnumConstantLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "OMIT"; final int lineNumber = 63; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } @Test @@ -238,10 +248,11 @@ public void testAnonymousClassAttributeLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "serialVersionUID"; final int lineNumber = 104; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Attribute.class); + assertEquals(((Attribute)codeElement).getUmlAttribute().getName(), name); } @Test @@ -251,10 +262,11 @@ public void testAnonymousClassMethodLocator() throws Exception { final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; final String name = "actionPerformed"; final int lineNumber = 107; - CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, name, lineNumber); + CodeElementLocatorWithLocalFiles locator = new CodeElementLocatorWithLocalFiles(cloneURL, commitId, filePath, lineNumber); CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Method.class); + assertEquals(((Method)codeElement).getUmlOperation().getName(), name); } @Test @@ -280,6 +292,7 @@ public void testStatementLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.EXPRESSION_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -293,11 +306,12 @@ public void testForLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.ENHANCED_FOR_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @Test - public void testNesterForLocator() throws Exception { + public void testNestedForLocator() throws Exception { final String cloneURL = "https://github.com/checkstyle/checkstyle.git"; final String filePath = "src/main/java/com/puppycrawl/tools/checkstyle/Checker.java"; final String commitId = "119fd4fb33bef9f5c66fc950396669af842c21a3"; @@ -306,6 +320,7 @@ public void testNesterForLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.ENHANCED_FOR_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -319,6 +334,7 @@ public void testNestedIfLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -332,6 +348,7 @@ public void testIfLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -345,6 +362,7 @@ public void testIfLocator2() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -358,6 +376,7 @@ public void testTryLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.TRY_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -371,6 +390,7 @@ public void testIfInsideCatchLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -384,6 +404,7 @@ public void testForInsideTryLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.ENHANCED_FOR_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -397,6 +418,7 @@ public void testElseIfLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } @@ -410,6 +432,7 @@ public void testIfInsideAnonymousLocator() throws Exception { CodeElement codeElement = locator.locate(); assertNotNull(codeElement); assertEquals(codeElement.getClass(), Block.class); + assertEquals(codeElement.getLocation().getCodeElementType(), CodeElementType.IF_STATEMENT); assertEquals(codeElement.getLocation().getStartLine(), lineNumber); } }