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.shared.filtering;
20  
21  import java.io.InputStream;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Properties;
28  
29  import org.apache.maven.api.di.Inject;
30  import org.apache.maven.api.di.testing.MavenDITest;
31  import org.apache.maven.api.plugin.testing.stubs.ProjectStub;
32  import org.apache.maven.di.Injector;
33  import org.junit.jupiter.api.Test;
34  
35  import static org.apache.maven.api.di.testing.MavenDIExtension.getBasedir;
36  import static org.junit.jupiter.api.Assertions.assertEquals;
37  
38  /**
39   * @author Olivier Lamy
40   * @since 1.0-beta-1
41   *
42   */
43  @MavenDITest
44  public class TestReflectionProperties {
45  
46      @Inject
47      Injector container;
48  
49      @Test
50      public void testSimpleFiltering() throws Exception {
51          ProjectStub mavenProject = new ProjectStub();
52          mavenProject.setVersion("1.0");
53          mavenProject.setGroupId("org.apache");
54          Map<String, String> userProperties = new HashMap<>();
55          userProperties.put("foo", "bar");
56          MavenFileFilter mavenFileFilter = container.getInstance(MavenFileFilter.class);
57  
58          Path from = Paths.get(getBasedir() + "/src/test/units-files/reflection-test.properties");
59          Path to = Paths.get(getBasedir() + "/target/reflection-test.properties");
60  
61          Files.deleteIfExists(to);
62  
63          mavenFileFilter.copyFile(from, to, true, mavenProject, null, false, null, new StubSession(userProperties));
64  
65          Properties reading = new Properties();
66  
67          try (InputStream readFileInputStream = Files.newInputStream(to)) {
68              reading.load(readFileInputStream);
69          }
70  
71          assertEquals("1.0", reading.get("version"));
72          assertEquals("org.apache", reading.get("groupId"));
73          assertEquals("bar", reading.get("foo"));
74          assertEquals("none filtered", reading.get("none"));
75      }
76  
77      @Test
78      public void testSimpleNonFiltering() throws Exception {
79  
80          ProjectStub mavenProject = new ProjectStub();
81          mavenProject.setVersion("1.0");
82          mavenProject.setGroupId("org.apache");
83          Map<String, String> userProperties = new HashMap<>();
84          userProperties.put("foo", "bar");
85          MavenFileFilter mavenFileFilter = container.getInstance(MavenFileFilter.class);
86  
87          Path from = Paths.get(getBasedir() + "/src/test/units-files/reflection-test.properties");
88          Path to = Paths.get(getBasedir() + "/target/reflection-test.properties");
89  
90          Files.deleteIfExists(to);
91  
92          mavenFileFilter.copyFile(from, to, false, mavenProject, null, false, null, new StubSession(userProperties));
93  
94          Properties reading = new Properties();
95  
96          try (InputStream readFileInputStream = Files.newInputStream(to); ) {
97              reading.load(readFileInputStream);
98          }
99  
100         assertEquals("${pom.version}", reading.get("version"));
101         assertEquals("${pom.groupId}", reading.get("groupId"));
102         assertEquals("${foo}", reading.get("foo"));
103         assertEquals("none filtered", reading.get("none"));
104     }
105 }