View Javadoc

1   package org.apache.maven.shared.filtering;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *    http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Properties;
27  
28  import org.apache.maven.model.Resource;
29  import org.codehaus.plexus.PlexusTestCase;
30  import org.codehaus.plexus.util.FileUtils;
31  import org.codehaus.plexus.util.IOUtil;
32  
33  /**
34   * @author Olivier Lamy
35   *
36   */
37  public class EscapeStringTest
38      extends PlexusTestCase
39  {
40      
41      File outputDirectory = new File( getBasedir(), "target/EscapeStringTest" );
42  
43      File unitDirectory = new File( getBasedir(), "src/test/units-files/escape-remove-char" );
44  
45      protected void setUp()
46          throws Exception
47      {
48          super.setUp();
49          if ( outputDirectory.exists() )
50          {
51              FileUtils.forceDelete( 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 = (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class
71              .getName() );
72  
73          Resource resource = new Resource();
74          List resources = new ArrayList();
75          resources.add( resource );
76          resource.setDirectory( unitDirectory.getPath() );
77          resource.setFiltering( true );
78  
79          List filtersFile = new ArrayList();
80  
81          List nonFilteredFileExtensions = Collections.singletonList( "gif" );
82  
83          MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( resources, outputDirectory,
84                                                                                         mavenProject, "UTF-8",
85                                                                                         filtersFile,
86                                                                                         nonFilteredFileExtensions,
87                                                                                         new StubMavenSession() );
88          mavenResourcesExecution.setUseDefaultFilterWrappers( true );
89  
90          mavenResourcesExecution.setEscapeString( "!" );
91  
92          mavenResourcesFiltering.filterResources( mavenResourcesExecution );
93  
94          FileInputStream in = null;
95          try
96          {
97              String content = IOUtil.toString( new FileInputStream( new File( outputDirectory, "content.xml" ) ) );
98  
99              System.out.println( "content " + content );
100             assertTrue( content.contains( "<broken-tag>Content with replacement: I am the replacement !</broken-tag>" ) );
101             assertTrue( content.contains( "<broken-tag>Content with escaped replacement: Do not ${replaceThis} !</broken-tag>") );
102         }
103         finally
104         {
105             IOUtil.close( in );
106         }
107 
108     }
109 }