Skip to content

Commit 4a818c7

Browse files
authored
Merge pull request mitreid-connect#1385 from elennick/master
"Unable to load locale" log should not be ERROR level
2 parents 64fbee7 + fe000d9 commit 4a818c7

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

openid-connect-server/src/main/java/org/mitre/openid/connect/config/JsonMessageSource.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.mitre.openid.connect.config;
1818

1919
import java.io.File;
20+
import java.io.FileNotFoundException;
2021
import java.io.IOException;
2122
import java.io.InputStreamReader;
2223
import java.text.MessageFormat;
@@ -42,10 +43,9 @@
4243

4344
/**
4445
* @author jricher
45-
*
4646
*/
4747
public class JsonMessageSource extends AbstractMessageSource {
48-
// Logger for this class
48+
4949
private static final Logger logger = LoggerFactory.getLogger(JsonMessageSource.class);
5050

5151
private Resource baseDirectory;
@@ -107,7 +107,6 @@ private String getValue(String code, List<JsonObject> langs) {
107107
/**
108108
* Get a value from a single map
109109
* @param code
110-
* @param locale
111110
* @param lang
112111
* @return
113112
*/
@@ -147,9 +146,7 @@ private String getValue(String code, JsonObject lang) {
147146
}
148147
}
149148

150-
151149
return value;
152-
153150
}
154151

155152
/**
@@ -174,23 +171,23 @@ private List<JsonObject> getLanguageMap(Locale locale) {
174171
r = getBaseDirectory().createRelative(filename);
175172
}
176173

177-
logger.info("No locale loaded, trying to load from " + r);
174+
logger.info("No locale loaded, trying to load from {}", r);
178175

179176
JsonParser parser = new JsonParser();
180177
JsonObject obj = (JsonObject) parser.parse(new InputStreamReader(r.getInputStream(), "UTF-8"));
181178

182179
set.add(obj);
183180
}
184181
languageMaps.put(locale, set);
182+
} catch (FileNotFoundException e) {
183+
logger.info("Unable to load locale because no messages file was found for locale {}", locale.getDisplayName());
184+
languageMaps.put(locale, null);
185185
} catch (JsonIOException | JsonSyntaxException | IOException e) {
186186
logger.error("Unable to load locale", e);
187187
}
188188
}
189189

190190
return languageMaps.get(locale);
191-
192-
193-
194191
}
195192

196193
/**
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.mitre.openid.connect.config;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.mockito.InjectMocks;
7+
import org.mockito.Spy;
8+
import org.mockito.runners.MockitoJUnitRunner;
9+
import org.springframework.core.io.ClassPathResource;
10+
import org.springframework.core.io.Resource;
11+
12+
import java.text.MessageFormat;
13+
import java.util.Locale;
14+
15+
import static org.junit.Assert.assertEquals;
16+
import static org.junit.Assert.assertNull;
17+
18+
@RunWith(MockitoJUnitRunner.class)
19+
public class TestJsonMessageSource {
20+
21+
@InjectMocks
22+
private JsonMessageSource jsonMessageSource;
23+
24+
@Spy
25+
private ConfigurationPropertiesBean config;
26+
27+
private Locale localeThatHasAFile = new Locale("en");
28+
29+
private Locale localeThatDoesNotHaveAFile = new Locale("xx");
30+
31+
@Before
32+
public void setup() {
33+
//test message files are located in test/resources/js/locale/
34+
Resource resource = new ClassPathResource("/resources/js/locale/");
35+
jsonMessageSource.setBaseDirectory(resource);
36+
}
37+
38+
@Test
39+
public void verifyWhenLocaleExists_canResolveCode() {
40+
MessageFormat mf = jsonMessageSource.resolveCode("testAttribute", localeThatHasAFile);
41+
assertEquals(mf.getLocale().getLanguage(), "en");
42+
assertEquals(mf.toPattern(), "testValue");
43+
}
44+
45+
@Test
46+
public void verifyWhenLocaleDoesNotExist_cannotResolveCode() {
47+
MessageFormat mf = jsonMessageSource.resolveCode("test", localeThatDoesNotHaveAFile);
48+
assertNull(mf);
49+
}
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"testAttribute": "testValue"
3+
}

0 commit comments

Comments
 (0)