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.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Properties;
29  
30  import org.apache.commons.io.FileUtils;
31  import org.apache.maven.model.Resource;
32  import org.codehaus.plexus.testing.PlexusTest;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  
39  /**
40   * @author Olivier Lamy
41   *
42   */
43  @PlexusTest
44  class MuliLinesMavenResourcesFilteringTest {
45  
46      @Inject
47      MavenResourcesFiltering mavenResourcesFiltering;
48  
49      File outputDirectory = new File(getBasedir(), "target/MuliLinesMavenResourcesFilteringTest");
50  
51      @BeforeEach
52      void setUp() throws Exception {
53          if (outputDirectory.exists()) {
54              FileUtils.deleteDirectory(outputDirectory);
55          }
56          outputDirectory.mkdirs();
57      }
58  
59      /**
60       * @throws Exception
61       */
62      @Test
63      void filteringTokenOnce() throws Exception {
64          File baseDir = new File(getBasedir());
65          StubMavenProject mavenProject = new StubMavenProject(baseDir);
66          mavenProject.setVersion("1.0");
67          mavenProject.setGroupId("org.apache");
68          mavenProject.setName("test project");
69  
70          Properties projectProperties = new Properties();
71          projectProperties.put("foo", "bar");
72          projectProperties.put("java.version", "zloug");
73          mavenProject.setProperties(projectProperties);
74  
75          String unitFilesDir = getBasedir() + "/src/test/units-files/MRESOURCES-104";
76  
77          Resource resource = new Resource();
78          List<Resource> resources = new ArrayList<>();
79          resources.add(resource);
80          resource.setDirectory(unitFilesDir);
81          resource.setFiltering(true);
82  
83          List<String> filtersFile = new ArrayList<>();
84          filtersFile.add(getBasedir() + "/src/test/units-files/MRESOURCES-104/test.properties");
85  
86          List<String> nonFilteredFileExtensions = Collections.singletonList("gif");
87  
88          MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(
89                  resources,
90                  outputDirectory,
91                  mavenProject,
92                  "UTF-8",
93                  filtersFile,
94                  nonFilteredFileExtensions,
95                  new StubMavenSession());
96          mavenResourcesExecution.setUseDefaultFilterWrappers(true);
97  
98          mavenResourcesFiltering.filterResources(mavenResourcesExecution);
99  
100         Properties result = new Properties();
101 
102         try (FileInputStream in = new FileInputStream(new File(outputDirectory, "test.properties"))) {
103             result.load(in);
104         }
105 
106         // email=foo@bar.com
107         // foo=${project.version}
108         // bar=@project.version@
109         assertEquals("1.0", result.get("foo"));
110         assertEquals("1.0", result.get("bar"));
111         assertEquals("foo@bar.com", result.get("email"));
112     }
113 }