]> source.dussan.org Git - sonarqube.git/commitdiff
Override distance_of_time_in_words_to_now in order to have consistent age dates betwe...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 30 Jan 2014 09:34:38 +0000 (10:34 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 30 Jan 2014 09:34:47 +0000 (10:34 +0100)
sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java
sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb
sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb
sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java

index e7f5e260d0993c43e3fd6b69c3fe72d0c44ad4c1..fc5b66bcb42ad81378e3f455dacd46491f435aa5 100644 (file)
@@ -24,9 +24,11 @@ import org.apache.commons.lang.StringUtils;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.i18n.I18n;
 import org.sonar.core.i18n.GwtI18n;
+import org.sonar.server.user.UserSession;
 
 import javax.annotation.Nullable;
 
+import java.util.Date;
 import java.util.Locale;
 import java.util.Map;
 
@@ -79,4 +81,9 @@ public class JRubyI18n implements ServerComponent {
   public String getJsDictionnary(String rubyLocale) {
     return gwtI18n.getJsDictionnary(toLocale(rubyLocale));
   }
+
+  public String ageFromNow(Date date) {
+    return i18n.ageFromNow(UserSession.get().locale(), date);
+  }
+
 }
index 19ab206e7c17e5b8fde0cb661c7722abda08944d..c45644d988a9a72862db1ee11d38d600657373b7 100644 (file)
@@ -73,6 +73,13 @@ module ApplicationHelper
     end
   end
 
+  # Override date_helper methods to be consistent with Java age format
+  # java.util.Date and Time are supported
+  #
+  def distance_of_time_in_words_to_now(date)
+    Internal.i18n.ageFromNow(date)
+  end
+
   def sonar_version
     Java::OrgSonarServerPlatform::Platform.getServer().getVersion()
   end
index bb01e8f8b972521068caa5e2e29371061774b437..98792e1daee563f3050718428073263a3660461f 100644 (file)
@@ -89,8 +89,11 @@ class Api::Utils
     java_date && Time.at(java_date.time/1000)
   end
 
-  def self.age_from_now(java_date)
-    ActionController::Base.helpers.time_ago_in_words(java_to_ruby_datetime(java_date))
+  # Format date to age format
+  #
+  # Added in 4.2
+  def self.age_from_now(date)
+    Internal.i18n.ageFromNow(date)
   end
 
   def self.is_number?(s)
index 2d6162c2cc130f428de96a9a279762b124402987..4c56be7800dab8a0c4303796cba11b4fded0ecb9 100644 (file)
@@ -86,6 +86,10 @@ class Internal
     component(Java::OrgSonarServerRule::RuleTags.java_class)
   end
 
+  def self.i18n
+    component(Java::OrgSonarServerUi::JRubyI18n.java_class)
+  end
+
   private
 
   def self.component(component_java_class)
index 094490dcdd3b0fde1a3f2091758355a93c7c12d5..031be00e869437cc6d159cf1900d12fee44a0988 100644 (file)
  */
 package org.sonar.server.ui;
 
+import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.i18n.I18n;
 import org.sonar.core.i18n.GwtI18n;
 
+import java.util.Date;
 import java.util.Locale;
 
 import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.verify;
 
+@RunWith(MockitoJUnitRunner.class)
 public class JRubyI18nTest {
+
+  @Mock
+  I18n i18n;
+
+  @Mock
+  GwtI18n gwtI18n;
+
+  JRubyI18n jRubyI18n;
+
+
+  @Before
+  public void setUp() throws Exception {
+    jRubyI18n = new JRubyI18n(i18n, gwtI18n);
+  }
+
   @Test
-  public void shouldConvertLocales() {
+  public void convert_locales() {
     assertThat(JRubyI18n.toLocale("fr")).isEqualTo(Locale.FRENCH);
     assertThat(JRubyI18n.toLocale("fr-CH")).isEqualTo(new Locale("fr", "CH"));
   }
 
   @Test
-  public void shouldCacheLocales() {
-    JRubyI18n i18n = new JRubyI18n(mock(I18n.class), mock(GwtI18n.class));
-    assertThat(i18n.getLocalesByRubyKey()).isEmpty();
+  public void cache_locales() {
+    assertThat(jRubyI18n.getLocalesByRubyKey()).isEmpty();
 
-    i18n.getLocale("fr");
+    jRubyI18n.getLocale("fr");
 
-    assertThat(i18n.getLocalesByRubyKey()).hasSize(1);
-    assertThat(i18n.getLocalesByRubyKey().get("fr")).isNotNull();
+    assertThat(jRubyI18n.getLocalesByRubyKey()).hasSize(1);
+    assertThat(jRubyI18n.getLocalesByRubyKey().get("fr")).isNotNull();
   }
 
   @Test
   public void default_locale_should_be_english() throws Exception {
     assertThat(JRubyI18n.toLocale(null)).isEqualTo(Locale.ENGLISH);
+  }
 
+  @Test
+  public void message() throws Exception {
+    jRubyI18n.message("en", "my.key", "default");
+    verify(i18n).message(any(Locale.class), eq("my.key"), eq("default"));
+  }
+
+  @Test
+  public void get_js_dictionnary() throws Exception {
+    jRubyI18n.getJsDictionnary("en");
+    verify(gwtI18n).getJsDictionnary(any(Locale.class));
   }
+
+  @Test
+  public void age_from_now() throws Exception {
+    Date date = new Date();
+    jRubyI18n.ageFromNow(new Date());
+    verify(i18n).ageFromNow(any(Locale.class), eq(date));
+  }
+
 }