|
7 | 7 | import java.lang.reflect.Modifier;
|
8 | 8 | import java.net.URI;
|
9 | 9 | import java.net.URISyntaxException;
|
| 10 | +import java.time.Duration; |
| 11 | +import java.time.LocalDate; |
| 12 | +import java.time.LocalDateTime; |
| 13 | +import java.time.LocalTime; |
| 14 | +import java.time.ZoneId; |
| 15 | +import java.time.ZonedDateTime; |
10 | 16 | import java.util.ArrayList;
|
11 | 17 | import java.util.Arrays;
|
12 | 18 | import java.util.Collections;
|
| 19 | +import java.util.Date; |
13 | 20 | import java.util.HashMap;
|
14 | 21 | import java.util.HashSet;
|
15 | 22 | import java.util.List;
|
|
20 | 27 | import java.util.concurrent.atomic.AtomicInteger;
|
21 | 28 | import java.util.concurrent.atomic.AtomicReference;
|
22 | 29 | import java.util.stream.Collectors;
|
| 30 | +import java.util.stream.DoubleStream; |
23 | 31 |
|
24 | 32 | import net.jcip.annotations.NotThreadSafe;
|
25 | 33 | import org.junit.Assert;
|
|
33 | 41 | import com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation;
|
34 | 42 | import com.vaadin.flow.component.page.PendingJavaScriptResult;
|
35 | 43 | import com.vaadin.flow.dom.impl.BasicElementStateProvider;
|
| 44 | +import com.vaadin.flow.internal.JsonUtils; |
36 | 45 | import com.vaadin.flow.internal.NullOwner;
|
37 | 46 | import com.vaadin.flow.internal.StateNode;
|
38 | 47 | import com.vaadin.flow.internal.nodefeature.ComponentMapping;
|
@@ -553,6 +562,24 @@ public double getDbl() {
|
553 | 562 | }
|
554 | 563 | }
|
555 | 564 |
|
| 565 | + public static class BeanWithTemporalFields { |
| 566 | + |
| 567 | + public LocalTime localTime = LocalTime.of(10, 23, 55); |
| 568 | + |
| 569 | + public LocalDate localDate = LocalDate.of(2023, 6, 26); |
| 570 | + |
| 571 | + public LocalDateTime localDateTime = localDate.atTime(localTime); |
| 572 | + |
| 573 | + public java.sql.Date sqlDate = java.sql.Date.valueOf(localDate); |
| 574 | + |
| 575 | + public Date date = new Date(sqlDate.getTime()); |
| 576 | + |
| 577 | + public ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, |
| 578 | + ZoneId.of("Europe/Rome")); |
| 579 | + |
| 580 | + public Duration duration = Duration.ofSeconds(10); |
| 581 | + } |
| 582 | + |
556 | 583 | @Test
|
557 | 584 | public void propertyRawValues() {
|
558 | 585 | Element element = ElementFactory.createDiv();
|
@@ -633,6 +660,33 @@ public void propertyNames() {
|
633 | 660 | Assert.assertEquals(0, element.getPropertyNames().count());
|
634 | 661 | }
|
635 | 662 |
|
| 663 | + @Test |
| 664 | + public void setProperty_javaTimeObject() { |
| 665 | + BeanWithTemporalFields bean = new BeanWithTemporalFields(); |
| 666 | + Element element = ElementFactory.createDiv(); |
| 667 | + |
| 668 | + element.setPropertyBean("bean", bean); |
| 669 | + JsonObject json = (JsonObject) element.getPropertyRaw("bean"); |
| 670 | + |
| 671 | + Assert.assertTrue("LocalTime not serialized as expected", |
| 672 | + JsonUtils.jsonEquals(createNumberArray(10, 23, 55), |
| 673 | + json.getArray("localTime"))); |
| 674 | + Assert.assertTrue("LocalDate not serialized as expected", |
| 675 | + JsonUtils.jsonEquals(createNumberArray(2023, 6, 26), |
| 676 | + json.getArray("localDate"))); |
| 677 | + Assert.assertTrue("LocalDateTime not serialized as expected", |
| 678 | + JsonUtils.jsonEquals(createNumberArray(2023, 6, 26, 10, 23, 55), |
| 679 | + json.getArray("localDateTime"))); |
| 680 | + Assert.assertEquals("ZonedDateTime not serialized as expected", |
| 681 | + bean.zonedDateTime.toEpochSecond(), |
| 682 | + json.getNumber("zonedDateTime"), 0); |
| 683 | + Assert.assertEquals("ZonedDateTime not serialized as expected", |
| 684 | + bean.sqlDate.getTime(), json.getNumber("sqlDate"), 0); |
| 685 | + Assert.assertEquals("ZonedDateTime not serialized as expected", |
| 686 | + bean.date.getTime(), json.getNumber("date"), 0); |
| 687 | + Assert.assertEquals(10.0, json.getNumber("duration"), 0); |
| 688 | + } |
| 689 | + |
636 | 690 | private static Element createPropertyAssertElement(Object value) {
|
637 | 691 | Element element = ElementFactory.createDiv();
|
638 | 692 |
|
@@ -2534,4 +2588,10 @@ private void assertEquals(JavaScriptInvocation expected,
|
2534 | 2588 | actual.getParameters().toArray());
|
2535 | 2589 |
|
2536 | 2590 | }
|
| 2591 | + |
| 2592 | + private static JsonArray createNumberArray(double... items) { |
| 2593 | + return DoubleStream.of(items).mapToObj(Json::create) |
| 2594 | + .collect(JsonUtils.asArray()); |
| 2595 | + } |
| 2596 | + |
2537 | 2597 | }
|
0 commit comments