]> source.dussan.org Git - sonarqube.git/commitdiff
Fix some quality flaws
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 13 Feb 2014 11:12:14 +0000 (12:12 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 13 Feb 2014 14:29:14 +0000 (15:29 +0100)
plugins/sonar-cpd-plugin/src/main/java/org/sonar/plugins/cpd/SonarBridgeEngine.java
sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ProjectFileSystemAdapter.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ComponentIndexerTest.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java

index 6512f6fc8fab945ea81814bc8c079ce4cc77d9f0..9f6bb9d1f67db5d5941284d7c31cff961dd647ac 100644 (file)
@@ -92,6 +92,9 @@ public class SonarBridgeEngine extends CpdEngine {
     }
 
     CpdMapping mapping = getMapping(languageKey);
+    if (mapping == null) {
+      return;
+    }
 
     // Create index
     SonarDuplicationsIndex index = indexFactory.create(project);
index ded2cfed6867e3cc487c6133172f3b1160515f4d..576af394edbf03ecfc1e2c00120cb50ca2b0b33d 100644 (file)
@@ -26,7 +26,14 @@ import org.slf4j.LoggerFactory;
 import org.sonar.api.BatchComponent;
 import org.sonar.api.database.DatabaseSession;
 import org.sonar.api.database.model.ResourceModel;
-import org.sonar.api.resources.*;
+import org.sonar.api.resources.Directory;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Resource;
+import org.sonar.api.resources.Scopes;
 import org.sonar.api.scan.filesystem.InputFile;
 import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 import org.sonar.api.utils.PathUtils;
@@ -119,9 +126,9 @@ public class ResourceKeyMigration implements BatchComponent {
         }
         resourceModel.setKey(newEffectiveKey);
         resourceModel.setDeprecatedKey(oldEffectiveKey);
-        logger.info("Migrated resource " + oldEffectiveKey + " to " + newEffectiveKey);
+        logger.info("Migrated resource {} to {}", oldEffectiveKey, newEffectiveKey);
       } else {
-        logger.warn("Unable to migrate resource " + oldEffectiveKey + ". No match was found.");
+        logger.warn("Unable to migrate resource {}. No match was found.", oldEffectiveKey);
       }
     }
   }
@@ -148,9 +155,9 @@ public class ResourceKeyMigration implements BatchComponent {
         String newEffectiveKey = deprecatedDirectoryKeyMapper.get(oldEffectiveKey);
         resourceModel.setKey(newEffectiveKey);
         resourceModel.setDeprecatedKey(oldEffectiveKey);
-        logger.info("Migrated resource " + oldEffectiveKey + " to " + newEffectiveKey);
+        logger.info("Migrated resource {} to {}", oldEffectiveKey, newEffectiveKey);
       } else {
-        logger.warn("Unable to migrate resource " + oldEffectiveKey);
+        logger.warn("Unable to migrate resource {}. No match was found.", oldEffectiveKey);
       }
     }
   }
index d6911d66f331ac4334074b5ebd743f014faa23a8..f8e374e52b86ded4b942c6b15d1bae46307f9504 100644 (file)
@@ -38,7 +38,6 @@ import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 import org.sonar.api.utils.SonarException;
 import org.sonar.batch.index.ResourceKeyMigration;
 import org.sonar.batch.scan.language.DefaultModuleLanguages;
