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.nio.charset.StandardCharsets;
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.assertTrue;
38  
39  /**
40   * @author Olivier Lamy
41   */
42  @PlexusTest
43  class EscapeStringTest {
44  
45      @Inject
46      MavenResourcesFiltering mavenResourcesFiltering;
47  
48      File outputDirectory = new File(getBasedir(), "target/EscapeStringTest");
49  
50      File unitDirectory = new File(getBasedir(), "src/test/units-files/escape-remove-char");
51  
52      @BeforeEach
53      void setUp() throws Exception {
54          if (outputDirectory.exists()) {
55              FileUtils.deleteDirectory(outputDirectory);
56          }
57          outputDirectory.mkdirs();
58      }
59  
60      @Test
61      void escape() throws Exception {
62          File baseDir = new File(getBasedir());
63          StubMavenProject mavenProject = new StubMavenProject(baseDir);
64          mavenProject.setVersion("1.0");
65          mavenProject.setGroupId("org.apache");
66          mavenProject.setName("test project");
67  
68          Properties projectProperties = new Properties();
69          projectProperties.put("foo", "bar");
70          projectProperties.put("java.version", "zloug");
71          projectProperties.put("replaceThis", "I am the replacement");
72          mavenProject.setProperties(projectProperties);
73  
74          Resource resource = new Resource();
75          List<Resource> resources = new ArrayList<>();
76          resources.add(resource);
77          resource.setDirectory(unitDirectory.getPath());
78          resource.setFiltering(true);
79  
80          List<String> filtersFile = new ArrayList<>();
81  
82          List<String> nonFilteredFileExtensions = Collections.singletonList("gif");
83  
84          MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(
85                  resources,
86                  outputDirectory,
87                  mavenProject,
88                  "UTF-8",
89                  filtersFile,
90                  nonFilteredFileExtensions,
91                  new StubMavenSession());
92          mavenResourcesExecution.setUseDefaultFilterWrappers(true);
93  
94          mavenResourcesExecution.setEscapeString("!");
95  
96          mavenResourcesFiltering.filterResources(mavenResourcesExecution);
97  
98          File file = new File(outputDirectory, "content.xml");
99          String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
100         assertTrue(content.contains("<broken-tag>Content with replacement: I am the replacement !</broken-tag>"));
101         assertTrue(
102                 content.contains("<broken-tag>Content with escaped replacement: Do not ${replaceThis} !</broken-tag>"));
103     }
104 }