PageRenderTime 38ms CodeModel.GetById 23ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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

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