1
1
/*
2
- * Copyright 2002-2019 the original author or authors.
2
+ * Copyright 2002-2022 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import org .springframework .core .convert .ConversionFailedException ;
27
27
import org .springframework .core .convert .ConverterNotFoundException ;
28
28
import org .springframework .core .convert .TypeDescriptor ;
29
- import org .springframework .core .convert .converter .Converter ;
30
29
31
30
import static org .assertj .core .api .Assertions .assertThat ;
32
31
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
33
32
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
33
+ import static org .assertj .core .api .InstanceOfAssertFactories .list ;
34
+ import static org .assertj .core .api .InstanceOfAssertFactories .stream ;
34
35
35
36
/**
36
37
* Tests for {@link StreamConverter}.
37
38
*
38
39
* @author Stephane Nicoll
40
+ * @author Sam Brannen
39
41
* @since 4.2
40
42
*/
41
43
class StreamConverterTests {
@@ -57,74 +59,53 @@ void setup() {
57
59
@ Test
58
60
void convertFromStreamToList () throws NoSuchFieldException {
59
61
this .conversionService .addConverter (Number .class , String .class , new ObjectToStringConverter ());
60
- Stream <Integer > stream = Arrays . asList (1 , 2 , 3 ). stream ( );
62
+ Stream <Integer > stream = Stream . of (1 , 2 , 3 );
61
63
TypeDescriptor listOfStrings = new TypeDescriptor (Types .class .getField ("listOfStrings" ));
62
64
Object result = this .conversionService .convert (stream , listOfStrings );
63
65
64
- assertThat (result ).as ("Converted object must not be null" ).isNotNull ();
65
- boolean condition = result instanceof List ;
66
- assertThat (condition ).as ("Converted object must be a list" ).isTrue ();
67
- @ SuppressWarnings ("unchecked" )
68
- List <String > content = (List <String >) result ;
69
- assertThat (content .get (0 )).isEqualTo ("1" );
70
- assertThat (content .get (1 )).isEqualTo ("2" );
71
- assertThat (content .get (2 )).isEqualTo ("3" );
72
- assertThat (content .size ()).as ("Wrong number of elements" ).isEqualTo (3 );
66
+ assertThat (result ).asInstanceOf (list (String .class )).containsExactly ("1" , "2" , "3" );
73
67
}
74
68
75
69
@ Test
76
70
void convertFromStreamToArray () throws NoSuchFieldException {
77
71
this .conversionService .addConverterFactory (new NumberToNumberConverterFactory ());
78
- Stream <Integer > stream = Arrays . asList (1 , 2 , 3 ). stream ( );
72
+ Stream <Integer > stream = Stream . of (1 , 2 , 3 );
79
73
TypeDescriptor arrayOfLongs = new TypeDescriptor (Types .class .getField ("arrayOfLongs" ));
80
74
Object result = this .conversionService .convert (stream , arrayOfLongs );
81
75
82
76
assertThat (result ).as ("Converted object must not be null" ).isNotNull ();
83
77
assertThat (result .getClass ().isArray ()).as ("Converted object must be an array" ).isTrue ();
84
78
Long [] content = (Long []) result ;
85
- assertThat (content [0 ]).isEqualTo (Long .valueOf (1L ));
86
- assertThat (content [1 ]).isEqualTo (Long .valueOf (2L ));
87
- assertThat (content [2 ]).isEqualTo (Long .valueOf (3L ));
88
- assertThat (content .length ).as ("Wrong number of elements" ).isEqualTo (3 );
79
+ assertThat (content ).containsExactly (1L , 2L , 3L );
89
80
}
90
81
91
82
@ Test
92
83
void convertFromStreamToRawList () throws NoSuchFieldException {
93
- Stream <Integer > stream = Arrays . asList (1 , 2 , 3 ). stream ( );
84
+ Stream <Integer > stream = Stream . of (1 , 2 , 3 );
94
85
TypeDescriptor listOfStrings = new TypeDescriptor (Types .class .getField ("rawList" ));
95
86
Object result = this .conversionService .convert (stream , listOfStrings );
96
87
97
- assertThat (result ).as ("Converted object must not be null" ).isNotNull ();
98
- boolean condition = result instanceof List ;
99
- assertThat (condition ).as ("Converted object must be a list" ).isTrue ();
100
- @ SuppressWarnings ("unchecked" )
101
- List <Object > content = (List <Object >) result ;
102
- assertThat (content .get (0 )).isEqualTo (1 );
103
- assertThat (content .get (1 )).isEqualTo (2 );
104
- assertThat (content .get (2 )).isEqualTo (3 );
105
- assertThat (content .size ()).as ("Wrong number of elements" ).isEqualTo (3 );
88
+ assertThat (result ).asInstanceOf (list (Object .class )).containsExactly (1 , 2 , 3 );
106
89
}
107
90
108
91
@ Test
109
92
void convertFromStreamToArrayNoConverter () throws NoSuchFieldException {
110
- Stream <Integer > stream = Arrays . asList (1 , 2 , 3 ). stream ( );
93
+ Stream <Integer > stream = Stream . of (1 , 2 , 3 );
111
94
TypeDescriptor arrayOfLongs = new TypeDescriptor (Types .class .getField ("arrayOfLongs" ));
112
- assertThatExceptionOfType (ConversionFailedException .class ). isThrownBy (() ->
113
- this .conversionService .convert (stream , arrayOfLongs ))
95
+ assertThatExceptionOfType (ConversionFailedException .class )
96
+ . isThrownBy (() -> this .conversionService .convert (stream , arrayOfLongs ))
114
97
.withCauseInstanceOf (ConverterNotFoundException .class );
115
98
}
116
99
117
100
@ Test
118
101
@ SuppressWarnings ("resource" )
119
102
void convertFromListToStream () throws NoSuchFieldException {
120
103
this .conversionService .addConverterFactory (new StringToNumberConverterFactory ());
121
- List <String > stream = Arrays .asList ("1" , "2" , "3" );
104
+ List <String > list = Arrays .asList ("1" , "2" , "3" );
122
105
TypeDescriptor streamOfInteger = new TypeDescriptor (Types .class .getField ("streamOfIntegers" ));
123
- Object result = this .conversionService .convert (stream , streamOfInteger );
106
+ Object result = this .conversionService .convert (list , streamOfInteger );
124
107
125
- assertThat (result ).as ("Converted object must not be null" ).isNotNull ();
126
- boolean condition = result instanceof Stream ;
127
- assertThat (condition ).as ("Converted object must be a stream" ).isTrue ();
108
+ assertThat (result ).as ("Converted object must be a stream" ).isInstanceOf (Stream .class );
128
109
@ SuppressWarnings ("unchecked" )
129
110
Stream <Integer > content = (Stream <Integer >) result ;
130
111
assertThat (content .mapToInt (x -> x ).sum ()).isEqualTo (6 );
@@ -133,39 +114,25 @@ void convertFromListToStream() throws NoSuchFieldException {
133
114
@ Test
134
115
@ SuppressWarnings ("resource" )
135
116
void convertFromArrayToStream () throws NoSuchFieldException {
136
- Integer [] stream = new Integer [] {1 , 0 , 1 };
137
- this .conversionService .addConverter (new Converter <Integer , Boolean >() {
138
- @ Override
139
- public Boolean convert (Integer source ) {
140
- return source == 1 ;
141
- }
142
- });
117
+ Integer [] array = new Integer [] {1 , 0 , 1 };
118
+ this .conversionService .addConverter (Integer .class , Boolean .class , source -> source == 1 );
143
119
TypeDescriptor streamOfBoolean = new TypeDescriptor (Types .class .getField ("streamOfBooleans" ));
144
- Object result = this .conversionService .convert (stream , streamOfBoolean );
120
+ Object result = this .conversionService .convert (array , streamOfBoolean );
145
121
146
- assertThat (result ).as ("Converted object must not be null" ).isNotNull ();
147
- boolean condition = result instanceof Stream ;
148
- assertThat (condition ).as ("Converted object must be a stream" ).isTrue ();
149
- @ SuppressWarnings ("unchecked" )
150
- Stream <Boolean > content = (Stream <Boolean >) result ;
151
- assertThat (content .filter (x -> x ).count ()).isEqualTo (2 );
122
+ assertThat (result ).asInstanceOf (stream (Boolean .class )).filteredOn (x -> x ).hasSize (2 );
152
123
}
153
124
154
125
@ Test
155
126
@ SuppressWarnings ("resource" )
156
127
void convertFromListToRawStream () throws NoSuchFieldException {
157
- List <String > stream = Arrays .asList ("1" , "2" , "3" );
128
+ List <String > list = Arrays .asList ("1" , "2" , "3" );
158
129
TypeDescriptor streamOfInteger = new TypeDescriptor (Types .class .getField ("rawStream" ));
159
- Object result = this .conversionService .convert (stream , streamOfInteger );
130
+ Object result = this .conversionService .convert (list , streamOfInteger );
160
131
161
- assertThat (result ).as ("Converted object must not be null" ).isNotNull ();
162
- boolean condition = result instanceof Stream ;
163
- assertThat (condition ).as ("Converted object must be a stream" ).isTrue ();
132
+ assertThat (result ).as ("Converted object must be a stream" ).isInstanceOf (Stream .class );
164
133
@ SuppressWarnings ("unchecked" )
165
134
Stream <Object > content = (Stream <Object >) result ;
166
- StringBuilder sb = new StringBuilder ();
167
- content .forEach (sb ::append );
168
- assertThat (sb .toString ()).isEqualTo ("123" );
135
+ assertThat (content ).containsExactly ("1" , "2" , "3" );
169
136
}
170
137
171
138
@ Test
0 commit comments