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 {
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(TestUtil.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          Map<String, Object> properties = new HashMap<>();
60          properties.put("foo", "bar");
61          properties.put("version", "2.0-SNAPSHOT");
62          CompositeMap compositeMap = new CompositeMap(buildMavenProjectStub(), properties, false);
63          assertThat(compositeMap).containsEntry("pom.version", "1.0-SNAPSHOT");
64          assertThat(compositeMap).containsEntry("foo", "bar");
65          assertThat(compositeMap).containsEntry("pom.groupId", "bar");
66          assertThat(compositeMap).containsEntry("pom.scm.connection", "http://blabla");
67          assertThat(compositeMap).containsEntry("fooOnProject", "barOnProject");
68      }
69  
70      @Test
71      void testPomInterpolation() throws Exception {
72          InvokerMojo invokerMojo = new InvokerMojo(null, null, null, null);
73          TestUtil.setVariableValueToObject(invokerMojo, "project", buildMavenProjectStub());
74          TestUtil.setVariableValueToObject(invokerMojo, "settings", new Settings());
75          Properties properties = new Properties();
76          properties.put("foo", "bar");
77          properties.put("version", "2.0-SNAPSHOT");
78          TestUtil.setVariableValueToObject(invokerMojo, "filterProperties", properties);
79          String dirPath = TestUtil.getBasedir() + File.separatorChar + "src" + File.separatorChar + "test"
80                  + File.separatorChar + "resources" + File.separatorChar + "unit" + File.separatorChar + "interpolation";
81  
82          File interpolatedPomFile = new File(TestUtil.getBasedir(), "target/interpolated-pom.xml");
83          invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
84          try (Reader reader = new XmlStreamReader(interpolatedPomFile)) {
85              String content = IOUtil.toString(reader);
86              assertThat(content.indexOf("<interpolateValue>bar</interpolateValue>"))
87                      .isPositive();
88          }
89          // recreate it to test delete if exists before creation
90          invokerMojo.buildInterpolatedFile(new File(dirPath, "pom.xml"), interpolatedPomFile);
91          try (Reader reader = new XmlStreamReader(interpolatedPomFile)) {
92              String content = IOUtil.toString(reader);
93              assertThat(content.indexOf("<interpolateValue>bar</interpolateValue>"))
94                      .isPositive();
95          }
96      }
97  }