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