1 package org.apache.maven;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
73 assertEquals( 9, result.keySet().size() );
74
75
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
90 Map result0 = MavenUtils.mergeMaps( new Map[] {} );
91
92 assertNull( result0 );
93
94
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
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
113 Map result3 = MavenUtils.mergeMaps( new Map[] { map2, map1 } );
114
115 assertEquals( "aa", result3.get( "a" ) );
116 assertEquals( "bb", result3.get( "b" ) );
117
118
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
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
141
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
149 systemProperties.setProperty( "maven.foo", "/projects/maven" );
150
151
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
157 projectBuildProperties.setProperty( "maven.final.name", "maven" );
158
159 String mavenRepoRemote = "http://repo1.maven.org/maven,http://foo/bar";
160
161
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
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
189 assertEquals( "/projects/maven", (String) context.getVariable( "maven.foo" ) );
190
191
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
197 assertEquals( "maven", (String) context.getVariable( "maven.final.name" ) );
198
199
200 assertEquals( mavenRepoRemote, (String) context.getVariable( "maven.repo.remote" ) );
201
202
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 }