1   package org.apache.maven;
2   
3   /* ====================================================================
4    *   Licensed to the Apache Software Foundation (ASF) under one or more
5    *   contributor license agreements.  See the NOTICE file distributed with
6    *   this work for additional information regarding copyright ownership.
7    *   The ASF licenses this file to You under the Apache License, Version 2.0
8    *   (the "License"); you may not use this file except in compliance with
9    *   the License.  You may obtain a copy of the License at
10   *
11   *       http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *   Unless required by applicable law or agreed to in writing, software
14   *   distributed under the License is distributed on an "AS IS" BASIS,
15   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *   See the License for the specific language governing permissions and
17   *   limitations under the License.
18   * ====================================================================
19   */
20  
21  import java.io.File;
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.maven.jelly.MavenJellyContext;
31  
32  /**
33   * Test cases for various MavenUtils methods.
34   *
35   * @author Brett Porter <a href="mailto:brett@apache.org">brett@apache.org</a>
36   * @author others
37   * @version $Id: MavenUtilsTest.java 517014 2007-03-11 21:15:50Z ltheussl $
38   */
39  public class MavenUtilsTest
40      extends TestCase
41  {
42      public MavenUtilsTest( String testName )
43      {
44          super( testName );
45      }
46  
47      public static Test suite()
48      {
49          return new TestSuite( MavenUtilsTest.class );
50      }
51  
52      public void testMergeMaps()
53      {
54          Map dominantMap = new HashMap();
55          dominantMap.put( "a", "a" );
56          dominantMap.put( "b", "b" );
57          dominantMap.put( "c", "c" );
58          dominantMap.put( "d", "d" );
59          dominantMap.put( "e", "e" );
60          dominantMap.put( "f", "f" );
61  
62          Map recessiveMap = new HashMap();
63          recessiveMap.put( "a", "invalid" );
64          recessiveMap.put( "b", "invalid" );
65          recessiveMap.put( "c", "invalid" );
66          recessiveMap.put( "x", "x" );
67          recessiveMap.put( "y", "y" );
68          recessiveMap.put( "z", "z" );
69  
70          Map result = MavenUtils.mergeMaps( dominantMap, recessiveMap );
71  
72          // We should have 9 elements
73          assertEquals( 9, result.keySet().size() );
74  
75          // Check the elements.
76          assertEquals( "a", result.get( "a" ) );
77          assertEquals( "b", result.get( "b" ) );
78          assertEquals( "c", result.get( "c" ) );
79          assertEquals( "d", result.get( "d" ) );
80          assertEquals( "e", result.get( "e" ) );
81          assertEquals( "f", result.get( "f" ) );
82          assertEquals( "x", result.get( "x" ) );
83          assertEquals( "y", result.get( "y" ) );
84          assertEquals( "z", result.get( "z" ) );
85      }
86  
87      public void testMergeMapArray()
88      {
89          // Test empty array of Maps
90          Map result0 = MavenUtils.mergeMaps( new Map[] {} );
91  
92          assertNull( result0 );
93  
94          // Test with an array with a single element.
95          Map map1 = new HashMap();
96          map1.put( "a", "a" );
97  
98          Map result1 = MavenUtils.mergeMaps( new Map[] { map1 } );
99  
100         assertEquals( "a", result1.get( "a" ) );
101 
102         // Test with an array with two elements.
103         Map map2 = new HashMap();
104         map2.put( "a", "aa" );
105         map2.put( "b", "bb" );
106 
107         Map result2 = MavenUtils.mergeMaps( new Map[] { map1, map2 } );
108 
109         assertEquals( "a", result2.get( "a" ) );
110         assertEquals( "bb", result2.get( "b" ) );
111 
112         // Now swap the dominant order.
113         Map result3 = MavenUtils.mergeMaps( new Map[] { map2, map1 } );
114 
115         assertEquals( "aa", result3.get( "a" ) );
116         assertEquals( "bb", result3.get( "b" ) );
117 
118         // Test with an array with three elements.
119         Map map3 = new HashMap();
120         map3.put( "a", "aaa" );
121         map3.put( "b", "bbb" );
122         map3.put( "c", "ccc" );
123 
124         Map result4 = MavenUtils.mergeMaps( new Map[] { map1, map2, map3 } );
125 
126         assertEquals( "a", result4.get( "a" ) );
127         assertEquals( "bb", result4.get( "b" ) );
128         assertEquals( "ccc", result4.get( "c" ) );
129 
130         // Now swap the dominant order.
131         Map result5 = MavenUtils.mergeMaps( new Map[] { map3, map2, map1 } );
132 
133         assertEquals( "aaa", result5.get( "a" ) );
134         assertEquals( "bbb", result5.get( "b" ) );
135         assertEquals( "ccc", result5.get( "c" ) );
136     }
137 
138     public void testMavenPropertiesLoading()
139     {
140         // Mimic MavenSession properties loading. Properties listed
141         // in dominant order.
142         Properties systemProperties = new Properties();
143         Properties userBuildProperties = new Properties();
144         Properties projectBuildProperties = new Properties();
145         Properties projectProperties = new Properties();
146         Properties driverProperties = new Properties();
147 
148         // System properties
149         systemProperties.setProperty( "maven.foo", "/projects/maven" );
150 
151         // User build properties
152         userBuildProperties.setProperty( "maven.username", "jvanzyl" );
153         userBuildProperties.setProperty( "maven.repo.remote.enabled", "false" );
154         userBuildProperties.setProperty( "maven.repo.local", "/opt/maven/repository" );
155 
156         // Project build properties
157         projectBuildProperties.setProperty( "maven.final.name", "maven" );
158 
159         String mavenRepoRemote = "http://repo1.maven.org/maven,http://foo/bar";
160 
161         // Project properties
162         projectProperties.setProperty( "maven.repo.remote", mavenRepoRemote );
163 
164         String basedir = "/home/jvanzyl/projects/maven";
165         String oldBasedir = System.getProperty( "basedir" );
166         System.getProperties().remove( "basedir" );
167 
168         // Driver properties
169         driverProperties.setProperty( "maven.build.src", "${basedir}/src" );
170         driverProperties.setProperty( "maven.build.dir", "${basedir}/target" );
171         driverProperties.setProperty( "maven.build.dest", "${maven.build.dir}/classes" );
172         driverProperties.setProperty( "maven.repo.remote", "http://repo1.maven.org/maven" );
173         driverProperties.setProperty( "maven.final.name", "maven-1.0" );
174         driverProperties.setProperty( "maven.repo.remote.enabled", "true" );
175         driverProperties.setProperty( "maven.repo.local", "${mavenHome}/repository" );
176 
177         Map result = MavenUtils.mergeMaps( new Map[] {
178             systemProperties,
179             userBuildProperties,
180             projectBuildProperties,
181             projectProperties,
182             driverProperties } );
183 
184         MavenJellyContext context = new MavenJellyContext();
185         context.setVariable( "basedir", basedir );
186         MavenUtils.integrateMapInContext( result, context );
187 
188         // Values that should be taken from systemProperties.
189         assertEquals( "/projects/maven", (String) context.getVariable( "maven.foo" ) );
190 
191         // Values that should be taken from userBuildProperties.
192         assertEquals( "/opt/maven/repository", (String) context.getVariable( "maven.repo.local" ) );
193         assertEquals( "false", (String) context.getVariable( "maven.repo.remote.enabled" ) );
194         assertEquals( "jvanzyl", (String) context.getVariable( "maven.username" ) );
195 
196         // Values take from projectBuildProperties.
197         assertEquals( "maven", (String) context.getVariable( "maven.final.name" ) );
198 
199         // Values take from projectProperties.
200         assertEquals( mavenRepoRemote, (String) context.getVariable( "maven.repo.remote" ) );
201 
202         // Values taken from driver properties.
203         assertEquals( basedir + "/target", (String) context.getVariable( "maven.build.dir" ) );
204         assertEquals( basedir + "/src", (String) context.getVariable( "maven.build.src" ) );
205         assertEquals( basedir + "/target/classes", (String) context.getVariable( "maven.build.dest" ) );
206 
207         System.setProperty( "basedir", oldBasedir );
208     }
209 
210     /**
211      * Test makeAbsolutePath.
212      * @throws Exception if there was a problem
213      */
214     public void testMakeAbsolutePath()
215         throws Exception
216     {
217         String basedir = System.getProperty( "basedir" );
218         File basedirFile = new File( basedir ).getCanonicalFile();
219         assertEquals( "Check relative path", new File( basedir + "/project.xml" ).getCanonicalPath(), MavenUtils
220             .makeAbsolutePath( basedirFile, "project.xml" ) );
221         assertEquals( "Check unix relative path", new File( basedir + "/src/test/basedir/project.xml" )
222             .getCanonicalPath(), MavenUtils.makeAbsolutePath( basedirFile, "src/test/basedir/project.xml" ) );
223         assertEquals( "Check absolute path outside basedir", new File( "/www/docs/index.html" ).getCanonicalPath(),
224                       MavenUtils.makeAbsolutePath( basedirFile, new File( "/www/docs/index.html" ).getCanonicalPath() ) );
225     }
226 
227     /**
228      * Test makeRelativePath.
229      * @throws Exception if there was a problem
230      */
231     public void testMakeRelativePath()
232         throws Exception
233     {
234         String basedir = new File( System.getProperty( "basedir" ) ).getCanonicalPath();
235         File basedirFile = new File( basedir ).getCanonicalFile();
236         assertEquals( "Check relative path", "project.xml", MavenUtils.makeRelativePath( basedirFile, new File( basedir
237             + "/project.xml" ).getCanonicalPath() ) );
238         assertEquals( "Check unix relative path", "src" + File.separatorChar + "test" + File.separatorChar + "basedir"
239             + File.separatorChar + "project.xml", MavenUtils.makeRelativePath( basedirFile, new File( basedir
240             + "/src/test/basedir/project.xml" ).getCanonicalPath() ) );
241         assertEquals( "Check absolute path outside basedir", new File( "/www/docs/index.html" ).getCanonicalPath(),
242                       MavenUtils.makeRelativePath( basedirFile, new File( "/www/docs/index.html" ).getCanonicalPath() ) );
243 
244         assertEquals( "Check absolute path == basedir", ".", MavenUtils.makeRelativePath( basedirFile, basedir ) );
245     }
246 }