From f0d910eafbf09897f26be35af116b1369b2027d8 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 30 Jan 2014 10:34:38 +0100 Subject: [PATCH] Override distance_of_time_in_words_to_now in order to have consistent age dates between rails and java --- .../java/org/sonar/server/ui/JRubyI18n.java | 7 +++ .../WEB-INF/app/helpers/application_helper.rb | 7 +++ .../webapp/WEB-INF/app/models/api/utils.rb | 7 ++- .../webapp/WEB-INF/app/models/internal.rb | 4 ++ .../org/sonar/server/ui/JRubyI18nTest.java | 57 ++++++++++++++++--- 5 files changed, 72 insertions(+), 10 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java b/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java index e7f5e260d09..fc5b66bcb42 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/JRubyI18n.java @@ -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); + } + } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 19ab206e7c1..c45644d988a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -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 diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb index bb01e8f8b97..98792e1daee 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb @@ -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) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb index 2d6162c2cc1..4c56be7800d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb @@ -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) diff --git a/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java b/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java index 094490dcdd3..031be00e869 100644 --- a/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java +++ b/sonar-server/src/test/java/org/sonar/server/ui/JRubyI18nTest.java @@ -19,36 +19,77 @@ */ 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)); + } + } -- 2.39.5