Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
[FIXED JENKINS-19047] Also use column position when comparing warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Aug 6, 2013
1 parent 4a15aae commit 8f00f25
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ target
analysis-core.iws
analysis-core.iml
analysis-core.ipr
/.DS_Store
Expand Up @@ -284,6 +284,26 @@ public String getOrigin() {
return StringUtils.defaultString(origin);
}

/**
* Returns the end column of the position of this warning.
*
* @return the end column
* @since 1.51
*/
public int getColumnEnd() {
return primaryColumnEnd;
}

/**
* Returns the start column of the position of this warning.
*
* @return the start column
* @since 1.51
*/
public int getColumnStart() {
return primaryColumnStart;
}

/**
* Sets the origin of this annotation to the specified value.
*
Expand Down Expand Up @@ -558,6 +578,13 @@ public int compareTo(final FileAnnotation other) {
if (result != 0) {
return result;
}
if (other instanceof AbstractAnnotation) {
AbstractAnnotation otherWarning = (AbstractAnnotation)other;
result = getColumnStart() - otherWarning.getColumnStart();
if (result != 0) {
return result;
}
}

return hashCode() - other.hashCode(); // fallback
}
Expand Down
@@ -0,0 +1,75 @@
package hudson.plugins.analysis.util.model;

import static org.junit.Assert.*;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import com.google.common.collect.Lists;

/**
* Tests the class {@link AbstractAnnotation}.
*
* @author Ulli Hafner
*/
public class AbstractAnnotationTest {
/**
* Verifies that the comparator sorts by filename, then linenumber, then column.
*/
@Test
public void testCompareTo() {
List<AbstractAnnotation> warnings = Lists.newArrayList();
warnings.add(createWarning(6, "cccc", 2, 2));
warnings.add(createWarning(4, "cccc", 2, 0));
warnings.add(createWarning(0, "aaaa", 0, 0));
warnings.add(createWarning(3, "cccc", 1, 0));
warnings.add(createWarning(2, "cccc", 0, 0));
warnings.add(createWarning(8, "cccc", 3, 1));
warnings.add(createWarning(5, "cccc", 2, 1));
warnings.add(createWarning(1, "bbbb", 0, 0));
warnings.add(createWarning(7, "cccc", 2, 3));

Collections.sort(warnings);
verifyOrder(warnings, true);

Collections.reverse(warnings);
verifyOrder(warnings, false);
}

private void verifyOrder(final List<AbstractAnnotation> warnings, final boolean isAscending) {
int position = 0;
for (AbstractAnnotation warning : warnings) {
int actualPosition = isAscending ? position : warnings.size() - position - 1;
assertEquals("Wrong position: ", String.valueOf(actualPosition), warning.getMessage());

position++;
}
}

private Warning createWarning(final int id, final String fileName, final int line, final int column) {
Warning warning = new Warning(Priority.HIGH, String.valueOf(id), line, line, "category", "type");
warning.setFileName(fileName);
warning.setColumnPosition(column, column);
return warning;
}

/**
* A concrete warning that will be sorted.
*/
private static class Warning extends AbstractAnnotation {
private static final long serialVersionUID = -8082297852537003624L;

Warning(final Priority priority, final String message, final int start, final int end, final String category, final String type) {
super(priority, message, start, end, category, type);
}

/** {@inheritDoc} */
public String getToolTip() {
return StringUtils.EMPTY;
}
}
}

0 comments on commit 8f00f25

Please sign in to comment.