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.io.FileInputStream;
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.maven.model.Resource;
30  import org.codehaus.plexus.PlexusTestCase;
31  import org.codehaus.plexus.util.FileUtils;
32  import org.codehaus.plexus.util.IOUtil;
33  
34  /**
35   * @author Olivier Lamy
36   *
37   */
38  public class EscapeStringTest
39      extends PlexusTestCase
40  {
41      
42      File outputDirectory = new File( getBasedir(), "target/EscapeStringTest" );
43  
44      File unitDirectory = new File( getBasedir(), "src/test/units-files/escape-remove-char" );
45  
46      protected void setUp()
47          throws Exception
48      {
49          super.setUp();
50          if ( outputDirectory.exists() )
51          {
52              FileUtils.forceDelete( outputDirectory );
53          }
54          outputDirectory.mkdirs();
55      }
56  
57      public void testEscape()
58          throws Exception
59      {
60          File baseDir = new File( "c:\\foo\\bar" );
61          StubMavenProject mavenProject = new StubMavenProject( baseDir );
62          mavenProject.setVersion( "1.0" );
63          mavenProject.setGroupId( "org.apache" );
64          mavenProject.setName( "test project" );
65  
66          Properties projectProperties = new Properties();
67          projectProperties.put( "foo", "bar" );
68          projectProperties.put( "java.version", "zloug" );
69          projectProperties.put( "replaceThis", "I am the replacement" );
70          mavenProject.setProperties( projectProperties );
71          MavenResourcesFiltering mavenResourcesFiltering = (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class
72              .getName() );
73  
74          Resource resource = new Resource();
75          List resources = new ArrayList();
76          resources.add( resource );
77          resource.setDirectory( unitDirectory.getPath() );
78          resource.setFiltering( true );
79  
80          List filtersFile = new ArrayList();
81  
82          List nonFilteredFileExtensions = Collections.singletonList( "gif" );
83  
84          MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( resources, outputDirectory,
85                                                                                         mavenProject, "UTF-8",
86                                                                                         filtersFile,
87                                                                                         nonFilteredFileExtensions,
88                                                                                         new StubMavenSession() );
89          mavenResourcesExecution.setUseDefaultFilterWrappers( true );
90  
91          mavenResourcesExecution.setEscapeString( "!" );
92  
93          mavenResourcesFiltering.filterResources( mavenResourcesExecution );
94  
95          FileInputStream in = null;
96          try
97          {
98              String content = IOUtil.toString( new FileInputStream( new File( outputDirectory, "content.xml" ) ) );
99  
100             System.out.println( "content " + content );
101             assertTrue( content.indexOf( "<broken-tag>Content with replacement: I am the replacement !</broken-tag>" ) >= 0 );
102             assertTrue( content.indexOf( "<broken-tag>Content with escaped replacement: Do not ${replaceThis} !</broken-tag>" ) >= 0 );
103         }
104         finally
105         {
106             IOUtil.close( in );
107         }
108 
109     }
110 }