-import org.sonar.core.resource.ResourceDao;
 
 /**
  * Index all files/directories of the module in SQ database and importing source code.
@@ -52,18 +51,16 @@ public class ComponentIndexer implements BatchComponent {
   private final ResourceKeyMigration migration;
   private final Project module;
   private final DefaultModuleLanguages moduleLanguages;
-  private final ResourceDao resourceDao;
   private InputFileCache fileCache;
 
   public ComponentIndexer(Project module, Languages languages, SonarIndex sonarIndex, Settings settings, ResourceKeyMigration migration,
-    DefaultModuleLanguages moduleLanguages, ResourceDao resourceDao, InputFileCache fileCache) {
+    DefaultModuleLanguages moduleLanguages, InputFileCache fileCache) {
     this.module = module;
     this.languages = languages;
     this.sonarIndex = sonarIndex;
     this.settings = settings;
     this.migration = migration;
     this.moduleLanguages = moduleLanguages;
-    this.resourceDao = resourceDao;
     this.fileCache = fileCache;
   }
 
index 226f84a198ddcfcbf6958eaa85e784a648e40516..080b3a4e2e93d47247ad8bc8795163e3a89e146e 100644 (file)
@@ -43,6 +43,8 @@ import java.util.Set;
  */
 public class FileIndex implements BatchComponent {
 
+  private static final String FILE_S_IS_NOT_DECLARED_IN_MODULE_BASEDIR_S = "File '%s' is not declared in module basedir %s";
+
   private static class Progress {
     private final Set<String> removedPaths;
     private final Set<String> indexed;
@@ -122,7 +124,7 @@ public class FileIndex implements BatchComponent {
       String path = pathResolver.relativePath(fileSystem.baseDir(), sourceFile);
       if (path == null) {
         LoggerFactory.getLogger(getClass()).warn(String.format(
-          "File '%s' is not declared in module basedir %s", sourceFile.getAbsoluteFile(), fileSystem.baseDir()
+          FILE_S_IS_NOT_DECLARED_IN_MODULE_BASEDIR_S, sourceFile.getAbsoluteFile(), fileSystem.baseDir()
           ));
       } else {
         if (exclusionFilters.accept(sourceFile, path, type)) {
@@ -147,7 +149,7 @@ public class FileIndex implements BatchComponent {
       String path = pathResolver.relativePath(fileSystem.baseDir(), sourceFile);
       if (path == null) {
         LoggerFactory.getLogger(getClass()).warn(String.format(
-          "File '%s' is not declared in module basedir %s", sourceFile.getAbsoluteFile(), fileSystem.baseDir()
+          FILE_S_IS_NOT_DECLARED_IN_MODULE_BASEDIR_S, sourceFile.getAbsoluteFile(), fileSystem.baseDir()
           ));
       } else {
         if (exclusionFilters.accept(sourceFile, path, InputFile.TYPE_MAIN)) {
index de2dc83f85cc303401eeafd11f228c73b3527493..30f6b5c6fb6129341503aed07d3a10ccd025c22a 100644 (file)
@@ -182,11 +182,11 @@ public class ProjectFileSystemAdapter implements ProjectFileSystem {
     if (file == null || !file.exists()) {
       return null;
     }
-    PathResolver.RelativePath relativePath = pathResolver.relativePath(getSourceDirs(), file);
+    String relativePath = pathResolver.relativePath(getBasedir(), file);
     if (relativePath == null) {
       return null;
     }
-    return file.isFile() ? new org.sonar.api.resources.File(relativePath.path()) : new org.sonar.api.resources.Directory(relativePath.path());
+    return file.isFile() ? org.sonar.api.resources.File.create(relativePath) : org.sonar.api.resources.Directory.create(relativePath);
   }
 
   public List<InputFile> mainFiles(String... langs) {
index c45ac2b2a8e45e348280613bcfb7ccc69e98738f..3bd5f2dbbdcc17d74b01492e0c5503cd4f38e082 100644 (file)
@@ -43,7 +43,6 @@ import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
 import org.sonar.api.scan.filesystem.internal.InputFileBuilder;
 import org.sonar.batch.index.ResourceKeyMigration;
 import org.sonar.batch.scan.language.DefaultModuleLanguages;
-import org.sonar.core.resource.ResourceDao;
 
 import java.io.File;
 import java.io.IOException;
@@ -97,7 +96,7 @@ public class ComponentIndexerTest {
       newInputFile("src/test/java/foo/bar/FooTest.java", "", "foo/bar/FooTest.java", "java", true)));
     Languages languages = new Languages(Java.INSTANCE);
     ComponentIndexer indexer = new ComponentIndexer(project, languages, sonarIndex, settings, mock(ResourceKeyMigration.class), new DefaultModuleLanguages(settings, languages),
-      mock(ResourceDao.class), mock(InputFileCache.class));
+      mock(InputFileCache.class));
     indexer.execute(fs);
 
     verify(sonarIndex).index(org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false));
@@ -122,7 +121,7 @@ public class ComponentIndexerTest {
 
     Languages languages = new Languages(cobolLanguage);
     ComponentIndexer indexer = new ComponentIndexer(project, languages, sonarIndex, settings, mock(ResourceKeyMigration.class), new DefaultModuleLanguages(settings, languages),
-      mock(ResourceDao.class), mock(InputFileCache.class));
+      mock(InputFileCache.class));
     indexer.execute(fs);
 
     verify(sonarIndex).index(org.sonar.api.resources.File.create("/src/foo/bar/Foo.cbl", "foo/bar/Foo.cbl", cobolLanguage, false));
@@ -138,7 +137,7 @@ public class ComponentIndexerTest {
       newInputFile("src/main/java/foo/bar/Foo.java", "sample code", "foo/bar/Foo.java", "java", false)));
     Languages languages = new Languages(Java.INSTANCE);
     ComponentIndexer indexer = new ComponentIndexer(project, languages, sonarIndex, settings, mock(ResourceKeyMigration.class), new DefaultModuleLanguages(settings, languages),
-      mock(ResourceDao.class), mock(InputFileCache.class));
+      mock(InputFileCache.class));
     indexer.execute(fs);
 
     Resource sonarFile = org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false);
@@ -180,7 +179,7 @@ public class ComponentIndexerTest {
         .build()));
     Languages languages = new Languages(Java.INSTANCE);
     ComponentIndexer indexer = new ComponentIndexer(project, languages, sonarIndex, settings, mock(ResourceKeyMigration.class), new DefaultModuleLanguages(settings, languages),
-      mock(ResourceDao.class), mock(InputFileCache.class));
+      mock(InputFileCache.class));
     indexer.execute(fs);
 
     Resource sonarFile = org.sonar.api.resources.File.create("src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false);
@@ -208,7 +207,7 @@ public class ComponentIndexerTest {
             .build()));
     Languages languages = new Languages(Java.INSTANCE);
     ComponentIndexer indexer = new ComponentIndexer(project, languages, sonarIndex, settings, mock(ResourceKeyMigration.class), new DefaultModuleLanguages(settings, languages),
-      mock(ResourceDao.class), mock(InputFileCache.class));
+      mock(InputFileCache.class));
     indexer.execute(fs);
 
     Resource sonarFile = org.sonar.api.resources.File.create("/src/main/java/foo/bar/Foo.java", "foo/bar/Foo.java", Java.INSTANCE, false);
