Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/netsupport #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.15.0</version>
<version>2.18.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -139,7 +139,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<version>0.8.2</version>
<executions>
<execution>
<id>default-prepare-agent</id>
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/org/sonar/generic/metrics/GenericMetricsSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
Expand Down Expand Up @@ -41,7 +42,10 @@ public void setFileReader(FileReader fileReader) {

@Override
public void describe(SensorDescriptor descriptor) {
descriptor.name("Generic Metrics Sensor").onlyOnFileType(InputFile.Type.MAIN);
descriptor
.name("Generic Metrics Sensor")
.onlyOnFileType(InputFile.Type.MAIN)
.global();
}

@Override
Expand Down Expand Up @@ -93,13 +97,32 @@ private void processFileMeasure(SensorContext context, JSONObject measure) {
return;
}

InputComponent file = context.fileSystem().inputFile(predicate);
InputFile file = context.fileSystem().inputFile(predicate);

if (file == null) {
predicate = context.fileSystem().predicates().hasFilename(new File(fileName).getName());
Iterable<InputFile> inputFilesWithMatchingFileName = context.fileSystem().inputFiles(predicate);

for (InputFile inputFile : inputFilesWithMatchingFileName) {
if (inputFile.uri().getPath().endsWith(fileName))
{
if (file != null)
LOG.error("For entry from measure file " + fileName + " found more then one entries in file system");
file = inputFile;
}
}
}

if (file == null) {
LOG.warn(fileName + " not found during scan");
return;
}

if (file.type() == Type.TEST) {
LOG.debug("Skipping " + fileName + " , this is test file.");
return;
}

Object value = measure.get("value");
if (!saveMeasure(context, m, file, value))
LOG.error("Processing file " + fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,35 @@ private NewMeasure setupMockNewMeasure(){
}

@Test
public void whenDefiningShouldAppendNameAndFileTypeOnDescriptor(){
when(descriptor.name("Generic Metrics Sensor")).thenReturn(descriptor);
public void whenDefiningShouldAppendName(){
when(descriptor.name(anyString())).thenReturn(descriptor);
when(descriptor.onlyOnFileType(any())).thenReturn(descriptor);

sensor.describe(descriptor);

verify(descriptor, times(1)).name("Generic Metrics Sensor");
}

@Test
public void whenDefiningShouldAppendFileTypeOnDescriptor(){
when(descriptor.name(anyString())).thenReturn(descriptor);
when(descriptor.onlyOnFileType(any())).thenReturn(descriptor);

sensor.describe(descriptor);

verify(descriptor, times(1)).onlyOnFileType(InputFile.Type.MAIN);
}

@Test
public void whenDefiningShouldAppendGlobal(){
when(descriptor.name(anyString())).thenReturn(descriptor);
when(descriptor.onlyOnFileType(any())).thenReturn(descriptor);

sensor.describe(descriptor);

verify(descriptor, times(1)).global();
}

@Test(expected = JSONException.class)
public void whenExecutingAndHasCorruptFileShouldThrow() throws FileNotFoundException{
String json = "this a badly formatted json";
Expand Down