diff options
author | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-01-18 16:23:09 +0400 |
---|---|---|
committer | Evgeny Mandrikov <mandrikov@gmail.com> | 2012-01-18 16:46:18 +0400 |
commit | 6b2b041383ff5cded872faa0828219b81485fd4d (patch) | |
tree | 6da761a08a178457f199efe23b3282b063c6e083 /sonar-plugin-api | |
parent | cf0426185f6b1048e83656e9a43caebfc783058f (diff) | |
download | sonarqube-6b2b041383ff5cded872faa0828219b81485fd4d.tar.gz sonarqube-6b2b041383ff5cded872faa0828219b81485fd4d.zip |
SONAR-3179 Add API to define resources
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceDefinition.java | 97 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceDefinitionTest.java | 54 |
2 files changed, 151 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceDefinition.java new file mode 100644 index 00000000000..97d4e2a8316 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ResourceDefinition.java @@ -0,0 +1,97 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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 com.google.common.annotations.Beta; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import org.sonar.api.ServerExtension; + +/** + * @since 2.14 + */ +@Beta +public final class ResourceDefinition implements ServerExtension { + + public static class Builder { + private String qualifier; + private String name; + private String iconPath; + + public Builder(String qualifier) { + this.qualifier = qualifier; + } + + /** + * @param name name which would be used for localization + */ + public Builder setName(String name) { + this.name = name; + return this; + } + + /** + * @param iconPath path to icon, relative to context of web-application (e.g. "/images/q/DIR.png") + */ + public Builder setIconPath(String iconPath) { + this.iconPath = iconPath; + return this; + } + + public ResourceDefinition build() { + if (Strings.isNullOrEmpty(name)) { + name = qualifier; + } + if (Strings.isNullOrEmpty(iconPath)) { + iconPath = "/images/q/" + qualifier + ".png"; + } + return new ResourceDefinition(qualifier, name, iconPath); + } + } + + public static Builder builder(String qualifier) { + Preconditions.checkNotNull(qualifier); + Preconditions.checkArgument(qualifier.length() <= 10, "Qualifier is limited to 10 characters in database"); + return new Builder(qualifier); + } + + private final String qualifier; + private final String name; + private final String iconPath; + + private ResourceDefinition(String qualifier, String name, String iconPath) { + this.qualifier = qualifier; + this.name = name; + this.iconPath = iconPath; + } + + public String getQualifier() { + return qualifier; + } + + public String getName() { + return name; + } + + public String getIconPath() { + return iconPath; + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceDefinitionTest.java new file mode 100644 index 00000000000..852919406b1 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ResourceDefinitionTest.java @@ -0,0 +1,54 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * 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 ResourceDefinitionTest { + + @Test + public void shouldCreateWithDefaults() { + ResourceDefinition def = ResourceDefinition.builder("qualifier") + .build(); + assertThat(def.getQualifier(), is("qualifier")); + assertThat(def.getName(), is("qualifier")); + assertThat(def.getIconPath(), is("/images/q/qualifier.png")); + } + + @Test + public void shouldCreate() { + ResourceDefinition def = ResourceDefinition.builder("qualifier") + .setIconPath("/custom-icon.png") + .setName("custom-name") + .build(); + assertThat(def.getQualifier(), is("qualifier")); + assertThat(def.getName(), is("custom-name")); + assertThat(def.getIconPath(), is("/custom-icon.png")); + } + + @Test(expected = IllegalArgumentException.class) + public void shouldCheckQualifierLength() { + ResourceDefinition.builder("qualifier bigger than 10 characters"); + } + +} |