*/
package org.sonar.batch.index;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.sonar.api.batch.SonarIndex;
import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.design.Dependency;
-import org.sonar.api.measures.*;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.MeasuresFilter;
+import org.sonar.api.measures.MeasuresFilters;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.MetricFinder;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.ProjectLink;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.ResourceUtils;
+import org.sonar.api.resources.Scopes;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;
import org.sonar.batch.ResourceFilters;
import org.sonar.batch.ViolationFilters;
-import java.util.*;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
public class DefaultIndex extends SonarIndex {
return Collections.emptyList();
}
List<Violation> filteredViolations = Lists.newArrayList();
- boolean ignoreSwitchedOff = violationQuery.ignoreSwitchedOff();
+ boolean isSwitchedOff = violationQuery.isSwitchedOff();
for (Violation violation : bucket.getViolations()) {
- if ( ignoreSwitchedOff && violation.isSwitchedOff()) {
- continue;
+ if ( violation.isSwitchedOff() == isSwitchedOff) {
+ filteredViolations.add(violation);
}
- filteredViolations.add(violation);
}
return filteredViolations;
}
violation3.setSwitchedOff(true);
index.addViolation(violation3);
- assertThat(index.getViolations(ViolationQuery.create().forResource(file).ignoreSwitchedOff(false)).size(), is(3));
+ assertThat(index.getViolations(ViolationQuery.create().forResource(file).setSwitchedOff(true)).size(), is(2));
}
@Test(expected = IllegalArgumentException.class)
* @return the list of violations
*/
public final List<Violation> getViolations(Resource resource) {
- return getViolations(ViolationQuery.create().forResource(resource).ignoreSwitchedOff(true));
+ return getViolations(ViolationQuery.create().forResource(resource));
}
/**
*/
public final class ViolationQuery {
- private boolean ignoreSwitchedOff;
+ private boolean isSwitchedOff;
private Resource resource;
/**
* Specifies if the query should returned switched-off violations or not.
*
* @param ignore
- * if true, the query will return only active violations.
+ * if true, the query will return only switched-off violations. if false, it will return only active violations.
* @return the current violation query
*/
- public ViolationQuery ignoreSwitchedOff(boolean ignore) {
- this.ignoreSwitchedOff = ignore;
+ public ViolationQuery setSwitchedOff(boolean ignore) {
+ this.isSwitchedOff = ignore;
return this;
}
/**
- * Tells if the query should returned switched-off violations or not.
+ * Tells if the query should returned switched-off violations or active violations.
*
* @return
*/
- public boolean ignoreSwitchedOff() {
- return ignoreSwitchedOff;
+ public boolean isSwitchedOff() {
+ return isSwitchedOff;
}
/**
json['message'] = message
json['line'] = line if line
json['priority'] = Sonar::RulePriority.to_s(failure_level).upcase
+ json['switchedOff'] = switched_off ? true : false
if created_at
json['createdAt'] = format_datetime(created_at)
end
xml.message(message)
xml.line(line) if line
xml.priority(Sonar::RulePriority.to_s(failure_level))
+ xml.switchedOff(switched_off ? true : false)
if created_at
xml.createdAt(format_datetime(created_at))
end
private String resourceScope = null;
private String resourceQualifier = null;
private Date createdAt = null;
+ private boolean switchedOff;
public String getMessage() {
return message;
* @since 2.5
*/
public boolean isCreatedAfter(Date date) {
- return createdAt!=null && date!=null && createdAt.after(date);
+ return createdAt != null && date != null && createdAt.after(date);
}
+
+ /**
+ * @since 2.8
+ */
+ public Violation setSwitchedOff(boolean switchedOff) {
+ this.switchedOff = switchedOff;
+ return this;
+ }
+
+ /**
+ * @since 2.8
+ */
+ public boolean isSwitchedOff() {
+ return switchedOff;
+ }
+
}
package org.sonar.wsclient.services;
public class ViolationQuery extends Query<Violation> {
+
public static final String BASE_URL = "/api/violations";
private String resourceKeyOrId;
private String[] categories;
private String[] severities;
private Integer limit;
+ private boolean isSwitchedOff;
public ViolationQuery(String resourceKeyOrId) {
this.resourceKeyOrId = resourceKeyOrId;
return this;
}
+ /**
+ * @since 2.8
+ */
+ public ViolationQuery setSwitchedOff(boolean ignore) {
+ this.isSwitchedOff = ignore;
+ return this;
+ }
+
+ /**
+ * @since 2.8
+ */
+ public boolean isSwitchedOff() {
+ return isSwitchedOff;
+ }
+
@Override
public String getUrl() {
StringBuilder url = new StringBuilder(BASE_URL);
appendUrlParameter(url, "rules", ruleKeys);
appendUrlParameter(url, "categories", categories);
appendUrlParameter(url, "priorities", severities);
+ appendUrlParameter(url, "switched_off", isSwitchedOff);
+
return url.toString();
}
violation.setLine(utils.getInteger(json, "line"));
violation.setSeverity(utils.getString(json, "priority"));
violation.setCreatedAt(utils.getDateTime(json, "createdAt"));
+ violation.setSwitchedOff(utils.getBoolean(json, "switchedOff"));
Object rule = utils.getField(json, "rule");
if (rule != null) {
*/
package org.sonar.wsclient.unmarshallers;
-import org.junit.Test;
-import org.sonar.wsclient.services.Violation;
-
-import java.util.List;
-
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
+import java.util.List;
+
+import org.junit.Test;
+import org.sonar.wsclient.services.Violation;
+
public class ViolationUnmarshallerTest extends UnmarshallerTestCase {
@Test
assertThat(violation.getResourceName(), is("TraceableResourceLimitingPool"));
assertThat(violation.getResourceQualifier(), is("CLA"));
assertThat(violation.getResourceScope(), is("FIL"));
+ assertThat(violation.isSwitchedOff(), is(true));
}
@Test
[
- {"message":"throw java.lang.Exception","priority":"MAJOR","rule":{"key":"pmd:SignatureDeclareThrowsException","name":"Signature Declare Throws Exception","category":"Maintainability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}},
+ {"message":"throw java.lang.Exception","priority":"MAJOR","switchedOff":true,"rule":{"key":"pmd:SignatureDeclareThrowsException","name":"Signature Declare Throws Exception","category":"Maintainability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}},
]
\ No newline at end of file
[
- {"message":"throw java.lang.Exception","line":97,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","rule":{"key":"pmd:SignatureDeclareThrowsException","name":"Signature Declare Throws Exception","category":"Maintainability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}},
- {"message":"The user-supplied array 'threads' is stored directly.","line":242,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","rule":{"key":"pmd:ArrayIsStoredDirectly","name":"Security - Array is stored directly","category":"Reliability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}}
+ {"message":"throw java.lang.Exception","line":97,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","switchedOff":true,"rule":{"key":"pmd:SignatureDeclareThrowsException","name":"Signature Declare Throws Exception","category":"Maintainability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}},
+ {"message":"The user-supplied array 'threads' is stored directly.","line":242,"createdAt":"2010-12-01T13:55:22+0300","priority":"MAJOR","switchedOff":false,"rule":{"key":"pmd:ArrayIsStoredDirectly","name":"Security - Array is stored directly","category":"Reliability"},"resource":{"key":"org.apache.excalibur.components:excalibur-pool-instrumented:org.apache.avalon.excalibur.pool.TraceableResourceLimitingPool","name":"TraceableResourceLimitingPool","scope":"FIL","qualifier":"CLA","language":"java"}}
]
\ No newline at end of file