aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-03-20 22:46:03 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2012-03-20 22:47:41 +0100
commit2b43df61e8ab7ded55f565eb7f43528ece3d66f3 (patch)
treec45ac8c139e9ef2dc783fe5aafa70e1f4060550e /sonar-plugin-api
parent69515943c83b24c8dbefefe7420bab6d5de4c81f (diff)
downloadsonarqube-2b43df61e8ab7ded55f565eb7f43528ece3d66f3.tar.gz
sonarqube-2b43df61e8ab7ded55f565eb7f43528ece3d66f3.zip
SONAR-3344 Display metadata of SonarSource licenses
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java48
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/License.java123
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java15
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/LicenseTest.java125
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java19
5 files changed, 323 insertions, 7 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java b/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java
index 356e4c1a65c..aacf82cd1ce 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java
@@ -19,6 +19,52 @@
*/
package org.sonar.api;
+/**
+ * @since 2.15
+ */
public enum PropertyType {
- STRING, TEXT, PASSWORD, BOOLEAN, INTEGER, FLOAT, SINGLE_SELECT_LIST
+ /**
+ * Basic single line input field
+ */
+ STRING,
+
+ /**
+ * Multiple line text-area
+ */
+ TEXT,
+
+ /**
+ * Variation of {#STRING} with masked characters
+ */
+ PASSWORD,
+
+ /**
+ * True/False
+ */
+ BOOLEAN,
+
+ /**
+ * Integer value, positive or negative
+ */
+ INTEGER,
+
+ /**
+ * Floating point number
+ */
+ FLOAT,
+
+ /**
+ * Single select list with a list of options
+ */
+ SINGLE_SELECT_LIST,
+
+ /**
+ * Sonar Metric
+ */
+ METRIC,
+
+ /**
+ * SonarSource license
+ */
+ LICENSE
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java
new file mode 100644
index 00000000000..bfc0cab23df
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/License.java
@@ -0,0 +1,123 @@
+/*
+ * 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.config;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Maps;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.utils.DateUtils;
+
+import javax.annotation.Nullable;
+import java.io.StringReader;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * SonarSource license. This class aims to extract metadata but not to validate or - of course -
+ * to generate license
+ *
+ * @since 2.15
+ */
+public final class License {
+ private String product;
+ private String organization;
+ private String expirationDate;
+ private String type;
+ private String server;
+
+ private License(Map<String, String> properties) {
+ product = StringUtils.defaultString(properties.get("Product"), properties.get("Plugin"));
+ organization = StringUtils.defaultString(properties.get("Organisation"), properties.get("Name"));
+ expirationDate = StringUtils.defaultString(properties.get("Expiration"), properties.get("Expires"));
+ type = properties.get("Type");
+ server = properties.get("Server");
+ }
+
+ @Nullable
+ public String getProduct() {
+ return product;
+ }
+
+ @Nullable
+ public String getOrganization() {
+ return organization;
+ }
+
+ @Nullable
+ public String getExpirationDateAsString() {
+ return expirationDate;
+ }
+
+ @Nullable
+ public Date getExpirationDate() {
+ return DateUtils.parseDateQuietly(expirationDate);
+ }
+
+ public boolean isExpired() {
+ return isExpired(new Date());
+ }
+
+ @VisibleForTesting
+ boolean isExpired(Date now) {
+ Date date = getExpirationDate();
+ return date != null && !date.after(org.apache.commons.lang.time.DateUtils.truncate(now, Calendar.DATE));
+ }
+
+ @Nullable
+ public String getType() {
+ return type;
+ }
+
+ @Nullable
+ public String getServer() {
+ return server;
+ }
+
+ public static License readBase64(String base64) {
+ return readPlainText(new String(Base64.decodeBase64(base64.getBytes())));
+ }
+
+ @VisibleForTesting
+ static License readPlainText(String data) {
+ Map<String, String> props = Maps.newHashMap();
+ StringReader reader = new StringReader(data);
+ try {
+ List<String> lines = IOUtils.readLines(reader);
+ for (String line : lines) {
+ if (StringUtils.isNotBlank(line) && line.indexOf(':') > 0) {
+ String key = StringUtils.substringBefore(line, ":");
+ String value = StringUtils.substringAfter(line, ":");
+ props.put(StringUtils.trimToEmpty(key), StringUtils.trimToEmpty(value));
+ }
+ }
+
+ } catch (Exception e) {
+ // silently ignore
+
+ } finally {
+ IOUtils.closeQuietly(reader);
+ }
+ return new License(props);
+ }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
index ab4446eb01e..658d328422c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java
@@ -82,12 +82,17 @@ public final class PropertyDefinition {
}
private PropertyType fixType(String key, PropertyType type) {
- // Auto-detect passwords for old versions of plugins that
- // do not declare the type
- if (type==PropertyType.STRING && StringUtils.endsWith(key, ".password.secured")) {
- return PropertyType.PASSWORD;
+ // Auto-detect passwords and licenses for old versions of plugins that
+ // do not declare property types
+ PropertyType fix = type;
+ if (type == PropertyType.STRING) {
+ if (StringUtils.endsWith(key, ".password.secured")) {
+ fix = PropertyType.PASSWORD;
+ } else if (StringUtils.endsWith(key, ".license.secured")) {
+ fix = PropertyType.LICENSE;
+ }
}
- return type;
+ return fix;
}
@VisibleForTesting
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/LicenseTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/LicenseTest.java
new file mode 100644
index 00000000000..37e880e21fd
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/LicenseTest.java
@@ -0,0 +1,125 @@
+/*
+ * 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.config;
+
+import org.apache.commons.codec.binary.Base64;
+import org.hamcrest.core.Is;
+import org.junit.Test;
+import org.sonar.api.utils.DateUtils;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class LicenseTest {
+
+ private static final String V2_FORMAT = "Foo: bar\n" +
+ "Organisation: ABC \n" +
+ "Server: 12345 \n" +
+ "Product: SQALE\n" +
+ " Expiration: 2012-05-18 \n" +
+ "Type: EVALUATION \n" +
+ "Other: field\n";
+
+ private static final String V1_FORMAT = "Foo: bar\n" +
+ "Name: ABC \n" +
+ "Plugin: SQALE\n" +
+ " Expires: 2012-05-18 \n" +
+ "Other: field\n";
+
+ @Test
+ public void readPlainTest() {
+ License license = License.readPlainText(V2_FORMAT);
+
+ assertThat(license.getOrganization(), Is.is("ABC"));
+ assertThat(license.getServer(), Is.is("12345"));
+ assertThat(license.getProduct(), Is.is("SQALE"));
+ assertThat(license.getExpirationDateAsString(), Is.is("2012-05-18"));
+ assertThat(license.getType(), Is.is("EVALUATION"));
+ }
+
+ @Test
+ public void readPlainText_empty_fields() {
+ License license = License.readPlainText("");
+
+ assertThat(license.getOrganization(), nullValue());
+ assertThat(license.getServer(), nullValue());
+ assertThat(license.getProduct(), nullValue());
+ assertThat(license.getExpirationDateAsString(), nullValue());
+ assertThat(license.getExpirationDate(), nullValue());
+ assertThat(license.getType(), nullValue());
+ }
+
+ @Test
+ public void readPlainText_not_valid_input() {
+ License license = License.readPlainText("old pond ... a frog leaps in water’s sound");
+
+ assertThat(license.getOrganization(), nullValue());
+ assertThat(license.getServer(), nullValue());
+ assertThat(license.getProduct(), nullValue());
+ assertThat(license.getExpirationDateAsString(), nullValue());
+ assertThat(license.getExpirationDate(), nullValue());
+ assertThat(license.getType(), nullValue());
+ }
+
+ @Test
+ public void readPlainTest_version_1() {
+ License license = License.readPlainText(V1_FORMAT);
+
+ assertThat(license.getOrganization(), Is.is("ABC"));
+ assertThat(license.getServer(), nullValue());
+ assertThat(license.getProduct(), Is.is("SQALE"));
+ assertThat(license.getExpirationDateAsString(), Is.is("2012-05-18"));
+ assertThat(license.getType(), nullValue());
+ }
+
+ @Test
+ public void readBase64() {
+ License license = License.readBase64(new String(Base64.encodeBase64(V2_FORMAT.getBytes())));
+
+ assertThat(license.getOrganization(), Is.is("ABC"));
+ assertThat(license.getServer(), Is.is("12345"));
+ assertThat(license.getProduct(), Is.is("SQALE"));
+ assertThat(license.getExpirationDateAsString(), Is.is("2012-05-18"));
+ assertThat(license.getType(), Is.is("EVALUATION"));
+ }
+
+ @Test
+ public void readBase64_not_base64() {
+ License license = License.readBase64("çé '123$@");
+
+ assertThat(license.getOrganization(), nullValue());
+ assertThat(license.getServer(), nullValue());
+ assertThat(license.getProduct(), nullValue());
+ assertThat(license.getExpirationDateAsString(), nullValue());
+ assertThat(license.getExpirationDate(), nullValue());
+ assertThat(license.getType(), nullValue());
+ }
+
+ @Test
+ public void isExpired() {
+ License license = License.readPlainText(V2_FORMAT);
+
+ assertThat(license.isExpired(DateUtils.parseDate("2013-06-23")), is(true));
+ assertThat(license.isExpired(DateUtils.parseDate("2012-05-18")), is(true));
+ assertThat(license.isExpired(DateUtils.parseDateTime("2012-05-18T15:50:45+0100")), is(true));
+ assertThat(license.isExpired(DateUtils.parseDate("2011-01-01")), is(false));
+ }
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
index 427698793d4..00557bdc231 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
@@ -158,7 +158,7 @@ public class PropertyDefinitionTest {
}
@Test
- public void autodetectPasswordType() {
+ public void autoDetectPasswordType() {
Properties props = AnnotationUtils.getClassAnnotation(OldScmPlugin.class, Properties.class);
Property prop = props.value()[0];
@@ -167,4 +167,21 @@ public class PropertyDefinitionTest {
assertThat(def.getKey(), Is.is("scm.password.secured"));
assertThat(def.getType(), Is.is(PropertyType.PASSWORD));
}
+
+ @Properties({
+ @Property(key = "views.license.secured", name = "Views license")
+ })
+ static class ViewsPlugin {
+ }
+
+ @Test
+ public void autoDetectLicenseType() {
+ Properties props = AnnotationUtils.getClassAnnotation(ViewsPlugin.class, Properties.class);
+ Property prop = props.value()[0];
+
+ PropertyDefinition def = PropertyDefinition.create(prop);
+
+ assertThat(def.getKey(), Is.is("views.license.secured"));
+ assertThat(def.getType(), Is.is(PropertyType.LICENSE));
+ }
}