You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateFieldErrorMessageTest.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.server.component.datefield;
  17. import java.lang.reflect.Field;
  18. import java.time.LocalDate;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.junit.Test;
  22. import com.vaadin.shared.ui.datefield.AbstractDateFieldServerRpc;
  23. import com.vaadin.shared.ui.datefield.DateResolution;
  24. import com.vaadin.tests.server.component.abstractdatefield.AbstractLocalDateFieldDeclarativeTest;
  25. import com.vaadin.ui.AbstractDateField;
  26. import com.vaadin.ui.InlineDateField;
  27. /**
  28. * Tests the resetting of component error after setting empty date string in
  29. * {@link AbstractDateField}.
  30. */
  31. public class DateFieldErrorMessageTest
  32. extends AbstractLocalDateFieldDeclarativeTest<InlineDateField> {
  33. @Test
  34. public void testErrorMessageRemoved() throws Exception {
  35. InlineDateField field = new InlineDateField("Day is",
  36. LocalDate.of(2003, 2, 27));
  37. checkValueAndComponentError(field, "2003-02-27",
  38. LocalDate.of(2003, 2, 27), false);
  39. checkValueAndComponentError(field, "", null, false);
  40. checkValueAndComponentError(field, "2003-04-27",
  41. LocalDate.of(2003, 4, 27), false);
  42. checkValueAndComponentError(field, "foo", null, true);
  43. checkValueAndComponentError(field, "2013-07-03",
  44. LocalDate.of(2013, 7, 3), false);
  45. checkValueAndComponentError(field, "foo", null, true);
  46. checkValueAndComponentError(field, "", null, false);
  47. }
  48. @Override
  49. protected String getComponentTag() {
  50. return "vaadin-inline-date-field";
  51. }
  52. @Override
  53. protected Class<? extends InlineDateField> getComponentClass() {
  54. return InlineDateField.class;
  55. }
  56. private void checkValueAndComponentError(InlineDateField field,
  57. String newInput, LocalDate expectedFieldValue,
  58. boolean componentErrorExpected) throws Exception {
  59. setDateByText(field, newInput);
  60. assertEquals(expectedFieldValue, field.getValue());
  61. assertEquals(componentErrorExpected, field.getComponentError() != null);
  62. }
  63. private void setDateByText(InlineDateField field, String dateText)
  64. throws Exception {
  65. Field rcpField = AbstractDateField.class.getDeclaredField("rpc");
  66. rcpField.setAccessible(true);
  67. AbstractDateFieldServerRpc rcp = (AbstractDateFieldServerRpc) rcpField
  68. .get(field);
  69. Map<String, Integer> resolutions = new HashMap<String, Integer>();
  70. try {
  71. LocalDate date = LocalDate.parse(dateText);
  72. resolutions.put(DateResolution.YEAR.name(), date.getYear());
  73. resolutions.put(DateResolution.MONTH.name(), date.getMonthValue());
  74. resolutions.put(DateResolution.DAY.name(), date.getDayOfMonth());
  75. } catch (Exception e) {
  76. // ignore
  77. }
  78. rcp.update(dateText, resolutions);
  79. }
  80. }