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   * 
23   */
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Random;
30  
31  import junit.framework.TestCase;
32  
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.artifact.DefaultArtifact;
35  import org.apache.maven.artifact.handler.ArtifactHandler;
36  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
37  import org.apache.maven.artifact.versioning.VersionRange;
38  import org.apache.maven.plugin.MojoExecutionException;
39  import org.apache.maven.plugin.dependency.testUtils.stubs.StubSourcesFileMarkerHandler;
40  import org.apache.maven.plugin.logging.Log;
41  import org.apache.maven.plugin.testing.SilentLog;
42  
43  /**
44   * @author brianf
45   * 
46   */
47  public class TestSourcesMarkerFileHandler
48      extends TestCase
49  {
50      List artifacts = new ArrayList();
51  
52      Log log = new SilentLog();
53  
54      File outputFolder;
55  
56      protected void setUp()
57          throws Exception
58      {
59          super.setUp();
60  
61          ArtifactHandler ah = new DefaultArtifactHandler();
62          VersionRange vr = VersionRange.createFromVersion( "1.1" );
63          Artifact artifact = new DefaultArtifact( "test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false );
64          artifacts.add( artifact );
65          artifact = new DefaultArtifact( "test", "2", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false );
66          artifacts.add( artifact );
67          artifact = new DefaultArtifact( "test", "3", vr, Artifact.SCOPE_TEST, "sources", "", ah, false );
68          artifacts.add( artifact );
69          artifact = new DefaultArtifact( "test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false );
70          artifacts.add( artifact );
71  
72          // pick random output location
73          Random a = new Random();
74          outputFolder = new File( "target/markers" + a.nextLong() + "/" );
75          outputFolder.delete();
76          assertFalse( outputFolder.exists() );
77      }
78  
79      protected void tearDown()
80      {
81          outputFolder.delete();
82      }
83  
84      public void testSetMarkerResolved()
85          throws MojoExecutionException
86      {
87          DefaultFileMarkerHandler handler = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
88                                                                           this.outputFolder, true );
89          assertFalse( handler.isMarkerSet() );
90          handler.setMarker();
91          assertTrue( handler.isMarkerSet() );
92          handler.clearMarker();
93          assertFalse( handler.isMarkerSet() );
94  
95          handler.setMarker();
96          handler.setMarker();
97          assertTrue( handler.isMarkerSet() );
98  
99          handler.clearMarker();
100         handler.clearMarker();
101         outputFolder.delete();
102         assertFalse( outputFolder.exists() );
103     }
104 
105     public void testSetMarkerUnresolved()
106         throws MojoExecutionException
107     {
108         DefaultFileMarkerHandler handler = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
109                                                                          this.outputFolder, false );
110         assertFalse( handler.isMarkerSet() );
111         handler.setMarker();
112         assertTrue( handler.isMarkerSet() );
113         handler.clearMarker();
114         assertFalse( handler.isMarkerSet() );
115 
116         handler.setMarker();
117         handler.setMarker();
118         assertTrue( handler.isMarkerSet() );
119 
120         handler.clearMarker();
121         handler.clearMarker();
122         outputFolder.delete();
123         assertFalse( outputFolder.exists() );
124     }
125 
126     public void testBothMarkers()
127         throws MojoExecutionException
128     {
129         DefaultFileMarkerHandler handler = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 1 ),
130                                                                          this.outputFolder, true );
131         DefaultFileMarkerHandler handler2 = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 1 ),
132                                                                           this.outputFolder, false );
133 
134         handler.setMarker();
135         assertTrue( handler.isMarkerSet() );
136         assertTrue( handler2.isMarkerSet() );
137 
138         handler2.clearMarker();
139         assertFalse( handler.isMarkerSet() );
140         assertFalse( handler2.isMarkerSet() );
141         outputFolder.delete();
142         assertFalse( outputFolder.exists() );
143     }
144 
145     public void testMarkerFile()
146         throws MojoExecutionException, IOException
147     {
148         DefaultFileMarkerHandler handler = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
149                                                                          this.outputFolder, true );
150         DefaultFileMarkerHandler handler2 = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
151                                                                           this.outputFolder, false );
152 
153         File handle = handler.getMarkerFile();
154         File handle2 = handler2.getMarkerFile();
155         assertFalse( handle.exists() );
156         assertFalse( handler.isMarkerSet() );
157         assertFalse( handle2.exists() );
158         assertFalse( handler2.isMarkerSet() );
159 
160         // if either file exists, the marker is set
161         handler.setMarker();
162         assertTrue( handler.isMarkerSet() );
163         assertTrue( handle.exists() );
164         assertTrue( handler2.isMarkerSet() );
165         assertFalse( handle2.exists() );
166 
167         // if either file exists, the marker is set
168         // setting 1 will clear the other marker
169         handler2.setMarker();
170         assertTrue( handler.isMarkerSet() );
171         assertFalse( handle.exists() );
172         assertTrue( handler2.isMarkerSet() );
173         assertTrue( handle2.exists() );
174 
175         // reset file for next test
176         handle.createNewFile();
177 
178         // only delete one, should be still true
179         handle2.delete();
180         assertTrue( handler.isMarkerSet() );
181         assertTrue( handler2.isMarkerSet() );
182 
183         // delete the 2nd, should be false.
184         handle.delete();
185         assertFalse( handler.isMarkerSet() );
186         assertFalse( handler2.isMarkerSet() );
187 
188         handle.createNewFile();
189         assertTrue( handler.isMarkerSet() );
190         assertTrue( handler2.isMarkerSet() );
191 
192         handler.clearMarker();
193         assertFalse( handle.exists() );
194         handler2.clearMarker();
195         assertFalse( handle2.exists() );
196 
197         handle.delete();
198         handle2.delete();
199 
200         outputFolder.delete();
201         assertFalse( outputFolder.exists() );
202     }
203 
204     public void testMarkerTimeStampResolved()
205         throws MojoExecutionException, IOException, InterruptedException
206     {
207         doTestMarkerTimeStamp( true );
208     }
209 
210     public void testMarkerTimeStampUnResolved()
211         throws MojoExecutionException, IOException, InterruptedException
212     {
213         doTestMarkerTimeStamp( false );
214     }
215 
216     public void doTestMarkerTimeStamp( boolean resolved )
217         throws MojoExecutionException, IOException, InterruptedException
218     {
219         File theFile = new File( outputFolder, "theFile.jar" );
220         outputFolder.mkdirs();
221         theFile.createNewFile();
222         Artifact theArtifact = (Artifact) artifacts.get( 0 );
223         theArtifact.setFile( theFile );
224         SourcesFileMarkerHandler resolvedHandler = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
225                                                                                  this.outputFolder, resolved );
226         SourcesFileMarkerHandler unResolvedHandler = new SourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
227                                                                                    this.outputFolder, !resolved );
228 
229         assertFalse( resolvedHandler.isMarkerSet() );
230         assertFalse( unResolvedHandler.isMarkerSet() );
231         // if the marker is not set, assume it is infinately older than the
232         // artifact.
233         assertTrue( resolvedHandler.isMarkerOlder( theArtifact ) );
234         assertTrue( unResolvedHandler.isMarkerOlder( theArtifact ) );
235         resolvedHandler.setMarker();
236         assertFalse( resolvedHandler.isMarkerOlder( theArtifact ) );
237         assertFalse( unResolvedHandler.isMarkerOlder( theArtifact ) );
238 
239         resolvedHandler.clearMarker();
240         unResolvedHandler.setMarker();
241         assertFalse( resolvedHandler.isMarkerOlder( theArtifact ) );
242         assertFalse( unResolvedHandler.isMarkerOlder( theArtifact ) );
243 
244         theFile.setLastModified( theFile.lastModified() + 60000 );
245         assertTrue( resolvedHandler.isMarkerOlder( theArtifact ) );
246         assertTrue( unResolvedHandler.isMarkerOlder( theArtifact ) );
247 
248         theFile.delete();
249         resolvedHandler.clearMarker();
250         assertFalse( resolvedHandler.isMarkerSet() );
251     }
252 
253     public void testMarkerFileException()
254     {
255         // this stub wraps the file with an object to throw exceptions
256         StubSourcesFileMarkerHandler handler = new StubSourcesFileMarkerHandler( (Artifact) artifacts.get( 0 ),
257                                                                                  this.outputFolder, true );
258         try
259         {
260             handler.setMarker();
261             fail( "Expected an Exception here" );
262         }
263         catch ( MojoExecutionException e )
264         {
265         }
266     }
267 
268     public void testMarkerFileResolvedSetter()
269     {
270         SourcesFileMarkerHandler handler = new SourcesFileMarkerHandler( null, null, true );
271         assertTrue( handler.isResolved() );
272         handler.setResolved( false );
273         assertFalse( handler.isResolved() );
274     }
275 
276     public void testNullParent()
277         throws MojoExecutionException
278     {
279         // the parent isn't set so this will create the marker in the local
280         // folder. We must clear the
281         // marker to avoid leaving test droppings in root.
282         DefaultFileMarkerHandler handler = new SourcesFileMarkerHandler( null, null, false );
283         handler.setArtifact( (Artifact) artifacts.get( 0 ) );
284         handler.setMarker();
285         assertTrue( handler.isMarkerSet() );
286         handler.clearMarker();
287         assertFalse( handler.isMarkerSet() );
288     }
289 
290     public void testNullParentResolved()
291         throws MojoExecutionException
292     {
293         // the parent isn't set so this will create the marker in the local
294         // folder. We must clear the
295         // marker to avoid leaving test droppings in root.
296         DefaultFileMarkerHandler handler = new SourcesFileMarkerHandler( null, null, true );
297         handler.setArtifact( (Artifact) artifacts.get( 0 ) );
298         handler.setMarker();
299         assertTrue( handler.isMarkerSet() );
300         handler.clearMarker();
301         assertFalse( handler.isMarkerSet() );
302 
303     }
304 }