View Javadoc
1   package org.apache.maven.shared.filtering;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.nio.charset.StandardCharsets;
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Properties;
28  
29  import org.apache.commons.io.FileUtils;
30  import org.apache.maven.model.Resource;
31  import org.codehaus.plexus.PlexusTestCase;
32  
33  /**
34   * @author Olivier Lamy
35   */
36  public class EscapeStringTest
37      extends PlexusTestCase
38  {
39  
40      File outputDirectory = new File( getBasedir(), "target/EscapeStringTest" );
41  
42      File unitDirectory = new File( getBasedir(), "src/test/units-files/escape-remove-char" );
43  
44      @Override
45      protected void setUp()
46          throws Exception
47      {
48          super.setUp();
49          if ( outputDirectory.exists() )
50          {
51              FileUtils.deleteDirectory( outputDirectory );
52          }
53          outputDirectory.mkdirs();
54      }
55  
56      public void testEscape()
57          throws Exception
58      {
59          File baseDir = new File( "c:\\foo\\bar" );
60          StubMavenProject mavenProject = new StubMavenProject( baseDir );
61          mavenProject.setVersion( "1.0" );
62          mavenProject.setGroupId( "org.apache" );
63          mavenProject.setName( "test project" );
64  
65          Properties projectProperties = new Properties();
66          projectProperties.put( "foo", "bar" );
67          projectProperties.put( "java.version", "zloug" );
68          projectProperties.put( "replaceThis", "I am the replacement" );
69          mavenProject.setProperties( projectProperties );
70          MavenResourcesFiltering mavenResourcesFiltering = lookup( MavenResourcesFiltering.class );
71  
72          Resource resource = new Resource();
73          List<Resource> resources = new ArrayList<>();
74          resources.add( resource );
75          resource.setDirectory( unitDirectory.getPath() );
76          resource.setFiltering( true );
77  
78          List<String> filtersFile = new ArrayList<>();
79  
80          List<String> nonFilteredFileExtensions = Collections.singletonList( "gif" );
81  
82          MavenResourcesExecution mavenResourcesExecution =
83              new MavenResourcesExecution( resources, outputDirectory, mavenProject, "UTF-8", filtersFile,
84                                           nonFilteredFileExtensions, new StubMavenSession() );
85          mavenResourcesExecution.setUseDefaultFilterWrappers( true );
86  
87          mavenResourcesExecution.setEscapeString( "!" );
88  
89          mavenResourcesFiltering.filterResources( mavenResourcesExecution );
90          
91          File file = new File( outputDirectory, "content.xml" );
92          String content = FileUtils.readFileToString( file, StandardCharsets.UTF_8 );
93          assertTrue( content.contains( "<broken-tag>Content with replacement: I am the replacement !</broken-tag>" ) );
94          assertTrue( content.contains( "<broken-tag>Content with escaped replacement: Do not ${replaceThis} !</broken-tag>" ) );
95      }
96  }