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