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.IOException;
25  import java.io.Reader;
26  import java.io.StringReader;
27  import java.nio.file.Files;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.LinkedHashSet;
32  import java.util.List;
33  import java.util.Properties;
34  
35  import org.apache.commons.io.IOUtils;
36  import org.apache.maven.project.MavenProject;
37  import org.codehaus.plexus.interpolation.AbstractValueSource;
38  import org.codehaus.plexus.testing.PlexusTest;
39  import org.junit.jupiter.api.BeforeEach;
40  import org.junit.jupiter.api.Test;
41  import org.sonatype.plexus.build.incremental.BuildContext;
42  
43  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
44  import static org.junit.jupiter.api.Assertions.assertEquals;
45  import static org.mockito.Mockito.mock;
46  
47  /**
48   * @author Olivier Lamy
49   *
50   */
51  @PlexusTest
52  class DefaultMavenFileFilterTest {
53  
54      @Inject
55      MavenFileFilter mavenFileFilter;
56  
57      File to = new File(getBasedir(), "target/reflection-test.properties");
58  
59      @BeforeEach
60      void setUp() throws Exception {
61          Files.deleteIfExists(to.toPath());
62      }
63  
64      @Test
65      void overwriteFile() throws Exception {
66          File from = new File(getBasedir(), "src/test/units-files/reflection-test.properties");
67  
68          mavenFileFilter.copyFile(from, to, false, null, null);
69  
70          from = new File(getBasedir(), "src/test/units-files/reflection-test-older.properties");
71  
72          // very old file :-)
73          from.setLastModified(1);
74  
75          to.setLastModified(System.currentTimeMillis());
76  
77          mavenFileFilter.copyFile(from, to, false, null, null);
78  
79          Properties properties = PropertyUtils.loadPropertyFile(to, null);
80          assertEquals("older file", properties.getProperty("version"));
81      }
82  
83      @Test
84      void nullSafeDefaultFilterWrappers() throws Exception {
85          mavenFileFilter.getDefaultFilterWrappers(null, null, false, null, null);
86          // shouldn't fail
87      }
88  
89      @Test
90      void multiFilterFileInheritance() throws Exception {
91          DefaultMavenFileFilter mavenFileFilter = new DefaultMavenFileFilter(mock(BuildContext.class));
92  
93          File testDir = new File(getBasedir(), "src/test/units-files/MSHARED-177");
94  
95          List<String> filters = new ArrayList<>();
96  
97          filters.add(new File(testDir, "first_filter_file.properties").getAbsolutePath());
98          filters.add(new File(testDir, "second_filter_file.properties").getAbsolutePath());
99          filters.add(new File(testDir, "third_filter_file.properties").getAbsolutePath());
100 
101         final Properties filterProperties = new Properties();
102 
103         mavenFileFilter.loadProperties(filterProperties, new File(getBasedir()), filters, new Properties());
104 
105         assertEquals("first and second", filterProperties.getProperty("third_filter_key"));
106     }
107 
108     // MSHARED-161: DefaultMavenFileFilter.getDefaultFilterWrappers loads
109     // filters from the current directory instead of using basedir
110     @Test
111     void mavenBasedir() throws Exception {
112         AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
113         req.setFileFilters(Collections.singletonList("src/main/filters/filefilter.properties"));
114 
115         MavenProject mavenProject = new StubMavenProject(new File("src/test/units-files/MSHARED-161"));
116         mavenProject.getBuild().setFilters(Collections.singletonList("src/main/filters/buildfilter.properties"));
117         req.setMavenProject(mavenProject);
118         req.setInjectProjectBuildFilters(true);
119 
120         List<FilterWrapper> wrappers = mavenFileFilter.getDefaultFilterWrappers(req);
121 
122         try (Reader reader = wrappers.get(0).getReader(new StringReader("${filefilter} ${buildfilter}"))) {
123             assertEquals("true true", IOUtils.toString(reader));
124         }
125     }
126 
127     // MSHARED-198: custom delimiters doesn't work as expected
128     @Test
129     void customDelimiters() throws Exception {
130         AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
131         Properties additionalProperties = new Properties();
132         additionalProperties.setProperty("FILTER.a.ME", "DONE");
133         req.setAdditionalProperties(additionalProperties);
134         req.setDelimiters(new LinkedHashSet<>(Arrays.asList("aaa*aaa", "abc*abc")));
135 
136         List<FilterWrapper> wrappers = mavenFileFilter.getDefaultFilterWrappers(req);
137 
138         Reader reader = wrappers.get(0).getReader(new StringReader("aaaFILTER.a.MEaaa"));
139         assertEquals("DONE", IOUtils.toString(reader));
140 
141         reader = wrappers.get(0).getReader(new StringReader("abcFILTER.a.MEabc"));
142         assertEquals("DONE", IOUtils.toString(reader));
143     }
144 
145     // MSHARED-199: Filtering doesn't work if 2 delimiters are used on the same line, the first one being left open
146     @Test
147     void lineWithSingleAtAndExpression() throws Exception {
148         AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
149         Properties additionalProperties = new Properties();
150         additionalProperties.setProperty("foo", "bar");
151         req.setAdditionalProperties(additionalProperties);
152 
153         List<FilterWrapper> wrappers = mavenFileFilter.getDefaultFilterWrappers(req);
154 
155         try (Reader reader = wrappers.get(0).getReader(new StringReader("toto@titi.com ${foo}"))) {
156             assertEquals("toto@titi.com bar", IOUtils.toString(reader));
157         }
158     }
159 
160     @Test
161     void testInterpolatorCustomizer() throws MavenFilteringException, IOException {
162         AbstractMavenFilteringRequest req = new AbstractMavenFilteringRequest();
163         req.setInterpolatorCustomizer(i -> {
164             i.addValueSource(new AbstractValueSource(false) {
165 
166                 @Override
167                 public Object getValue(String expression) {
168                     if (expression.equals("foo")) {
169                         return "bar";
170                     }
171                     return null;
172                 }
173             });
174         });
175 
176         List<FilterWrapper> wrappers = mavenFileFilter.getDefaultFilterWrappers(req);
177 
178         try (Reader reader = wrappers.get(0).getReader(new StringReader("toto@titi.com ${foo}"))) {
179             assertEquals("toto@titi.com bar", IOUtils.toString(reader));
180         }
181     }
182 }