View Javadoc
1   package org.apache.maven.plugins.dependency.utils.markers;
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.IOException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.commons.io.FileUtils;
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
31  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
32  import org.apache.maven.plugins.dependency.testUtils.stubs.StubUnpackFileMarkerHandler;
33  import org.apache.maven.plugin.logging.Log;
34  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
35  import org.apache.maven.plugin.testing.SilentLog;
36  
37  public class TestUnpackMarkerFileHandler
38      extends AbstractMojoTestCase
39  {
40      List<ArtifactItem> artifactItems = new ArrayList<>();
41  
42      Log log = new SilentLog();
43  
44      File outputFolder;
45  
46      protected File testDir;
47  
48      protected DependencyArtifactStubFactory stubFactory;
49  
50      protected void setUp()
51          throws Exception
52      {
53          super.setUp();
54  
55          testDir = new File( getBasedir(), "target" + File.separatorChar + "unit-tests" + File.separatorChar
56              + "unpack-markers" + File.separatorChar );
57          FileUtils.deleteDirectory( testDir );
58          assertFalse( testDir.exists() );
59  
60          stubFactory = new DependencyArtifactStubFactory( this.testDir, false );
61          Artifact artifact = stubFactory.createArtifact( "test", "test", "1" );
62          ArtifactItem artifactItem;
63          stubFactory.getArtifactItem( artifact );
64          artifactItems.add( stubFactory.getArtifactItem( stubFactory.createArtifact( "test", "test", "1" ) ) );
65          artifact = stubFactory.createArtifact( "test2", "test2", "2" );
66          artifactItem = new ArtifactItem( artifact );
67          artifactItem.setIncludes( "**/*.xml" );
68          artifactItems.add( artifactItem );
69          artifact = stubFactory.createArtifact( "test3", "test3", "3" );
70          artifactItem = new ArtifactItem( artifact );
71          artifactItem.setExcludes( "**/*.class" );
72          artifactItems.add( artifactItem );
73          artifact = stubFactory.createArtifact( "test4", "test4", "4" );
74          artifactItem = new ArtifactItem( artifact );
75          artifactItem.setIncludes( "**/*.xml" );
76          artifactItem.setExcludes( "**/*.class" );
77          artifactItems.add( artifactItem );
78  
79          outputFolder = new File( "target/markers/" );
80          FileUtils.deleteDirectory( this.outputFolder );
81          assertFalse( outputFolder.exists() );
82      }
83  
84      protected void tearDown()
85          throws IOException
86      {
87          FileUtils.deleteDirectory( this.outputFolder );
88      }
89  
90      /**
91       * Assert that default functionality still exists
92       * 
93       * @throws MojoExecutionException in case of an error.
94       */
95      public void testSetMarker()
96          throws MojoExecutionException
97      {
98          UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( artifactItems.get( 0 ), this.outputFolder );
99          assertFalse( handler.isMarkerSet() );
100         handler.setMarker();
101         assertTrue( handler.isMarkerSet() );
102         handler.clearMarker();
103         assertFalse( handler.isMarkerSet() );
104 
105         handler.setMarker();
106         assertTrue( handler.isMarkerSet() );
107         handler.setMarker();
108         assertTrue( handler.isMarkerSet() );
109 
110         handler.clearMarker();
111         assertFalse( handler.isMarkerSet() );
112         handler.clearMarker();
113         assertFalse( handler.isMarkerSet() );
114     }
115 
116     public void testMarkerFile()
117         throws MojoExecutionException, IOException
118     {
119         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( artifactItems.get( 0 ), this.outputFolder );
120 
121         File handle = handler.getMarkerFile();
122         assertFalse( handle.exists() );
123         assertFalse( handler.isMarkerSet() );
124 
125         handler.setMarker();
126         assertTrue( handler.isMarkerSet() );
127         assertTrue( handle.exists() );
128 
129         handle.delete();
130         assertFalse( handler.isMarkerSet() );
131 
132         handle.createNewFile();
133         assertTrue( handler.isMarkerSet() );
134 
135         handler.clearMarker();
136         assertFalse( handle.exists() );
137     }
138 
139     public void testMarkerTimeStamp()
140         throws MojoExecutionException, IOException, InterruptedException
141     {
142         File theFile = new File( outputFolder, "theFile.jar" );
143         outputFolder.mkdirs();
144         theFile.createNewFile();
145         ArtifactItem theArtifactItem = artifactItems.get( 0 );
146         Artifact theArtifact = theArtifactItem.getArtifact();
147         theArtifact.setFile( theFile );
148         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( theArtifactItem, this.outputFolder );
149         assertFalse( handler.isMarkerSet() );
150         // if the marker is not set, assume it is infinately older than the
151         // artifact.
152         assertTrue( handler.isMarkerOlder( theArtifact ) );
153         handler.setMarker();
154         assertFalse( handler.isMarkerOlder( theArtifact ) );
155 
156         assertTrue( theFile.setLastModified( theFile.lastModified() + 60000 ) );
157         assertTrue( handler.isMarkerOlder( theArtifact ) );
158 
159         theFile.delete();
160         handler.clearMarker();
161         assertFalse( handler.isMarkerSet() );
162     }
163 
164     public void testMarkerFileException()
165     {
166         // this stub wraps the file with an object to throw exceptions
167         StubUnpackFileMarkerHandler handler =
168             new StubUnpackFileMarkerHandler( artifactItems.get( 0 ), this.outputFolder );
169         try
170         {
171             handler.setMarker();
172             fail( "Expected an Exception here" );
173         }
174         catch ( MojoExecutionException e )
175         {
176 
177         }
178     }
179 
180     public void testGetterSetter()
181     {
182         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( null, null );
183         assertNull( handler.getArtifactItem() );
184         assertNull( handler.getArtifact() );
185         handler.setArtifactItem( artifactItems.get( 0 ) );
186         assertSame( artifactItems.get( 0 ), handler.getArtifactItem() );
187         assertSame( artifactItems.get( 0 ).getArtifact(), handler.getArtifact() );
188 
189         assertNull( handler.getMarkerFilesDirectory() );
190         handler.setMarkerFilesDirectory( outputFolder );
191         assertSame( outputFolder, handler.getMarkerFilesDirectory() );
192     }
193 
194     public void testNullParent()
195         throws MojoExecutionException
196     {
197         // the parent isn't set so this will create the marker in the local
198         // folder. We must clear the
199         // marker to avoid leaving test droppings in root.
200         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( null, null );
201         handler.setArtifactItem( artifactItems.get( 0 ) );
202         handler.setMarker();
203         assertTrue( handler.isMarkerSet() );
204         handler.clearMarker();
205         assertFalse( handler.isMarkerSet() );
206     }
207 
208     public void testIncludesMarker()
209         throws MojoExecutionException, IOException
210     {
211         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( artifactItems.get( 1 ), outputFolder );
212         File handle = handler.getMarkerFile();
213         assertFalse( handle.exists() );
214         assertFalse( handler.isMarkerSet() );
215 
216         handler.setMarker();
217         assertTrue( handler.isMarkerSet() );
218         assertTrue( handle.exists() );
219         String hashCode = "" + ( "**/*.xml".hashCode() );
220         assertTrue( handle.getName().contains( hashCode ) );
221 
222         handle.delete();
223         assertFalse( handler.isMarkerSet() );
224 
225         handle.createNewFile();
226         assertTrue( handler.isMarkerSet() );
227 
228         handler.clearMarker();
229         assertFalse( handle.exists() );
230     }
231 
232     public void testExcludesMarker()
233         throws MojoExecutionException, IOException
234     {
235         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( artifactItems.get( 2 ), outputFolder );
236         File handle = handler.getMarkerFile();
237         assertFalse( handle.exists() );
238         assertFalse( handler.isMarkerSet() );
239 
240         handler.setMarker();
241         assertTrue( handler.isMarkerSet() );
242         assertTrue( handle.exists() );
243         String hashCode = "" + ( "**/*.class".hashCode() );
244         assertTrue( handle.getName().contains( hashCode ) );
245 
246         handle.delete();
247         assertFalse( handler.isMarkerSet() );
248 
249         handle.createNewFile();
250         assertTrue( handler.isMarkerSet() );
251 
252         handler.clearMarker();
253         assertFalse( handle.exists() );
254     }
255 
256     public void testIncludesExcludesMarker()
257         throws MojoExecutionException, IOException
258     {
259         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler( artifactItems.get( 3 ), outputFolder );
260         File handle = handler.getMarkerFile();
261         assertFalse( handle.exists() );
262         assertFalse( handler.isMarkerSet() );
263 
264         handler.setMarker();
265         assertTrue( handler.isMarkerSet() );
266         assertTrue( handle.exists() );
267         String hashCode = "" + ( 0 + "**/*.class".hashCode() + "**/*.xml".hashCode() );
268         assertTrue( handle.getName().contains( hashCode ) );
269 
270         handle.delete();
271         assertFalse( handler.isMarkerSet() );
272 
273         handle.createNewFile();
274         assertTrue( handler.isMarkerSet() );
275 
276         handler.clearMarker();
277         assertFalse( handle.exists() );
278     }
279 }