From a1a700fc6eb8793d7bfa08a31a4529eb2e49b84c Mon Sep 17 00:00:00 2001 From: Alexander Kriegisch Date: Sun, 29 Jan 2023 14:57:58 +0100 Subject: Implement source location matching for weave messages in XML tests WIP (work in progress). Closes #218. Signed-off-by: Alexander Kriegisch --- .../compiler/lookup/EclipseSourceLocation.java | 8 +++- .../java/org/aspectj/tools/ajc/AjcTestCase.java | 52 +++++++++++++++------- 2 files changed, 42 insertions(+), 18 deletions(-) (limited to 'org.aspectj.ajdt.core') diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceLocation.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceLocation.java index cbd21e585..274bf1e90 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceLocation.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/compiler/lookup/EclipseSourceLocation.java @@ -108,8 +108,12 @@ public class EclipseSourceLocation implements ISourceLocation { public String getContext() { if (null == context) { - ICompilationUnit compilationUnit = result.compilationUnit; - IProblem[] problems = result.problems; + ICompilationUnit compilationUnit = null; + IProblem[] problems = null; + if (result != null) { + compilationUnit = result.compilationUnit; + problems = result.problems; + } if ((null == compilationUnit) || (null == problems) || (1 != problems.length)) { // ?? which of n>1 problems? context = NO_CONTEXT; diff --git a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java index bc934815c..0a8f0fcfc 100644 --- a/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java +++ b/org.aspectj.ajdt.core/src/test/java/org/aspectj/tools/ajc/AjcTestCase.java @@ -33,6 +33,7 @@ import java.util.stream.Collectors; import org.aspectj.bridge.IMessage; import org.aspectj.bridge.ISourceLocation; +import org.aspectj.bridge.WeaveMessage; import org.aspectj.testing.util.TestUtil; import org.aspectj.util.LangUtil; @@ -134,8 +135,10 @@ public abstract class AjcTestCase extends TestCase { */ public static class Message { private int line = -1; + private int aspectLine = -1; private String text; private String sourceFileName; + private String aspectFileName; private ISourceLocation[] seeAlsos; public boolean careAboutOtherMessages = true; @@ -172,19 +175,28 @@ public abstract class AjcTestCase extends TestCase { *

*/ public Message(int line, String srcFile, String text, ISourceLocation[] seeAlso) { + this(line, srcFile, -1, null, text, seeAlso); + } + + public Message(int line, String srcFile, int aspectLine, String aspectFile, String text, ISourceLocation[] seeAlso) { this.line = line; StringBuilder srcFileName = new StringBuilder(); if (srcFile != null) { char[] chars = srcFile.toCharArray(); for (char c : chars) { - if ((c == '\\') || (c == '/')) { - srcFileName.append(separator); - } else { - srcFileName.append(c); - } + srcFileName.append((c == '\\' || c == '/') ? separator : c); } this.sourceFileName = srcFileName.toString(); } + this.aspectLine = aspectLine; + StringBuilder aspectFileName = new StringBuilder(); + if (aspectFile != null) { + char[] chars = aspectFile.toCharArray(); + for (char c : chars) { + aspectFileName.append((c == '\\' || c == '/') ? separator : c); + } + this.aspectFileName = aspectFileName.toString(); + } this.text = text; if (this.text != null && text.startsWith("*")) { // Don't care what other messages are around @@ -211,33 +223,41 @@ public abstract class AjcTestCase extends TestCase { */ public boolean matches(IMessage message) { ISourceLocation loc = message.getSourceLocation(); - if ((loc == null) && ((line != -1) || (sourceFileName != null))) { + if ((loc == null) && ((line != -1) || (sourceFileName != null))) return false; - } if (line != -1) { - if (loc.getLine() != line) { + if (loc.getLine() != line) return false; - } } if (sourceFileName != null) { - if (!loc.getSourceFile().getPath().endsWith(sourceFileName)) { + if (!loc.getSourceFile().getPath().endsWith(sourceFileName)) return false; + } + if (message instanceof WeaveMessage) { + List extraLocations = message.getExtraSourceLocations(); + loc = extraLocations.size() > 0 ? extraLocations.get(0) : null; + if ((loc == null) && ((aspectLine != -1) || (aspectFileName != null))) + return false; + if (aspectLine != -1) { + if (loc.getLine() != aspectLine) + return false; + } + if (aspectFileName != null) { + if (!loc.getSourceFile().getPath().endsWith(aspectFileName)) + return false; } } if (text != null) { - if (!message.getMessage().contains(text)) { + if (!message.getMessage().contains(text)) return false; - } } if (seeAlsos != null) { List extraLocations = message.getExtraSourceLocations(); - if (extraLocations.size() != seeAlsos.length) { + if (extraLocations.size() != seeAlsos.length) return false; - } for (ISourceLocation seeAlso : seeAlsos) { - if (!hasAMatch(extraLocations, seeAlso)) { + if (!hasAMatch(extraLocations, seeAlso)) return false; - } } } return true; -- cgit v1.2.3