aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-11-21 19:05:11 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2011-11-21 19:10:50 +0100
commit25268a877bb0ffb7c045780dc9670636ccd787bf (patch)
tree9cfb16a5c60deeab8f7c0eb8d7d265e911419f34
parent2907b2322404490ce99138d60cc029248adcbc7b (diff)
downloadsonarqube-25268a877bb0ffb7c045780dc9670636ccd787bf.tar.gz
sonarqube-25268a877bb0ffb7c045780dc9670636ccd787bf.zip
LCOM4 of Java files without any class - like package-info.java - must be 1
-rw-r--r--plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java6
-rw-r--r--plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java20
2 files changed, 23 insertions, 3 deletions
diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java
index e4c99ecd65d..a262b639b5d 100644
--- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java
+++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridge.java
@@ -35,6 +35,10 @@ public class ChidamberKemererBridge extends Bridge {
context.saveMeasure(sonarFile, CoreMetrics.DEPTH_IN_TREE, squidFile.getDouble(Metric.DIT));
context.saveMeasure(sonarFile, CoreMetrics.NUMBER_OF_CHILDREN, squidFile.getDouble(Metric.NOC));
context.saveMeasure(sonarFile, CoreMetrics.RFC, squidFile.getDouble(Metric.RFC));
- context.saveMeasure(sonarFile, CoreMetrics.LCOM4, squidFile.getDouble(Metric.LCOM4));
+ context.saveMeasure(sonarFile, CoreMetrics.LCOM4, getLcom4(squidFile));
+ }
+
+ static double getLcom4(SourceFile squidFile) {
+ return Math.max(squidFile.getDouble(Metric.LCOM4), 1.0);
}
}
diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java
index c032a434b31..0d67ba202c5 100644
--- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java
+++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/bridges/ChidamberKemererBridgeTest.java
@@ -23,13 +23,15 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyDouble;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.*;
import org.junit.Test;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.JavaPackage;
+import org.sonar.squid.api.SourceFile;
+import org.sonar.squid.measures.Metric;
+import org.sonar.squid.measures.MetricDef;
public class ChidamberKemererBridgeTest extends BridgeTestCase {
@@ -58,4 +60,18 @@ public class ChidamberKemererBridgeTest extends BridgeTestCase {
verify(context, never()).saveMeasure(eq(new JavaPackage("org.apache.struts.config")), eq(CoreMetrics.LCOM4), anyDouble());
verify(context, never()).saveMeasure(eq(project), eq(CoreMetrics.LCOM4), anyDouble());
}
+
+ @Test
+ public void lcom4ShouldBeGreaterThanZero() {
+ SourceFile sourceFile = mock(SourceFile.class);
+
+ when(sourceFile.getDouble(Metric.LCOM4)).thenReturn(2.0);
+ assertThat(ChidamberKemererBridge.getLcom4(sourceFile), is(2.0));
+
+ when(sourceFile.getDouble(Metric.LCOM4)).thenReturn(1.0);
+ assertThat(ChidamberKemererBridge.getLcom4(sourceFile), is(1.0));
+
+ when(sourceFile.getDouble(Metric.LCOM4)).thenReturn(0.0);
+ assertThat(ChidamberKemererBridge.getLcom4(sourceFile), is(1.0));
+ }
}