Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[FIXED JENKINS-21569] Expose variable lineNumber in groovy.
  • Loading branch information
uhafner committed Jan 31, 2014
1 parent fb9924a commit c2fcaec495f69bb8a90b0edc83b03a22635bb30a
@@ -41,9 +41,7 @@ public DynamicParser(final String name, final String regexp, final String script
*/
@Override
protected Warning createWarning(final Matcher matcher) {
expressionMatcher.setCurrentLine(getCurrentLine());

return expressionMatcher.createWarning(matcher);
return expressionMatcher.createWarning(matcher, getCurrentLine());
}
}

@@ -23,7 +23,6 @@ public class GroovyExpressionMatcher implements Serializable {

private final Warning falsePositive;
private final String script;
private int currentLine;

private transient Script compiled;

@@ -55,36 +54,21 @@ private void compileScriptIfNotYetDone() {
}
}

/**
* Returns the current line number that is handled by the parser.
*
* @return the current line number
*/
public int getCurrentLine() {
return currentLine;
}

/**
* Sets the current line number to the specified value.
*
* @param currentLine the new line number
*/
void setCurrentLine(int currentLine) {
this.currentLine = currentLine;
}

/**
* Creates a new annotation for the specified match.
*
* @param matcher
* the regular expression matcher
* @param lineNumber
* the current line number
* @return a new annotation for the specified pattern
*/
public Warning createWarning(final Matcher matcher) {
public Warning createWarning(Matcher matcher, int lineNumber) {
compileScriptIfNotYetDone();

Binding binding = new Binding();
binding.setVariable("matcher", matcher);
binding.setVariable("lineNumber", lineNumber);
Object result = null;
try {
compiled.setBinding(binding);
@@ -99,6 +83,17 @@ public Warning createWarning(final Matcher matcher) {
return falsePositive;
}

/**
* Creates a new annotation for the specified match.
*
* @param matcher
* the regular expression matcher
* @return a new annotation for the specified pattern
*/
public Warning createWarning(final Matcher matcher) {
return createWarning(matcher, 0);
}

private static final Logger LOGGER = Logger.getLogger(GroovyExpressionMatcher.class.getName());
}

@@ -63,6 +63,32 @@ public void issue12280() throws IOException {
assertEquals(WRONG_NUMBER_OF_WARNINGS_DETECTED, 9, warnings.size());
}

/**
* Parses a file with 9 warnings of a custom parser. Should show all line numbers correctly.
*
* @throws IOException
* if the file could not be read
* @see <a href="http://issues.jenkins-ci.org/browse/JENKINS-21569">Issue 21569</a>
*/
@Test
public void issue21569() throws IOException {
Collection<FileAnnotation> warnings = new DynamicParser("issue12280",
"^.*: XmlDoc warning (\\w+): (.* type ([^\\s]+)\\..*)$",
"import hudson.plugins.warnings.parser.Warning\n"
+ " String fileName = matcher.group(3)\n"
+ " String category = matcher.group(1)\n"
+ " String message = matcher.group(2)\n"
+ " return new Warning(fileName, lineNumber, \"Xml Doc\", category, message);", TYPE, TYPE)
.parse(openFile("issue12280.txt"));

assertEquals(WRONG_NUMBER_OF_WARNINGS_DETECTED, 9, warnings.size());

int lineNumber = 2;
for (FileAnnotation warning : warnings) {
assertEquals("Wrong line number parsed", lineNumber++, warning.getPrimaryLineNumber());
}
}

/**
* Parses a file with several warnings from a custom parser.
*

0 comments on commit c2fcaec

Please sign in to comment.