PageRenderTime 94ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks/src/test/java/com/google/sitebricks/LocalizationTest.java

https://github.com/juven/sitebricks
Java | 289 lines | 210 code | 71 blank | 8 comment | 0 complexity | 91e3b557fc69233e486d41e27913e3db MD5 | raw file
  1. package com.google.sitebricks;
  2. import com.google.common.collect.Maps;
  3. import com.google.common.collect.Sets;
  4. import com.google.inject.AbstractModule;
  5. import com.google.inject.CreationException;
  6. import com.google.inject.Guice;
  7. import com.google.inject.Injector;
  8. import com.google.inject.Provider;
  9. import com.google.inject.name.Named;
  10. import com.google.sitebricks.i18n.Message;
  11. import org.testng.annotations.BeforeMethod;
  12. import org.testng.annotations.Test;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.util.HashMap;
  15. import java.util.Locale;
  16. import java.util.Map;
  17. import java.util.Set;
  18. import java.util.concurrent.atomic.AtomicReference;
  19. import static org.easymock.EasyMock.createNiceMock;
  20. import static org.easymock.EasyMock.expect;
  21. import static org.easymock.EasyMock.replay;
  22. import static org.easymock.EasyMock.verify;
  23. /**
  24. * Unit test for the i18n binder utility.
  25. */
  26. public class LocalizationTest {
  27. private static final String HELLO = "hello";
  28. private HttpServletRequest requestMock;
  29. @BeforeMethod
  30. public final void setup() {
  31. requestMock = createNiceMock(HttpServletRequest.class);
  32. expect(requestMock.getLocale()).andReturn(Locale.ENGLISH);
  33. replay(requestMock);
  34. }
  35. @Test
  36. public final void simpleLocalize() {
  37. final Map<String, String> resourceBundle = Maps.newHashMap();
  38. resourceBundle.put(HELLO, "hello there!");
  39. String msg = Guice.createInjector(new AbstractModule() {
  40. @Override
  41. protected void configure() {
  42. Set<Localizer.Localization> locs = Sets.newHashSet();
  43. locs.add(new Localizer.Localization(Localized.class, Locale.ENGLISH, resourceBundle));
  44. Localizer.localizeAll(binder(), locs);
  45. bind(HttpServletRequest.class).toInstance(requestMock);
  46. }
  47. }).getInstance(Localized.class)
  48. .hello();
  49. assert resourceBundle.get(HELLO).equals(msg);
  50. }
  51. @Test(expectedExceptions = CreationException.class)
  52. public final void simpleLocalizeMissingEntry() {
  53. final Map<String, String> resourceBundle = Maps.newHashMap();
  54. Guice.createInjector(new AbstractModule() {
  55. @Override
  56. protected void configure() {
  57. Set<Localizer.Localization> locs = Sets.newHashSet();
  58. locs.add(new Localizer.Localization(Localized.class, Locale.ENGLISH, resourceBundle));
  59. Localizer.localizeAll(binder(), locs);
  60. bind(HttpServletRequest.class).toInstance(requestMock);
  61. }
  62. });
  63. }
  64. @Test(expectedExceptions = CreationException.class)
  65. public final void simpleLocalizeMissingAnnotation() {
  66. final Map<String, String> resourceBundle = Maps.newHashMap();
  67. resourceBundle.put(LocalizationTest.HELLO, "stuff");
  68. Guice.createInjector(new AbstractModule() {
  69. @Override
  70. protected void configure() {
  71. Set<Localizer.Localization> locs = Sets.newHashSet();
  72. locs.add(new Localizer.Localization(LocalizedMissingAnnotation.class, Locale.ENGLISH, resourceBundle));
  73. Localizer.localizeAll(binder(), locs);
  74. bind(HttpServletRequest.class).toInstance(requestMock);
  75. }
  76. }).getInstance(LocalizedMissingAnnotation.class);
  77. }
  78. @Test(expectedExceptions = CreationException.class)
  79. public final void simpleLocalizeWrongReturnType() {
  80. final Map<String, String> resourceBundle = Maps.newHashMap();
  81. resourceBundle.put(LocalizationTest.HELLO, "stuff");
  82. Guice.createInjector(new AbstractModule() {
  83. @Override
  84. protected void configure() {
  85. Set<Localizer.Localization> locs = Sets.newHashSet();
  86. locs.add(new Localizer.Localization(LocalizedWrongReturnType.class, Locale.ENGLISH, resourceBundle));
  87. Localizer.localizeAll(binder(), locs);
  88. bind(HttpServletRequest.class).toInstance(requestMock);
  89. }
  90. }).getInstance(LocalizedWrongReturnType.class);
  91. }
  92. @Test(expectedExceptions = CreationException.class)
  93. public final void parameterizedLocalizeWrongArgAnnotation() {
  94. final Map<String, String> resourceBundle = Maps.newHashMap();
  95. resourceBundle.put(LocalizationTest.HELLO, "stuff");
  96. Guice.createInjector(new AbstractModule() {
  97. @Override
  98. protected void configure() {
  99. Set<Localizer.Localization> locs = Sets.newHashSet();
  100. locs.add(new Localizer.Localization(LocalizedWrongArgAnnotation.class, Locale.ENGLISH, resourceBundle));
  101. Localizer.localizeAll(binder(), locs);
  102. bind(HttpServletRequest.class).toInstance(requestMock);
  103. }
  104. }).getInstance(LocalizedWrongArgAnnotation.class);
  105. }
  106. @Test(expectedExceptions = CreationException.class)
  107. public final void parameterizedLocalizeBrokenTemplate() {
  108. final Map<String, String> resourceBundle = Maps.newHashMap();
  109. resourceBundle.put(LocalizationTest.HELLO, "stuff");
  110. Guice.createInjector(new AbstractModule() {
  111. @Override
  112. protected void configure() {
  113. Set<Localizer.Localization> locs = Sets.newHashSet();
  114. locs.add(new Localizer.Localization(LocalizedBrokenTemplate.class, Locale.ENGLISH, resourceBundle));
  115. Localizer.localizeAll(binder(), locs);
  116. bind(HttpServletRequest.class).toInstance(requestMock);
  117. }
  118. }).getInstance(LocalizedBrokenTemplate.class);
  119. }
  120. @Test
  121. public final void parameterizedLocalizeTemplate() {
  122. final Map<String, String> resourceBundle = Maps.newHashMap();
  123. resourceBundle.put(LocalizationTest.HELLO, "hello ${name}");
  124. String msg = Guice.createInjector(new AbstractModule() {
  125. @Override
  126. protected void configure() {
  127. Set<Localizer.Localization> locs = Sets.newHashSet();
  128. locs.add(new Localizer.Localization(LocalizedTemplate.class, Locale.ENGLISH, resourceBundle));
  129. Localizer.localizeAll(binder(), locs);
  130. bind(HttpServletRequest.class).toInstance(requestMock);
  131. }
  132. }).getInstance(LocalizedTemplate.class)
  133. .hello("Dude");
  134. assert "hello Dude".equals(msg);
  135. }
  136. @Test
  137. public final void parameterizedLocalizeTemplateMultipleLocales() {
  138. Locale.setDefault(Locale.ENGLISH);
  139. final Map<String, String> resourceBundle = Maps.newHashMap();
  140. resourceBundle.put(LocalizationTest.HELLO, "hello ${name}");
  141. final HashMap<String, String> japaneseBundle = Maps.newHashMap();
  142. japaneseBundle.put(LocalizationTest.HELLO, "konichiwa ${name}");
  143. // Simulate an Accept-Language of Japanese
  144. HttpServletRequest japaneseRequest = createNiceMock(HttpServletRequest.class);
  145. expect(japaneseRequest.getLocale()).andReturn(Locale.JAPANESE);
  146. replay(japaneseRequest);
  147. final AtomicReference<HttpServletRequest> mockToUse
  148. = new AtomicReference<HttpServletRequest>(japaneseRequest);
  149. Injector injector = Guice.createInjector(new AbstractModule() {
  150. @Override
  151. protected void configure() {
  152. Set<Localizer.Localization> locs = Sets.newHashSet();
  153. locs.add(new Localizer.Localization(LocalizedTemplate.class, Locale.ENGLISH, resourceBundle));
  154. locs.add(new Localizer.Localization(LocalizedTemplate.class, Locale.JAPANESE, japaneseBundle));
  155. Localizer.localizeAll(binder(), locs);
  156. bind(HttpServletRequest.class).toProvider(new Provider<HttpServletRequest>() {
  157. public HttpServletRequest get() {
  158. return mockToUse.get();
  159. }
  160. });
  161. }
  162. });
  163. String msg = injector.getInstance(LocalizedTemplate.class).hello("Dude");
  164. assert "konichiwa Dude".equals(msg) : msg;
  165. verify(japaneseRequest);
  166. // Now let's simulate english.
  167. mockToUse.set(requestMock);
  168. msg = injector.getInstance(LocalizedTemplate.class).hello("Dude");
  169. assert "hello Dude".equals(msg);
  170. // Now let's simulate a totally different locale (should default to english).
  171. // Simulate an Accept-Language of French
  172. HttpServletRequest frenchRequest = createNiceMock(HttpServletRequest.class);
  173. expect(frenchRequest.getLocale()).andReturn(Locale.FRENCH);
  174. replay(frenchRequest);
  175. mockToUse.set(frenchRequest);
  176. // Assert that it uses the english locale (set as default above)
  177. msg = injector.getInstance(LocalizedTemplate.class).hello("Dude");
  178. assert "hello Dude".equals(msg);
  179. verify(frenchRequest, requestMock);
  180. }
  181. @Test
  182. public final void parameterizedWithNoExternalBundle() {
  183. String msg = Guice.createInjector(new AbstractModule() {
  184. @Override
  185. protected void configure() {
  186. Set<Localizer.Localization> locs = Sets.newHashSet();
  187. locs.add(Localizer.defaultLocalizationFor(LocalizedTemplate.class));
  188. Localizer.localizeAll(binder(), locs);
  189. bind(HttpServletRequest.class).toInstance(requestMock);
  190. }
  191. }).getInstance(LocalizedTemplate.class)
  192. .hello("Dudette");
  193. assert "hello Dudette!".equals(msg);
  194. }
  195. public static interface Localized {
  196. @Message(message = "hello world!")
  197. String hello();
  198. }
  199. public static interface LocalizedMissingAnnotation {
  200. String hello();
  201. }
  202. public static interface LocalizedWrongReturnType {
  203. @Message(message = "hello world!")
  204. void hello();
  205. }
  206. public static interface LocalizedWrongArgAnnotation {
  207. @Message(message = "hello world!")
  208. String hello(String val);
  209. }
  210. public static interface LocalizedBrokenTemplate {
  211. @Message(message = "hello ${named}!")
  212. String hello(@Named("name") String val);
  213. }
  214. public static interface LocalizedTemplate {
  215. @Message(message = "hello ${name}!")
  216. String hello(@Named("name") String val);
  217. }
  218. }