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.Map;
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 Olivier Lamy
36   * @since 22 nov. 07
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      {
59  
60          Properties properties = new Properties();
61          properties.put( "foo", "bar" );
62          properties.put( "version", "2.0-SNAPSHOT" );
63          CompositeMap compositeMap = new CompositeMap( buildMavenProjectStub(), (Map) properties, false );
64          assertEquals( "1.0-SNAPSHOT", compositeMap.get( "pom.version" ) );
65          assertEquals( "bar", compositeMap.get( "foo" ) );
66          assertEquals( "bar", compositeMap.get( "pom.groupId" ) );
67          assertEquals( "http://blabla", compositeMap.get( "pom.scm.connection" ) );
68          assertEquals( "barOnProject", compositeMap.get( "fooOnProject" ) );
69      }
70  
71      public void testPomInterpolation()
72          throws Exception
73      {
74          Reader reader = null;
75          File interpolatedPomFile;
76          try
77          {
78              InvokerMojo invokerMojo = new InvokerMojo();
79              setVariableValueToObject( invokerMojo, "project", buildMavenProjectStub() );
80              setVariableValueToObject( invokerMojo, "settings", new Settings() );
81              Properties properties = new Properties();
82              properties.put( "foo", "bar" );
83              properties.put( "version", "2.0-SNAPSHOT" );
84              setVariableValueToObject( invokerMojo, "filterProperties", properties );
85              String dirPath = getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
86                  + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
87  
88              interpolatedPomFile = new File( getBasedir(), "target/interpolated-pom.xml" );
89              invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
90              reader = ReaderFactory.newXmlReader( interpolatedPomFile );
91              String content = IOUtil.toString( reader );
92              assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
93              reader.close();
94              reader = null;
95              // recreate it to test delete if exists before creation
96              invokerMojo.buildInterpolatedFile( new File( dirPath, "pom.xml" ), interpolatedPomFile );
97              reader = ReaderFactory.newXmlReader( interpolatedPomFile );
98              content = IOUtil.toString( reader );
99              assertTrue( content.indexOf( "<interpolateValue>bar</interpolateValue>" ) > 0 );
100             reader.close();
101             reader = null;
102         }
103         finally
104         {
105             IOUtil.close( reader );
106         }
107     }
108 }