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.nio.charset.StandardCharsets;
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  
29  import org.apache.maven.api.di.Inject;
30  import org.apache.maven.api.di.testing.MavenDITest;
31  import org.apache.maven.api.plugin.testing.stubs.ProjectStub;
32  import org.apache.maven.di.Injector;
33  import org.junit.jupiter.api.BeforeEach;
34  import org.junit.jupiter.api.Test;
35  
36  import static org.apache.maven.api.di.testing.MavenDIExtension.getBasedir;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  /**
40   * @author Olivier Lamy
41   */
42  @MavenDITest
43  public class EscapeStringTest {
44  
45      @Inject
46      Injector container;
47  
48      Path outputDirectory = Paths.get(getBasedir(), "target/EscapeStringTest");
49  
50      Path unitDirectory = Paths.get(getBasedir(), "src/test/units-files/escape-remove-char");
51  
52      @BeforeEach
53      protected void setUp() throws Exception {
54          IOUtils.deleteDirectory(outputDirectory);
55          Files.createDirectories(outputDirectory);
56      }
57  
58      @Test
59      public void testEscape() throws Exception {
60          Path baseDir = Paths.get(getBasedir());
61          ProjectStub mavenProject = new ProjectStub().setBasedir(baseDir);
62          mavenProject.setVersion("1.0");
63          mavenProject.setGroupId("org.apache");
64          mavenProject.setName("test project");
65  
66          mavenProject.addProperty("foo", "bar");
67          mavenProject.addProperty("java.version", "zloug");
68          mavenProject.addProperty("replaceThis", "I am the replacement");
69          MavenResourcesFiltering mavenResourcesFiltering = container.getInstance(MavenResourcesFiltering.class);
70  
71          Resource resource = new Resource();
72          List<Resource> resources = new ArrayList<>();
73          resources.add(resource);
74          resource.setDirectory(unitDirectory.toString());
75          resource.setFiltering(true);
76  
77          List<String> filtersFile = new ArrayList<>();
78  
79          List<String> nonFilteredFileExtensions = Collections.singletonList("gif");
80  
81          MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(
82                  resources,
83                  outputDirectory,
84                  mavenProject,
85                  "UTF-8",
86                  filtersFile,
87                  nonFilteredFileExtensions,
88                  new StubSession());
89          mavenResourcesExecution.setUseDefaultFilterWrappers(true);
90  
91          mavenResourcesExecution.setEscapeString("!");
92  
93          mavenResourcesFiltering.filterResources(mavenResourcesExecution);
94  
95          Path file = outputDirectory.resolve("content.xml");
96          String content = Files.readString(file, StandardCharsets.UTF_8);
97          assertTrue(content.contains("<broken-tag>Content with replacement: I am the replacement !</broken-tag>"));
98          assertTrue(
99                  content.contains("<broken-tag>Content with escaped replacement: Do not ${replaceThis} !</broken-tag>"));
100     }
101 }