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.Reader;
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  import org.codehaus.plexus.util.ReaderFactory;
33  
34  /**
35   * @author <a href="mailto:olamy@apache.org">olamy</a>
36   * @since 22 nov. 07
37   * @version $Id: InterpolationTest.java 685126 2008-08-12 12:05:11Z bentmann $
38   */
39  public class InterpolationTest
40      extends AbstractMojoTestCase
41  {
42  
43      protected MavenProjectStub buildMavenProjectStub()
44      {
45          ExtendedMavenProjectStub project = new ExtendedMavenProjectStub();
46          project.setVersion( "1.0-SNAPSHOT" );
47          project.setArtifactId( "foo" );
48          project.setGroupId( "bar" );
49          Properties properties = new Properties();
50          properties.put( "fooOnProject", "barOnProject" );
51          project.setProperties( properties );
52          Scm scm = new Scm();
53          scm.setConnection( "http://blabla" );
54          project.setScm( scm );
55          return project;
56      }
57  
58      public void testCompositeMap()
59          throws Exception
60      {
61  
62          Properties properties = new Properties();
63          properties.put( "foo", "bar" );
64          properties.put( "version", "2.0-SNAPSHOT" );
65          CompositeMap compositeMap = new CompositeMap( buildMavenProjectStub(), properties );
66          assertEquals( "1.0-SNAPSHOT", compositeMap.get( "pom.version" ) );
67          assertEquals( "bar", compositeMap.get( "foo" ) );
68          assertEquals( "bar", compositeMap.get( "pom.groupId" ) );
69          assertEquals( "http://blabla", compositeMap.get( "pom.scm.connection" ) );
70          assertEquals( "barOnProject", compositeMap.get( "fooOnProject" ) );
71      }
72  
73      public void testInterpolationGoalsFile()
74          throws Exception
75      {
76          InvokerMojo invokerMojo = new InvokerMojo();
77          setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
78          setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
79          setVariableValueToObject( invokerMojo, "settings", new Settings() );
80          Properties properties = new Properties();
81          properties.put( "cleanProps", "clean" );
82          properties.put( "version", "2.0-SNAPSHOT" );
83          setVariableValueToObject( invokerMojo, "interpolationsProperties", properties );
84          String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
85          + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
86          List goals = invokerMojo.getGoals( new File( dirPath ) );
87          assertEquals( goals.toString(), 2, goals.size() );
88          assertEquals( "clean", goals.get( 0 ) );
89          assertEquals( "bar:foo:1.0-SNAPSHOT:mygoal", goals.get( 1 ) );
90      }
91  
92      public void testPomInterpolation()
93          throws Exception
94      {
95          Reader reader = null;
96          File interpolatedPomFile = null;
97          try
98          {
99              InvokerMojo invokerMojo = new InvokerMojo();
100             setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
101             setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
102             setVariableValueToObject( invokerMojo, "settings", new Settings() );
103             Properties properties = new Properties();
104             properties.put( "foo", "bar" );
105             properties.put( "version", "2.0-SNAPSHOT" );
106             setVariableValueToObject( invokerMojo, "interpolationsProperties", properties );
107             String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
108                 + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
109             
110             interpolatedPomFile = new File( getBasedir(), "target/interpolated-pom.xml" );
111             invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
112             reader = ReaderFactory.newXmlReader( interpolatedPomFile );
113             String content = IOUtil.toString( reader );
114             assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
115             reader.close();
116             // recreate it to test delete if exists before creation
117             invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
118             reader = ReaderFactory.newXmlReader( interpolatedPomFile );
119             content = IOUtil.toString( reader );
120             assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
121             reader.close();
122         }
123         finally
124         {
125             IOUtil.close( reader );
126         }
127     }
128 
129     public void testProfilesFromFile()
130         throws Exception
131     {
132         InvokerMojo invokerMojo = new InvokerMojo();
133         setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
134         setVariableValueToObject( invokerMojo, "profilesFile", "profiles.txt" );
135         setVariableValueToObject( invokerMojo, "settings", new Settings() );
136         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
137             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
138         List profiles = invokerMojo.getProfiles( new File( dirPath ) );
139         assertEquals( 2, profiles.size() );
140         assertTrue( profiles.contains( "foo" ) );
141     }
142 
143     public void testEmptyProfilesFromFile()
144         throws Exception
145     {
146 
147         InvokerMojo invokerMojo = new InvokerMojo();
148         setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
149         setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( new String[] { "zloug" } ) );
150         setVariableValueToObject( invokerMojo, "profilesFile", "emptyProfiles.txt" );
151         setVariableValueToObject( invokerMojo, "settings", new Settings() );
152         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
153             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
154         List profiles = invokerMojo.getProfiles( new File( dirPath ) );
155         assertFalse( profiles.contains( "zloug" ) );
156         assertEquals( 0, profiles.size() );
157 
158     }
159 
160     public void testProfilesWithNoFile()
161         throws Exception
162     {
163 
164         InvokerMojo invokerMojo = new InvokerMojo();
165         setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( new String[] { "zloug" } ) );
166         setVariableValueToObject( invokerMojo, "profilesFile", "zorglubProfiles.txt" );
167         setVariableValueToObject( invokerMojo, "settings", new Settings() );
168         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
169             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
170         List profiles = invokerMojo.getProfiles( new File( dirPath ) );
171         assertTrue( profiles.contains( "zloug" ) );
172         assertEquals( 1, profiles.size() );
173 
174     }
175 }