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