diff options
author | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-19 12:32:18 -0500 |
---|---|---|
committer | Stewart Stewart <stewinsalot@gmail.com> | 2016-12-19 12:32:18 -0500 |
commit | 8515d672a9fdbb0eb9038a96cee661828cafa61a (patch) | |
tree | 3f04b773de6cea3def3566d2ca4bdff9b78ace8b /src/test/scala/xyz/driver/core/MessagesTest.scala | |
parent | 1702e1c44c45e36e2d6d289ef1b7d703f65ec422 (diff) | |
parent | 861ceb03e8faeb564dd027b13250b5604af8645f (diff) | |
download | driver-core-8515d672a9fdbb0eb9038a96cee661828cafa61a.tar.gz driver-core-8515d672a9fdbb0eb9038a96cee661828cafa61a.tar.bz2 driver-core-8515d672a9fdbb0eb9038a96cee661828cafa61a.zip |
Merge branch 'master' into implicit-companions
Diffstat (limited to 'src/test/scala/xyz/driver/core/MessagesTest.scala')
-rw-r--r-- | src/test/scala/xyz/driver/core/MessagesTest.scala | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/test/scala/xyz/driver/core/MessagesTest.scala b/src/test/scala/xyz/driver/core/MessagesTest.scala new file mode 100644 index 0000000..dc44ee1 --- /dev/null +++ b/src/test/scala/xyz/driver/core/MessagesTest.scala @@ -0,0 +1,80 @@ +package xyz.driver.core + +import java.util.Locale + +import com.typesafe.config.{ConfigException, ConfigFactory} +import org.mockito.Mockito._ +import org.scalatest.mock.MockitoSugar +import org.scalatest.{FlatSpec, Matchers} +import xyz.driver.core.logging.Logger +import xyz.driver.core.messages.Messages + +import scala.collection.JavaConversions._ + +class MessagesTest extends FlatSpec with Matchers with MockitoSugar { + + val englishLocaleMessages = + Map("en.greeting" -> "Hello {0}!", "en.greetingFullName" -> "Hello {0} {1} {2}!", "en.hello" -> "Hello world!") + + "Messages" should "read messages from config and format with parameters" in { + + val log = mock[Logger] + val messagesConfig = ConfigFactory.parseMap(englishLocaleMessages) + + val messages = Messages.messages(messagesConfig, log, Locale.US) + + messages("hello") should be("Hello world!") + messages("greeting", "Homer") should be("Hello Homer!") + messages("greetingFullName", "Homer", "J", "Simpson") should be("Hello Homer J Simpson!") + } + + it should "be able to read messages for different locales" in { + + val log = mock[Logger] + + val messagesConfig = ConfigFactory.parseMap( + englishLocaleMessages ++ Map( + "zh.hello" -> "你好,世界!", + "zh.greeting" -> "你好,{0}!", + "zh.greetingFullName" -> "你好,{0} {1} {2}!" + )) + + val englishMessages = Messages.messages(messagesConfig, log, Locale.US) + val englishMessagesToo = Messages.messages(messagesConfig, log, Locale.ENGLISH) + val chineseMessages = Messages.messages(messagesConfig, log, Locale.CHINESE) + + englishMessages("hello") should be("Hello world!") + englishMessages("greeting", "Homer") should be("Hello Homer!") + englishMessages("greetingFullName", "Homer", "J", "Simpson") should be("Hello Homer J Simpson!") + + englishMessagesToo("hello") should be(englishMessages("hello")) + englishMessagesToo("greeting", "Homer") should be(englishMessages("greeting", "Homer")) + englishMessagesToo("greetingFullName", "Homer", "J", "Simpson") should be( + englishMessages("greetingFullName", "Homer", "J", "Simpson")) + + chineseMessages("hello") should be("你好,世界!") + chineseMessages("greeting", "Homer") should be("你好,Homer!") + chineseMessages("greetingFullName", "Homer", "J", "Simpson") should be("你好,Homer J Simpson!") + } + + it should "raise exception when locale is not available" in { + + val log = mock[Logger] + val messagesConfig = ConfigFactory.parseMap(englishLocaleMessages) + + an[ConfigException.Missing] should be thrownBy + Messages.messages(messagesConfig, log, Locale.GERMAN) + } + + it should "log a problem, when there is no message for key" in { + + val log = mock[Logger] + val messagesConfig = ConfigFactory.parseMap(englishLocaleMessages) + + val messages = Messages.messages(messagesConfig, log, Locale.US) + + messages("howdy") should be("howdy") + + verify(log).error(s"Message with key 'howdy' not found for locale 'en'") + } +} |