From a6a7f78c3ffeb191f02c002b3ef450a29e621ee9 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 17 May 2016 20:39:05 +0200 Subject: [PATCH] SONAR-7639 WS api/l10n/index supports only BCP47 language tags --- .../org/sonar/server/platform/ws/L10nWs.java | 14 +++--- .../sonar/server/platform/ws/L10nWsTest.java | 50 +++++++++++++++---- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/L10nWs.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/L10nWs.java index 87f8b0ecdb6..4bad445f2ed 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/L10nWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/L10nWs.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.net.HttpURLConnection; import java.util.Date; import java.util.Locale; -import org.apache.commons.lang.LocaleUtils; import org.sonar.api.platform.Server; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; @@ -33,6 +32,8 @@ import org.sonar.api.utils.text.JsonWriter; import org.sonar.core.i18n.DefaultI18n; import org.sonar.server.user.UserSession; +import static com.google.common.base.Preconditions.checkArgument; + public class L10nWs implements WebService { private final DefaultI18n i18n; @@ -65,13 +66,13 @@ public class L10nWs implements WebService { .setDescription("BCP47 language tag, used to override the browser Accept-Language header") .setExampleValue("fr-CH"); indexAction.createParam("ts") - .setDescription("Date of the last cache update.") - .setExampleValue("2014-06-04T09:31:42+0000"); + .setDescription("Date of the last cache update.") + .setExampleValue("2014-06-04T09:31:42+0000"); l10n.done(); } - protected void serializeMessages(Request request, Response response) throws IOException { + private void serializeMessages(Request request, Response response) throws IOException { Date timestamp = request.paramAsDateTime("ts"); if (timestamp != null && timestamp.after(server.getStartedAt())) { response.stream().setStatus(HttpURLConnection.HTTP_NOT_MODIFIED).output().close(); @@ -79,10 +80,11 @@ public class L10nWs implements WebService { Locale locale = userSession.locale(); String localeParam = request.param("locale"); if (localeParam != null) { - locale = LocaleUtils.toLocale(localeParam); + locale = Locale.forLanguageTag(localeParam); + checkArgument(!locale.getISO3Language().isEmpty(), "'%s' cannot be parsed as a BCP47 language tag", localeParam); } JsonWriter json = response.newJsonWriter().beginObject(); - for (String messageKey: i18n.getPropertyKeys()) { + for (String messageKey : i18n.getPropertyKeys()) { json.prop(messageKey, i18n.message(locale, messageKey, messageKey)); } json.endObject().close(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/L10nWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/L10nWsTest.java index 7f6ab292909..13e7c8d752a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/L10nWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/L10nWsTest.java @@ -24,9 +24,7 @@ import java.util.Date; import java.util.Locale; import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; +import org.junit.rules.ExpectedException; import org.sonar.api.platform.Server; import org.sonar.api.utils.DateUtils; import org.sonar.core.i18n.DefaultI18n; @@ -34,20 +32,19 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; import org.sonar.server.ws.WsTester.Result; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) public class L10nWsTest { @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); + @Rule + public ExpectedException expectedException = ExpectedException.none(); - @Mock - DefaultI18n i18n; - - @Mock - Server server; + DefaultI18n i18n = mock(DefaultI18n.class); + Server server = mock(Server.class); @Test public void should_allow_client_to_cache_messages() throws Exception { @@ -115,4 +112,39 @@ public class L10nWsTest { result.assertJson("{\"key1\":\"key1\",\"key2\":\"key2\",\"key3\":\"key3\"}"); } + + @Test + public void support_BCP47_formatted_language_tags() throws Exception { + Locale locale = Locale.PRC; + userSessionRule.setLocale(locale); + Locale override = Locale.UK; + + String key1 = "key1"; + + when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(key1)); + when(i18n.message(override, key1, key1)).thenReturn(key1); + + Result result = new WsTester(new L10nWs(i18n, server, userSessionRule)).newGetRequest("api/l10n", "index").setParam("locale", "en-GB").execute(); + verify(i18n).getPropertyKeys(); + verify(i18n).message(override, key1, key1); + + result.assertJson("{\"key1\":\"key1\"}"); + } + + @Test + public void fail_when_java_formatted_language_tags() throws Exception { + Locale locale = Locale.PRC; + userSessionRule.setLocale(locale); + Locale override = Locale.UK; + + String key1 = "key1"; + + when(i18n.getPropertyKeys()).thenReturn(ImmutableSet.of(key1)); + when(i18n.message(override, key1, key1)).thenReturn(key1); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("'en_GB' cannot be parsed as a BCP47 language tag"); + + new WsTester(new L10nWs(i18n, server, userSessionRule)).newGetRequest("api/l10n", "index").setParam("locale", "en_GB").execute(); + } } -- 2.39.5