]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2171 Do not compute LCOM4/RFC measures when bytecode is not available
authorsimonbrandhof <simon.brandhof@gmail.com>
Fri, 4 Feb 2011 17:18:52 +0000 (18:18 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Fri, 4 Feb 2011 18:08:27 +0000 (19:08 +0100)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/SquidExecutor.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/SquidExecutorTest.java

index c96dd325793c433eaf0d66daa7bca475f5265c34..a8b26ed1b2a7ba01f7991c2b7c3629f8e56afea7 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.plugins.squid;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.batch.SensorContext;
 import org.sonar.api.checks.CheckFactory;
@@ -172,7 +173,7 @@ public final class SquidExecutor {
   }
 
   void scanBytecode(Collection<File> bytecodeFilesOrDirectories) {
-    if (bytecodeFilesOrDirectories != null && !bytecodeFilesOrDirectories.isEmpty()) {
+    if (hasBytecode(bytecodeFilesOrDirectories)) {
       TimeProfiler profiler = new TimeProfiler(getClass()).start("Java bytecode scan");
       BytecodeScanner bytecodeScanner = squid.register(BytecodeScanner.class);
       bytecodeScanner.scan(bytecodeFilesOrDirectories);
@@ -183,6 +184,20 @@ public final class SquidExecutor {
     }
   }
 
+  static boolean hasBytecode(Collection<File> bytecodeFilesOrDirectories) {
+    if (bytecodeFilesOrDirectories == null) {
+      return false;
+    }
+    for (File bytecodeFilesOrDirectory : bytecodeFilesOrDirectories) {
+      if (bytecodeFilesOrDirectory.exists() &&
+          (bytecodeFilesOrDirectory.isFile() ||
+          !FileUtils.listFiles(bytecodeFilesOrDirectory, new String[]{"class"}, true).isEmpty())) {
+        return true;
+      }
+    }
+    return false;
+  }
+
   void scanSquidIndex() {
     TimeProfiler profiler = new TimeProfiler(getClass()).start("Java Squid scan");
     SquidScanner squidScanner = squid.register(SquidScanner.class);
index 18350132b502a11c57e3f31a2c13f64f413f505c..0238de92434b9028a63e0306e7081fdee88f826a 100644 (file)
@@ -21,7 +21,9 @@ package org.sonar.plugins.squid;
 
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.number.OrderingComparisons.greaterThan;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.anyDouble;
 import static org.mockito.Matchers.anyObject;
 import static org.mockito.Mockito.atLeast;
@@ -101,6 +103,18 @@ public class SquidExecutorTest {
     verify(context, atLeast(100)).saveMeasure((Resource) anyObject(), (org.sonar.api.measures.Metric) anyObject(), anyDouble());
   }
 
+  @Test
+  public void shouldNotHaveBytecode() {
+    assertFalse(SquidExecutor.hasBytecode(Collections.<File>emptyList()));
+
+    assertFalse(SquidExecutor.hasBytecode(Arrays.asList(new File("unknown"))));
+  }
+
+  @Test
+  public void shouldHaveBytecode() {
+    assertTrue(SquidExecutor.hasBytecode(Arrays.asList(new File("test-resources/commons-collections-3.2.1/bin"))));
+  }
+
   @Test
   public void flushSquidAfterUsage() {
     Squid squid = mock(Squid.class);