Skip to content

Instantly share code, notes, and snippets.

@twendelmuth
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twendelmuth/f4eb64a99f54d41c8c56 to your computer and use it in GitHub Desktop.
Save twendelmuth/f4eb64a99f54d41c8c56 to your computer and use it in GitHub Desktop.
WicketTester StackOverflow problem
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title>Apache Wicket Quickstart</title>
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:regular,bold' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" title="Stylesheet" />
</head>
<body>
<wicket:panel>
<div wicket:id="locales">
<a wicket:id="link"><wicket:container wicket:id="label" /></a>
</div>
</wicket:panel>
</body>
</html>
package com.stackoverflow;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.StringResourceModel;
public class SwitchLocale extends Panel {
public SwitchLocale(String id) {
super(id);
add(new ListView<Locale>("locales", getLocales()) {
@Override
protected void populateItem(final ListItem<Locale> item) {
AjaxLink<Void> link = new AjaxLink<Void>("link") {
@Override
public void onClick(AjaxRequestTarget target) {
Session.get().setLocale(item.getModelObject());
// Add whole page to update instead of single components
target.add(getPage());
}
};
item.add(link);
link.add(new Label("label", new StringResourceModel("lang.${}", item.getModel())));
}
});
}
private IModel<List<Locale>> getLocales() {
return new AbstractReadOnlyModel<List<Locale>>() {
@Override
public List<Locale> getObject() {
return Arrays.asList(Locale.ITALIAN, Locale.ENGLISH, Locale.GERMAN);
}
};
}
}
lang.it=it default
lang.en=en default
lang.de=de default
package com.stackoverflow;
import org.apache.wicket.util.tester.WicketTester;
import org.junit.Before;
import org.junit.Test;
/**
* Simple test using the WicketTester
*/
public class SwitchLocaleTester {
private WicketTester tester;
@Before
public void setUp() {
tester = new WicketTester(new WicketApplication());
}
@Test
public void homepageRendersSuccessfully() {
tester.startComponentInPage(SwitchLocale.class);
tester.assertModelValue("locales:" + 0 + ":link:label", "it default");
tester.clickLink("locales:" + 0 + ":link", true);
tester.assertModelValue("locales:" + 0 + ":link:label", "it italian");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment