View Javadoc

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