Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

HasValueTest.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2000-2016 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.data;
  17. import java.time.LocalDate;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import org.mockito.Mockito;
  21. import com.vaadin.ui.DateField;
  22. import com.vaadin.ui.TextField;
  23. /**
  24. * @author Vaadin Ltd
  25. *
  26. */
  27. public class HasValueTest {
  28. public abstract static class TestHasValue implements HasValue<String> {
  29. @Override
  30. public void clear() {
  31. HasValue.super.clear();
  32. }
  33. }
  34. @Test
  35. public void clear() {
  36. TestHasValue hasValue = Mockito.mock(TestHasValue.class);
  37. Mockito.doCallRealMethod().when(hasValue).clear();
  38. String value = "foo";
  39. Mockito.when(hasValue.getEmptyValue()).thenReturn(value);
  40. hasValue.clear();
  41. Mockito.verify(hasValue).setValue(value);
  42. }
  43. @Test
  44. public void getOptionalValue_nullableHasValue() {
  45. HasValue<LocalDate> nullable = new DateField();
  46. // Not using Assert since we're only verifying that DateField is working
  47. // in a way appropriate for this test
  48. assert nullable.isEmpty();
  49. assert nullable.getValue() == null;
  50. Assert.assertFalse(nullable.getOptionalValue().isPresent());
  51. nullable.setValue(LocalDate.now());
  52. assert !nullable.isEmpty();
  53. Assert.assertSame(nullable.getValue(),
  54. nullable.getOptionalValue().get());
  55. }
  56. @Test
  57. public void getOptionalValue_nonNullableHasValue() {
  58. HasValue<String> nonNullable = new TextField();
  59. // Not using Assert since we're only verifying that TextField is working
  60. // in a way appropriate for this test
  61. assert nonNullable.isEmpty();
  62. assert nonNullable.getValue() != null;
  63. Assert.assertFalse(nonNullable.getOptionalValue().isPresent());
  64. nonNullable.setValue("foo");
  65. assert !nonNullable.isEmpty();
  66. Assert.assertSame(nonNullable.getValue(),
  67. nonNullable.getOptionalValue().get());
  68. }
  69. }