Browse Source

Merge pull request #1385 from elennick/master

"Unable to load locale" log should not be ERROR level
pull/1390/head
Justin Richer 7 years ago committed by GitHub
parent
commit
4a818c7b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      openid-connect-server/src/main/java/org/mitre/openid/connect/config/JsonMessageSource.java
  2. 50
      openid-connect-server/src/test/java/org/mitre/openid/connect/config/TestJsonMessageSource.java
  3. 3
      openid-connect-server/src/test/resources/resources/js/locale/en/messages.json

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

@ -17,6 +17,7 @@
package org.mitre.openid.connect.config;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
@ -42,10 +43,9 @@ import com.google.gson.JsonSyntaxException;
/**
* @author jricher
*
*/
public class JsonMessageSource extends AbstractMessageSource {
// Logger for this class
private static final Logger logger = LoggerFactory.getLogger(JsonMessageSource.class);
private Resource baseDirectory;
@ -107,7 +107,6 @@ public class JsonMessageSource extends AbstractMessageSource {
/**
* Get a value from a single map
* @param code
* @param locale
* @param lang
* @return
*/
@ -147,9 +146,7 @@ public class JsonMessageSource extends AbstractMessageSource {
}
}
return value;
}
/**
@ -174,7 +171,7 @@ public class JsonMessageSource extends AbstractMessageSource {
r = getBaseDirectory().createRelative(filename);
}
logger.info("No locale loaded, trying to load from " + r);
logger.info("No locale loaded, trying to load from {}", r);
JsonParser parser = new JsonParser();
JsonObject obj = (JsonObject) parser.parse(new InputStreamReader(r.getInputStream(), "UTF-8"));
@ -182,15 +179,15 @@ public class JsonMessageSource extends AbstractMessageSource {
set.add(obj);
}
languageMaps.put(locale, set);
} catch (FileNotFoundException e) {
logger.info("Unable to load locale because no messages file was found for locale {}", locale.getDisplayName());
languageMaps.put(locale, null);
} catch (JsonIOException | JsonSyntaxException | IOException e) {
logger.error("Unable to load locale", e);
}
}
return languageMaps.get(locale);
}
/**

50
openid-connect-server/src/test/java/org/mitre/openid/connect/config/TestJsonMessageSource.java

@ -0,0 +1,50 @@
package org.mitre.openid.connect.config;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.text.MessageFormat;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@RunWith(MockitoJUnitRunner.class)
public class TestJsonMessageSource {
@InjectMocks
private JsonMessageSource jsonMessageSource;
@Spy
private ConfigurationPropertiesBean config;
private Locale localeThatHasAFile = new Locale("en");
private Locale localeThatDoesNotHaveAFile = new Locale("xx");
@Before
public void setup() {
//test message files are located in test/resources/js/locale/
Resource resource = new ClassPathResource("/resources/js/locale/");
jsonMessageSource.setBaseDirectory(resource);
}
@Test
public void verifyWhenLocaleExists_canResolveCode() {
MessageFormat mf = jsonMessageSource.resolveCode("testAttribute", localeThatHasAFile);
assertEquals(mf.getLocale().getLanguage(), "en");
assertEquals(mf.toPattern(), "testValue");
}
@Test
public void verifyWhenLocaleDoesNotExist_cannotResolveCode() {
MessageFormat mf = jsonMessageSource.resolveCode("test", localeThatDoesNotHaveAFile);
assertNull(mf);
}
}

3
openid-connect-server/src/test/resources/resources/js/locale/en/messages.json

@ -0,0 +1,3 @@
{
"testAttribute": "testValue"
}
Loading…
Cancel
Save