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  
30  import junit.framework.TestCase;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.DefaultArtifact;
34  import org.apache.maven.artifact.handler.ArtifactHandler;
35  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
36  import org.apache.maven.artifact.versioning.VersionRange;
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.apache.maven.plugin.dependency.testUtils.DependencyTestUtils;
39  import org.apache.maven.plugin.dependency.testUtils.stubs.StubDefaultFileMarkerHandler;
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 TestDefaultMarkerFileHandler
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          outputFolder = new File( "target/markers/" );
73          DependencyTestUtils.removeDirectory( this.outputFolder );
74          assertFalse( outputFolder.exists() );
75      }
76  
77      protected void tearDown()
78          throws IOException
79      {
80          DependencyTestUtils.removeDirectory( this.outputFolder );
81      }
82  
83      public void testSetMarker()
84          throws MojoExecutionException
85      {
86          DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( (Artifact) artifacts.get( 0 ),
87                                                                           this.outputFolder );
88          assertFalse( handler.isMarkerSet() );
89          handler.setMarker();
90          assertTrue( handler.isMarkerSet() );
91          handler.clearMarker();
92          assertFalse( handler.isMarkerSet() );
93  
94          handler.setMarker();
95          assertTrue( handler.isMarkerSet() );
96          handler.setMarker();
97          assertTrue( handler.isMarkerSet() );
98  
99          handler.clearMarker();
100         assertFalse( handler.isMarkerSet() );
101         handler.clearMarker();
102         assertFalse( handler.isMarkerSet() );
103     }
104 
105     public void testMarkerFile()
106         throws MojoExecutionException, IOException
107     {
108         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( (Artifact) artifacts.get( 0 ),
109                                                                          this.outputFolder );
110 
111         File handle = handler.getMarkerFile();
112         assertFalse( handle.exists() );
113         assertFalse( handler.isMarkerSet() );
114 
115         handler.setMarker();
116         assertTrue( handler.isMarkerSet() );
117         assertTrue( handle.exists() );
118 
119         handle.delete();
120         assertFalse( handler.isMarkerSet() );
121 
122         handle.createNewFile();
123         assertTrue( handler.isMarkerSet() );
124 
125         handler.clearMarker();
126         assertFalse( handle.exists() );
127     }
128 
129     public void testMarkerTimeStamp()
130         throws MojoExecutionException, IOException, InterruptedException
131     {
132         File theFile = new File( outputFolder, "theFile.jar" );
133         outputFolder.mkdirs();
134         theFile.createNewFile();
135         Artifact theArtifact = (Artifact) artifacts.get( 0 );
136         theArtifact.setFile( theFile );
137         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( theArtifact, this.outputFolder );
138         assertFalse( handler.isMarkerSet() );
139         // if the marker is not set, assume it is infinately older than the
140         // artifact.
141         assertTrue( handler.isMarkerOlder( theArtifact ) );
142         handler.setMarker();
143         assertFalse( handler.isMarkerOlder( theArtifact ) );
144 
145         theFile.setLastModified( theFile.lastModified() + 60000 );
146         assertTrue( handler.isMarkerOlder( theArtifact ) );
147 
148         theFile.delete();
149         handler.clearMarker();
150         assertFalse( handler.isMarkerSet() );
151     }
152 
153     public void testMarkerFileException()
154     {
155         // this stub wraps the file with an object to throw exceptions
156         StubDefaultFileMarkerHandler handler = new StubDefaultFileMarkerHandler( (Artifact) artifacts.get( 0 ),
157                                                                                  this.outputFolder );
158         try
159         {
160             handler.setMarker();
161             fail( "Expected an Exception here" );
162         }
163         catch ( MojoExecutionException e )
164         {
165 
166         }
167     }
168 
169     public void testGetterSetter()
170     {
171         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( null, null );
172         assertTrue( handler.getArtifact() == null );
173         handler.setArtifact( (Artifact) artifacts.get( 0 ) );
174         assertSame( artifacts.get( 0 ), handler.getArtifact() );
175 
176         assertTrue( handler.getMarkerFilesDirectory() == null );
177         handler.setMarkerFilesDirectory( outputFolder );
178         assertSame( outputFolder, handler.getMarkerFilesDirectory() );
179     }
180 
181     public void testNullParent()
182         throws MojoExecutionException
183     {
184         // the parent isn't set so this will create the marker in the local
185         // folder. We must clear the
186         // marker to avoid leaving test droppings in root.
187         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( null, null );
188         handler.setArtifact( (Artifact) artifacts.get( 0 ) );
189         handler.setMarker();
190         assertTrue( handler.isMarkerSet() );
191         handler.clearMarker();
192         assertFalse( handler.isMarkerSet() );
193     }
194 }