1
+ /*
2
+ * Copyright 2002-2020 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
+
1
17
package org .springframework .core .convert .converter ;
2
18
3
19
import org .junit .jupiter .api .Test ;
4
20
5
21
import static org .assertj .core .api .Assertions .assertThat ;
6
- import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
22
+ import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
7
23
8
24
/**
9
25
* Tests for {@link Converter}
10
26
*
11
27
* @author Josh Cummings
28
+ * @author Sam Brannen
29
+ * @since 5.3
12
30
*/
13
- public class ConverterTests {
14
- Converter <Integer , Integer > moduloTwo = number -> number % 2 ;
31
+ class ConverterTests {
32
+
33
+ private final Converter <Integer , Integer > moduloTwo = number -> number % 2 ;
34
+ private final Converter <Integer , Integer > addOne = number -> number + 1 ;
35
+
15
36
16
37
@ Test
17
- public void andThenWhenGivenANullConverterThenThrowsException () {
18
- assertThatExceptionOfType (IllegalArgumentException .class )
19
- .isThrownBy (() -> this .moduloTwo .andThen (null ));
38
+ void andThenWhenGivenANullConverterThenThrowsException () {
39
+ assertThatIllegalArgumentException ().isThrownBy (() -> this .moduloTwo .andThen (null ));
20
40
}
21
41
22
42
@ Test
23
- public void andThenWhenGivenConverterThenComposesInOrder () {
24
- Converter <Integer , Integer > addOne = number -> number + 1 ;
25
- assertThat (this .moduloTwo .andThen (addOne ).convert (13 )).isEqualTo (2 );
26
- assertThat (addOne .andThen (this .moduloTwo ).convert (13 )).isEqualTo (0 );
43
+ void andThenWhenGivenConverterThenComposesInOrder () {
44
+ assertThat (this .moduloTwo .andThen (this .addOne ).convert (13 )).isEqualTo (2 );
45
+ assertThat (this .addOne .andThen (this .moduloTwo ).convert (13 )).isEqualTo (0 );
27
46
}
28
- }
47
+
48
+ @ Test
49
+ void andThenCanConvertfromDifferentSourceType () {
50
+ Converter <String , Integer > length = String ::length ;
51
+ assertThat (length .andThen (this .moduloTwo ).convert ("example" )).isEqualTo (1 );
52
+ assertThat (length .andThen (this .addOne ).convert ("example" )).isEqualTo (8 );
53
+ }
54
+
55
+ @ Test
56
+ void andThenCanConvertToDifferentTargetType () {
57
+ Converter <String , Integer > length = String ::length ;
58
+ Converter <Integer , String > toString = Object ::toString ;
59
+ assertThat (length .andThen (toString ).convert ("example" )).isEqualTo ("7" );
60
+ assertThat (toString .andThen (length ).convert (1_000 )).isEqualTo (4 );
61
+ }
62
+
63
+ }
0 commit comments