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.plugins.invoker;
20  
21  import java.io.File;
22  import java.io.Reader;
23  import java.util.Map;
24  import java.util.Properties;
25  
26  import org.apache.maven.model.Scm;
27  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
28  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
29  import org.apache.maven.settings.Settings;
30  import org.codehaus.plexus.util.IOUtil;
31  import org.codehaus.plexus.util.ReaderFactory;
32  
33  /**
34   * @author Olivier Lamy
35   * @since 22 nov. 07
36   */
37  public class InterpolationTest extends AbstractMojoTestCase {
38  
39      protected MavenProjectStub buildMavenProjectStub() {
40          ExtendedMavenProjectStub project = new ExtendedMavenProjectStub();
41          project.setVersion("1.0-SNAPSHOT");
42          project.setArtifactId("foo");
43          project.setGroupId("bar");
44          Properties properties = new Properties();
45          properties.put("fooOnProject", "barOnProject");
46          project.setProperties(properties);
47          Scm scm = new Scm();
48          scm.setConnection("http://blabla");
49          project.setScm(scm);
50          return project;
51      }
52  
53      public void testCompositeMap() {
54  
55          Properties properties = new Properties();
56          properties.put("foo", "bar");
57          properties.put("version", "2.0-SNAPSHOT");
58          CompositeMap compositeMap = new CompositeMap(buildMavenProjectStub(), (Map) properties, false);
59          assertEquals("1.0-SNAPSHOT", compositeMap.get("pom.version"));
60          assertEquals("bar", compositeMap.get("foo"));
61          assertEquals("bar", compositeMap.get("pom.groupId"));
62          assertEquals("http://blabla", compositeMap.get("pom.scm.connection"));
63          assertEquals("barOnProject", compositeMap.get("fooOnProject"));
64      }
65  
66      public void testPomInterpolation() throws Exception {
67          Reader reader = null;
68          File interpolatedPomFile;
69          try {
70              InvokerMojo invokerMojo = new InvokerMojo();
71              setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub());
72              setVariableValueToObject(invokerMojo, "settings", new Settings());
73              Properties properties = new Properties();
74              properties.put("foo", "bar");
75              properties.put("version", "2.0-SNAPSHOT");
76              setVariableValueToObject(invokerMojo, "filterProperties", properties);
77              String dirPath =
78                      getBasedir() + File.separatorChar + "src" + File.separatorChar + "test" + File.separatorChar
79                              + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
80  
81              interpolatedPomFile = new File(getBasedir(), "target/interpolated-pom.xml");
82              invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
83              reader = ReaderFactory.newXmlReader(interpolatedPomFile);
84              String content = IOUtil.toString(reader);
85              assertTrue(content.indexOf("<interpolateValue>bar</interpolateValue>") > 0);
86              reader.close();
87              reader = null;
88              // recreate it to test delete if exists before creation
89              invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
90              reader = ReaderFactory.newXmlReader(interpolatedPomFile);
91              content = IOUtil.toString(reader);
92              assertTrue(content.indexOf("<interpolateValue>bar</interpolateValue>") > 0);
93              reader.close();
94              reader = null;
95          } finally {
96              IOUtil.close(reader);
97          }
98      }
99  }