1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.invoker;
20  
21  import java.io.File;
22  import java.io.FileReader;
23  import java.util.Arrays;
24  import java.util.List;
25  import java.util.Properties;
26  
27  import org.apache.maven.model.Scm;
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.apache.maven.settings.Settings;
31  import org.codehaus.plexus.util.IOUtil;
32  
33  /**
34   * @author <a href="mailto:olamy@apache.org">olamy</a>
35   * @since 22 nov. 07
36   * @version $Id: InterpolationTest.java 655038 2008-05-10 10:12:02Z bentmann $
37   */
38  public class InterpolationTest
39      extends AbstractMojoTestCase
40  {
41  
42      protected MavenProjectStub buildMavenProjectStub()
43      {
44          ExtendedMavenProjectStub project = new ExtendedMavenProjectStub();
45          project.setVersion( "1.0-SNAPSHOT" );
46          project.setArtifactId( "foo" );
47          project.setGroupId( "bar" );
48          Properties properties = new Properties();
49          properties.put( "fooOnProject", "barOnProject" );
50          project.setProperties( properties );
51          Scm scm = new Scm();
52          scm.setConnection( "http://blabla" );
53          project.setScm( scm );
54          return project;
55      }
56  
57      public void testCompositeMap()
58          throws Exception
59      {
60  
61          Properties properties = new Properties();
62          properties.put( "foo", "bar" );
63          properties.put( "version", "2.0-SNAPSHOT" );
64          CompositeMap compositeMap = new CompositeMap( buildMavenProjectStub(), properties );
65          assertEquals( "1.0-SNAPSHOT", compositeMap.get( "pom.version" ) );
66          assertEquals( "bar", compositeMap.get( "foo" ) );
67          assertEquals( "bar", compositeMap.get( "pom.groupId" ) );
68          assertEquals( "http://blabla", compositeMap.get( "pom.scm.connection" ) );
69          assertEquals( "barOnProject", compositeMap.get( "fooOnProject" ) );
70      }
71  
72      public void testInterpolationGoalsFile()
73          throws Exception
74      {
75          InvokerMojo invokerMojo = new InvokerMojo();
76          setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
77          setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
78          setVariableValueToObject( invokerMojo, "settings", new Settings() );
79          Properties properties = new Properties();
80          properties.put( "cleanProps", "clean" );
81          properties.put( "version", "2.0-SNAPSHOT" );
82          setVariableValueToObject( invokerMojo, "interpolationsProperties", properties );
83          String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
84          + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
85          List goals = invokerMojo.getGoals( new File( dirPath ) );
86          assertEquals( goals.toString(), 2, goals.size() );
87          assertEquals( "clean", goals.get( 0 ) );
88          assertEquals( "bar:foo:1.0-SNAPSHOT:mygoal", goals.get( 1 ) );
89      }
90  
91      public void testPomInterpolation()
92          throws Exception
93      {
94          FileReader fileReader = null;
95          File interpolatedPomFile = null;
96          try
97          {
98              InvokerMojo invokerMojo = new InvokerMojo();
99              setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
100             setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
101             setVariableValueToObject( invokerMojo, "settings", new Settings() );
102             Properties properties = new Properties();
103             properties.put( "foo", "bar" );
104             properties.put( "version", "2.0-SNAPSHOT" );
105             setVariableValueToObject( invokerMojo, "interpolationsProperties", properties );
106             String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
107                 + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
108             interpolatedPomFile = invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ),
109                                                                         new File( getBasedir() + File.separatorChar
110                                                                             + "target" ), "interpolated-pom.xml"  );
111             fileReader = new FileReader( interpolatedPomFile );
112             String content = IOUtil.toString( fileReader );
113             assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
114             fileReader.close();
115             // recreate it to test delete if exists before creation
116             interpolatedPomFile = invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ),
117                                                                         new File( getBasedir() + File.separatorChar
118                                                                             + "target" ), "interpolated-pom.xml"  );
119             fileReader = new FileReader( interpolatedPomFile );
120             content = IOUtil.toString( fileReader );
121             assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
122             fileReader.close();
123         } catch (Exception e)
124         {
125             throw e;
126         }
127         finally
128         {
129             if ( fileReader != null )
130             {
131                 fileReader.close();
132             }
133         }
134     }
135 
136     public void testProfilesFromFile()
137         throws Exception
138     {
139         InvokerMojo invokerMojo = new InvokerMojo();
140         setVariableValueToObject( invokerMojo, "profilesFile", "profiles.txt" );
141         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
142             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
143         List profiles = invokerMojo.getProfiles( new File( dirPath ) );
144         assertEquals( 2, profiles.size() );
145         assertTrue( profiles.contains( "foo" ) );
146     }
147 
148     public void testEmptyProfilesFromFile()
149         throws Exception
150     {
151 
152         InvokerMojo invokerMojo = new InvokerMojo();
153         setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( new String[] { "zloug" } ) );
154         setVariableValueToObject( invokerMojo, "profilesFile", "emptyProfiles.txt" );
155         setVariableValueToObject( invokerMojo, "settings", new Settings() );
156         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
157             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
158         List profiles = invokerMojo.getProfiles( new File( dirPath ) );
159         assertFalse( profiles.contains( "zloug" ) );
160         assertEquals( 0, profiles.size() );
161 
162     }
163 
164     public void testProfilesWithNoFile()
165         throws Exception
166     {
167 
168         InvokerMojo invokerMojo = new InvokerMojo();
169         setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( new String[] { "zloug" } ) );
170         setVariableValueToObject( invokerMojo, "profilesFile", "zorglubProfiles.txt" );
171         setVariableValueToObject( invokerMojo, "settings", new Settings() );
172         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
173             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
174         List profiles = invokerMojo.getProfiles( new File( dirPath ) );
175         assertTrue( profiles.contains( "zloug" ) );
176         assertEquals( 1, profiles.size() );
177 
178     }
179 }