1616 */
1717package org .apache .commons .collections4 ;
1818
19+ import static org .easymock .EasyMock .expect ;
20+ import static org .easymock .EasyMock .replay ;
21+ import static org .easymock .EasyMock .verify ;
22+ import static org .easymock .EasyMock .createMock ;
1923import static org .junit .jupiter .api .Assertions .assertEquals ;
2024import static org .junit .jupiter .api .Assertions .assertFalse ;
2125import static org .junit .jupiter .api .Assertions .assertNull ;
2832import java .util .List ;
2933import java .util .Set ;
3034
35+ import org .apache .commons .collections4 .bag .HashBag ;
3136import org .apache .commons .collections4 .multimap .ArrayListValuedHashMap ;
3237import org .apache .commons .collections4 .multimap .HashSetValuedHashMap ;
3338import org .apache .commons .collections4 .multimap .LinkedHashSetValuedLinkedHashMap ;
@@ -105,7 +110,7 @@ void testGetValuesAsList() {
105110
106111 @ Test
107112 void testGetValuesAsSet () {
108- assertNull (MultiMapUtils .getValuesAsList (null , "key1" ));
113+ assertNull (MultiMapUtils .getValuesAsSet (null , "key1" ));
109114
110115 final String [] values = { "v1" , "v2" , "v3" };
111116 final MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
@@ -118,6 +123,47 @@ void testGetValuesAsSet() {
118123 assertEquals (new HashSet <>(Arrays .asList (values )), set );
119124 }
120125
126+ @ Test
127+ void testGetValuesAsBagIsSafeCopy () {
128+ final String [] values = { "v1" , "v2" , "v3" };
129+ final MultiValuedMap <String , String > mockMap = createMock (MultiValuedMap .class );
130+ final Bag <String > bagToReturn = new HashBag <>();
131+ bagToReturn .addAll (Arrays .asList (values ));
132+ expect (mockMap .get ("key1" )).andReturn (bagToReturn );
133+ replay (mockMap );
134+
135+ final Bag <String > bag = MultiMapUtils .getValuesAsBag (mockMap , "key1" );
136+ bag .add ("v4" );
137+ assertFalse (bagToReturn .contains ("v4" ));
138+ verify (mockMap );
139+ }
140+
141+ @ Test
142+ void testGetValuesAsListIsSafeCopy () {
143+ final String [] values = { "v1" , "v2" , "v3" };
144+ final MultiValuedMap <String , String > map = new ArrayListValuedHashMap <>();
145+ for (final String val : values ) {
146+ map .put ("key1" , val );
147+ }
148+
149+ final List <String > list = MultiMapUtils .getValuesAsList (map , "key1" );
150+ list .add ("v4" );
151+ assertFalse (map .containsMapping ("key1" , "v4" ));
152+ }
153+
154+ @ Test
155+ void testGetValuesAsSetIsSafeCopy () {
156+ final String [] values = { "v1" , "v2" , "v3" };
157+ final MultiValuedMap <String , String > map = new HashSetValuedHashMap <>();
158+ for (final String val : values ) {
159+ map .put ("key1" , val );
160+ }
161+
162+ final Set <String > set = MultiMapUtils .getValuesAsSet (map , "key1" );
163+ set .add ("v4" );
164+ assertFalse (map .containsMapping ("key1" , "v4" ));
165+ }
166+
121167 @ Test
122168 void testInvert () {
123169 final HashSetValuedHashMap <String , String > usages = new HashSetValuedHashMap <>();
0 commit comments