|
| 1 | +/* |
| 2 | + * Copyright 2018-2019 Karakun AG. |
| 3 | + * Copyright 2015-2018 Canoo Engineering AG. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package dev.rico.internal.remoting.converters; |
| 18 | + |
| 19 | +import dev.rico.remoting.converter.Converter; |
| 20 | +import dev.rico.remoting.converter.ValueConverterException; |
| 21 | +import org.testng.Assert; |
| 22 | +import org.testng.annotations.Test; |
| 23 | + |
| 24 | +import java.text.ParseException; |
| 25 | +import java.time.LocalDate; |
| 26 | +import java.time.LocalDateTime; |
| 27 | +import java.time.LocalTime; |
| 28 | +import java.time.ZonedDateTime; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.Objects; |
| 31 | +import java.util.TimeZone; |
| 32 | + |
| 33 | +public class LocalDateConverterTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testSupportedType() { |
| 37 | + //given |
| 38 | + final LocalDateConverterFactory factory = new LocalDateConverterFactory(); |
| 39 | + |
| 40 | + //then |
| 41 | + Assert.assertTrue(factory.supportsType(LocalDate.class)); |
| 42 | + Assert.assertFalse(factory.supportsType(ZonedDateTime.class)); |
| 43 | + Assert.assertFalse(factory.supportsType(LocalDateTime.class)); |
| 44 | + Assert.assertFalse(factory.supportsType(LocalTime.class)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testNullValue() throws ValueConverterException { |
| 49 | + //given |
| 50 | + final LocalDateConverterFactory factory = new LocalDateConverterFactory(); |
| 51 | + final Converter converter = factory.getConverterForType(LocalDate.class); |
| 52 | + |
| 53 | + //when |
| 54 | + final Object rawObject = converter.convertToRemoting(null); |
| 55 | + final Object reConverted = converter.convertFromRemoting(rawObject); |
| 56 | + |
| 57 | + //then |
| 58 | + Assert.assertNull(reConverted); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testSameTimeZone() throws ValueConverterException { |
| 63 | + //given |
| 64 | + final LocalDateConverterFactory factory = new LocalDateConverterFactory(); |
| 65 | + final LocalDate time = LocalDate.now(); |
| 66 | + final Converter converter = factory.getConverterForType(LocalDate.class); |
| 67 | + |
| 68 | + //when |
| 69 | + final Object rawObject = converter.convertToRemoting(time); |
| 70 | + System.out.println(rawObject); |
| 71 | + final Object reConverted = converter.convertFromRemoting(rawObject); |
| 72 | + |
| 73 | + //then |
| 74 | + Assert.assertNotNull(rawObject); |
| 75 | + Assert.assertNotNull(reConverted); |
| 76 | + Assert.assertTrue(LocalDate.class.isAssignableFrom(reConverted.getClass())); |
| 77 | + final LocalDate reconvertedTime = (LocalDate) reConverted; |
| 78 | + Assert.assertEquals(reconvertedTime, time); |
| 79 | + } |
| 80 | + |
| 81 | + @Test(enabled = false) |
| 82 | + public void testDifferentTimeZone() throws ValueConverterException { |
| 83 | + final TimeZone defaultZone = TimeZone.getDefault(); |
| 84 | + try { |
| 85 | + |
| 86 | + //given |
| 87 | + final LocalDateConverterFactory factory = new LocalDateConverterFactory(); |
| 88 | + final LocalDate time = LocalDate.now(); |
| 89 | + final Converter converter = factory.getConverterForType(LocalDate.class); |
| 90 | + final TimeZone differentZone = Arrays.asList(TimeZone.getAvailableIDs()).stream() |
| 91 | + .map(id -> TimeZone.getTimeZone(id)) |
| 92 | + .filter(zone -> !Objects.equals(defaultZone, zone)) |
| 93 | + .findAny() |
| 94 | + .orElseThrow(() -> new RuntimeException("No time zone found")); |
| 95 | + |
| 96 | + //when |
| 97 | + final Object rawObject = converter.convertToRemoting(time); |
| 98 | + TimeZone.setDefault(differentZone); |
| 99 | + final Object reConverted = converter.convertFromRemoting(rawObject); |
| 100 | + |
| 101 | + //then |
| 102 | + Assert.assertNotNull(rawObject); |
| 103 | + Assert.assertNotNull(reConverted); |
| 104 | + Assert.assertTrue(LocalDate.class.isAssignableFrom(reConverted.getClass())); |
| 105 | + final LocalDate reconvertedTime = (LocalDate) reConverted; |
| 106 | + Assert.assertEquals(reconvertedTime, time); |
| 107 | + } finally { |
| 108 | + TimeZone.setDefault(defaultZone); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testRawSameTimeZone() throws ValueConverterException, ParseException { |
| 114 | + final TimeZone defaultZone = TimeZone.getDefault(); |
| 115 | + try { |
| 116 | + |
| 117 | + //given |
| 118 | + final LocalDateConverterFactory localDateFactory = new LocalDateConverterFactory(); |
| 119 | + final Converter localDateConverter = localDateFactory.getConverterForType(LocalDate.class); |
| 120 | + final ZonedDateTimeConverterFactory zonedDateTimeFactory = new ZonedDateTimeConverterFactory(); |
| 121 | + final Converter zonedDateTimeConverter = zonedDateTimeFactory.getConverterForType(LocalDate.class); |
| 122 | + final String rawObject = "2019-01-30T11:25:07.341+03:00"; |
| 123 | + final ZonedDateTime zonedDateTime = (ZonedDateTime) zonedDateTimeConverter.convertFromRemoting(rawObject); |
| 124 | + final LocalDate fromZonedTime = LocalDate.from(zonedDateTime); |
| 125 | + |
| 126 | + //when |
| 127 | + final Object reconvertedLocalDate = localDateConverter.convertFromRemoting(rawObject); |
| 128 | + |
| 129 | + //then |
| 130 | + Assert.assertNotNull(reconvertedLocalDate); |
| 131 | + Assert.assertTrue(LocalDate.class.isAssignableFrom(reconvertedLocalDate.getClass())); |
| 132 | + final LocalDate reconvertedTime = (LocalDate) reconvertedLocalDate; |
| 133 | + Assert.assertEquals(reconvertedTime, fromZonedTime); |
| 134 | + |
| 135 | + } finally { |
| 136 | + TimeZone.setDefault(defaultZone); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments