View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Properties;
24  
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertNotNull;
29  import static org.junit.jupiter.api.Assertions.assertNull;
30  
31  /**
32   * <p>CollectionUtilsTest class.</p>
33   *
34   * @author herve
35   * @since 3.4.0
36   */
37  class CollectionUtilsTest {
38  
39      @Test
40      void mergeMaps() {
41          Map<String, String> dominantMap = new HashMap<>();
42          dominantMap.put("a", "a");
43          dominantMap.put("b", "b");
44          dominantMap.put("c", "c");
45          dominantMap.put("d", "d");
46          dominantMap.put("e", "e");
47          dominantMap.put("f", "f");
48  
49          Map<String, String> recessiveMap = new HashMap<>();
50          recessiveMap.put("a", "invalid");
51          recessiveMap.put("b", "invalid");
52          recessiveMap.put("c", "invalid");
53          recessiveMap.put("x", "x");
54          recessiveMap.put("y", "y");
55          recessiveMap.put("z", "z");
56  
57          Map<String, String> result = CollectionUtils.mergeMaps(dominantMap, recessiveMap);
58  
59          // We should have 9 elements
60          assertEquals(9, result.size());
61  
62          // Check the elements.
63          assertEquals("a", result.get("a"));
64          assertEquals("b", result.get("b"));
65          assertEquals("c", result.get("c"));
66          assertEquals("d", result.get("d"));
67          assertEquals("e", result.get("e"));
68          assertEquals("f", result.get("f"));
69          assertEquals("x", result.get("x"));
70          assertEquals("y", result.get("y"));
71          assertEquals("z", result.get("z"));
72      }
73  
74      @SuppressWarnings("unchecked")
75      @Test
76      void mergeMapArray() {
77          // Test empty array of Maps
78          Map<String, String> result0 = CollectionUtils.mergeMaps(new Map[] {});
79  
80          assertNull(result0);
81  
82          // Test with an array with a single element.
83          Map<String, String> map1 = new HashMap<>();
84          map1.put("a", "a");
85  
86          Map<String, String> result1 = CollectionUtils.mergeMaps(new Map[] {map1});
87  
88          assertEquals("a", result1.get("a"));
89  
90          // Test with an array with two elements.
91          Map<String, String> map2 = new HashMap<>();
92          map2.put("a", "aa");
93          map2.put("b", "bb");
94  
95          Map<String, String> result2 = CollectionUtils.mergeMaps(new Map[] {map1, map2});
96  
97          assertEquals("a", result2.get("a"));
98          assertEquals("bb", result2.get("b"));
99  
100         // Now swap the dominant order.
101         Map<String, String> result3 = CollectionUtils.mergeMaps(new Map[] {map2, map1});
102 
103         assertEquals("aa", result3.get("a"));
104         assertEquals("bb", result3.get("b"));
105 
106         // Test with an array with three elements.
107         Map<String, String> map3 = new HashMap<>();
108         map3.put("a", "aaa");
109         map3.put("b", "bbb");
110         map3.put("c", "ccc");
111 
112         Map<String, String> result4 = CollectionUtils.mergeMaps(new Map[] {map1, map2, map3});
113 
114         assertEquals("a", result4.get("a"));
115         assertEquals("bb", result4.get("b"));
116         assertEquals("ccc", result4.get("c"));
117 
118         // Now swap the dominant order.
119         Map<String, String> result5 = CollectionUtils.mergeMaps(new Map[] {map3, map2, map1});
120 
121         assertEquals("aaa", result5.get("a"));
122         assertEquals("bbb", result5.get("b"));
123         assertEquals("ccc", result5.get("c"));
124     }
125 
126     @Test
127     void mavenPropertiesLoading() {
128         // Mimic MavenSession properties loading. Properties listed
129         // in dominant order.
130         Properties systemProperties = new Properties();
131         Properties userBuildProperties = new Properties();
132         Properties projectBuildProperties = new Properties();
133         Properties projectProperties = new Properties();
134         Properties driverProperties = new Properties();
135 
136         // System properties
137         systemProperties.setProperty("maven.home", "/projects/maven");
138 
139         // User build properties
140         userBuildProperties.setProperty("maven.username", "jvanzyl");
141         userBuildProperties.setProperty("maven.repo.remote.enabled", "false");
142         userBuildProperties.setProperty("maven.repo.local", "/opt/maven/artifact");
143 
144         // Project build properties
145         projectBuildProperties.setProperty("maven.final.name", "maven");
146 
147         String mavenRepoRemote = "http://www.ibiblio.org/maven,http://foo/bar";
148 
149         // Project properties
150         projectProperties.setProperty("maven.repo.remote", mavenRepoRemote);
151 
152         String basedir = "/home/jvanzyl/projects/maven";
153 
154         // Driver properties
155         driverProperties.setProperty("basedir", basedir);
156         driverProperties.setProperty("maven.build.src", "${basedir}/src");
157         driverProperties.setProperty("maven.build.dir", "${basedir}/target");
158         driverProperties.setProperty("maven.build.dest", "${maven.build.dir}/classes");
159         driverProperties.setProperty("maven.repo.remote", "http://www.ibiblio.org/maven");
160         driverProperties.setProperty("maven.final.name", "maven-1.0");
161         driverProperties.setProperty("maven.repo.remote.enabled", "true");
162         driverProperties.setProperty("maven.repo.local", "${maven.home}/artifact");
163 
164         Map result = CollectionUtils.mergeMaps(new Map[] {
165             systemProperties, userBuildProperties, projectBuildProperties, projectProperties, driverProperties
166         });
167 
168         // Values that should be taken from systemProperties.
169         assertEquals("/projects/maven", result.get("maven.home"));
170 
171         // Values that should be taken from userBuildProperties.
172         assertEquals("/opt/maven/artifact", result.get("maven.repo.local"));
173         assertEquals("false", result.get("maven.repo.remote.enabled"));
174         assertEquals("jvanzyl", result.get("maven.username"));
175 
176         // Values take from projectBuildProperties.
177         assertEquals("maven", result.get("maven.final.name"));
178 
179         // Values take from projectProperties.
180         assertEquals(mavenRepoRemote, result.get("maven.repo.remote"));
181     }
182 
183     @Test
184     void iteratorToListWithAPopulatedList() {
185         List<String> original = new ArrayList<>();
186 
187         original.add("en");
188         original.add("to");
189         original.add("tre");
190 
191         List<String> copy = CollectionUtils.iteratorToList(original.iterator());
192 
193         assertNotNull(copy);
194 
195         assertEquals(3, copy.size());
196 
197         assertEquals("en", copy.get(0));
198         assertEquals("to", copy.get(1));
199         assertEquals("tre", copy.get(2));
200     }
201 
202     @Test
203     void iteratorToListWithAEmptyList() {
204         List<String> original = new ArrayList<>();
205 
206         List<String> copy = CollectionUtils.iteratorToList(original.iterator());
207 
208         assertNotNull(copy);
209 
210         assertEquals(0, copy.size());
211     }
212 }