aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-deprecated
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2014-10-03 08:40:53 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2014-10-03 08:40:53 +0200
commitab3a0bc0746a03e3e52860c3e8571073aa06bce5 (patch)
tree33a79b7c6f40d5e0e1feb2beca35bb7391fce2fc /sonar-deprecated
parentddb4d8c4adcbf86be6b2c70e685893963e9cddbb (diff)
parentee925d0a8d31556a37f98b6738e6f767b489e288 (diff)
downloadsonarqube-ab3a0bc0746a03e3e52860c3e8571073aa06bce5.tar.gz
sonarqube-ab3a0bc0746a03e3e52860c3e8571073aa06bce5.zip
Merge remote-tracking branch 'remotes/origin/branch-4.5'
Conflicts: plugins/sonar-xoo-plugin/pom.xml sonar-plugin-api/pom.xml sonar-plugin-api/src/main/java/org/sonar/api/config/Settings.java
Diffstat (limited to 'sonar-deprecated')
-rw-r--r--sonar-deprecated/pom.xml5
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/charts/AbstractChart.java99
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/charts/Chart.java43
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/charts/ChartParameters.java186
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/charts/package-info.java26
-rw-r--r--sonar-deprecated/src/main/java/org/sonar/api/checks/NoSonarFilter.java67
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/charts/ChartParametersTest.java79
-rw-r--r--sonar-deprecated/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java80
8 files changed, 585 insertions, 0 deletions
diff --git a/sonar-deprecated/pom.xml b/sonar-deprecated/pom.xml
index a394e6983c3..9a80e797996 100644
--- a/sonar-deprecated/pom.xml
+++ b/sonar-deprecated/pom.xml
@@ -15,6 +15,10 @@
<artifactId>sonar-plugin-api</artifactId>
</dependency>
<dependency>
+ <groupId>jfree</groupId>
+ <artifactId>jfreechart</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
</dependency>
@@ -24,6 +28,7 @@
<scope>provided</scope>
</dependency>
+
<!-- unit test -->
<dependency>
<groupId>junit</groupId>
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/charts/AbstractChart.java b/sonar-deprecated/src/main/java/org/sonar/api/charts/AbstractChart.java
new file mode 100644
index 00000000000..016ab133322
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/charts/AbstractChart.java
@@ -0,0 +1,99 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.charts;
+
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.CategoryPlot;
+import org.jfree.chart.plot.Plot;
+import org.jfree.chart.renderer.AbstractRenderer;
+import org.jfree.chart.title.TextTitle;
+import org.jfree.data.Values2D;
+
+import java.awt.Color;
+import java.awt.image.BufferedImage;
+
+/**
+ * Base implementation to generate charts with JFreechart
+ *
+ * @since 1.10
+ * @deprecated in 4.5.1, replaced by Javascript charts
+ */
+@Deprecated
+public abstract class AbstractChart implements Chart {
+
+ public static final int FONT_SIZE = 13;
+ public static final Color OUTLINE_COLOR = new Color(51, 51, 51);
+ public static final Color GRID_COLOR = new Color(204, 204, 204);
+ public static final Color[] COLORS = new Color[] { Color.decode("#4192D9"), Color.decode("#800000"), Color.decode("#A7B307"),
+ Color.decode("#913C9F"), Color.decode("#329F4D") };
+
+ protected abstract Plot getPlot(ChartParameters params);
+
+ protected boolean hasLegend() {
+ return false;
+ }
+
+ /**
+ * Generates a JFreeChart chart using a set of parameters
+ *
+ * @param params the chart parameters
+ * @return the generated chart
+ */
+ public BufferedImage generateImage(ChartParameters params) {
+ JFreeChart chart = new JFreeChart(null, TextTitle.DEFAULT_FONT, getPlot(params), hasLegend());
+ improveChart(chart, params);
+ return chart.createBufferedImage(params.getWidth(), params.getHeight());
+ }
+
+ private void improveChart(JFreeChart jfrechart, ChartParameters params) {
+ Color background = Color.decode("#" + params.getValue(ChartParameters.PARAM_BACKGROUND_COLOR, "FFFFFF", false));
+ jfrechart.setBackgroundPaint(background);
+
+ jfrechart.setBorderVisible(false);
+ jfrechart.setAntiAlias(true);
+ jfrechart.setTextAntiAlias(true);
+ jfrechart.removeLegend();
+ }
+
+ @Override
+ public String toString() {
+ return getKey();
+ }
+
+ /**
+ * Helper to set color of series. If the parameter colorsHex is null, then default Sonar colors are used.
+ */
+ protected void configureColors(Values2D dataset, CategoryPlot plot, String[] colorsHex) {
+ Color[] colors = COLORS;
+ if (colorsHex != null && colorsHex.length > 0) {
+ colors = new Color[colorsHex.length];
+ for (int i = 0; i < colorsHex.length; i++) {
+ colors[i] = Color.decode("#" + colorsHex[i]);
+ }
+ }
+
+ dataset.getColumnCount();
+ AbstractRenderer renderer = (AbstractRenderer) plot.getRenderer();
+ for (int i = 0; i < dataset.getColumnCount(); i++) {
+ renderer.setSeriesPaint(i, colors[i % colors.length]);
+
+ }
+ }
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/charts/Chart.java b/sonar-deprecated/src/main/java/org/sonar/api/charts/Chart.java
new file mode 100644
index 00000000000..f9404bd835d
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/charts/Chart.java
@@ -0,0 +1,43 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.charts;
+
+import org.sonar.api.ServerExtension;
+
+import java.awt.image.BufferedImage;
+
+/**
+ * Extension point to generate charts
+ *
+ * @since 1.10
+ * @deprecated in 4.5.1, replaced by Javascript charts
+ */
+@Deprecated
+public interface Chart extends ServerExtension {
+ String getKey();
+
+ /**
+ * The method to implement to generate the chart
+ *
+ * @param params the chart parameters
+ * @return the image generated
+ */
+ BufferedImage generateImage(ChartParameters params);
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/charts/ChartParameters.java b/sonar-deprecated/src/main/java/org/sonar/api/charts/ChartParameters.java
new file mode 100644
index 00000000000..dbd6cc9addc
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/charts/ChartParameters.java
@@ -0,0 +1,186 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.charts;
+
+import org.apache.commons.lang.CharEncoding;
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.text.StrTokenizer;
+import org.sonar.api.utils.SonarException;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * The class to hold parameters to configure a chart
+ * @since 1.10
+ * @deprecated in 4.5.1, replaced by Javascript charts
+ */
+@Deprecated
+public class ChartParameters {
+ private static final String[] EMPTY = new String[0];
+
+ public static final String PARAM_WIDTH = "w";
+ public static final String PARAM_BACKGROUND_COLOR = "bgc";
+ public static final String PARAM_HEIGHT = "h";
+ public static final int MAX_WIDTH = 900;
+ public static final String PARAM_LOCALE = "locale";
+
+ public static final int MAX_HEIGHT = 900;
+ public static final int DEFAULT_WIDTH = 200;
+
+ public static final int DEFAULT_HEIGHT = 200;
+
+
+ private final Map<String, String> params;
+
+ /**
+ * Creates a ChartParameter based on a list of parameters
+ * @param params the list of parameters
+ */
+ public ChartParameters(Map<String, String> params) {
+ this.params = params;
+ }
+
+ /**
+ * Creates a Chartparameter based on a query string with a format key1=value1&key2=value2...
+ *
+ * @param queryString string
+ */
+ public ChartParameters(String queryString) {
+ this.params = new HashMap<String, String>();
+ String[] groups = StringUtils.split(queryString, "&");
+ for (String group : groups) {
+ String[] keyval = StringUtils.split(group, "=");
+ params.put(keyval[0], keyval[1]);
+ }
+ }
+
+ /**
+ * Shortcut to getValue with no decoding and no default value
+ * @param key the param ket
+ * @return the value of the param
+ */
+ public String getValue(String key) {
+ return getValue(key, "", false);
+ }
+
+ /**
+ * Returns the [decoded or not] value of a param from its key or the default value
+ * if id does not exist
+ *
+ * @param key the param ket
+ * @param defaultValue the default value if not exist
+ * @param decode whther the value should be decoded
+ * @return the value of the param
+ */
+
+ public String getValue(String key, String defaultValue, boolean decode) {
+ String val = params.get(key);
+ if (decode) {
+ val = decode(val);
+ }
+ if (val == null) {
+ val = defaultValue;
+ }
+ return val;
+ }
+
+ /**
+ * Returns an array of a param values, given its key and the values delimiter
+ *
+ * @param key the param key
+ * @param delimiter the values delimiter
+ * @return the list of vaalues
+ */
+ public String[] getValues(String key, String delimiter) {
+ String value = params.get(key);
+ if (value != null) {
+ return StringUtils.split(value, delimiter);
+ }
+ return EMPTY;
+ }
+
+ /**
+ * Returns an array of a param values, given its key and the values delimiter
+ * Values can be decoded or not
+ *
+ * @param key the param key
+ * @param delimiter the values delimiter
+ * @param decode whether to decode values
+ * @return the list of vaalues
+ */
+ public String[] getValues(String key, String delimiter, boolean decode) {
+ String value = params.get(key);
+ if (value != null) {
+ if (decode) {
+ value = decode(value);
+ }
+ return new StrTokenizer(value, delimiter).setIgnoreEmptyTokens(false).getTokenArray();
+ }
+ return EMPTY;
+ }
+
+ /**
+ * Get the chart width
+ *
+ * @return width
+ */
+ public int getWidth() {
+ int width = Integer.parseInt(getValue(PARAM_WIDTH, "" + DEFAULT_WIDTH, false));
+ return Math.min(width, MAX_WIDTH);
+ }
+
+ /**
+ * Get the chart height
+ *
+ * @return height
+ */
+ public int getHeight() {
+ int height = Integer.parseInt(getValue(PARAM_HEIGHT, "" + DEFAULT_HEIGHT, false));
+ return Math.min(height, MAX_HEIGHT);
+ }
+
+ /**
+ * Get the Locale
+ *
+ * @return Locale
+ */
+ public Locale getLocale() {
+ String locale = getValue(PARAM_LOCALE);
+ if (StringUtils.isNotBlank(locale)) {
+ return new Locale(locale);
+ }
+ return Locale.ENGLISH;
+ }
+
+ private String decode(String val) {
+ if (val != null) {
+ try {
+ val = URLDecoder.decode(val, CharEncoding.UTF_8);
+ } catch (UnsupportedEncodingException e) {
+ throw new SonarException("Decoding chart parameter : " + val, e);
+ }
+ }
+ return val;
+ }
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/charts/package-info.java b/sonar-deprecated/src/main/java/org/sonar/api/charts/package-info.java
new file mode 100644
index 00000000000..e5922bf75f0
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/charts/package-info.java
@@ -0,0 +1,26 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+/**
+ * Deprecated in 4.5.1. JFreechart charts are replaced by Javascript charts.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.api.charts;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/checks/NoSonarFilter.java b/sonar-deprecated/src/main/java/org/sonar/api/checks/NoSonarFilter.java
new file mode 100644
index 00000000000..05c4bd9bfaf
--- /dev/null
+++ b/sonar-deprecated/src/main/java/org/sonar/api/checks/NoSonarFilter.java
@@ -0,0 +1,67 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.rules.Violation;
+import org.sonar.api.rules.ViolationFilter;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @since 2.1
+ * @deprecated in 3.6. Replaced by {@link org.sonar.api.issue.NoSonarFilter}
+ */
+@Deprecated
+public class NoSonarFilter implements ViolationFilter {
+
+ private final Map<Resource, Set<Integer>> noSonarLinesByResource = Maps.newHashMap();
+ private SensorContext context;
+
+ public NoSonarFilter(SensorContext context) {
+ this.context = context;
+ }
+
+ public void addResource(Resource model, Set<Integer> noSonarLines) {
+ if (model != null && noSonarLines != null) {
+ // Reload resource to handle backward compatibility of resource keys
+ Resource resource = context.getResource(model);
+ if (resource != null) {
+ noSonarLinesByResource.put(resource, noSonarLines);
+ }
+ }
+ }
+
+ public boolean isIgnored(Violation violation) {
+ boolean ignored = false;
+ if (violation.getResource() != null && violation.getLineId() != null) {
+ Set<Integer> noSonarLines = noSonarLinesByResource.get(violation.getResource());
+ ignored = noSonarLines != null && noSonarLines.contains(violation.getLineId());
+ if (ignored && violation.getRule() != null && StringUtils.containsIgnoreCase(violation.getRule().getKey(), "nosonar")) {
+ ignored = false;
+ }
+ }
+ return ignored;
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/charts/ChartParametersTest.java b/sonar-deprecated/src/test/java/org/sonar/api/charts/ChartParametersTest.java
new file mode 100644
index 00000000000..7ef39e3b730
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/charts/ChartParametersTest.java
@@ -0,0 +1,79 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.charts;
+
+import org.junit.Test;
+
+import java.util.Locale;
+
+import static org.junit.Assert.*;
+
+public class ChartParametersTest {
+ @Test
+ public void shouldForbidHighSizeForSecurityReasons() {
+ String url = ChartParameters.PARAM_WIDTH + "=100000&" + ChartParameters.PARAM_HEIGHT + "=9999999";
+ ChartParameters params = new ChartParameters(url);
+ assertEquals(ChartParameters.MAX_WIDTH, params.getWidth());
+ assertEquals(ChartParameters.MAX_HEIGHT, params.getHeight());
+ }
+
+ @Test
+ public void shouldReadImageSizeFromParameters() {
+ String url = ChartParameters.PARAM_WIDTH + "=200&" + ChartParameters.PARAM_HEIGHT + "=300";
+ ChartParameters params = new ChartParameters(url);
+ assertEquals(200, params.getWidth());
+ assertEquals(300, params.getHeight());
+ }
+
+ @Test
+ public void shouldGetDefaultSizesIfNoParameters() {
+ ChartParameters params = new ChartParameters("foo=bar");
+ assertEquals(ChartParameters.DEFAULT_WIDTH, params.getWidth());
+ assertEquals(ChartParameters.DEFAULT_HEIGHT, params.getHeight());
+ }
+
+ @Test
+ public void shouldDecodeValue() {
+ ChartParameters params = new ChartParameters("foo=0%3D10,3%3D8");
+ assertEquals("0=10,3=8", params.getValue("foo", "", true));
+ assertEquals("0%3D10,3%3D8", params.getValue("foo"));
+ assertNull(params.getValue("bar", null, true));
+ }
+
+ @Test
+ public void shouldDecodeValues() {
+ ChartParameters params = new ChartParameters("foo=0%3D10,3%3D8|5%3D5,7%3D17");
+ assertArrayEquals(new String[]{"0%3D10,3%3D8", "5%3D5,7%3D17"}, params.getValues("foo", "|"));
+ assertArrayEquals(new String[]{"0=10,3=8", "5=5,7=17"}, params.getValues("foo", "|", true));
+ assertArrayEquals(new String[0], params.getValues("bar", "|", true));
+ }
+
+ @Test
+ public void getLocale() {
+ ChartParameters params = new ChartParameters("foo=0&locale=fr");
+ assertEquals(Locale.FRENCH, params.getLocale());
+
+ params = new ChartParameters("foo=0&locale=fr-CH");
+ assertEquals("fr-ch", params.getLocale().getLanguage());
+
+ params = new ChartParameters("foo=0");
+ assertEquals(Locale.ENGLISH, params.getLocale());
+ }
+}
diff --git a/sonar-deprecated/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java b/sonar-deprecated/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java
new file mode 100644
index 00000000000..7f4793b77cd
--- /dev/null
+++ b/sonar-deprecated/src/test/java/org/sonar/api/checks/NoSonarFilterTest.java
@@ -0,0 +1,80 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.checks;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.resources.File;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.Violation;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class NoSonarFilterTest {
+
+ private SensorContext sensorContext = mock(SensorContext.class);
+ NoSonarFilter filter = new NoSonarFilter(sensorContext);
+ private File javaFile;
+
+ @Before
+ public void prepare() {
+ javaFile = new File("org.foo.Bar");
+ when(sensorContext.getResource(javaFile)).thenReturn(javaFile);
+ }
+
+ @Test
+ public void ignoreLinesCommentedWithNoSonar() {
+ Set<Integer> noSonarLines = new HashSet<Integer>();
+ noSonarLines.add(31);
+ noSonarLines.add(55);
+ filter.addResource(javaFile, noSonarLines);
+
+ // violation on class
+ assertThat(filter.isIgnored(new Violation(null, javaFile))).isFalse();
+
+ // violation on lines
+ assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30))).isFalse();
+ assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(31))).isTrue();
+ }
+
+ @Test
+ public void doNotIgnoreWhenNotFoundInSquid() {
+ assertThat(filter.isIgnored(new Violation(null, javaFile).setLineId(30))).isFalse();
+ }
+
+ @Test
+ public void should_accept_violations_from_no_sonar_rules() throws Exception {
+ // The "No Sonar" rule logs violations on the lines that are flagged with "NOSONAR" !!
+
+ Set<Integer> noSonarLines = new HashSet<Integer>();
+ noSonarLines.add(31);
+ filter.addResource(javaFile, noSonarLines);
+
+ Rule noSonarRule = new Rule("squid", "NoSonarCheck");
+ assertThat(filter.isIgnored(new Violation(noSonarRule, javaFile).setLineId(31))).isFalse();
+
+ }
+}