From: Julien HENRY Date: Mon, 14 Apr 2014 13:54:58 +0000 (+0200) Subject: Fix some quality flaws X-Git-Tag: 4.3~28 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=456a2311733cd6001fb9d8c75261a48b44ddc0e6;p=sonarqube.git Fix some quality flaws --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java index 7464af12e33..29aebf3396d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BootstrapContainer.java @@ -102,14 +102,14 @@ public class BootstrapContainer extends ComponentContainer { projectReactorBuilder()); } - private Class projectReactorBuilder() { - if (isRunnerPre2_4()) { + private Class projectReactorBuilder() { + if (isRunnerVersionLessThan2Dot4()) { return DeprecatedProjectReactorBuilder.class; } return ProjectReactorBuilder.class; } - private boolean isRunnerPre2_4() { + private boolean isRunnerVersionLessThan2Dot4() { EnvironmentInformation env = this.getComponentByType(EnvironmentInformation.class); // Starting from SQ Runner 2.4 the key is "SonarQubeRunner" return env != null && "SonarRunner".equals(env.getKey()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java index b0b6cf50cb6..997a66d3645 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/ResourceKeyMigration.java @@ -44,6 +44,8 @@ import java.util.Map; public class ResourceKeyMigration implements BatchComponent { + private static final String UNABLE_TO_UPDATE_COMPONENT_NO_MATCH_WAS_FOUND = "Unable to update component {}. No match was found."; + private static final String COMPONENT_CHANGED_TO = "Component {} changed to {}"; private final Logger logger; private final DatabaseSession session; @@ -99,10 +101,7 @@ public class ResourceKeyMigration implements BatchComponent { Map deprecatedDirectoryKeyMapper, int moduleId) { // Find all FIL or CLA resources for this module - StringBuilder hql = new StringBuilder().append("from ") - .append(ResourceModel.class.getSimpleName()) - .append(" where enabled = true ") - .append(" and rootId = :rootId ") + StringBuilder hql = newResourceQuery() .append(" and scope = '").append(Scopes.FILE).append("' order by qualifier, key"); List resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList(); for (ResourceModel resourceModel : resources) { @@ -130,13 +129,20 @@ public class ResourceKeyMigration implements BatchComponent { } resourceModel.setKey(newEffectiveKey); resourceModel.setDeprecatedKey(oldEffectiveKey); - logger.info("Component {} changed to {}", oldEffectiveKey, newEffectiveKey); + logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey); } else { - logger.warn("Unable to update component {}. No match was found.", oldEffectiveKey); + logger.warn(UNABLE_TO_UPDATE_COMPONENT_NO_MATCH_WAS_FOUND, oldEffectiveKey); } } } + private StringBuilder newResourceQuery() { + return new StringBuilder().append("from ") + .append(ResourceModel.class.getSimpleName()) + .append(" where enabled = true ") + .append(" and rootId = :rootId "); + } + private InputFile findInputFile(Map deprecatedFileKeyMapper, Map deprecatedTestKeyMapper, String oldEffectiveKey, boolean isTest) { if (isTest) { return deprecatedTestKeyMapper.get(oldEffectiveKey); @@ -147,10 +153,7 @@ public class ResourceKeyMigration implements BatchComponent { private void migrateDirectories(Map deprecatedDirectoryKeyMapper, int moduleId) { // Find all DIR resources for this module - StringBuilder hql = new StringBuilder().append("from ") - .append(ResourceModel.class.getSimpleName()) - .append(" where enabled = true ") - .append(" and rootId = :rootId ") + StringBuilder hql = newResourceQuery() .append(" and qualifier = '").append(Qualifiers.DIRECTORY).append("'"); List resources = session.createQuery(hql.toString()).setParameter("rootId", moduleId).getResultList(); for (ResourceModel resourceModel : resources) { @@ -159,9 +162,9 @@ public class ResourceKeyMigration implements BatchComponent { String newEffectiveKey = deprecatedDirectoryKeyMapper.get(oldEffectiveKey); resourceModel.setKey(newEffectiveKey); resourceModel.setDeprecatedKey(oldEffectiveKey); - logger.info("Component {} changed to {}", oldEffectiveKey, newEffectiveKey); + logger.info(COMPONENT_CHANGED_TO, oldEffectiveKey, newEffectiveKey); } else { - logger.warn("Unable to update component {}. No match was found.", oldEffectiveKey); + logger.warn(UNABLE_TO_UPDATE_COMPONENT_NO_MATCH_WAS_FOUND, oldEffectiveKey); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java index 663fa45a1b6..da4900f2145 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java @@ -34,6 +34,8 @@ import org.sonar.api.resources.Languages; import org.sonar.api.utils.MessageException; import javax.annotation.CheckForNull; + +import java.text.MessageFormat; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -92,19 +94,13 @@ class LanguageDetection { String language(InputFile inputFile) { String detectedLanguage = null; for (String languageKey : languagesToConsider) { - PathPattern[] patterns = patternsByLanguage.get(languageKey); - if (patterns != null) { - for (PathPattern pathPattern : patterns) { - if (pathPattern.match(inputFile, false)) { - if (detectedLanguage == null) { - detectedLanguage = languageKey; - break; - } else { - // Language was already forced by another pattern - throw MessageException.of("Language of file '" + inputFile.relativePath() + "' can not be decided as the file matches patterns of both " + getDetails(detectedLanguage) - + " and " + getDetails(languageKey)); - } - } + if (isCandidateForLanguage(inputFile, languageKey)) { + if (detectedLanguage == null) { + detectedLanguage = languageKey; + } else { + // Language was already forced by another pattern + throw MessageException.of(MessageFormat.format("Language of file ''{0}'' can not be decided as the file matches patterns of both {1} and {2}", + inputFile.relativePath(), getDetails(detectedLanguage), getDetails(languageKey))); } } } @@ -121,6 +117,18 @@ class LanguageDetection { return null; } + private boolean isCandidateForLanguage(InputFile inputFile, String languageKey) { + PathPattern[] patterns = patternsByLanguage.get(languageKey); + if (patterns != null) { + for (PathPattern pathPattern : patterns) { + if (pathPattern.match(inputFile, false)) { + return true; + } + } + } + return false; + } + private String getFileLangPatternPropKey(String languageKey) { return "sonar.lang.patterns." + languageKey; } diff --git a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java index 7e2de16f8e7..b2248c80ee6 100644 --- a/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java +++ b/sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java @@ -40,4 +40,24 @@ public class ComponentKeysTest { assertThat(ComponentKeys.createEffectiveKey(project, library)).isEqualTo("junit:junit"); } + @Test + public void isValidModuleKey() { + assertThat(ComponentKeys.isValidModuleKey("")).isFalse(); + assertThat(ComponentKeys.isValidModuleKey("abc")).isTrue(); + assertThat(ComponentKeys.isValidModuleKey("0123")).isFalse(); + assertThat(ComponentKeys.isValidModuleKey("ab 12")).isFalse(); + assertThat(ComponentKeys.isValidModuleKey("ab_12")).isTrue(); + assertThat(ComponentKeys.isValidModuleKey("ab/12")).isFalse(); + } + + @Test + public void isValidBranchKey() { + assertThat(ComponentKeys.isValidBranch("")).isTrue(); + assertThat(ComponentKeys.isValidBranch("abc")).isTrue(); + assertThat(ComponentKeys.isValidBranch("0123")).isTrue(); + assertThat(ComponentKeys.isValidBranch("ab 12")).isFalse(); + assertThat(ComponentKeys.isValidBranch("ab_12")).isTrue(); + assertThat(ComponentKeys.isValidBranch("ab/12")).isFalse(); + } + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java index 27a0323b33c..e40bd0254d8 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/database/model/ResourceModel.java @@ -261,19 +261,21 @@ public class ResourceModel extends BaseIdentifiable implements Cloneable { * @throws IllegalArgumentException if the key is longer than KEY_SIZE */ public void setKey(String key) { + checkSize(key); + this.key = key; + } + + private void checkSize(String key) { if (key.length() > KEY_SIZE) { throw new IllegalArgumentException("Resource key is too long, max is " + KEY_SIZE + " characters. Got : " + key); } - this.key = key; } /** * @throws IllegalArgumentException if the key is longer than KEY_SIZE */ public void setDeprecatedKey(String deprecatedKey) { - if (deprecatedKey.length() > KEY_SIZE) { - throw new IllegalArgumentException("Resource deprecated key is too long, max is " + KEY_SIZE + " characters. Got : " + deprecatedKey); - } + checkSize(deprecatedKey); this.deprecatedKey = deprecatedKey; }