private final PMD pmd;
public PmdTemplate(String javaVersion) {
- pmd = new PMD();
+ this(new PMD());
setJavaVersion(pmd, javaVersion);
}
+ @VisibleForTesting
+ PmdTemplate(PMD pmd) {
+ this.pmd = pmd;
+ }
+
public void process(InputFile inputFile, Charset encoding, RuleSets rulesets, RuleContext ruleContext) {
File file = inputFile.getFile();
ruleContext.setSourceCodeFilename(file.getAbsolutePath());
@VisibleForTesting
static void setJavaVersion(PMD pmd, String javaVersion) {
String version = normalize(javaVersion);
- if (version == null) {
- return; // Do nothing
- }
-
SourceType sourceType = SourceType.getSourceTypeForId("java " + version);
if (sourceType == null) {
throw new SonarException("Unsupported Java version for PMD: " + version);
private static String normalize(String version) {
return Functions.forMap(JAVA_VERSIONS, version).apply(version);
}
-
- @Override
- public String toString() {
- return getClass().getSimpleName();
- }
}
}
return ruleFinder.findByKey(PmdConstants.TEST_REPOSITORY_KEY, ruleKey);
}
-
- @Override
- public String toString() {
- return getClass().getSimpleName();
- }
}
*/
package org.sonar.plugins.pmd;
+import com.google.common.base.Charsets;
import net.sourceforge.pmd.PMD;
+import net.sourceforge.pmd.PMDException;
+import net.sourceforge.pmd.RuleContext;
+import net.sourceforge.pmd.RuleSets;
import net.sourceforge.pmd.SourceType;
import org.junit.Test;
+import org.sonar.api.resources.InputFile;
+import org.sonar.api.utils.SonarException;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class PmdTemplateTest {
+ InputFile inputFile = mock(InputFile.class);
+ RuleSets rulesets = mock(RuleSets.class);
+ RuleContext ruleContext = mock(RuleContext.class);
+ InputStream inputStream = mock(InputStream.class);
PMD pmd = mock(PMD.class);
+ @Test
+ public void should_process_input_file() throws PMDException, FileNotFoundException {
+ when(inputFile.getFile()).thenReturn(new File("source.java"));
+ when(inputFile.getInputStream()).thenReturn(inputStream);
+
+ new PmdTemplate(pmd).process(inputFile, Charsets.UTF_8, rulesets, ruleContext);
+
+ verify(ruleContext).setSourceCodeFilename(new File("source.java").getAbsolutePath());
+ verify(pmd).processFile(inputStream, Charsets.UTF_8.displayName(), rulesets, ruleContext);
+ }
+
+ @Test
+ public void should_ignore_PMD_error() throws PMDException, FileNotFoundException {
+ when(inputFile.getFile()).thenReturn(new File("source.java"));
+ when(inputFile.getInputStream()).thenReturn(inputStream);
+ doThrow(new PMDException("BUG")).when(pmd).processFile(inputStream, Charsets.UTF_8.displayName(), rulesets, ruleContext);
+
+ new PmdTemplate(pmd).process(inputFile, Charsets.UTF_8, rulesets, ruleContext);
+ }
+
@Test
public void should_set_java11_version() {
PmdTemplate.setJavaVersion(pmd, "1.1");
verify(pmd).setJavaVersion(SourceType.JAVA_16);
}
+
+ @Test(expected = SonarException.class)
+ public void should_fail_on_invalid_java_version() {
+ new PmdTemplate("12.2");
+ }
+
+ @Test
+ public void shouldnt_fail_on_valid_java_version() {
+ new PmdTemplate("6");
+ }
}
--- /dev/null
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.pmd;
+
+import net.sourceforge.pmd.IRuleViolation;
+import net.sourceforge.pmd.Rule;
+import org.junit.Test;
+import org.sonar.api.batch.SensorContext;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.ProjectFileSystem;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.Violation;
+
+import java.io.File;
+import java.util.Arrays;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.test.MoreConditions.reflectionEqualTo;
+
+public class PmdViolationToRuleViolationTest {
+ private org.sonar.api.rules.Rule sonarRule = org.sonar.api.rules.Rule.create("pmd", "RULE");
+ private ProjectFileSystem projectFileSystem = mock(ProjectFileSystem.class);
+ private IRuleViolation pmdViolation = mock(IRuleViolation.class);
+ private SensorContext context = mock(SensorContext.class);
+ private RuleFinder ruleFinder = mock(RuleFinder.class);
+ private Rule rule = mock(Rule.class);
+
+ @Test
+ public void should_convert_pmd_violation_to_sonar_violation() {
+ when(projectFileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/src")));
+ when(pmdViolation.getFilename()).thenReturn("/src/source.java");
+ when(pmdViolation.getBeginLine()).thenReturn(42);
+ when(pmdViolation.getDescription()).thenReturn("Description");
+ when(pmdViolation.getRule()).thenReturn(rule);
+ when(rule.getName()).thenReturn("RULE");
+ when(context.getResource(new JavaFile("[default].source"))).thenReturn(new JavaFile("[default].source"));
+ when(ruleFinder.findByKey("pmd", "RULE")).thenReturn(sonarRule);
+
+ PmdViolationToRuleViolation pmdViolationToRuleViolation = new PmdViolationToRuleViolation(projectFileSystem, ruleFinder);
+ Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
+
+ assertThat(violation).is(reflectionEqualTo(Violation.create(sonarRule, new JavaFile("[default].source")).setLineId(42).setMessage("Description")));
+ }
+
+ @Test
+ public void should_ignore_violation_on_unknown_resource() {
+ when(projectFileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/src")));
+ when(pmdViolation.getFilename()).thenReturn("/src/UNKNOWN.java");
+
+ PmdViolationToRuleViolation pmdViolationToRuleViolation = new PmdViolationToRuleViolation(projectFileSystem, ruleFinder);
+ Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
+
+ assertThat(violation).isNull();
+ }
+
+ @Test
+ public void should_ignore_violation_on_unknown_rule() {
+ when(projectFileSystem.getTestDirs()).thenReturn(Arrays.asList(new File("/test")));
+ when(pmdViolation.getFilename()).thenReturn("/test/source.java");
+ when(pmdViolation.getRule()).thenReturn(rule);
+ when(rule.getName()).thenReturn("UNKNOWN");
+ when(context.getResource(new JavaFile("[default].source"))).thenReturn(new JavaFile("[default].source"));
+
+ PmdViolationToRuleViolation pmdViolationToRuleViolation = new PmdViolationToRuleViolation(projectFileSystem, ruleFinder);
+ Violation violation = pmdViolationToRuleViolation.toViolation(pmdViolation, context);
+
+ assertThat(violation).isNull();
+ }
+}
*/
package org.sonar.api.web;
-import org.junit.rules.ExpectedException;
-
import org.junit.Rule;
-
import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.fest.assertions.Assertions.assertThat;
public class FilterTest {
@Rule
public ExpectedException exception = ExpectedException.none();
+ @Test
+ public void should_create_filter() {
+ Filter filter = Filter.create()
+ .setDisplayAs("list")
+ .setFavouritesOnly(true)
+ .setPageSize(100);
+
+ assertThat(filter.getDisplayAs()).isEqualTo("list");
+ assertThat(filter.isFavouritesOnly()).isTrue();
+ assertThat(filter.getPageSize()).isEqualTo(100);
+ }
+
+ @Test
+ public void should_add_criteria() {
+ Criterion criterion1 = Criterion.createForQualifier("A");
+ Criterion criterion2 = Criterion.createForQualifier("A");
+ Filter filter = Filter.create()
+ .add(criterion1)
+ .add(criterion2);
+
+ assertThat(filter.getCriteria()).containsExactly(criterion1, criterion2);
+ }
+
+ @Test
+ public void should_add_columns() {
+ FilterColumn column1 = FilterColumn.create("", "", "ASC", false);
+ FilterColumn column2 = FilterColumn.create("", "", "DESC", false);
+ Filter filter = Filter.create()
+ .add(column1)
+ .add(column2);
+
+ assertThat(filter.getColumns()).containsExactly(column1, column2);
+ }
+
@Test
public void should_accept_valid_periods() {
Filter.create().setDisplayAs("list");
Filter.create().setDisplayAs("<invalid>");
}
+
+ @Test
+ public void should_accept_valid_pageSize() {
+ Filter.create().setPageSize(20);
+ Filter.create().setPageSize(200);
+ }
+
+ @Test
+ public void should_fail_on_pageSize_too_small() {
+ exception.expect(IllegalArgumentException.class);
+ exception.expectMessage("page size should be between 20 and 200");
+
+ Filter.create().setPageSize(19);
+ }
+
+ @Test
+ public void should_fail_on_pageSize_too_high() {
+ exception.expect(IllegalArgumentException.class);
+ exception.expectMessage("page size should be between 20 and 200");
+
+ Filter.create().setPageSize(201);
+ }
}