Skip to content

Commit 38d11f4

Browse files
committed
WW-5422 Adds dedicate unit test to cover DefaultLocaleProvider
1 parent 649760d commit 38d11f4

File tree

7 files changed

+247
-23
lines changed

7 files changed

+247
-23
lines changed

core/src/main/java/com/opensymphony/xwork2/ActionSupport.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public boolean isValidLocale(Locale locale) {
9090
return getLocaleProvider().isValidLocale(locale);
9191
}
9292

93+
@Override
94+
public Locale toLocale(String localeStr) {
95+
return getLocaleProvider().toLocale(localeStr);
96+
}
97+
9398
@Override
9499
public boolean hasKey(String key) {
95100
return getTextProvider().hasKey(key);

core/src/main/java/com/opensymphony/xwork2/DefaultLocaleProvider.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,23 @@ public Locale getLocale() {
4646

4747
@Override
4848
public boolean isValidLocaleString(String localeStr) {
49+
Locale locale = this.toLocale(localeStr);
50+
return isValidLocale(locale);
51+
}
52+
53+
@Override
54+
public boolean isValidLocale(Locale locale) {
55+
return locale != null && LocaleUtils.isAvailableLocale(locale);
56+
}
57+
58+
@Override
59+
public Locale toLocale(String localeStr) {
4960
Locale locale = null;
5061
try {
5162
locale = LocaleUtils.toLocale(StringUtils.trimToNull(localeStr));
5263
} catch (IllegalArgumentException e) {
5364
LOG.warn(new ParameterizedMessage("Cannot convert [{}] to proper locale", localeStr), e);
5465
}
55-
return isValidLocale(locale);
56-
}
57-
58-
@Override
59-
public boolean isValidLocale(Locale locale) {
60-
return LocaleUtils.isAvailableLocale(locale);
66+
return locale;
6167
}
6268
}

core/src/main/java/com/opensymphony/xwork2/LocaleProvider.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
package com.opensymphony.xwork2;
2020

21+
import org.apache.commons.lang3.LocaleUtils;
22+
import org.apache.commons.lang3.StringUtils;
23+
2124
import java.util.Locale;
2225

2326

@@ -58,4 +61,17 @@ public interface LocaleProvider {
5861
*/
5962
boolean isValidLocale(Locale locale);
6063

64+
/**
65+
* Tries to convert provided locale string into {@link Locale} or returns null
66+
* @param localeStr a String representing locale, e.g.: en_EN
67+
* @return instance of {@link Locale} or null
68+
* @since Struts 6.5.0
69+
*/
70+
default Locale toLocale(String localeStr) {
71+
try {
72+
return LocaleUtils.toLocale(StringUtils.trimToNull(localeStr));
73+
} catch (IllegalArgumentException e) {
74+
return null;
75+
}
76+
}
6177
}

core/src/main/java/com/opensymphony/xwork2/validator/DelegatingValidatorContext.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,15 @@ public boolean isValidLocale(Locale locale) {
122122
return localeProvider.isValidLocale(locale);
123123
}
124124

125+
@Override
126+
public Locale toLocale(String localeStr) {
127+
return localeProvider.toLocale(localeStr);
128+
}
129+
125130
public boolean hasKey(String key) {
126131
return textProvider.hasKey(key);
127132
}
128-
133+
129134
public String getText(String aTextName) {
130135
return textProvider.getText(aTextName);
131136
}
@@ -280,6 +285,11 @@ public boolean isValidLocaleString(String localeStr) {
280285
public boolean isValidLocale(Locale locale) {
281286
return getLocaleProvider().isValidLocale(locale);
282287
}
288+
289+
@Override
290+
public Locale toLocale(String localeStr) {
291+
return getLocaleProvider().toLocale(localeStr);
292+
}
283293
}
284294

285295
/**

core/src/main/java/org/apache/struts2/interceptor/I18nInterceptor.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.opensymphony.xwork2.inject.Inject;
2525
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
2626
import com.opensymphony.xwork2.util.TextParseUtil;
27-
import org.apache.commons.lang3.LocaleUtils;
2827
import org.apache.logging.log4j.LogManager;
2928
import org.apache.logging.log4j.Logger;
3029
import org.apache.logging.log4j.message.ParameterizedMessage;
@@ -85,7 +84,7 @@ public void setRequestCookieParameterName(String requestCookieParameterName) {
8584
}
8685

8786
public void setLocaleStorage(String storageName) {
88-
if (storageName == null || "".equals(storageName)) {
87+
if (storageName == null || storageName.isEmpty()) {
8988
this.storage = Storage.ACCEPT_LANGUAGE;
9089
} else {
9190
try {
@@ -169,27 +168,21 @@ protected LocaleHandler getLocaleHandler(ActionInvocation invocation) {
169168
}
170169

171170
/**
172-
* Creates a Locale object from the request param, which might
173-
* be already a Local or a String
171+
* Creates a Locale object from the request param
174172
*
175173
* @param requestedLocale the parameter from the request
176-
* @return the Locale
174+
* @return instance of {@link Locale} or null
177175
*/
178-
protected Locale getLocaleFromParam(Object requestedLocale) {
176+
protected Locale getLocaleFromParam(String requestedLocale) {
179177
LocaleProvider localeProvider = localeProviderFactory.createLocaleProvider();
180178

181179
Locale locale = null;
182180
if (requestedLocale != null) {
183-
if (requestedLocale instanceof Locale) {
184-
locale = (Locale) requestedLocale;
185-
} else {
186-
String localeStr = requestedLocale.toString();
187-
if (localeProvider.isValidLocaleString(localeStr)) {
188-
locale = LocaleUtils.toLocale(localeStr);
189-
} else {
190-
locale = localeProvider.getLocale();
191-
}
181+
locale = localeProvider.toLocale(requestedLocale);
182+
if (locale == null) {
183+
locale = localeProvider.getLocale();
192184
}
185+
193186
if (locale != null) {
194187
LOG.debug("Found locale: {}", locale);
195188
}
@@ -285,7 +278,7 @@ protected AcceptLanguageLocaleHandler(ActionInvocation invocation) {
285278
@Override
286279
@SuppressWarnings("rawtypes")
287280
public Locale find() {
288-
if (supportedLocale.size() > 0) {
281+
if (!supportedLocale.isEmpty()) {
289282
Enumeration locales = actionInvocation.getInvocationContext().getServletRequest().getLocales();
290283
while (locales.hasMoreElements()) {
291284
Locale locale = (Locale) locales.nextElement();
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.opensymphony.xwork2;
20+
21+
import org.junit.AfterClass;
22+
import org.junit.Before;
23+
import org.junit.BeforeClass;
24+
import org.junit.Test;
25+
26+
import java.util.Locale;
27+
28+
import static org.junit.Assert.assertEquals;
29+
import static org.junit.Assert.assertFalse;
30+
import static org.junit.Assert.assertNull;
31+
import static org.junit.Assert.assertTrue;
32+
33+
public class DefaultLocaleProviderTest {
34+
35+
private DefaultLocaleProvider provider;
36+
37+
@Before
38+
public void setUp() throws Exception {
39+
provider = new DefaultLocaleProvider();
40+
}
41+
42+
@BeforeClass
43+
public static void beforeClass() throws Exception {
44+
ActionContext.of().bind();
45+
}
46+
47+
@AfterClass
48+
public static void afterClass() throws Exception {
49+
ActionContext.clear();
50+
}
51+
52+
@Test
53+
public void getLocale() {
54+
// given
55+
ActionContext.getContext().withLocale(Locale.ITALY);
56+
57+
// when
58+
Locale expected = provider.getLocale();
59+
60+
// then
61+
assertEquals(expected, Locale.ITALY);
62+
}
63+
64+
@Test
65+
public void getLocaleNull() {
66+
// given
67+
ActionContext backup = ActionContext.getContext();
68+
ActionContext.clear();
69+
70+
// when
71+
Locale expected = provider.getLocale();
72+
73+
// then
74+
assertNull(expected);
75+
ActionContext.bind(backup);
76+
}
77+
78+
@Test
79+
public void toLocale() {
80+
// given
81+
ActionContext.getContext().withLocale(Locale.GERMAN);
82+
83+
// when
84+
Locale expected = provider.toLocale("it");
85+
86+
// then
87+
assertEquals(expected, Locale.ITALIAN);
88+
}
89+
90+
@Test
91+
public void toLocaleFull() {
92+
// given
93+
ActionContext.getContext().withLocale(Locale.GERMAN);
94+
95+
// when
96+
Locale expected = provider.toLocale("it_IT");
97+
98+
// then
99+
assertEquals(expected, Locale.ITALY);
100+
}
101+
102+
@Test
103+
public void toLocaleTrimEndOfLine() {
104+
// given
105+
ActionContext.getContext().withLocale(Locale.GERMAN);
106+
107+
// when
108+
Locale expected = provider.toLocale("it_IT\n");
109+
110+
// then
111+
assertEquals(expected, Locale.ITALY);
112+
}
113+
114+
@Test
115+
public void toLocaleTrimEmptySpace() {
116+
// given
117+
ActionContext.getContext().withLocale(Locale.GERMAN);
118+
119+
// when
120+
Locale expected = provider.toLocale(" it_IT ");
121+
122+
// then
123+
assertEquals(expected, Locale.ITALY);
124+
}
125+
126+
@Test
127+
public void isValidLocaleNull() {
128+
// given
129+
ActionContext.getContext().withLocale(Locale.GERMAN);
130+
131+
// when
132+
boolean expected = provider.isValidLocale(null);
133+
134+
// then
135+
assertFalse(expected);
136+
}
137+
138+
@Test
139+
public void isValidLocale() {
140+
// given
141+
ActionContext.getContext().withLocale(Locale.GERMAN);
142+
143+
// when
144+
boolean expected = provider.isValidLocale(Locale.ITALIAN);
145+
146+
// then
147+
assertTrue(expected);
148+
}
149+
150+
@Test
151+
public void isValidLocaleString() {
152+
// given
153+
ActionContext.getContext().withLocale(Locale.GERMAN);
154+
155+
// when
156+
boolean expected = provider.isValidLocaleString("it");
157+
158+
// then
159+
assertTrue(expected);
160+
}
161+
162+
@Test
163+
public void isValidLocaleStringNot() {
164+
// given
165+
ActionContext.getContext().withLocale(Locale.GERMAN);
166+
167+
// when
168+
boolean expected = provider.isValidLocaleString("italy");
169+
170+
// then
171+
assertFalse(expected);
172+
}
173+
174+
}

core/src/test/java/org/apache/struts2/interceptor/I18nInterceptorTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,26 @@ public void testNotExistingLocale() throws Exception {
147147
assertEquals(Locale.getDefault(), session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
148148
}
149149

150+
public void testTrimableLocaleString1() throws Exception {
151+
prepare(I18nInterceptor.DEFAULT_PARAMETER, "de\n");
152+
153+
interceptor.intercept(mai);
154+
155+
assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed
156+
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
157+
assertEquals(Locale.GERMAN, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
158+
}
159+
160+
public void testTrimableLocaleString2() throws Exception {
161+
prepare(I18nInterceptor.DEFAULT_PARAMETER, "de ");
162+
163+
interceptor.intercept(mai);
164+
165+
assertFalse(mai.getInvocationContext().getParameters().get(I18nInterceptor.DEFAULT_PARAMETER).isDefined()); // should have been removed
166+
assertNotNull(session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should be stored here
167+
assertEquals(Locale.GERMAN, session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE)); // should create a locale object
168+
}
169+
150170
public void testWithVariant() throws Exception {
151171
prepare(I18nInterceptor.DEFAULT_PARAMETER, "ja_JP_JP");
152172
interceptor.intercept(mai);

0 commit comments

Comments
 (0)