@@ -231,7 +230,7 @@ public class ComponentIndexerTest {
     when(fs.inputFiles(FileQuery.all())).thenReturn((Iterable) Arrays.asList(inputFile3lines, inputFileEmpty, inputFileEndsWithNewLine, inputFileMixedLineEnds));
     Languages languages = new Languages(Java.INSTANCE);
     ComponentIndexer indexer = new ComponentIndexer(project, languages, sonarIndex, settings, mock(ResourceKeyMigration.class), new DefaultModuleLanguages(settings, languages),
-      mock(ResourceDao.class), mock(InputFileCache.class));
+      mock(InputFileCache.class));
     indexer.execute(fs);
 
     assertThat(inputFile3lines.attribute(InputFile.ATTRIBUTE_LINE_COUNT)).isEqualTo("3");
index d9c2eb06d61ee0cba5b1faeff1ac1d15a362fc8e..53a27f858bfb5babbd8d98ac4d98a73c4468c87b 100644 (file)
@@ -131,7 +131,7 @@ public class Directory extends Resource {
    * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed.
    * Internal use only.
    */
-  static Directory create(String relativePathFromBaseDir) {
+  public static Directory create(String relativePathFromBaseDir) {
     Directory d = new Directory();
     String normalizedPath = normalize(relativePathFromBaseDir);
     d.setKey(normalizedPath);
index bb752c16980ce33a7c9a0a60c8464142c7aab2ef..b19a32bcf7ffe8b6a5ddb22a2541d89e3643c7cc 100644 (file)
@@ -46,7 +46,7 @@ public class Project extends Resource implements Component {
   public static final Language NONE_LANGUAGE = new AbstractLanguage("none", "None") {
     @Override
     public String[] getFileSuffixes() {
-      return null;
+      return new String[0];
     }
   };