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