View Javadoc
1   package org.apache.maven.plugins.invoker;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.Reader;
24  import java.util.Arrays;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.maven.model.Scm;
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
32  import org.apache.maven.plugins.invoker.CompositeMap;
33  import org.apache.maven.plugins.invoker.InvokerMojo;
34  import org.apache.maven.settings.Settings;
35  import org.codehaus.plexus.util.IOUtil;
36  import org.codehaus.plexus.util.ReaderFactory;
37  
38  /**
39   * @author Olivier Lamy
40   * @since 22 nov. 07
41   */
42  public class InterpolationTest
43      extends AbstractMojoTestCase
44  {
45  
46      protected MavenProjectStub buildMavenProjectStub()
47      {
48          ExtendedMavenProjectStub project = new ExtendedMavenProjectStub();
49          project.setVersion( "1.0-SNAPSHOT" );
50          project.setArtifactId( "foo" );
51          project.setGroupId( "bar" );
52          Properties properties = new Properties();
53          properties.put( "fooOnProject", "barOnProject" );
54          project.setProperties( properties );
55          Scm scm = new Scm();
56          scm.setConnection( "http://blabla" );
57          project.setScm( scm );
58          return project;
59      }
60  
61      public void testCompositeMap()
62          throws Exception
63      {
64  
65          Properties properties = new Properties();
66          properties.put( "foo", "bar" );
67          properties.put( "version", "2.0-SNAPSHOT" );
68          CompositeMap compositeMap = new CompositeMap( buildMavenProjectStub(), (Map) properties, false );
69          assertEquals( "1.0-SNAPSHOT", compositeMap.get( "pom.version" ) );
70          assertEquals( "bar", compositeMap.get( "foo" ) );
71          assertEquals( "bar", compositeMap.get( "pom.groupId" ) );
72          assertEquals( "http://blabla", compositeMap.get( "pom.scm.connection" ) );
73          assertEquals( "barOnProject", compositeMap.get( "fooOnProject" ) );
74      }
75  
76      public void testPomInterpolation()
77          throws Exception
78      {
79          Reader reader = null;
80          File interpolatedPomFile;
81          try
82          {
83              InvokerMojo invokerMojo = new InvokerMojo();
84              setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
85              setVariableValueToObject( invokerMojo, "settings", new Settings() );
86              Properties properties = new Properties();
87              properties.put( "foo", "bar" );
88              properties.put( "version", "2.0-SNAPSHOT" );
89              setVariableValueToObject( invokerMojo, "filterProperties", properties );
90              String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
91                  + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
92  
93              interpolatedPomFile = new File( getBasedir(), "target/interpolated-pom.xml" );
94              invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
95              reader = ReaderFactory.newXmlReader( interpolatedPomFile );
96              String content = IOUtil.toString( reader );
97              assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
98              reader.close();
99              reader = null;
100             // recreate it to test delete if exists before creation
101             invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
102             reader = ReaderFactory.newXmlReader( interpolatedPomFile );
103             content = IOUtil.toString( reader );
104             assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
105             reader.close();
106             reader = null;
107         }
108         finally
109         {
110             IOUtil.close( reader );
111         }
112     }
113 
114     public void testProfilesWithNoFile()
115         throws Exception
116     {
117 
118         InvokerMojo invokerMojo = new InvokerMojo();
119         setVariableValueToObject( invokerMojo, "profiles", Arrays.asList( "zloug" ) );
120         setVariableValueToObject( invokerMojo, "settings", new Settings() );
121         String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
122             + "resources" + File.separatorChar + "unit" + File.separatorChar + "profiles-from-file";
123         List<String> profiles = invokerMojo.getProfiles( new File( dirPath ) );
124         assertTrue( profiles.contains( "zloug" ) );
125         assertEquals( 1, profiles.size() );
126 
127     }
128 }