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