RuleSets rulesets = createRulesets(repositoryKey);
for (InputFile file : files) {
- pmdFactory.process(file.getFile(), encoding, rulesets, ruleContext);
+ pmdFactory.process(file, encoding, rulesets, ruleContext);
}
}
import net.sourceforge.pmd.SourceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.utils.SonarException;
import java.io.File;
-import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Map;
setJavaVersion(pmd, javaVersion);
}
- public void process(File file, Charset encoding, RuleSets rulesets, RuleContext ruleContext) {
+ public void process(InputFile inputFile, Charset encoding, RuleSets rulesets, RuleContext ruleContext) {
+ File file = inputFile.getFile();
ruleContext.setSourceCodeFilename(file.getAbsolutePath());
InputStream inputStream = null;
try {
- inputStream = new FileInputStream(file);
+ inputStream = inputFile.getInputStream();
pmd.processFile(inputStream, encoding.displayName(), rulesets, ruleContext);
} catch (PMDException e) {
Report report = pmdExecutor.execute();
- verify(pmdTemplate).process(eq(new File("src/Class.java")), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class));
- verify(pmdTemplate).process(eq(new File("test/ClassTest.java")), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class));
+ verify(pmdTemplate).process(eq(srcFile), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class));
+ verify(pmdTemplate).process(eq(tstFile), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class));
assertThat(report).isNotNull();
}
pmdExecutor.execute();
- verify(pmdTemplate).process(eq(new File("src/Class.java")), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class));
+ verify(pmdTemplate).process(eq(srcFile), eq(Charsets.UTF_8), any(RuleSets.class), any(RuleContext.class));
verifyNoMoreInteractions(pmdTemplate);
}
*/
package org.sonar.plugins.pmd;
+import org.junit.rules.ExpectedException;
+
+import org.junit.Rule;
+
+import org.sonar.api.utils.XmlParserException;
+
import com.google.common.collect.Iterators;
import net.sourceforge.pmd.IRuleViolation;
import net.sourceforge.pmd.Report;
SensorContext sensorContext = mock(SensorContext.class);
Violation violation = mock(Violation.class);
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
@Before
public void setUpPmdSensor() {
pmdSensor = new PmdSensor(profile, executor, pmdViolationToRuleViolation);
}
@Test
- public void should_execute_on_project_with_main_files_and_active_rules() {
+ public void should_execute_on_project_without_main_files() {
+ when(project.getFileSystem().mainFiles(Java.KEY).isEmpty()).thenReturn(true);
+
+ boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project);
+
+ assertThat(shouldExecute).isTrue();
+ }
+
+ @Test
+ public void should_execute_on_project_without_test_files() {
+ when(project.getFileSystem().testFiles(Java.KEY).isEmpty()).thenReturn(true);
+
boolean shouldExecute = pmdSensor.shouldExecuteOnProject(project);
assertThat(shouldExecute).isTrue();
}
@Test
- public void should_not_execute_on_project_without_files() {
+ public void should_not_execute_on_project_without_any_files() {
when(project.getFileSystem().mainFiles(Java.KEY).isEmpty()).thenReturn(true);
when(project.getFileSystem().testFiles(Java.KEY).isEmpty()).thenReturn(true);
verifyZeroInteractions(sensorContext);
}
+ @Test
+ public void should_report_analyse_failure() {
+ when(executor.execute()).thenThrow(new RuntimeException());
+
+ exception.expect(XmlParserException.class);
+
+ pmdSensor.analyse(project, sensorContext);
+ }
+
+ @Test
+ public void should_to_string() {
+ String toString = pmdSensor.toString();
+
+ assertThat(toString).isEqualTo("PmdSensor");
+ }
+
static IRuleViolation violation() {
return mock(IRuleViolation.class);
}