]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3024 Fix regression with Library resources
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 16 Jan 2014 09:41:35 +0000 (10:41 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 16 Jan 2014 09:42:26 +0000 (10:42 +0100)
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java
sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java
sonar-core/src/test/java/org/sonar/core/component/ComponentKeysTest.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java

index c76b2498ae185da9881834df2652e00e75f44c5e..bb963fe162045b0e501f5756b53173e379904315 100644 (file)
@@ -554,8 +554,8 @@ public class DefaultIndex extends SonarIndex {
       return null;
     }
 
-    resource.setEffectiveKey(ComponentKeys.createKey(currentProject, resource));
-    resource.setDeprecatedEffectiveKey(ComponentKeys.createDeprecatedKey(currentProject, resource));
+    resource.setEffectiveKey(ComponentKeys.createEffectiveKey(currentProject, resource));
+    resource.setDeprecatedEffectiveKey(ComponentKeys.createDeprecatedEffectiveKey(currentProject, resource));
     bucket = new Bucket(resource).setParent(parentBucket);
     buckets.put(resource, bucket);
 
@@ -639,7 +639,7 @@ public class DefaultIndex extends SonarIndex {
       for (Map.Entry<Resource, Bucket> entry : buckets.entrySet()) {
         Resource indexedResource = entry.getKey();
         if (res.getClass() == indexedResource.getClass() && res.getDeprecatedKey().equals(indexedResource.getDeprecatedKey())) {
-          LOG.warn("Resource was found using deprecated key. Please update your plugin.");
+          LOG.warn("Resource " + res + " was found using deprecated key. Please update your plugin.");
           // Fix resource key
           Bucket bucket = entry.getValue();
           res.setKey(bucket.getResource().getKey());
index 267995931774e169c661f12b220e27173b08d518..5ef7e204d2751bcb3b50de1741bcf2e5cb470f2c 100644 (file)
@@ -38,7 +38,6 @@ import org.sonar.api.scan.filesystem.internal.InputFile;
 import org.sonar.api.security.ResourcePermissions;
 import org.sonar.api.utils.SonarException;
 
-import javax.annotation.Nullable;
 import javax.persistence.NonUniqueResultException;
 import javax.persistence.Query;
 
@@ -89,7 +88,7 @@ public final class DefaultResourcePersister implements ResourcePersister {
     // temporary hack
     project.setEffectiveKey(project.getKey());
 
-    ResourceModel model = findOrCreateModel(null, project);
+    ResourceModel model = findOrCreateModel(project);
     // ugly, only for projects
     model.setLanguageKey(project.getLanguageKey());
 
@@ -187,7 +186,7 @@ public final class DefaultResourcePersister implements ResourcePersister {
   }
 
   private Snapshot persistLibrary(Project project, Library library) {
-    ResourceModel model = findOrCreateModel(null, library);
+    ResourceModel model = findOrCreateModel(library);
     model = session.save(model);
     // TODO to be removed
     library.setId(model.getId());
@@ -231,7 +230,7 @@ public final class DefaultResourcePersister implements ResourcePersister {
   private Snapshot persistFileOrDirectory(Project project, Resource resource, Resource parentReference) {
     Snapshot moduleSnapshot = snapshotsByResource.get(project);
     Integer moduleId = moduleSnapshot.getResourceId();
-    ResourceModel model = findOrCreateModel(moduleId, resource);
+    ResourceModel model = findOrCreateModel(resource);
     model.setRootId(moduleId);
     model = session.save(model);
     resource.setId(model.getId());
@@ -268,14 +267,11 @@ public final class DefaultResourcePersister implements ResourcePersister {
     }
   }
 
-  /**
-   * @param rootModuleId can be null and in this case resource will be searched using deprecated key instead of path
-   */
-  private ResourceModel findOrCreateModel(@Nullable Integer rootModuleId, Resource resource) {
+  private ResourceModel findOrCreateModel(Resource resource) {
     ResourceModel model;
     try {
       model = session.getSingleResult(ResourceModel.class, "key", resource.getEffectiveKey());
-      if (model == null) {
+      if (model == null && !StringUtils.equals(resource.getEffectiveKey(), resource.getDeprecatedEffectiveKey())) {
         // Fallback on deprecated key when resource has not already been migrated
         model = session.getSingleResult(ResourceModel.class, "key", resource.getDeprecatedEffectiveKey(), "deprecatedKey", null);
       }
index 550bf27eee264613d2c766b4a5637279e931dc0d..d8ba9b5c648f80190e890dd9edf3046ab6504360 100644 (file)
@@ -42,7 +42,7 @@ public final class ComponentKeys {
    * @param resource
    * @return the full key of a component, based on its parent projects' key and own key
    */
-  public static String createKey(Project project, Resource resource) {
+  public static String createEffectiveKey(Project project, Resource resource) {
     String key = resource.getKey();
     if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
       // not a project nor a library
@@ -55,7 +55,7 @@ public final class ComponentKeys {
     return key;
   }
 
-  public static String createDeprecatedKey(Project project, Resource resource) {
+  public static String createDeprecatedEffectiveKey(Project project, Resource resource) {
     String key = resource.getKey();
     if (!StringUtils.equals(Scopes.PROJECT, resource.getScope())) {
       // not a project nor a library
index e758668c70c683c57aedfd51bc3cfbf2197524c7..3f90af3496fd888308c868de3252fa982c1f4954 100644 (file)
@@ -31,14 +31,14 @@ public class ComponentKeysTest {
   @Test
   public void shouldCreateUID() {
     Project project = new Project("my_project");
-    assertThat(ComponentKeys.createKey(project, project)).isEqualTo("my_project");
+    assertThat(ComponentKeys.createEffectiveKey(project, project)).isEqualTo("my_project");
 
     JavaPackage javaPackage = JavaPackage.create("src/org/foo", "org.foo");
-    assertThat(ComponentKeys.createKey(project, javaPackage)).isEqualTo("my_project:/src/org/foo");
-    assertThat(ComponentKeys.createDeprecatedKey(project, javaPackage)).isEqualTo("my_project:org.foo");
+    assertThat(ComponentKeys.createEffectiveKey(project, javaPackage)).isEqualTo("my_project:/src/org/foo");
+    assertThat(ComponentKeys.createDeprecatedEffectiveKey(project, javaPackage)).isEqualTo("my_project:org.foo");
 
     Library library = new Library("junit:junit", "4.7");
-    assertThat(ComponentKeys.createKey(project, library)).isEqualTo("junit:junit");
+    assertThat(ComponentKeys.createEffectiveKey(project, library)).isEqualTo("junit:junit");
   }
 
 }
index de4fb3bfa9d0a3ff98b4288f4fb13e48d11fac8d..bca7a01567b764c06d8d36e6cf21636f73280577 100644 (file)
@@ -29,6 +29,7 @@ public final class Library extends Resource {
 
   public Library(String key, String version) {
     setKey(key);
+    setDeprecatedKey(key);
     this.version = version;
   }
 
@@ -112,9 +113,9 @@ public final class Library extends Resource {
   @Override
   public String toString() {
     return new ToStringBuilder(this)
-        .append("key", getKey())
-        .append("name", getName())
-        .append("version", version)
-        .toString();
+      .append("key", getKey())
+      .append("name", getName())
+      .append("version", version)
+      .toString();
   }
 }
index 68c9b62e7fb9fbee307814a75d2a8505585267bf..7c89db07838e23fc6f1784ffdb38c23a2159958b 100644 (file)
@@ -84,7 +84,9 @@ public class Project extends Resource implements Component {
 
   public Project(String key) {
     setKey(key);
+    setDeprecatedKey(key);
     setEffectiveKey(key);
+    setDeprecatedEffectiveKey(key);
   }
 
   public Project(String key, String branch, String name) {
@@ -95,7 +97,9 @@ public class Project extends Resource implements Component {
       setKey(key);
       this.name = name;
     }
+    setDeprecatedKey(getKey());
     setEffectiveKey(getKey());
+    setDeprecatedEffectiveKey(getKey());
     this.branch = branch;
   }
 
@@ -178,7 +182,7 @@ public class Project extends Resource implements Component {
   }
 
   public Project getRoot() {
-    return parent==null ? this : parent.getRoot();
+    return parent == null ? this : parent.getRoot();
   }
 
   /**
@@ -321,7 +325,7 @@ public class Project extends Resource implements Component {
    */
   @Deprecated
   public boolean getReuseExistingRulesConfig() {
-    return configuration!=null && configuration.getBoolean(CoreProperties.REUSE_RULES_CONFIGURATION_PROPERTY, false);
+    return configuration != null && configuration.getBoolean(CoreProperties.REUSE_RULES_CONFIGURATION_PROPERTY, false);
   }
 
   /**
@@ -346,7 +350,7 @@ public class Project extends Resource implements Component {
    */
   @Deprecated
   public String[] getExclusionPatterns() {
-    return trimExclusions(ImmutableList.<String> builder()
+    return trimExclusions(ImmutableList.<String>builder()
       .add(configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY))
       .add(configuration.getStringArray(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY)).build());
   }
@@ -365,9 +369,9 @@ public class Project extends Resource implements Component {
       globalTestExclusions = new String[] {CoreProperties.GLOBAL_TEST_EXCLUSIONS_DEFAULT};
     }
 
-    return trimExclusions(ImmutableList.<String> builder()
-        .add(configuration.getStringArray(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY))
-        .add(globalTestExclusions).build());
+    return trimExclusions(ImmutableList.<String>builder()
+      .add(configuration.getStringArray(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY))
+      .add(globalTestExclusions).build());
   }
 
   // http://jira.codehaus.org/browse/SONAR-2261 - exclusion must be trimmed
@@ -466,10 +470,10 @@ public class Project extends Resource implements Component {
   @Override
   public String toString() {
     return new ToStringBuilder(this)
-        .append("id", getId())
-        .append("key", getKey())
-        .append("qualifier", getQualifier())
-        .toString();
+      .append("id", getId())
+      .append("key", getKey())
+      .append("qualifier", getQualifier())
+      .toString();
   }
 
   @Override