Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
[FIXED JENKINS-19047] Also use column position when comparing warnings.
  • Loading branch information
uhafner committed Aug 6, 2013
1 parent 4a15aae commit 8f00f2572e411591da0fc9e1f7b093543caaa253
@@ -3,3 +3,4 @@ target
analysis-core.iws
analysis-core.iml
analysis-core.ipr
/.DS_Store
@@ -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.
*
@@ -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
}
@@ -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.