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 junit.framework.TestCase;
26  
27  public class CollectionUtilsTest
28      extends TestCase
29  {
30      public void testMergeMaps()
31      {
32          Map<String, String> dominantMap = new HashMap<String, String>();
33          dominantMap.put( "a", "a" );
34          dominantMap.put( "b", "b" );
35          dominantMap.put( "c", "c" );
36          dominantMap.put( "d", "d" );
37          dominantMap.put( "e", "e" );
38          dominantMap.put( "f", "f" );
39  
40          Map<String, String> recessiveMap = new HashMap<String, String>();
41          recessiveMap.put( "a", "invalid" );
42          recessiveMap.put( "b", "invalid" );
43          recessiveMap.put( "c", "invalid" );
44          recessiveMap.put( "x", "x" );
45          recessiveMap.put( "y", "y" );
46          recessiveMap.put( "z", "z" );
47  
48          Map<String, String> result = CollectionUtils.mergeMaps( dominantMap, recessiveMap );
49  
50          // We should have 9 elements
51          assertEquals( 9, result.keySet().size() );
52  
53          // Check the elements.
54          assertEquals( "a", result.get( "a" ) );
55          assertEquals( "b", result.get( "b" ) );
56          assertEquals( "c", result.get( "c" ) );
57          assertEquals( "d", result.get( "d" ) );
58          assertEquals( "e", result.get( "e" ) );
59          assertEquals( "f", result.get( "f" ) );
60          assertEquals( "x", result.get( "x" ) );
61          assertEquals( "y", result.get( "y" ) );
62          assertEquals( "z", result.get( "z" ) );
63      }
64  
65      @SuppressWarnings( "unchecked" )
66      public void testMergeMapArray()
67      {
68          // Test empty array of Maps
69          Map<String, String> result0 = CollectionUtils.mergeMaps( new Map[] {} );
70  
71          assertNull( result0 );
72  
73          // Test with an array with a single element.
74          Map<String, String> map1 = new HashMap<String, String>();
75          map1.put( "a", "a" );
76  
77          Map<String, String> result1 = CollectionUtils.mergeMaps( new Map[] { map1 } );
78  
79          assertEquals( "a", result1.get( "a" ) );
80  
81          // Test with an array with two elements.
82          Map<String, String> map2 = new HashMap<String, String>();
83          map2.put( "a", "aa" );
84          map2.put( "b", "bb" );
85  
86          Map<String, String> result2 = CollectionUtils.mergeMaps( new Map[] { map1, map2 } );
87  
88          assertEquals( "a", result2.get( "a" ) );
89          assertEquals( "bb", result2.get( "b" ) );
90  
91          // Now swap the dominant order.
92          Map<String, String> result3 = CollectionUtils.mergeMaps( new Map[] { map2, map1 } );
93  
94          assertEquals( "aa", result3.get( "a" ) );
95          assertEquals( "bb", result3.get( "b" ) );
96  
97          // Test with an array with three elements.
98          Map<String, String> map3 = new HashMap<String, String>();
99          map3.put( "a", "aaa" );
100         map3.put( "b", "bbb" );
101         map3.put( "c", "ccc" );
102 
103         Map<String, String> result4 = CollectionUtils.mergeMaps( new Map[] { map1, map2, map3 } );
104 
105         assertEquals( "a", result4.get( "a" ) );
106         assertEquals( "bb", result4.get( "b" ) );
107         assertEquals( "ccc", result4.get( "c" ) );
108 
109         // Now swap the dominant order.
110         Map<String, String> result5 = CollectionUtils.mergeMaps( new Map[] { map3, map2, map1 } );
111 
112         assertEquals( "aaa", result5.get( "a" ) );
113         assertEquals( "bbb", result5.get( "b" ) );
114         assertEquals( "ccc", result5.get( "c" ) );
115     }
116 
117     public void testMavenPropertiesLoading()
118     {
119         // Mimic MavenSession properties loading. Properties listed
120         // in dominant order.
121         Properties systemProperties = new Properties();
122         Properties userBuildProperties = new Properties();
123         Properties projectBuildProperties = new Properties();
124         Properties projectProperties = new Properties();
125         Properties driverProperties = new Properties();
126 
127         // System properties
128         systemProperties.setProperty( "maven.home", "/projects/maven" );
129 
130         // User build properties
131         userBuildProperties.setProperty( "maven.username", "jvanzyl" );
132         userBuildProperties.setProperty( "maven.repo.remote.enabled", "false" );
133         userBuildProperties.setProperty( "maven.repo.local", "/opt/maven/artifact" );
134 
135         // Project build properties
136         projectBuildProperties.setProperty( "maven.final.name", "maven" );
137 
138         String mavenRepoRemote = "http://www.ibiblio.org/maven,http://foo/bar";
139 
140         // Project properties
141         projectProperties.setProperty( "maven.repo.remote", mavenRepoRemote );
142 
143         String basedir = "/home/jvanzyl/projects/maven";
144 
145         // Driver properties
146         driverProperties.setProperty( "basedir", basedir );
147         driverProperties.setProperty( "maven.build.src", "${basedir}/src" );
148         driverProperties.setProperty( "maven.build.dir", "${basedir}/target" );
149         driverProperties.setProperty( "maven.build.dest", "${maven.build.dir}/classes" );
150         driverProperties.setProperty( "maven.repo.remote", "http://www.ibiblio.org/maven" );
151         driverProperties.setProperty( "maven.final.name", "maven-1.0" );
152         driverProperties.setProperty( "maven.repo.remote.enabled", "true" );
153         driverProperties.setProperty( "maven.repo.local", "${maven.home}/artifact" );
154 
155         Map result = CollectionUtils.mergeMaps( new Map[] { systemProperties, userBuildProperties,
156             projectBuildProperties, projectProperties, driverProperties } );
157 
158         // Values that should be taken from systemProperties.
159         assertEquals( "/projects/maven", (String) result.get( "maven.home" ) );
160 
161         // Values that should be taken from userBuildProperties.
162         assertEquals( "/opt/maven/artifact", (String) result.get( "maven.repo.local" ) );
163         assertEquals( "false", (String) result.get( "maven.repo.remote.enabled" ) );
164         assertEquals( "jvanzyl", (String) result.get( "maven.username" ) );
165 
166         // Values take from projectBuildProperties.
167         assertEquals( "maven", (String) result.get( "maven.final.name" ) );
168 
169         // Values take from projectProperties.
170         assertEquals( mavenRepoRemote, (String) result.get( "maven.repo.remote" ) );
171     }
172 
173     public void testIteratorToListWithAPopulatedList()
174     {
175         List<String> original = new ArrayList<String>();
176 
177         original.add( "en" );
178         original.add( "to" );
179         original.add( "tre" );
180 
181         List<String> copy = CollectionUtils.iteratorToList( original.iterator() );
182 
183         assertNotNull( copy );
184 
185         assertEquals( 3, copy.size() );
186 
187         assertEquals( "en", copy.get( 0 ) );
188         assertEquals( "to", copy.get( 1 ) );
189         assertEquals( "tre", copy.get( 2 ) );
190     }
191 
192     public void testIteratorToListWithAEmptyList()
193     {
194         List<String> original = new ArrayList<String>();
195 
196         List<String> copy = CollectionUtils.iteratorToList( original.iterator() );
197 
198         assertNotNull( copy );
199 
200         assertEquals( 0, copy.size() );
201     }
202 }