From 2870e3114a698d24f17a554281b8696f52da5978 Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 20 May 2013 13:49:13 +0200 Subject: [PATCH] SONAR-4340 Display additional properties on licenses --- .../java/org/sonar/api/config/License.java | 28 ++++- .../org/sonar/api/config/LicenseTest.java | 100 ++++++++++-------- .../app/views/settings/_type_LICENSE.html.erb | 6 ++ 3 files changed, 82 insertions(+), 52 deletions(-) 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 index 8c23b1a641e..12d22ab6af1 100644 --- 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 @@ -27,6 +27,7 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.DateUtils; import javax.annotation.Nullable; + import java.io.IOException; import java.io.StringReader; import java.util.Calendar; @@ -46,13 +47,30 @@ public final class License { private String expirationDate; private String type; private String server; + private Map additionalProperties; private License(Map 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"); + this.additionalProperties = Maps.newHashMap(properties); + product = StringUtils.defaultString(get("Product", properties), get("Plugin", properties)); + organization = StringUtils.defaultString(get("Organisation", properties), get("Name", properties)); + expirationDate = StringUtils.defaultString(get("Expiration", properties), get("Expires", properties)); + type = get("Type", properties); + server = get("Server", properties); + // SONAR-4340 Don't expose Digest property + additionalProperties.remove("Digest"); + } + + private String get(String key, Map properties) { + additionalProperties.remove(key); + return properties.get(key); + } + + /** + * Get additional properties available on this license (like threshold conditions) + * @since 3.6 + */ + public Map additionalProperties() { + return additionalProperties; } @Nullable 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 index f71e3199636..a2125a7e4c9 100644 --- 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 @@ -20,13 +20,10 @@ 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; +import static org.fest.assertions.Assertions.assertThat; public class LicenseTest { @@ -42,63 +39,64 @@ public class LicenseTest { "Name: ABC \n" + "Plugin: SQALE\n" + " Expires: 2012-05-18 \n" + - "Other: field\n"; + "Other: field\n" + + "Digest: abcdef\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")); + assertThat(license.getOrganization()).isEqualTo("ABC"); + assertThat(license.getServer()).isEqualTo("12345"); + assertThat(license.getProduct()).isEqualTo("SQALE"); + assertThat(license.getExpirationDateAsString()).isEqualTo("2012-05-18"); + assertThat(license.getType()).isEqualTo("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()); + assertThat(license.getOrganization()).isNull(); + assertThat(license.getServer()).isNull(); + assertThat(license.getProduct()).isNull(); + assertThat(license.getExpirationDateAsString()).isNull(); + assertThat(license.getExpirationDate()).isNull(); + assertThat(license.getType()).isNull(); } @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()); + assertThat(license.getOrganization()).isNull(); + assertThat(license.getServer()).isNull(); + assertThat(license.getProduct()).isNull(); + assertThat(license.getExpirationDateAsString()).isNull(); + assertThat(license.getExpirationDate()).isNull(); + assertThat(license.getType()).isNull(); } @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()); + assertThat(license.getOrganization()).isEqualTo("ABC"); + assertThat(license.getServer()).isNull(); + assertThat(license.getProduct()).isEqualTo("SQALE"); + assertThat(license.getExpirationDateAsString()).isEqualTo("2012-05-18"); + assertThat(license.getType()).isNull(); } @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")); + assertThat(license.getOrganization()).isEqualTo("ABC"); + assertThat(license.getServer()).isEqualTo("12345"); + assertThat(license.getProduct()).isEqualTo("SQALE"); + assertThat(license.getExpirationDateAsString()).isEqualTo("2012-05-18"); + assertThat(license.getType()).isEqualTo("EVALUATION"); } @Test @@ -109,32 +107,40 @@ public class LicenseTest { License license = License.readBase64(new String(encodedKeyWithTrailingWhiteSpaces.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")); + assertThat(license.getOrganization()).isEqualTo("ABC"); + assertThat(license.getServer()).isEqualTo("12345"); + assertThat(license.getProduct()).isEqualTo("SQALE"); + assertThat(license.getExpirationDateAsString()).isEqualTo("2012-05-18"); + assertThat(license.getType()).isEqualTo("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()); + assertThat(license.getOrganization()).isNull(); + assertThat(license.getServer()).isNull(); + assertThat(license.getProduct()).isNull(); + assertThat(license.getExpirationDateAsString()).isNull(); + assertThat(license.getExpirationDate()).isNull(); + assertThat(license.getType()).isNull(); } @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)); + assertThat(license.isExpired(DateUtils.parseDate("2013-06-23"))).isTrue(); + assertThat(license.isExpired(DateUtils.parseDate("2012-05-18"))).isTrue(); + assertThat(license.isExpired(DateUtils.parseDateTime("2012-05-18T15:50:45+0100"))).isTrue(); + assertThat(license.isExpired(DateUtils.parseDate("2011-01-01"))).isFalse(); + } + + @Test + public void otherProperties() { + License license = License.readPlainText(V2_FORMAT); + + assertThat(license.additionalProperties().get("Other")).isEqualTo("field"); + assertThat(license.additionalProperties().containsKey("Digest")).isFalse(); } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb index fb0db83149b..1998850a446 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb @@ -38,6 +38,12 @@ Server: <%= license.getServer() || '-' -%> + <% license.additionalProperties().each do |k,v| -%> + + <%= k -%>: + <%= v || '-' -%> + + <% end %> -- 2.39.5