]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4340 Display additional properties on licenses
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 20 May 2013 11:49:13 +0000 (13:49 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 20 May 2013 12:47:40 +0000 (14:47 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/config/License.java
sonar-plugin-api/src/test/java/org/sonar/api/config/LicenseTest.java
sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb

index 8c23b1a641ed67a6d1d41cdb72a5ad0682ce9a24..12d22ab6af1f797dac0cc8e1eecf790be8e37908 100644 (file)
@@ -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<String, String> additionalProperties;
 
   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");
+    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<String, String> properties) {
+    additionalProperties.remove(key);
+    return properties.get(key);
+  }
+
+  /**
+   * Get additional properties available on this license (like threshold conditions)
+   * @since 3.6
+   */
+  public Map<String, String> additionalProperties() {
+    return additionalProperties;
   }
 
   @Nullable
index f71e31996368fa71ecb6cf643b9f0d2649ee2c80..a2125a7e4c9120b09a8ffd6f4481cc6e428a361b 100644 (file)
 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();
   }
 }
index fb0db83149b4f87c8cc95c93d995904e4cc5486d..1998850a44682a981b9c32c4b8c15b12651108a9 100644 (file)
           <td class="form-key-cell">Server:</td>
           <td><%= license.getServer() || '-' -%></td>
         </tr>
+        <% license.additionalProperties().each do |k,v| -%>
+        <tr>
+          <td class="form-key-cell"><%= k -%>:</td>
+          <td><%= v || '-' -%></td>
+        </tr>
+        <% end %>
       </table>
     </div>
   </div>