diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-12-05 12:13:48 +0100 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-12-05 12:43:20 +0100 |
commit | 483908be86265d48c16f67999530cec2b013c068 (patch) | |
tree | 2a4d02a290a10b55fd73103c0b35f6931875281a /sonar-plugin-api | |
parent | 1dcb87b93525d1b838a23212626ba1c7ffbaec81 (diff) | |
download | sonarqube-483908be86265d48c16f67999530cec2b013c068.tar.gz sonarqube-483908be86265d48c16f67999530cec2b013c068.zip |
SONAR-3862 Provide a link to all "Projects", "Views", "Developers", ...
=> Actually, a link to all "root" resources
Diffstat (limited to 'sonar-plugin-api')
4 files changed, 60 insertions, 19 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java index 88fb6f72379..2fc904c4db1 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypeTree.java @@ -22,13 +22,20 @@ package org.sonar.api.resources; import com.google.common.annotations.Beta; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; -import com.google.common.collect.*; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableListMultimap; +import com.google.common.collect.ListMultimap; +import com.google.common.collect.Lists; import org.sonar.api.BatchExtension; import org.sonar.api.ServerExtension; import org.sonar.api.batch.InstantiationStrategy; import javax.annotation.concurrent.Immutable; + import java.util.Arrays; +import java.util.Collection; import java.util.List; /** @@ -41,10 +48,12 @@ public final class ResourceTypeTree implements BatchExtension, ServerExtension { private List<ResourceType> types; private ListMultimap<String, String> relations; + private ResourceType root; private ResourceTypeTree(Builder builder) { this.types = ImmutableList.copyOf(builder.types); this.relations = ImmutableListMultimap.copyOf(builder.relations); + this.root = builder.root; } public List<ResourceType> getTypes() { @@ -55,6 +64,10 @@ public final class ResourceTypeTree implements BatchExtension, ServerExtension { return relations.get(qualifier); } + public ResourceType getRootType() { + return root; + } + public List<String> getLeaves() { return ImmutableList.copyOf(Collections2.filter(relations.values(), new Predicate<String>() { public boolean apply(String qualifier) { @@ -70,6 +83,7 @@ public final class ResourceTypeTree implements BatchExtension, ServerExtension { public static final class Builder { private List<ResourceType> types = Lists.newArrayList(); private ListMultimap<String, String> relations = ArrayListMultimap.create(); + private ResourceType root; private Builder() { } @@ -90,6 +104,13 @@ public final class ResourceTypeTree implements BatchExtension, ServerExtension { } public ResourceTypeTree build() { + Collection<String> children = relations.values(); + for (ResourceType type : types) { + if (!children.contains(type)) { + root = type; + break; + } + } return new ResourceTypeTree(this); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java index 4834fa61848..e834fd6b40a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceTypes.java @@ -25,13 +25,16 @@ import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; import javax.annotation.Nullable; + import java.util.Collection; import java.util.Collections; import java.util.List; @@ -51,14 +54,17 @@ public final class ResourceTypes implements BatchComponent, ServerComponent { private final Map<String, ResourceTypeTree> treeByQualifier; private final Map<String, ResourceType> typeByQualifier; + private final Collection<ResourceType> rootTypes; public ResourceTypes(ResourceTypeTree[] trees) { Preconditions.checkNotNull(trees); Map<String, ResourceTypeTree> treeMap = Maps.newHashMap(); Map<String, ResourceType> typeMap = Maps.newLinkedHashMap(); + Collection<ResourceType> rootsSet = Sets.newHashSet(); for (ResourceTypeTree tree : trees) { + rootsSet.add(tree.getRootType()); for (ResourceType type : tree.getTypes()) { if (treeMap.containsKey(type.getQualifier())) { throw new IllegalStateException("Qualifier " + type.getQualifier() + " is defined in several trees"); @@ -69,6 +75,7 @@ public final class ResourceTypes implements BatchComponent, ServerComponent { } treeByQualifier = ImmutableMap.copyOf(treeMap); typeByQualifier = ImmutableMap.copyOf(typeMap); + rootTypes = ImmutableList.copyOf(rootsSet); } public ResourceType get(String qualifier) { @@ -80,6 +87,10 @@ public final class ResourceTypes implements BatchComponent, ServerComponent { return typeByQualifier.values(); } + public Collection<ResourceType> getRoots() { + return rootTypes; + } + public Collection<ResourceType> getAll(Predicate<ResourceType> predicate) { return Collections2.filter(typeByQualifier.values(), predicate); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypeTreeTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypeTreeTest.java index c8f73ec0596..5e64e9866ff 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypeTreeTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypeTreeTest.java @@ -37,7 +37,6 @@ public class ResourceTypeTreeTest { .addRelations("DIR", "UTS") .build(); - @Test public void getTypes() { assertThat(tree.getTypes().size(), is(4)); @@ -56,6 +55,11 @@ public class ResourceTypeTreeTest { } @Test + public void getRoot() { + assertThat(tree.getRootType(), is(ResourceType.builder("TRK").build())); + } + + @Test public void getLeaves() { assertThat(tree.getLeaves().size(), is(2)); assertThat(tree.getLeaves(), hasItems("FIL", "UTS")); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypesTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypesTest.java index 673a60a70b3..440bd1fd60b 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypesTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceTypesTest.java @@ -30,21 +30,21 @@ import static org.fest.assertions.Assertions.assertThat; public class ResourceTypesTest { private ResourceTypeTree viewsTree = ResourceTypeTree.builder() - .addType(ResourceType.builder(Qualifiers.VIEW).setProperty("supportsMeasureFilters", "true").build()) - .addType(ResourceType.builder(Qualifiers.SUBVIEW).build()) - .addRelations(Qualifiers.VIEW, Qualifiers.SUBVIEW) - .addRelations(Qualifiers.SUBVIEW, Qualifiers.PROJECT) - .build(); + .addType(ResourceType.builder(Qualifiers.VIEW).setProperty("supportsMeasureFilters", "true").build()) + .addType(ResourceType.builder(Qualifiers.SUBVIEW).build()) + .addRelations(Qualifiers.VIEW, Qualifiers.SUBVIEW) + .addRelations(Qualifiers.SUBVIEW, Qualifiers.PROJECT) + .build(); private ResourceTypeTree defaultTree = ResourceTypeTree.builder() - .addType(ResourceType.builder(Qualifiers.PROJECT).setProperty("supportsMeasureFilters", "true").build()) - .addType(ResourceType.builder(Qualifiers.DIRECTORY).build()) - .addType(ResourceType.builder(Qualifiers.FILE).build()) - .addRelations(Qualifiers.PROJECT, Qualifiers.DIRECTORY) - .addRelations(Qualifiers.DIRECTORY, Qualifiers.FILE) - .build(); + .addType(ResourceType.builder(Qualifiers.PROJECT).setProperty("supportsMeasureFilters", "true").build()) + .addType(ResourceType.builder(Qualifiers.DIRECTORY).build()) + .addType(ResourceType.builder(Qualifiers.FILE).build()) + .addRelations(Qualifiers.PROJECT, Qualifiers.DIRECTORY) + .addRelations(Qualifiers.DIRECTORY, Qualifiers.FILE) + .build(); - private ResourceTypes types = new ResourceTypes(new ResourceTypeTree[]{viewsTree, defaultTree}); + private ResourceTypes types = new ResourceTypes(new ResourceTypeTree[] {viewsTree, defaultTree}); @Test public void get() { @@ -60,6 +60,11 @@ public class ResourceTypesTest { } @Test + public void getRoots() { + assertThat(qualifiers(types.getRoots())).containsOnly(Qualifiers.PROJECT, Qualifiers.VIEW); + } + + @Test public void getAll_predicate() { Collection<ResourceType> forFilters = types.getAll(ResourceTypes.AVAILABLE_FOR_FILTERS); assertThat(qualifiers(forFilters)).containsOnly(Qualifiers.PROJECT, Qualifiers.VIEW).doesNotHaveDuplicates(); @@ -111,13 +116,13 @@ public class ResourceTypesTest { @Test(expected = IllegalStateException.class) public void failOnDuplicatedQualifier() { ResourceTypeTree tree1 = ResourceTypeTree.builder() - .addType(ResourceType.builder("foo").build()) - .build(); + .addType(ResourceType.builder("foo").build()) + .build(); ResourceTypeTree tree2 = ResourceTypeTree.builder() - .addType(ResourceType.builder("foo").build()) - .build(); + .addType(ResourceType.builder("foo").build()) + .build(); - new ResourceTypes(new ResourceTypeTree[]{tree1, tree2}); + new ResourceTypes(new ResourceTypeTree[] {tree1, tree2}); } static Collection<String> qualifiers(Collection<ResourceType> types) { |