Skip to content

Commit

Permalink
Fixes the Localization test
Browse files Browse the repository at this point in the history
Exchanged the HttpServletRequest with the new LocaleProvider.
  • Loading branch information
v-antech committed Feb 10, 2014
1 parent f06c91d commit 28c9e53
Showing 1 changed file with 27 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.google.sitebricks.i18n.Message;
import com.google.sitebricks.locale.LocaleProvider;
import org.easymock.EasyMock;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand All @@ -29,15 +31,15 @@
*/
public class LocalizationTest {
private static final String HELLO = "hello";
private HttpServletRequest requestMock;
private LocaleProvider localeProviderMock;

@BeforeMethod
public final void setup() {
requestMock = createNiceMock(HttpServletRequest.class);
localeProviderMock = createNiceMock(LocaleProvider.class);

expect(requestMock.getLocale()).andReturn(Locale.ENGLISH);
expect(localeProviderMock.getLocale()).andReturn(Locale.ENGLISH);

replay(requestMock);
replay(localeProviderMock);
}

@Test
Expand All @@ -54,7 +56,7 @@ protected void configure() {

Localizer.localizeAll(binder(), locs);

bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(Localized.class)
.hello();
Expand All @@ -74,7 +76,7 @@ protected void configure() {
locs.add(new Localizer.Localization(Localized.class, Locale.ENGLISH, resourceBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
});
}
Expand All @@ -92,7 +94,7 @@ protected void configure() {
locs.add(new Localizer.Localization(LocalizedMissingAnnotation.class, Locale.ENGLISH, resourceBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(LocalizedMissingAnnotation.class);
}
Expand All @@ -111,7 +113,7 @@ protected void configure() {
locs.add(new Localizer.Localization(LocalizedWrongReturnType.class, Locale.ENGLISH, resourceBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(LocalizedWrongReturnType.class);
}
Expand All @@ -130,7 +132,7 @@ protected void configure() {
locs.add(new Localizer.Localization(LocalizedWrongArgAnnotation.class, Locale.ENGLISH, resourceBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(LocalizedWrongArgAnnotation.class);
}
Expand All @@ -150,7 +152,7 @@ protected void configure() {
locs.add(new Localizer.Localization(LocalizedBrokenTemplate.class, Locale.ENGLISH, resourceBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(LocalizedBrokenTemplate.class);
}
Expand All @@ -169,7 +171,7 @@ protected void configure() {
locs.add(new Localizer.Localization(LocalizedTemplate.class, Locale.ENGLISH, resourceBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(LocalizedTemplate.class)
.hello("Dude");
Expand All @@ -186,15 +188,11 @@ public final void parameterizedLocalizeTemplateMultipleLocales() {
resourceBundle.put(LocalizationTest.HELLO, "hello ${name}");

final HashMap<String, String> japaneseBundle = Maps.newHashMap();
japaneseBundle.put(LocalizationTest.HELLO, "konichiwa ${name}");
japaneseBundle.put(LocalizationTest.HELLO, "konichiwa ${name} sama");

// Simulate an Accept-Language of Japanese
HttpServletRequest japaneseRequest = createNiceMock(HttpServletRequest.class);
expect(japaneseRequest.getLocale()).andReturn(Locale.JAPANESE);
replay(japaneseRequest);

final AtomicReference<HttpServletRequest> mockToUse
= new AtomicReference<HttpServletRequest>(japaneseRequest);
final LocaleProvider customLocaleProviderMock = createNiceMock(LocaleProvider.class);
expect(customLocaleProviderMock.getLocale()).andReturn(Locale.JAPANESE);
replay(customLocaleProviderMock);

Injector injector = Guice.createInjector(new AbstractModule() {
@Override
Expand All @@ -204,38 +202,31 @@ protected void configure() {
locs.add(new Localizer.Localization(LocalizedTemplate.class, Locale.JAPANESE, japaneseBundle));

Localizer.localizeAll(binder(), locs);
bind(HttpServletRequest.class).toProvider(new Provider<HttpServletRequest>() {
public HttpServletRequest get() {
return mockToUse.get();
}
});
bind(LocaleProvider.class).toInstance(customLocaleProviderMock);
}
});

String msg = injector.getInstance(LocalizedTemplate.class).hello("Dude");
assert "konichiwa Dude".equals(msg) : msg;

verify(japaneseRequest);
assert "konichiwa Dude sama".equals(msg) : msg;

// Now let's simulate english.
mockToUse.set(requestMock);
EasyMock.reset(customLocaleProviderMock);
expect(customLocaleProviderMock.getLocale()).andReturn(Locale.ENGLISH);
replay(customLocaleProviderMock);

msg = injector.getInstance(LocalizedTemplate.class).hello("Dude");
assert "hello Dude".equals(msg);


// Now let's simulate a totally different locale (should default to english).
// Simulate an Accept-Language of French
HttpServletRequest frenchRequest = createNiceMock(HttpServletRequest.class);
expect(frenchRequest.getLocale()).andReturn(Locale.FRENCH);
replay(frenchRequest);

mockToUse.set(frenchRequest);
EasyMock.reset(customLocaleProviderMock);
expect(customLocaleProviderMock.getLocale()).andReturn(Locale.FRENCH);
replay(customLocaleProviderMock);

// Assert that it uses the english locale (set as default above)
msg = injector.getInstance(LocalizedTemplate.class).hello("Dude");
assert "hello Dude".equals(msg);

verify(frenchRequest, requestMock);
}


Expand All @@ -249,7 +240,7 @@ protected void configure() {

Localizer.localizeAll(binder(), locs);

bind(HttpServletRequest.class).toInstance(requestMock);
bind(LocaleProvider.class).toInstance(localeProviderMock);
}
}).getInstance(LocalizedTemplate.class)
.hello("Dudette");
Expand Down

0 comments on commit 28c9e53

Please sign in to comment.