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 javax.inject.Inject;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.util.Properties;
26  
27  import org.apache.maven.project.MavenProject;
28  import org.codehaus.plexus.testing.PlexusTest;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  
34  /**
35   * @author Olivier Lamy
36   * @since 1.0-beta-1
37   *
38   */
39  @PlexusTest
40  class TestReflectionProperties {
41  
42      @Inject
43      MavenFileFilter mavenFileFilter;
44  
45      @Test
46      void simpleFiltering() throws Exception {
47          MavenProject mavenProject = new MavenProject();
48          mavenProject.setVersion("1.0");
49          mavenProject.setGroupId("org.apache");
50          Properties userProperties = new Properties();
51          userProperties.setProperty("foo", "bar");
52  
53          File from = new File(getBasedir() + "/src/test/units-files/reflection-test.properties");
54          File to = new File(getBasedir() + "/target/reflection-test.properties");
55  
56          if (to.exists()) {
57              to.delete();
58          }
59  
60          mavenFileFilter.copyFile(from, to, true, mavenProject, null, false, null, new StubMavenSession(userProperties));
61  
62          Properties reading = new Properties();
63  
64          try (FileInputStream readFileInputStream = new FileInputStream(to)) {
65              reading.load(readFileInputStream);
66          }
67  
68          assertEquals("1.0", reading.get("version"));
69          assertEquals("org.apache", reading.get("groupId"));
70          assertEquals("bar", reading.get("foo"));
71          assertEquals("none filtered", reading.get("none"));
72      }
73  
74      @Test
75      void simpleNonFiltering() throws Exception {
76  
77          MavenProject mavenProject = new MavenProject();
78          mavenProject.setVersion("1.0");
79          mavenProject.setGroupId("org.apache");
80          Properties userProperties = new Properties();
81          userProperties.setProperty("foo", "bar");
82  
83          File from = new File(getBasedir() + "/src/test/units-files/reflection-test.properties");
84          File to = new File(getBasedir() + "/target/reflection-test.properties");
85  
86          if (to.exists()) {
87              to.delete();
88          }
89  
90          mavenFileFilter.copyFile(
91                  from, to, false, mavenProject, null, false, null, new StubMavenSession(userProperties));
92  
93          Properties reading = new Properties();
94  
95          try (FileInputStream readFileInputStream = new FileInputStream(to); ) {
96              reading.load(readFileInputStream);
97          }
98  
99          assertEquals("${pom.version}", reading.get("version"));
100         assertEquals("${pom.groupId}", reading.get("groupId"));
101         assertEquals("${foo}", reading.get("foo"));
102         assertEquals("none filtered", reading.get("none"));
103     }
104 }