2929import static org .assertj .core .util .Arrays .array ;
3030import static org .assertj .core .util .AssertionsUtil .expectAssertionError ;
3131import static org .assertj .core .util .FailureMessages .actualIsNull ;
32+ import static org .assertj .core .util .Lists .list ;
3233import static org .assertj .core .util .Sets .set ;
3334import static org .junit .jupiter .params .provider .Arguments .arguments ;
3435
3536import java .util .LinkedHashMap ;
37+ import java .util .List ;
3638import java .util .Map ;
3739import java .util .Map .Entry ;
3840import java .util .Set ;
4143import org .apache .commons .collections4 .map .CaseInsensitiveMap ;
4244import org .apache .commons .collections4 .map .SingletonMap ;
4345import org .apache .commons .lang3 .ArrayUtils ;
46+ import org .assertj .core .data .MapEntry ;
4447import org .assertj .core .internal .MapsBaseTest ;
4548import org .junit .jupiter .api .Test ;
4649import org .junit .jupiter .params .ParameterizedTest ;
4750import org .junit .jupiter .params .provider .Arguments ;
4851import org .junit .jupiter .params .provider .MethodSource ;
52+ import org .springframework .util .MultiValueMapAdapter ;
4953
5054import com .google .common .collect .ImmutableMap ;
5155
@@ -84,16 +88,29 @@ void should_fail_if_given_entries_array_is_empty() {
8488 then (error ).hasMessage (shouldBeEmpty (actual ).create ());
8589 }
8690
91+ @ Test
92+ void should_pass_if_value_type_is_array () {
93+ // GIVEN
94+ Map <String , byte []> actual = mapOf (entry ("key1" , new byte [] { 1 , 2 }), entry ("key2" , new byte [] { 3 , 4 , 5 }));
95+ Entry <String , byte []>[] expected = array (entry ("key2" , new byte [] { 3 , 4 , 5 }), entry ("key1" , new byte [] { 1 , 2 }));
96+ // WHEN/THEN
97+ assertThatNoException ().isThrownBy (() -> maps .assertContainsOnly (info , actual , expected ));
98+ }
99+
87100 @ ParameterizedTest
88101 @ MethodSource ({
89102 "unmodifiableMapsSuccessfulTestCases" ,
90103 "modifiableMapsSuccessfulTestCases" ,
91104 "caseInsensitiveMapsSuccessfulTestCases" ,
92105 })
93106 void should_pass (Map <String , String > actual , Entry <String , String >[] expected ) {
107+ // GIVEN
108+ int initialSize = actual .size ();
94109 // WHEN/THEN
95110 assertThatNoException ().as (actual .getClass ().getName ())
96111 .isThrownBy (() -> maps .assertContainsOnly (info , actual , expected ));
112+
113+ then (actual ).hasSize (initialSize );
97114 }
98115
99116 private static Stream <Arguments > unmodifiableMapsSuccessfulTestCases () {
@@ -132,6 +149,18 @@ private static Stream<Arguments> caseInsensitiveMapsSuccessfulTestCases() {
132149 array (entry ("Job" , "Jedi" ), entry ("Name" , "Yoda" )))));
133150 }
134151
152+ @ Test
153+ void should_pass_with_MultiValueMapAdapter () {
154+ // GIVEN
155+ MultiValueMapAdapter <String , String > actual = new MultiValueMapAdapter <>(mapOf (entry ("name" , list ("Yoda" ))));
156+ Entry <String , List <String >>[] expected = array (entry ("name" , list ("Yoda" )));
157+ int initialSize = actual .size ();
158+ // WHEN
159+ maps .assertContainsOnly (info , actual , expected );
160+ // THEN
161+ then (actual ).hasSize (initialSize );
162+ }
163+
135164 @ ParameterizedTest
136165 @ MethodSource ({
137166 "unmodifiableMapsFailureTestCases" ,
@@ -142,12 +171,16 @@ private static Stream<Arguments> caseInsensitiveMapsSuccessfulTestCases() {
142171 })
143172 void should_fail (Map <String , String > actual , Entry <String , String >[] expected ,
144173 Set <Entry <String , String >> notFound , Set <Entry <String , String >> notExpected ) {
174+ // GIVEN
175+ int initialSize = actual .size ();
145176 // WHEN
146177 assertThatExceptionOfType (AssertionError .class ).as (actual .getClass ().getName ())
147178 .isThrownBy (() -> maps .assertContainsOnly (info , actual , expected ))
148179 // THEN
149180 .withMessage (shouldContainOnly (actual , expected ,
150181 notFound , notExpected ).create ());
182+
183+ then (actual ).hasSize (initialSize );
151184 }
152185
153186 private static Stream <Arguments > unmodifiableMapsFailureTestCases () {
@@ -219,14 +252,20 @@ private static Stream<Arguments> orderDependentFailureTestCases() {
219252 set (entry ("name" , "Yoda" ), entry ("job" , "Jedi" ))));
220253 }
221254
222- @ SuppressWarnings ("unchecked" )
223255 @ Test
224- void should_pass_if_value_type_is_array () {
256+ void should_fail_with_MultiValueMapAdapter () {
225257 // GIVEN
226- Map <String , byte []> actual = mapOf (entry ("key1" , new byte [] { 1 , 2 }), entry ("key2" , new byte [] { 3 , 4 , 5 }));
227- Entry <String , byte []>[] expected = new Entry [] { entry ("key2" , new byte [] { 3 , 4 , 5 }), entry ("key1" , new byte [] { 1 , 2 }) };
228- // WHEN/THEN
229- assertThatNoException ().isThrownBy (() -> maps .assertContainsOnly (info , actual , expected ));
258+ MultiValueMapAdapter <String , String > actual = new MultiValueMapAdapter <>(mapOf (entry ("name" , list ("Yoda" )),
259+ entry ("job" , list ("Jedi" ))));
260+ MapEntry <String , List <String >>[] expected = array (entry ("name" , list ("Yoda" )), entry ("color" , list ("Green" )));
261+ Set <MapEntry <String , List <String >>> notFound = set (entry ("color" , list ("Green" )));
262+ Set <MapEntry <String , List <String >>> notExpected = set (entry ("job" , list ("Jedi" )));
263+ int initialSize = actual .size ();
264+ // WHEN
265+ AssertionError error = expectAssertionError (() -> maps .assertContainsOnly (info , actual , expected ));
266+ // THEN
267+ then (error ).hasMessage (shouldContainOnly (actual , expected , notFound , notExpected ).create ());
268+ then (actual ).hasSize (initialSize );
230269 }
231270
232271}
0 commit comments