Hi all,
I'm trying to test a wicket 1.5.7 application and I'm hitting a
strange (at least for me) issue.
I have two dropdowns (combo 1 and combo2) and when I select one value
from combo1 I fire an AjaxUpdateBehavior on onchange event which
selects
a value on combo2.
If I use the application everything is working as expected but If I
try
to simulate the application with wicket tester I come into trouble.
Basically the test code is the following one:
WicketTester tester= new WicketTester(new
WicketApplication());
tester.startPage(TestAjaxDropDown.class);
FormTester formTester=tester.newFormTester("form");
formTester.select("combo1", 0);
tester.executeAjaxEvent("form:combo1", "onchange");
formTester.submit();
tester.assertNoErrorMessage();
what I got is that after firing the onchange event combo1 gets a null
value as object model and this does not happen when I use a
browser. If
I do not fire the event the application reports that combo2 has no
element selected.
Am I missing something or is it a bug?
Grazie,
Giovanni
P.S. I built a simple test case that I insert here
main page class
package com.sg2net.test;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class TestAjaxDropDown extends WebPage {
private static final Logger
logger=LoggerFactory.getLogger(TestAjaxDropDown.class.getName());
public TestAjaxDropDown() {
add(new FeedbackPanel("feedback"));
add(new TestAjaxDropDownFrom("form"));
}
private class DomainObject1 implements Serializable {
public DomainObject1(long id, String description) {
super();
this.id = id;
this.description = description;
}
private static final long serialVersionUID = 1L;
private long id;
private String description;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DomainObject1 other = (DomainObject1) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (id != other.id)
return false;
return true;
}
private TestAjaxDropDown getOuterType() {
return TestAjaxDropDown.this;
}
}
private class DomainObject2 implements Serializable {
public DomainObject2(long id, String description) {
super();
this.id = id;
this.description = description;
}
private static final long serialVersionUID = 1L;
private long id;
private String description;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
DomainObject2 other = (DomainObject2) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (id != other.id)
return false;
return true;
}
private TestAjaxDropDown getOuterType() {
return TestAjaxDropDown.this;
}
}
private static final long serialVersionUID = 1L;
private class TestAjaxDropDownFrom extends Form<Void> {
private static final long serialVersionUID = 1L;
private DomainObject1 domainObject1;
private DomainObject2 domainObject2;
private DropDownChoice<DomainObject2> secondCombo;
public TestAjaxDropDownFrom(String id) {
super(id);
List<DomainObject1> choicesForCombo1= new
ArrayList<DomainObject1>();
choicesForCombo1.add(new DomainObject1(1L,"uno"));
choicesForCombo1.add(new DomainObject1(2L,"due"));
choicesForCombo1.add(new DomainObject1(3L,"tre"));
DropDownChoice<DomainObject1> firstCombo= new
DropDownChoice<DomainObject1>("combo1",
new
PropertyModel<DomainObject1>(this,"domainObject1"),choicesForCombo1,
new ChoiceRenderer<DomainObject1>("description",
"id"));
firstCombo.setRequired(true);
firstCombo.setNullValid(true);
firstCombo.add(new AjaxUpdateBehavior("onchange"));
add(firstCombo);
List<DomainObject2> choicesForCombo2= new
ArrayList<DomainObject2>();
choicesForCombo2.add(new DomainObject2(10L,"unozero"));
choicesForCombo2.add(new DomainObject2(20L,"duezero"));
choicesForCombo2.add(new DomainObject2(30L,"trezero"));
secondCombo= new DropDownChoice<DomainObject2>("combo2",
new
PropertyModel<DomainObject2>(this,"domainObject2"),choicesForCombo2,
new ChoiceRenderer<DomainObject2>("description",
"id"));
secondCombo.setOutputMarkupId(true);
secondCombo.setNullValid(true);
add(secondCombo);
}
@Override
public void onSubmit() {
if (domainObject2==null) {
error("Domain object2 is null");
} else {
info("OK");
}
}
private class AjaxUpdateBehavior extends
AjaxFormComponentUpdatingBehavior {
private static final long serialVersionUID = 1L;
public AjaxUpdateBehavior(String event) {
super(event);
}
@Override
protected void onUpdate(AjaxRequestTarget target) {
if (domainObject1!=null) {
logger.warn("domainObject1.getId()=" +
domainObject1.getId());
if (domainObject1.getId()==1L) {
domainObject2=new
DomainObject2(10L,"unozero");
logger.warn("domainObject2.getId()=" +
domainObject2.getId());
}
if (domainObject1.getId()==2L) {
domainObject2=new
DomainObject2(20L,"duezero");
logger.warn("domainObject2.getId()=" +
domainObject2.getId());
}
if (domainObject1.getId()==3L) {
domainObject2=new
DomainObject2(30L,"trezero");
logger.warn("domainObject2.getId()=" +
domainObject2.getId());
}
secondCombo.modelChanged();
target.add(secondCombo);
} else {
logger.warn("domainObject1==null");
}
}
}
}
}
test class
package com.sg2net.test;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.testng.annotations.Test;
public class WicketTesterAjaxDropDown {
@Test
public void testAjaxDropDown() {
WicketTester tester= new WicketTester(new
WicketApplication());
tester.startPage(TestAjaxDropDown.class);
FormTester formTester=tester.newFormTester("form");
formTester.select("combo1", 0);
tester.executeAjaxEvent("form:combo1", "onchange");
//formTester.select("combo1", 0);
formTester.submit();
tester.assertNoErrorMessage();
}
}
wicket application class
package com.sg2net.test;
import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;
import com.sg2net.test.TestAjaxDropDown;
public class WicketApplication extends WebApplication {
@Override
public Class<? extends Page> getHomePage() {
return TestAjaxDropDown.class;
}
protected void init() {
getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
}
}