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