|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://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, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.core.convert.support; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import org.springframework.core.convert.ConverterNotFoundException; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 27 | + |
| 28 | +/** |
| 29 | + * Unit tests for {@link ObjectToObjectConverter}. |
| 30 | + * |
| 31 | + * @author Sam Brannen |
| 32 | + * @author Phil Webb |
| 33 | + * @since 5.3.21 |
| 34 | + * @see org.springframework.core.convert.converter.DefaultConversionServiceTests#convertObjectToObjectUsingValueOfMethod() |
| 35 | + */ |
| 36 | +class ObjectToObjectConverterTests { |
| 37 | + |
| 38 | + private final GenericConversionService conversionService = new GenericConversionService() {{ |
| 39 | + addConverter(new ObjectToObjectConverter()); |
| 40 | + }}; |
| 41 | + |
| 42 | + |
| 43 | + /** |
| 44 | + * This test effectively verifies that the {@link ObjectToObjectConverter} |
| 45 | + * was properly registered with the {@link GenericConversionService}. |
| 46 | + */ |
| 47 | + @Test |
| 48 | + void nonStaticToTargetTypeSimpleNameMethodWithMatchingReturnType() { |
| 49 | + assertThat(conversionService.canConvert(Source.class, Data.class)) |
| 50 | + .as("can convert Source to Data").isTrue(); |
| 51 | + Data data = conversionService.convert(new Source("test"), Data.class); |
| 52 | + assertThat(data).asString().isEqualTo("test"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void nonStaticToTargetTypeSimpleNameMethodWithDifferentReturnType() { |
| 57 | + assertThat(conversionService.canConvert(Text.class, Data.class)) |
| 58 | + .as("can convert Text to Data").isFalse(); |
| 59 | + assertThat(conversionService.canConvert(Text.class, Optional.class)) |
| 60 | + .as("can convert Text to Optional").isFalse(); |
| 61 | + assertThatExceptionOfType(ConverterNotFoundException.class) |
| 62 | + .as("convert Text to Data") |
| 63 | + .isThrownBy(() -> conversionService.convert(new Text("test"), Data.class)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void staticValueOfFactoryMethodWithDifferentReturnType() { |
| 68 | + assertThat(conversionService.canConvert(String.class, Data.class)) |
| 69 | + .as("can convert String to Data").isFalse(); |
| 70 | + assertThatExceptionOfType(ConverterNotFoundException.class) |
| 71 | + .as("convert String to Data") |
| 72 | + .isThrownBy(() -> conversionService.convert("test", Data.class)); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + static class Source { |
| 77 | + |
| 78 | + private final String value; |
| 79 | + |
| 80 | + private Source(String value) { |
| 81 | + this.value = value; |
| 82 | + } |
| 83 | + |
| 84 | + public Data toData() { |
| 85 | + return new Data(this.value); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + static class Text { |
| 91 | + |
| 92 | + private final String value; |
| 93 | + |
| 94 | + private Text(String value) { |
| 95 | + this.value = value; |
| 96 | + } |
| 97 | + |
| 98 | + public Optional<Data> toData() { |
| 99 | + return Optional.of(new Data(this.value)); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + static class Data { |
| 105 | + |
| 106 | + private final String value; |
| 107 | + |
| 108 | + private Data(String value) { |
| 109 | + this.value = value; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return this.value; |
| 115 | + } |
| 116 | + |
| 117 | + public static Optional<Data> valueOf(String string) { |
| 118 | + return (string != null) ? Optional.of(new Data(string)) : Optional.empty(); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments