View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.utils.markers;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  import org.apache.commons.io.FileUtils;
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.DefaultArtifact;
30  import org.apache.maven.artifact.handler.ArtifactHandler;
31  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
32  import org.apache.maven.artifact.versioning.VersionRange;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.logging.Log;
35  import org.apache.maven.plugin.testing.SilentLog;
36  import org.apache.maven.plugins.dependency.testUtils.stubs.StubDefaultFileMarkerHandler;
37  
38  /**
39   * @author brianf
40   */
41  public class TestDefaultMarkerFileHandler extends TestCase {
42      List<Artifact> artifacts = new ArrayList<>();
43  
44      Log log = new SilentLog();
45  
46      File outputFolder;
47  
48      protected void setUp() throws Exception {
49          super.setUp();
50  
51          ArtifactHandler ah = new DefaultArtifactHandler();
52          VersionRange vr = VersionRange.createFromVersion("1.1");
53          Artifact artifact = new DefaultArtifact("test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false);
54          artifacts.add(artifact);
55          artifact = new DefaultArtifact("test", "2", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
56          artifacts.add(artifact);
57          artifact = new DefaultArtifact("test", "3", vr, Artifact.SCOPE_TEST, "sources", "", ah, false);
58          artifacts.add(artifact);
59          artifact = new DefaultArtifact("test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false);
60          artifacts.add(artifact);
61  
62          outputFolder = new File("target/markers/");
63          FileUtils.deleteDirectory(this.outputFolder);
64          assertFalse(outputFolder.exists());
65      }
66  
67      protected void tearDown() throws IOException {
68          FileUtils.deleteDirectory(this.outputFolder);
69      }
70  
71      public void testSetMarker() throws MojoExecutionException {
72          DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifacts.get(0), this.outputFolder);
73          assertFalse(handler.isMarkerSet());
74          handler.setMarker();
75          assertTrue(handler.isMarkerSet());
76          handler.clearMarker();
77          assertFalse(handler.isMarkerSet());
78  
79          handler.setMarker();
80          assertTrue(handler.isMarkerSet());
81          handler.setMarker();
82          assertTrue(handler.isMarkerSet());
83  
84          handler.clearMarker();
85          assertFalse(handler.isMarkerSet());
86          handler.clearMarker();
87          assertFalse(handler.isMarkerSet());
88      }
89  
90      public void testMarkerFile() throws MojoExecutionException, IOException {
91          DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifacts.get(0), this.outputFolder);
92  
93          File handle = handler.getMarkerFile();
94          assertFalse(handle.exists());
95          assertFalse(handler.isMarkerSet());
96  
97          handler.setMarker();
98          assertTrue(handler.isMarkerSet());
99          assertTrue(handle.exists());
100 
101         handle.delete();
102         assertFalse(handler.isMarkerSet());
103 
104         handle.createNewFile();
105         assertTrue(handler.isMarkerSet());
106 
107         handler.clearMarker();
108         assertFalse(handle.exists());
109     }
110 
111     public void testMarkerTimeStamp() throws MojoExecutionException, IOException, InterruptedException {
112         File theFile = new File(outputFolder, "theFile.jar");
113         outputFolder.mkdirs();
114         theFile.createNewFile();
115         Artifact theArtifact = artifacts.get(0);
116         theArtifact.setFile(theFile);
117         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(theArtifact, this.outputFolder);
118         assertFalse(handler.isMarkerSet());
119         // if the marker is not set, assume it is infinately older than the
120         // artifact.
121         assertTrue(handler.isMarkerOlder(theArtifact));
122         handler.setMarker();
123         assertFalse(handler.isMarkerOlder(theArtifact));
124 
125         assertTrue(theFile.setLastModified(theFile.lastModified() + 60000));
126         assertTrue(handler.isMarkerOlder(theArtifact));
127 
128         theFile.delete();
129         handler.clearMarker();
130         assertFalse(handler.isMarkerSet());
131     }
132 
133     public void testMarkerFileException() {
134         // this stub wraps the file with an object to throw exceptions
135         StubDefaultFileMarkerHandler handler = new StubDefaultFileMarkerHandler(artifacts.get(0), this.outputFolder);
136         try {
137             handler.setMarker();
138             fail("Expected an Exception here");
139         } catch (MojoExecutionException e) {
140 
141         }
142     }
143 
144     public void testGetterSetter() {
145         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(null, null);
146         assertNull(handler.getArtifact());
147         handler.setArtifact(artifacts.get(0));
148         assertSame(artifacts.get(0), handler.getArtifact());
149 
150         assertNull(handler.getMarkerFilesDirectory());
151         handler.setMarkerFilesDirectory(outputFolder);
152         assertSame(outputFolder, handler.getMarkerFilesDirectory());
153     }
154 
155     public void testNullParent() throws MojoExecutionException {
156         // the parent isn't set so this will create the marker in the local
157         // folder. We must clear the
158         // marker to avoid leaving test droppings in root.
159         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(null, null);
160         handler.setArtifact(artifacts.get(0));
161         handler.setMarker();
162         assertTrue(handler.isMarkerSet());
163         handler.clearMarker();
164         assertFalse(handler.isMarkerSet());
165     }
166 }