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