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.io.IOException;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Properties;
31  
32  import org.apache.maven.model.Resource;
33  import org.codehaus.plexus.PlexusTestCase;
34  import org.codehaus.plexus.util.FileUtils;
35  import org.codehaus.plexus.util.IOUtil;
36  import org.sonatype.plexus.build.incremental.ThreadBuildContext;
37  import org.sonatype.plexus.build.incremental.test.TestIncrementalBuildContext;
38  
39  public class IncrementalResourceFilteringTest
40      extends PlexusTestCase
41  {
42  
43      File outputDirectory = new File( getBasedir(), "target/IncrementalResourceFilteringTest" );
44  
45      File unitDirectory = new File( getBasedir(), "src/test/units-files/incremental" );
46  
47      protected void setUp()
48          throws Exception
49      {
50          super.setUp();
51          if ( outputDirectory.exists() )
52          {
53              FileUtils.forceDelete( outputDirectory );
54          }
55          outputDirectory.mkdirs();
56      }
57  
58      public void testSimpleIncrementalFiltering()
59          throws Exception
60      {
61          // run full build first
62          filter( "time" );
63  
64          assertTime( "time", "file01.txt" );
65          assertTime( "time", "file02.txt" );
66  
67          // only one file is expected to change
68          HashSet changedFiles = new HashSet();
69          changedFiles.add( "file01.txt" );
70  
71          TestIncrementalBuildContext ctx = new TestIncrementalBuildContext( unitDirectory, changedFiles, new HashMap() );
72          ThreadBuildContext.setThreadBuildContext( ctx );
73  
74          filter( "notime" );
75          assertTime( "notime", "file01.txt" );
76          assertTime( "time", "file02.txt" ); // this one is unchanged
77  
78          assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file01.txt" ) ) );
79  
80          // one file is expected to be deleted
81          HashSet deletedFiles = new HashSet();
82          deletedFiles.add( "file01.txt" );
83  
84          ctx = new TestIncrementalBuildContext( unitDirectory, new HashSet(), changedFiles, new HashMap() );
85          ThreadBuildContext.setThreadBuildContext( ctx );
86  
87          filter( "moretime" );
88          assertFalse( new File( outputDirectory, "file01.txt" ).exists() );
89          assertTime( "time", "file02.txt" ); // this one is unchanged
90  
91          assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file01.txt" ) ) );
92  
93      }
94  
95      public void testOutputChange()
96          throws Exception
97      {
98          // run full build first
99          filter( "time" );
100 
101         // all files are reprocessed after contents of output directory changed (e.g. was deleted)
102         HashSet changedFiles = new HashSet();
103         changedFiles.add( "target/IncrementalResourceFilteringTest" );
104         TestIncrementalBuildContext ctx = new TestIncrementalBuildContext( unitDirectory, changedFiles, new HashMap() );
105         ThreadBuildContext.setThreadBuildContext( ctx );
106 
107         filter( "notime" );
108         assertTime( "notime", "file01.txt" );
109         assertTime( "notime", "file02.txt" );
110 
111         assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file01.txt" ) ) );
112         assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file02.txt" ) ) );
113 
114     }
115 
116     public void testFilterChange()
117         throws Exception
118     {
119         // run full build first
120         filter( "time" );
121 
122         // all files are reprocessed after content of filters changes
123         HashSet changedFiles = new HashSet();
124         changedFiles.add( "filters.txt" );
125         TestIncrementalBuildContext ctx = new TestIncrementalBuildContext( unitDirectory, changedFiles, new HashMap() );
126         ThreadBuildContext.setThreadBuildContext( ctx );
127 
128         filter( "notime" );
129         assertTime( "notime", "file01.txt" );
130         assertTime( "notime", "file02.txt" );
131 
132         assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file01.txt" ) ) );
133         assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file02.txt" ) ) );
134 
135     }
136 
137     public void testFilterDeleted()
138         throws Exception
139     {
140         // run full build first
141         filter( "time" );
142 
143         // all files are reprocessed after content of filters changes
144         HashSet deletedFiles = new HashSet();
145         deletedFiles.add( "filters.txt" );
146         TestIncrementalBuildContext ctx =
147             new TestIncrementalBuildContext( unitDirectory, new HashSet(), deletedFiles, new HashMap() );
148         ThreadBuildContext.setThreadBuildContext( ctx );
149 
150         filter( "notime" );
151         assertTime( "notime", "file01.txt" );
152         assertTime( "notime", "file02.txt" );
153 
154         assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file01.txt" ) ) );
155         assertTrue( ctx.getRefreshFiles().contains( new File( outputDirectory, "file02.txt" ) ) );
156     }
157 
158     private void assertTime( String time, String relpath )
159         throws IOException
160     {
161         Properties properties = new Properties();
162 
163         InputStream is = new FileInputStream( new File( outputDirectory, relpath ) );
164         try
165         {
166             properties.load( is );
167         }
168         finally
169         {
170             IOUtil.close( is );
171         }
172 
173         assertEquals( time, properties.getProperty( "time" ) );
174     }
175 
176     private void filter( String time )
177         throws Exception, MavenFilteringException
178     {
179         File baseDir = new File( getBasedir() );
180         StubMavenProject mavenProject = new StubMavenProject( baseDir );
181         mavenProject.setVersion( "1.0" );
182         mavenProject.setGroupId( "org.apache" );
183         mavenProject.setName( "test project" );
184 
185         Properties projectProperties = new Properties();
186         projectProperties.put( "time", time );
187         projectProperties.put( "java.version", "zloug" );
188         mavenProject.setProperties( projectProperties );
189         MavenResourcesFiltering mavenResourcesFiltering =
190             (MavenResourcesFiltering) lookup( MavenResourcesFiltering.class.getName() );
191 
192         String unitFilesDir = new File( unitDirectory, "files" ).getPath();
193 
194         Resource resource = new Resource();
195         List resources = new ArrayList();
196         resources.add( resource );
197         resource.setDirectory( unitFilesDir );
198         resource.setFiltering( true );
199 
200         List filtersFile = new ArrayList();
201         filtersFile.add( new File( unitDirectory, "filters.txt" ).getPath() );
202 
203         mavenResourcesFiltering.filterResources( resources, outputDirectory, mavenProject, "UTF-8", filtersFile,
204                                                  new ArrayList(), new StubMavenSession() );
205     }
206 
207 }