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