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 junit.framework.TestCase;
28  
29  import org.apache.commons.io.FileUtils;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.artifact.DefaultArtifact;
32  import org.apache.maven.artifact.handler.ArtifactHandler;
33  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
34  import org.apache.maven.artifact.versioning.VersionRange;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugins.dependency.testUtils.stubs.StubDefaultFileMarkerHandler;
37  import org.apache.maven.plugin.logging.Log;
38  import org.apache.maven.plugin.testing.SilentLog;
39  
40  /**
41   * @author brianf
42   */
43  public class TestDefaultMarkerFileHandler
44      extends TestCase
45  {
46      List<Artifact> artifacts = new ArrayList<>();
47  
48      Log log = new SilentLog();
49  
50      File outputFolder;
51  
52      protected void setUp()
53          throws Exception
54      {
55          super.setUp();
56  
57          ArtifactHandler ah = new DefaultArtifactHandler();
58          VersionRange vr = VersionRange.createFromVersion( "1.1" );
59          Artifact artifact = new DefaultArtifact( "test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false );
60          artifacts.add( artifact );
61          artifact = new DefaultArtifact( "test", "2", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false );
62          artifacts.add( artifact );
63          artifact = new DefaultArtifact( "test", "3", vr, Artifact.SCOPE_TEST, "sources", "", ah, false );
64          artifacts.add( artifact );
65          artifact = new DefaultArtifact( "test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false );
66          artifacts.add( artifact );
67  
68          outputFolder = new File( "target/markers/" );
69          FileUtils.deleteDirectory( this.outputFolder );
70          assertFalse( outputFolder.exists() );
71      }
72  
73      protected void tearDown()
74          throws IOException
75      {
76          FileUtils.deleteDirectory( this.outputFolder );
77      }
78  
79      public void testSetMarker()
80          throws MojoExecutionException
81      {
82          DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifacts.get( 0 ), this.outputFolder );
83          assertFalse( handler.isMarkerSet() );
84          handler.setMarker();
85          assertTrue( handler.isMarkerSet() );
86          handler.clearMarker();
87          assertFalse( handler.isMarkerSet() );
88  
89          handler.setMarker();
90          assertTrue( handler.isMarkerSet() );
91          handler.setMarker();
92          assertTrue( handler.isMarkerSet() );
93  
94          handler.clearMarker();
95          assertFalse( handler.isMarkerSet() );
96          handler.clearMarker();
97          assertFalse( handler.isMarkerSet() );
98      }
99  
100     public void testMarkerFile()
101         throws MojoExecutionException, IOException
102     {
103         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( artifacts.get( 0 ), this.outputFolder );
104 
105         File handle = handler.getMarkerFile();
106         assertFalse( handle.exists() );
107         assertFalse( handler.isMarkerSet() );
108 
109         handler.setMarker();
110         assertTrue( handler.isMarkerSet() );
111         assertTrue( handle.exists() );
112 
113         handle.delete();
114         assertFalse( handler.isMarkerSet() );
115 
116         handle.createNewFile();
117         assertTrue( handler.isMarkerSet() );
118 
119         handler.clearMarker();
120         assertFalse( handle.exists() );
121     }
122 
123     public void testMarkerTimeStamp()
124         throws MojoExecutionException, IOException, InterruptedException
125     {
126         File theFile = new File( outputFolder, "theFile.jar" );
127         outputFolder.mkdirs();
128         theFile.createNewFile();
129         Artifact theArtifact = artifacts.get( 0 );
130         theArtifact.setFile( theFile );
131         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( theArtifact, 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         assertTrue( 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         StubDefaultFileMarkerHandler handler =
151             new StubDefaultFileMarkerHandler( artifacts.get( 0 ), 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         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( null, null );
166         assertNull( handler.getArtifact() );
167         handler.setArtifact( artifacts.get( 0 ) );
168         assertSame( artifacts.get( 0 ), handler.getArtifact() );
169 
170         assertNull( handler.getMarkerFilesDirectory() );
171         handler.setMarkerFilesDirectory( outputFolder );
172         assertSame( outputFolder, handler.getMarkerFilesDirectory() );
173     }
174 
175     public void testNullParent()
176         throws MojoExecutionException
177     {
178         // the parent isn't set so this will create the marker in the local
179         // folder. We must clear the
180         // marker to avoid leaving test droppings in root.
181         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler( null, null );
182         handler.setArtifact( artifacts.get( 0 ) );
183         handler.setMarker();
184         assertTrue( handler.isMarkerSet() );
185         handler.clearMarker();
186         assertFalse( handler.isMarkerSet() );
187     }
188 }