From f4b8c3e47166faded5c49adb6c49e817f9bd2bdf Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Thu, 16 Jan 2014 10:41:35 +0100 Subject: [PATCH] SONAR-3024 Fix regression with Library resources --- .../org/sonar/batch/index/DefaultIndex.java | 6 ++--- .../batch/index/DefaultResourcePersister.java | 14 ++++------- .../sonar/core/component/ComponentKeys.java | 4 ++-- .../core/component/ComponentKeysTest.java | 8 +++---- .../java/org/sonar/api/resources/Library.java | 9 +++---- .../java/org/sonar/api/resources/Project.java | 24 +++++++++++-------- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java index c76b2498ae1..bb963fe1620 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java @@ -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 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()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java index 26799593177..5ef7e204d27 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/DefaultResourcePersister.java @@ -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); } diff --git a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java index 550bf27eee2..d8ba9b5c648 100644 --- a/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java +++ b/sonar-core/src/main/java/org/sonar/core/component/ComponentKeys.java @@ -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 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 e758668c70c..3f90af3496f 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 @@ -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"); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java index de4fb3bfa9d..bca7a01567b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Library.java @@ -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(); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java index 68c9b62e7fb..7c89db07838 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java @@ -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. builder() + return trimExclusions(ImmutableList.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. builder() - .add(configuration.getStringArray(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY)) - .add(globalTestExclusions).build()); + return trimExclusions(ImmutableList.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 -- 2.39.5