aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2011-01-24 15:36:32 +0100
committersimonbrandhof <simon.brandhof@gmail.com>2011-01-24 19:25:00 +0100
commit1abf95bda06ebdbdd28f795dcf6bb997f1042e11 (patch)
tree9f5a99046e82404950371879e0b93c95bd6af80a /sonar-plugin-api
parenta4266a6a06a25d65f9961965286b1e5c1cf447ac (diff)
downloadsonarqube-1abf95bda06ebdbdd28f795dcf6bb997f1042e11.tar.gz
sonarqube-1abf95bda06ebdbdd28f795dcf6bb997f1042e11.zip
Improve org.sonar.api.resources.Scopes and Qualifiers
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Qualifiers.java86
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java105
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceUtils.java2
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java60
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java131
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java70
7 files changed, 408 insertions, 48 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java
index c2ece8bbb35..ef940898ced 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/JavaFile.java
@@ -158,7 +158,7 @@ public class JavaFile extends Resource<JavaPackage> {
* @return QUALIFIER_UNIT_TEST_CLASS or QUALIFIER_CLASS depending whether it is a unit test class
*/
public String getQualifier() {
- return unitTest ? Resource.QUALIFIER_UNIT_TEST_CLASS : Resource.QUALIFIER_CLASS;
+ return unitTest ? Qualifiers.UNIT_TEST_FILE : Resource.QUALIFIER_CLASS;
}
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Qualifiers.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Qualifiers.java
index bb1c77e99ac..cc3fca65827 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Qualifiers.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Qualifiers.java
@@ -19,42 +19,86 @@
*/
package org.sonar.api.resources;
+import org.apache.commons.lang.StringUtils;
+
/**
* The qualifier determines the exact type of a resource.
- * Plugins can use their own qualifiers.
+ * Plugins can define their own qualifiers.
+ *
* @since 2.6
*/
-public interface Qualifiers {
+public final class Qualifiers {
+
+ private Qualifiers() {
+ // only static methods
+ }
+
+ /**
+ * Root views. Scope of views is Scopes.PROJECT
+ */
+ public static final String VIEW = "VW";
- String VIEW = "VW";
- String SUBVIEW = "SVW";
+ /**
+ * Sub-views, defined in root views. Scope of sub-views is Scopes.PROJECT
+ */
+ public static final String SUBVIEW = "SVW";
/**
* Library, for example a JAR dependency of Java projects.
- * Scope is Scopes.PROJECT
+ * Scope of libraries is Scopes.PROJECT
*/
- String LIBRARY = "LIB";
+ public static final String LIBRARY = "LIB";
/**
* Single project or root of multi-modules projects
* Scope is Scopes.PROJECT
*/
- String PROJECT = "TRK";
+ public static final String PROJECT = "TRK";
/**
- * Module of multi-modules project. It's sometimes called sub-project.
- * Scope is Scopes.PROJECT
+ * Module of a multi-modules project. It's sometimes called "sub-project".
+ * Scope of modules is Scopes.PROJECT
*/
- String MODULE = "BRC";
-
-
- String PACKAGE = "PAC";
- String DIRECTORY = "DIR";
- String FILE = "FIL";
- String CLASS = "CLA";
- String METHOD = "MET";
- String FIELD = "FLD";
-
- // ugly, should be replaced by a nature
- String UNIT_TEST_CLASS = "UTS";
+ public static final String MODULE = "BRC";
+
+ public static final String PACKAGE = "PAC";
+ public static final String DIRECTORY = "DIR";
+ public static final String FILE = "FIL";
+ public static final String CLASS = "CLA";
+ public static final String PARAGRAPH = "PAR";
+ public static final String METHOD = "MET";
+ public static final String FIELD = "FLD";
+
+ // ugly, should be replaced by "natures"
+ public static final String UNIT_TEST_FILE = "UTS";
+
+ public static boolean isView(final Resource resource, final boolean acceptSubViews) {
+ boolean isView = false;
+ if (resource != null) {
+ isView = StringUtils.equals(VIEW, resource.getQualifier());
+ if (!isView && acceptSubViews) {
+ isView = StringUtils.equals(SUBVIEW, resource.getQualifier());
+ }
+ }
+ return isView;
+ }
+
+ public static boolean isSubview(final Resource resource) {
+ return resource != null && StringUtils.equals(SUBVIEW, resource.getScope());
+ }
+
+ public static boolean isProject(final Resource resource, final boolean acceptModules) {
+ boolean isProject = false;
+ if (resource != null) {
+ isProject = StringUtils.equals(PROJECT, resource.getQualifier());
+ if (!isProject && acceptModules) {
+ isProject = StringUtils.equals(MODULE, resource.getQualifier());
+ }
+ }
+ return isProject;
+ }
+
+ public static boolean isModule(final Resource resource) {
+ return resource != null && StringUtils.equals(MODULE, resource.getQualifier());
+ }
} \ No newline at end of file
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java
index ab1db1848c3..68d06907897 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Resource.java
@@ -26,22 +26,95 @@ package org.sonar.api.resources;
*/
public abstract class Resource<PARENT extends Resource> {
- public static final String SCOPE_SET = "PRJ";
- public static final String SCOPE_SPACE = "DIR";
- public static final String SCOPE_ENTITY = "FIL";
-
- public static final String QUALIFIER_VIEW = "VW";
- public static final String QUALIFIER_SUBVIEW = "SVW";
- public static final String QUALIFIER_LIB = "LIB";
- public static final String QUALIFIER_PROJECT = "TRK";
- public static final String QUALIFIER_MODULE = "BRC";
- public static final String QUALIFIER_PACKAGE = "PAC";
- public static final String QUALIFIER_DIRECTORY = "DIR";
- public static final String QUALIFIER_FILE = "FIL";
- public static final String QUALIFIER_CLASS = "CLA";
- public static final String QUALIFIER_FIELD = "FLD";
- public static final String QUALIFIER_METHOD = "MET";
- public static final String QUALIFIER_UNIT_TEST_CLASS = "UTS";
+ /**
+ * @deprecated since 2.6. Use Scopes.PROJECT.
+ */
+ @Deprecated
+ public static final String SCOPE_SET = Scopes.PROJECT;
+
+ /**
+ * @deprecated since 2.6. Use Scopes.DIRECTORY.
+ */
+ @Deprecated
+ public static final String SCOPE_SPACE = Scopes.DIRECTORY;
+
+ /**
+ * @deprecated since 2.6. Use Scopes.FILE.
+ */
+ @Deprecated
+ public static final String SCOPE_ENTITY = Scopes.FILE;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.VIEW.
+ */
+ @Deprecated
+ public static final String QUALIFIER_VIEW = Qualifiers.VIEW;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.SUBVIEW.
+ */
+ @Deprecated
+ public static final String QUALIFIER_SUBVIEW = Qualifiers.SUBVIEW;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.LIBRARY.
+ */
+ @Deprecated
+ public static final String QUALIFIER_LIB = Qualifiers.LIBRARY;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.PROJECT.
+ */
+ @Deprecated
+ public static final String QUALIFIER_PROJECT = Qualifiers.PROJECT;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.MODULE.
+ */
+ @Deprecated
+ public static final String QUALIFIER_MODULE = Qualifiers.MODULE;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.PACKAGE.
+ */
+ @Deprecated
+ public static final String QUALIFIER_PACKAGE = Qualifiers.PACKAGE;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.DIRECTORY.
+ */
+ @Deprecated
+ public static final String QUALIFIER_DIRECTORY = Qualifiers.DIRECTORY;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.FILE.
+ */
+ @Deprecated
+ public static final String QUALIFIER_FILE = Qualifiers.FILE;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.CLASS.
+ */
+ @Deprecated
+ public static final String QUALIFIER_CLASS = Qualifiers.CLASS;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.FIELD.
+ */
+ @Deprecated
+ public static final String QUALIFIER_FIELD = Qualifiers.FIELD;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.METHOD.
+ */
+ @Deprecated
+ public static final String QUALIFIER_METHOD = Qualifiers.METHOD;
+
+ /**
+ * @deprecated since 2.6. Use Qualifiers.UNIT_TEST_FILE.
+ */
+ @Deprecated
+ public static final String QUALIFIER_UNIT_TEST_CLASS = Qualifiers.UNIT_TEST_FILE;
private Integer id = null;
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceUtils.java
index e243e8b044a..71d92857f1a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceUtils.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceUtils.java
@@ -117,7 +117,7 @@ public final class ResourceUtils {
* @return whether a resource is a unit test class
*/
public static boolean isUnitTestClass(Resource resource) {
- return Resource.QUALIFIER_UNIT_TEST_CLASS.equals(resource.getQualifier());
+ return Qualifiers.UNIT_TEST_FILE.equals(resource.getQualifier());
}
/**
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
index 001fbd96e5e..d9fdf46fa97 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Scopes.java
@@ -19,26 +19,68 @@
*/
package org.sonar.api.resources;
+import org.apache.commons.lang.StringUtils;
+
/**
- * Resource scopes are used to group resources. They relate to persisted resources only (project, directories and files).
- * They are generally used in UI to display/hide some services or in web services.
+ * Resource scopes are used to group some types of resources. For example Java methods, Flex methods, C functions
+ * and Cobol paragraphs are grouped in the scope "block unit".
+ *
+ * Scopes are generally used in UI to display/hide some services or in web services.
+ *
+ * Scopes are not extensible by plugins.
*
- * Resource scopes are not extensible by plugins.
+ * @since 2.6
*/
-public interface Scopes {
+public final class Scopes {
+
+ private Scopes() {
+ // only static methods
+ }
/**
* For example view, subview, project, module or library. Persisted in database.
*/
- String PROJECT = "PRJ";
+ public static final String PROJECT = "PRJ";
+
+ /**
+ * For example directory or Java package. Persisted in database. A more generic term for this scope could
+ * be "namespace"
+ */
+ public static final String DIRECTORY = "DIR";
+
+ /**
+ * For example a Java file. Persisted in database. A more generic term for this scope could
+ * be "compilation unit". It's the lowest scope in file system units.
+ */
+ public static final String FILE = "FIL";
/**
- * For example directory or Java package. Persisted in database.
+ * Types like Java classes/interfaces. Not persisted in database.
*/
- String DIRECTORY = "DIR";
+ public static final String TYPE = "TYP";
/**
- * For example a Java file. Persisted in database.
+ * Block units like methods, functions or Cobol paragraphs.
*/
- String FILE = "FIL";
+ public static final String BLOCK_UNIT = "BLU";
+
+
+ public static boolean isProject(final Resource resource) {
+ return resource!=null && StringUtils.equals(PROJECT, resource.getScope());
+ }
+
+ public static boolean isDirectory(final Resource resource) {
+ return resource!=null && StringUtils.equals(DIRECTORY, resource.getScope());
+ }
+
+ public static boolean isFile(final Resource resource) {
+ return resource!=null && StringUtils.equals(FILE, resource.getScope());
+ }
+
+ public static boolean isType(final Resource resource) {
+ return resource!=null && StringUtils.equals(TYPE, resource.getScope());
+ }
+ public static boolean isBlockUnit(final Resource resource) {
+ return resource!=null && StringUtils.equals(BLOCK_UNIT, resource.getScope());
+ }
} \ No newline at end of file
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java
new file mode 100644
index 00000000000..a0a58d7f6b6
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/QualifiersTest.java
@@ -0,0 +1,131 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.resources;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class QualifiersTest {
+
+ @Test
+ public void testNullValues() {
+ assertThat(Qualifiers.isView(null, true), is(false));
+ assertThat(Qualifiers.isView(null, false), is(false));
+ assertThat(Qualifiers.isProject(null, true), is(false));
+ assertThat(Qualifiers.isProject(null, false), is(false));
+ }
+
+ @Test
+ public void testRootView() {
+ View root = View.createRootView();
+ assertThat(Qualifiers.isView(root, true), is(true));
+ assertThat(Qualifiers.isView(root, false), is(true));
+ assertThat(Qualifiers.isProject(root, true), is(false));
+ assertThat(Qualifiers.isProject(root, false), is(false));
+ }
+
+ @Test
+ public void testSubView() {
+ View subview = View.createSubView();
+ assertThat(Qualifiers.isView(subview, true), is(true));
+ assertThat(Qualifiers.isView(subview, false), is(false));
+ assertThat(Qualifiers.isProject(subview, true), is(false));
+ assertThat(Qualifiers.isProject(subview, false), is(false));
+ }
+
+ @Test
+ public void testProject() {
+ Resource root = new Project("foo");
+ assertThat(Qualifiers.isView(root, true), is(false));
+ assertThat(Qualifiers.isView(root, false), is(false));
+ assertThat(Qualifiers.isProject(root, true), is(true));
+ assertThat(Qualifiers.isProject(root, false), is(true));
+ }
+
+ @Test
+ public void testModule() {
+ Resource sub = new Project("sub").setParent(new Project("root"));
+ assertThat(Qualifiers.isView(sub, true), is(false));
+ assertThat(Qualifiers.isView(sub, false), is(false));
+ assertThat(Qualifiers.isProject(sub, true), is(true));
+ assertThat(Qualifiers.isProject(sub, false), is(false));
+ }
+
+ private static class View extends Resource {
+
+ private String qualifier;
+
+ private View(String qualifier) {
+ this.qualifier = qualifier;
+ }
+
+ static View createRootView() {
+ return new View(Qualifiers.VIEW);
+ }
+
+ static View createSubView() {
+ return new View(Qualifiers.SUBVIEW);
+ }
+
+ @Override
+ public String getName() {
+ return null;
+ }
+
+ @Override
+ public String getLongName() {
+ return null;
+ }
+
+ @Override
+ public String getDescription() {
+ return null;
+ }
+
+ @Override
+ public Language getLanguage() {
+ return null;
+ }
+
+ @Override
+ public String getScope() {
+ return Scopes.PROJECT;
+ }
+
+ @Override
+ public String getQualifier() {
+ return qualifier;
+ }
+
+ @Override
+ public Resource getParent() {
+ return null;
+ }
+
+ @Override
+ public boolean matchFilePattern(String antPattern) {
+ return false;
+ }
+ }
+}
+
+
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java
new file mode 100644
index 00000000000..66f5b7eec3a
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ScopesTest.java
@@ -0,0 +1,70 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.resources;
+
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class ScopesTest {
+
+ @Test
+ public void testProject() {
+ Project resource = new Project("key");
+ assertThat(Scopes.isProject(resource), is(true));
+ assertThat(Scopes.isDirectory(resource), is(false));
+ assertThat(Scopes.isFile(resource), is(false));
+ assertThat(Scopes.isBlockUnit(resource), is(false));
+ assertThat(Scopes.isType(resource), is(false));
+ }
+
+ @Test
+ public void testLibrary() {
+ Resource resource = new Library("key", "1.0");
+ assertThat(Scopes.isProject(resource), is(true));
+ assertThat(Scopes.isDirectory(resource), is(false));
+ assertThat(Scopes.isFile(resource), is(false));
+ assertThat(Scopes.isBlockUnit(resource), is(false));
+ assertThat(Scopes.isType(resource), is(false));
+ }
+
+ @Test
+ public void testDirectory() {
+ Resource resource = new Directory("org/foo");
+ assertThat(Scopes.isProject(resource), is(false));
+ assertThat(Scopes.isDirectory(resource), is(true));
+ assertThat(Scopes.isFile(resource), is(false));
+ assertThat(Scopes.isBlockUnit(resource), is(false));
+ assertThat(Scopes.isType(resource), is(false));
+ }
+
+ @Test
+ public void testFile() {
+ Resource resource = new File("org/foo/Bar.java");
+ assertThat(Scopes.isProject(resource), is(false));
+ assertThat(Scopes.isDirectory(resource), is(false));
+ assertThat(Scopes.isFile(resource), is(true));
+ assertThat(Scopes.isBlockUnit(resource), is(false));
+ assertThat(Scopes.isType(resource), is(false));
+ }
+
+
+}