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 <a href="mailto:olamy@apache.org">olamy</a>
36   * 
37   * 
38   * @version $Id: MuliLinesMavenResourcesFilteringTest.java 1052026 2010-12-22 18:42:50Z olamy $
39   */
40  public class MuliLinesMavenResourcesFilteringTest
41      extends PlexusTestCase
42  {
43  
44      File outputDirectory = new File( getBasedir(), "target/MuliLinesMavenResourcesFilteringTest" );
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      /**
58       * 
59       * @throws Exception
60       */
61      public void testFilteringTokenOnce()
62          throws Exception
63      {
64          File baseDir = new File( "c:\\foo\\bar" );
65          StubMavenProject mavenProject = new StubMavenProject( baseDir );
66          mavenProject.setVersion( "1.0" );
67          mavenProject.setGroupId( "org.apache" );
68          mavenProject.setName( "test project" );
69  
70          Properties projectProperties = new Properties();
71          projectProperties.put( "foo", "bar" );
72          projectProperties.put( "java.version", "zloug" );
73          mavenProject.setProperties( projectProperties );
74          MavenResourcesFiltering mavenResourcesFiltering = (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class.getName() );
75  
76          String unitFilesDir = getBasedir() + "/src/test/units-files/MRESOURCES-104";
77  
78          Resource resource = new Resource();
79          List resources = new ArrayList();
80          resources.add( resource );
81          resource.setDirectory( unitFilesDir );
82          resource.setFiltering( true );
83  
84          List filtersFile = new ArrayList();
85          filtersFile.add( getBasedir() + "/src/test/units-files/MRESOURCES-104/test.properties" );
86  
87          List nonFilteredFileExtensions = Collections.singletonList( "gif" );
88  
89          MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution( resources, outputDirectory,
90                                                                                         mavenProject, "UTF-8",
91                                                                                         filtersFile,
92                                                                                         nonFilteredFileExtensions,
93                                                                                         new StubMavenSession() );
94          mavenResourcesExecution.setUseDefaultFilterWrappers( true );
95          
96          
97          mavenResourcesFiltering.filterResources( mavenResourcesExecution );
98  
99          Properties result = new Properties();
100         FileInputStream in = null;
101         try
102         {
103             in = new FileInputStream( new File( outputDirectory, "test.properties" ) );
104             result.load( in );
105         }
106         finally
107         {
108             IOUtil.close( in );
109         }
110         
111         System.out.println("properties " + result.toString());
112         //email=foo@bar.com
113         //foo=${project.version}
114         //bar=@project.version@
115         assertEquals( "1.0", result.get( "foo" ) );
116         assertEquals( "1.0", result.get( "bar" ) );
117         assertEquals( "foo@bar.com", result.get( "email" ) );
118     }
119 
120 }