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.commons.io.FileUtils;
30  import org.apache.maven.model.Resource;
31  import org.codehaus.plexus.PlexusTestCase;
32  
33  /**
34   * @author Olivier Lamy
35   *
36   */
37  public class MuliLinesMavenResourcesFilteringTest
38      extends PlexusTestCase
39  {
40  
41      File outputDirectory = new File( getBasedir(), "target/MuliLinesMavenResourcesFilteringTest" );
42  
43      @Override
44      protected void setUp()
45          throws Exception
46      {
47          super.setUp();
48          if ( outputDirectory.exists() )
49          {
50              FileUtils.deleteDirectory( outputDirectory );
51          }
52          outputDirectory.mkdirs();
53      }
54  
55      /**
56       * @throws Exception
57       */
58      public void testFilteringTokenOnce()
59          throws Exception
60      {
61          File baseDir = new File( "c:\\foo\\bar" );
62          StubMavenProject mavenProject = new StubMavenProject( baseDir );
63          mavenProject.setVersion( "1.0" );
64          mavenProject.setGroupId( "org.apache" );
65          mavenProject.setName( "test project" );
66  
67          Properties projectProperties = new Properties();
68          projectProperties.put( "foo", "bar" );
69          projectProperties.put( "java.version", "zloug" );
70          mavenProject.setProperties( projectProperties );
71          MavenResourcesFiltering mavenResourcesFiltering = lookup( MavenResourcesFiltering.class );
72  
73          String unitFilesDir = getBasedir() + "/src/test/units-files/MRESOURCES-104";
74  
75          Resource resource = new Resource();
76          List<Resource> resources = new ArrayList<>();
77          resources.add( resource );
78          resource.setDirectory( unitFilesDir );
79          resource.setFiltering( true );
80  
81          List<String> filtersFile = new ArrayList<>();
82          filtersFile.add( getBasedir() + "/src/test/units-files/MRESOURCES-104/test.properties" );
83  
84          List<String> nonFilteredFileExtensions = Collections.singletonList( "gif" );
85  
86          MavenResourcesExecution mavenResourcesExecution =
87              new MavenResourcesExecution( resources, outputDirectory, mavenProject, "UTF-8", filtersFile,
88                                           nonFilteredFileExtensions, new StubMavenSession() );
89          mavenResourcesExecution.setUseDefaultFilterWrappers( true );
90  
91          mavenResourcesFiltering.filterResources( mavenResourcesExecution );
92  
93          Properties result = new Properties();
94          
95          try ( FileInputStream in = new FileInputStream( new File( outputDirectory, "test.properties" ) ) )
96          {
97              result.load( in );
98          }
99  
100         // email=foo@bar.com
101         // foo=${project.version}
102         // bar=@project.version@
103         assertEquals( "1.0", result.get( "foo" ) );
104         assertEquals( "1.0", result.get( "bar" ) );
105         assertEquals( "foo@bar.com", result.get( "email" ) );
106     }
107 
108 }