package org.apache.maven; /* ==================================================================== * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ==================================================================== */ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.maven.jelly.MavenJellyContext; import java.io.File; import java.util.HashMap; import java.util.Map; import java.util.Properties; /** * Test cases for various MavenUtils methods. * * @author Brett Porter brett@apache.org * @author others * @version $Id: MavenUtilsTest.java,v 1.18 2004/08/20 23:22:15 brett Exp $ */ public class MavenUtilsTest extends TestCase { public MavenUtilsTest( String testName ) { super( testName ); } public static Test suite() { return new TestSuite( MavenUtilsTest.class ); } public void testMergeMaps() { Map dominantMap = new HashMap(); dominantMap.put( "a", "a" ); dominantMap.put( "b", "b" ); dominantMap.put( "c", "c" ); dominantMap.put( "d", "d" ); dominantMap.put( "e", "e" ); dominantMap.put( "f", "f" ); Map recessiveMap = new HashMap(); recessiveMap.put( "a", "invalid" ); recessiveMap.put( "b", "invalid" ); recessiveMap.put( "c", "invalid" ); recessiveMap.put( "x", "x" ); recessiveMap.put( "y", "y" ); recessiveMap.put( "z", "z" ); Map result = MavenUtils.mergeMaps( dominantMap, recessiveMap ); // We should have 9 elements assertEquals( 9, result.keySet().size() ); // Check the elements. assertEquals( "a", result.get( "a" ) ); assertEquals( "b", result.get( "b" ) ); assertEquals( "c", result.get( "c" ) ); assertEquals( "d", result.get( "d" ) ); assertEquals( "e", result.get( "e" ) ); assertEquals( "f", result.get( "f" ) ); assertEquals( "x", result.get( "x" ) ); assertEquals( "y", result.get( "y" ) ); assertEquals( "z", result.get( "z" ) ); } public void testMergeMapArray() { // Test empty array of Maps Map result0 = MavenUtils.mergeMaps( new Map[] { } ); assertNull( result0 ); // Test with an array with a single element. Map map1 = new HashMap(); map1.put( "a", "a" ); Map result1 = MavenUtils.mergeMaps( new Map[] { map1 } ); assertEquals( "a", result1.get( "a" ) ); // Test with an array with two elements. Map map2 = new HashMap(); map2.put( "a", "aa" ); map2.put( "b", "bb" ); Map result2 = MavenUtils.mergeMaps( new Map[] { map1, map2 } ); assertEquals( "a", result2.get( "a" ) ); assertEquals( "bb", result2.get( "b" ) ); // Now swap the dominant order. Map result3 = MavenUtils.mergeMaps( new Map[] { map2, map1 } ); assertEquals( "aa", result3.get( "a" ) ); assertEquals( "bb", result3.get( "b" ) ); // Test with an array with three elements. Map map3 = new HashMap(); map3.put( "a", "aaa" ); map3.put( "b", "bbb" ); map3.put( "c", "ccc" ); Map result4 = MavenUtils.mergeMaps( new Map[] { map1, map2, map3 } ); assertEquals( "a", result4.get( "a" ) ); assertEquals( "bb", result4.get( "b" ) ); assertEquals( "ccc", result4.get( "c" ) ); // Now swap the dominant order. Map result5 = MavenUtils.mergeMaps( new Map[] { map3, map2, map1 } ); assertEquals( "aaa", result5.get( "a" ) ); assertEquals( "bbb", result5.get( "b" ) ); assertEquals( "ccc", result5.get( "c" ) ); } public void testMavenPropertiesLoading() { // Mimic MavenSession properties loading. Properties listed // in dominant order. Properties systemProperties = new Properties(); Properties userBuildProperties = new Properties(); Properties projectBuildProperties = new Properties(); Properties projectProperties = new Properties(); Properties driverProperties = new Properties(); // System properties systemProperties.setProperty( "maven.foo", "/projects/maven" ); // User build properties userBuildProperties.setProperty( "maven.username", "jvanzyl" ); userBuildProperties.setProperty( "maven.repo.remote.enabled", "false" ); userBuildProperties.setProperty( "maven.repo.local", "/opt/maven/repository" ); // Project build properties projectBuildProperties.setProperty( "maven.final.name", "maven" ); String mavenRepoRemote = "http://www.ibiblio.org/maven,http://foo/bar"; // Project properties projectProperties.setProperty( "maven.repo.remote", mavenRepoRemote ); String basedir = "/home/jvanzyl/projects/maven"; // Driver properties driverProperties.setProperty( "maven.build.src", "${basedir}/src" ); driverProperties.setProperty( "maven.build.dir", "${basedir}/target" ); driverProperties.setProperty( "maven.build.dest", "${maven.build.dir}/classes" ); driverProperties.setProperty( "maven.repo.remote", "http://www.ibiblio.org/maven" ); driverProperties.setProperty( "maven.final.name", "maven-1.0.2" ); driverProperties.setProperty( "maven.repo.remote.enabled", "true" ); driverProperties.setProperty( "maven.repo.local", "${maven.home}/repository" ); Map result = MavenUtils.mergeMaps( new Map[] { systemProperties, userBuildProperties, projectBuildProperties, projectProperties, driverProperties } ); MavenJellyContext context = new MavenJellyContext(); context.setVariable( "basedir", basedir ); MavenUtils.integrateMapInContext( result, context ); // Values that should be taken from systemProperties. assertEquals( "/projects/maven", ( String ) context.getVariable( "maven.foo" ) ); // Values that should be taken from userBuildProperties. assertEquals( "/opt/maven/repository", ( String ) context.getVariable( "maven.repo.local" ) ); assertEquals( "false", ( String ) context.getVariable( "maven.repo.remote.enabled" ) ); assertEquals( "jvanzyl", ( String ) context.getVariable( "maven.username" ) ); // Values take from projectBuildProperties. assertEquals( "maven", ( String ) context.getVariable( "maven.final.name" ) ); // Values take from projectProperties. assertEquals( mavenRepoRemote, ( String ) context.getVariable( "maven.repo.remote" ) ); // Values taken from driver properties. assertEquals( basedir + "/target", ( String ) context.getVariable( "maven.build.dir" ) ); assertEquals( basedir + "/src", ( String ) context.getVariable( "maven.build.src" ) ); assertEquals( basedir + "/target/classes", ( String ) context.getVariable( "maven.build.dest" ) ); } /** * Test makeAbsolutePath. * @throws Exception if there was a problem */ public void testMakeAbsolutePath() throws Exception { String basedir = System.getProperty( "basedir" ); File basedirFile = new File( basedir ).getCanonicalFile(); assertEquals( "Check relative path", new File( basedir + "/project.xml" ).getCanonicalPath(), MavenUtils.makeAbsolutePath( basedirFile, "project.xml" ) ); assertEquals( "Check unix relative path", new File( basedir + "/src/test/basedir/project.xml" ).getCanonicalPath(), MavenUtils.makeAbsolutePath( basedirFile, "src/test/basedir/project.xml" ) ); assertEquals( "Check absolute path outside basedir", new File( "/www/docs/index.html" ).getCanonicalPath(), MavenUtils.makeAbsolutePath( basedirFile, new File( "/www/docs/index.html" ).getCanonicalPath() ) ); } /** * Test makeRelativePath. * @throws Exception if there was a problem */ public void testMakeRelativePath() throws Exception { String basedir = new File( System.getProperty( "basedir" ) ).getCanonicalPath(); File basedirFile = new File( basedir ).getCanonicalFile(); assertEquals( "Check relative path", "project.xml", MavenUtils.makeRelativePath( basedirFile, new File( basedir + "/project.xml" ).getCanonicalPath() ) ); assertEquals( "Check unix relative path", "src" + File.separatorChar + "test" + File.separatorChar + "basedir" + File.separatorChar + "project.xml", MavenUtils.makeRelativePath( basedirFile, new File( basedir + "/src/test/basedir/project.xml" ).getCanonicalPath() ) ); assertEquals( "Check absolute path outside basedir", new File( "/www/docs/index.html" ).getCanonicalPath(), MavenUtils.makeRelativePath( basedirFile, new File( "/www/docs/index.html" ).getCanonicalPath() ) ); assertEquals( "Check absolute path == basedir", ".", MavenUtils.makeRelativePath( basedirFile, basedir ) ); } }