@Test public void checkCheckbox_shouldReturnTrueInModel() { startEditorPanel(attribBoolean); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); formTester.setValue(buildFormComponentId(attribBoolean.getId()), true); formTester.submit(); assertThat(editor.getValues().get(attribBoolean.getId()), is("true")); }
@Test public void submittingFormWithChanges_shouldReflectChangesInValues() throws Exception { startEditorPanel(attrib); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); tester.debugComponentTrees(); formTester.setValue(buildFormComponentId(attrib.getId()), "new_value_a"); formTester.submit(); assertThat(editor.getValues().get(attrib.getId()), is("new_value_a")); }
@Test public void selectLabelInDropDownChoice_shouldSetRightValueInModel() { startEditorPanel(attribOption); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); formTester.select(buildFormComponentId(attribOption.getId()), 1); formTester.submit(); assertThat( editor.getValues().get(attribOption.getId()), is(attribOption.getOptions().get(1).getValue())); }
private void testWithValidator(AttributeDefinition.Builder attributeDefinition) { AttributeDefinition a = attributeDefinition.validator(new FailValidator()).build(); startEditorPanel(a); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); String buildFormComponentId = buildFormComponentId(a.getId()); formTester.setValue(buildFormComponentId, "1"); tester.executeAjaxEvent(editor.getId() + ":form:submitButton", "onclick"); tester.assertErrorMessages(new String[] {"Validation Error"}); }
@Test public void testValidateOnlyAfterSubmit() throws Exception { startEditorPanel(numberAttrib); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); String buildFormComponentId = buildFormComponentId(numberAttrib.getId()); formTester.setValue(buildFormComponentId, "A"); tester.executeAjaxEvent(editor.getId() + ":form:submitButton", "onclick"); // tester. }
@Test public void putLetterIntoNumberField_shouldResultInError() throws Exception { startEditorPanel(numberAttrib); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); String buildFormComponentId = buildFormComponentId(numberAttrib.getId()); formTester.setValue(buildFormComponentId, "A"); tester.executeAjaxEvent(editor.getId() + ":form:submitButton", "onclick"); tester.assertErrorMessages(new String[] {"Number formating Error"}); }
@SuppressWarnings({"unchecked"}) @Test public void addFailFieldValidator_ShouldNotCallFormValidator() { AttributeDefinition attrib1 = newAttribute("attrib1", "name1", "desc1").validator(new FailValidator()).build(); FormValidator mock = Mockito.mock(FormValidator.class); Mockito.when(mock.fieldsToValidate()).thenReturn(Arrays.asList(new String[] {"attrib1"})); startEditorPanel(mock, attrib1); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); String component1Id = buildFormComponentId(attrib1.getId()); formTester.setValue(component1Id, "a"); tester.executeAjaxEvent(editor.getId() + ":form:submitButton", "onclick"); Mockito.verify(mock, Mockito.never()).validate(Mockito.anyMap()); }
@Test public void addFormValidator_ShouldExtractCorrectFormValues() { AttributeDefinition attrib1 = newAttribute("attrib1", "name1", "desc1").build(); AttributeDefinition attrib2 = newAttribute("attrib2", "name2", "desc2").build(); FormValidator validator = new FormValidator() { @Override public MultipleAttributeValidationResult validate(Map<String, String> attributes) { ArrayList<String> arrayList = new ArrayList<String>(attributes.keySet()); Collections.sort(arrayList); Assert.assertEquals("attrib1", arrayList.get(0)); Assert.assertEquals("attrib2", arrayList.get(1)); Assert.assertEquals("a", attributes.get(arrayList.get(0))); Assert.assertEquals("b", attributes.get(arrayList.get(1))); Map<String, String> errorMessages = new HashMap<String, String>(); for (String key : arrayList) { errorMessages.put(key, "Validation Error"); } return new MultipleAttributeValidationResultImpl(false, errorMessages); } @Override public List<String> fieldsToValidate() { return Arrays.asList(new String[] {"attrib1", "attrib2"}); } }; startEditorPanel(validator, attrib1, attrib2); FormTester formTester = tester.newFormTester(editor.getId() + ":form"); String component1Id = buildFormComponentId(attrib1.getId()); String component2Id = buildFormComponentId(attrib2.getId()); formTester.setValue(component1Id, "a"); formTester.setValue(component2Id, "b"); tester.executeAjaxEvent(editor.getId() + ":form:submitButton", "onclick"); tester.assertErrorMessages(new String[] {"Validation Error", "Validation Error"}); }
private void startEditorPanel( final FormValidator validator, final AttributeDefinition... attributes) { final HashMap<String, String> values = new HashMap<String, String>(); for (AttributeDefinition a : attributes) { values.put(a.getId(), a.getDefaultValue().getString(null)); } defaultValues = new HashMap<String, String>(values); tester = new WicketTester(); editor = (ServiceEditor) tester.startPanel( new TestPanelSource() { @Override public Panel getTestPanel(String panelId) { return new ServiceEditor( panelId, Arrays.asList(attributes), values, validator) { @Override public void onSubmit() {} }; } }); }