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 org.apache.commons.io.FileUtils;
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31  import org.apache.maven.plugin.testing.SilentLog;
32  import org.apache.maven.plugins.dependency.fromConfiguration.ArtifactItem;
33  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
34  import org.apache.maven.plugins.dependency.testUtils.stubs.StubUnpackFileMarkerHandler;
35  
36  public class TestUnpackMarkerFileHandler extends AbstractMojoTestCase {
37      List<ArtifactItem> artifactItems = new ArrayList<>();
38  
39      Log log = new SilentLog();
40  
41      File outputFolder;
42  
43      protected File testDir;
44  
45      protected DependencyArtifactStubFactory stubFactory;
46  
47      protected void setUp() throws Exception {
48          super.setUp();
49  
50          testDir = new File(
51                  getBasedir(),
52                  "target" + File.separatorChar + "unit-tests" + File.separatorChar + "unpack-markers"
53                          + File.separatorChar);
54          FileUtils.deleteDirectory(testDir);
55          assertFalse(testDir.exists());
56  
57          stubFactory = new DependencyArtifactStubFactory(this.testDir, false);
58          Artifact artifact = stubFactory.createArtifact("test", "test", "1");
59          ArtifactItem artifactItem;
60          stubFactory.getArtifactItem(artifact);
61          artifactItems.add(stubFactory.getArtifactItem(stubFactory.createArtifact("test", "test", "1")));
62          artifact = stubFactory.createArtifact("test2", "test2", "2");
63          artifactItem = new ArtifactItem(artifact);
64          artifactItem.setIncludes("**/*.xml");
65          artifactItems.add(artifactItem);
66          artifact = stubFactory.createArtifact("test3", "test3", "3");
67          artifactItem = new ArtifactItem(artifact);
68          artifactItem.setExcludes("**/*.class");
69          artifactItems.add(artifactItem);
70          artifact = stubFactory.createArtifact("test4", "test4", "4");
71          artifactItem = new ArtifactItem(artifact);
72          artifactItem.setIncludes("**/*.xml");
73          artifactItem.setExcludes("**/*.class");
74          artifactItems.add(artifactItem);
75  
76          outputFolder = new File("target/markers/");
77          FileUtils.deleteDirectory(this.outputFolder);
78          assertFalse(outputFolder.exists());
79      }
80  
81      protected void tearDown() throws IOException {
82          FileUtils.deleteDirectory(this.outputFolder);
83      }
84  
85      /**
86       * Assert that default functionality still exists
87       *
88       * @throws MojoExecutionException in case of an error.
89       */
90      public void testSetMarker() throws MojoExecutionException {
91          UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(0), this.outputFolder);
92          assertFalse(handler.isMarkerSet());
93          handler.setMarker();
94          assertTrue(handler.isMarkerSet());
95          handler.clearMarker();
96          assertFalse(handler.isMarkerSet());
97  
98          handler.setMarker();
99          assertTrue(handler.isMarkerSet());
100         handler.setMarker();
101         assertTrue(handler.isMarkerSet());
102 
103         handler.clearMarker();
104         assertFalse(handler.isMarkerSet());
105         handler.clearMarker();
106         assertFalse(handler.isMarkerSet());
107     }
108 
109     public void testMarkerFile() throws MojoExecutionException, IOException {
110         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(0), this.outputFolder);
111 
112         File handle = handler.getMarkerFile();
113         assertFalse(handle.exists());
114         assertFalse(handler.isMarkerSet());
115 
116         handler.setMarker();
117         assertTrue(handler.isMarkerSet());
118         assertTrue(handle.exists());
119 
120         handle.delete();
121         assertFalse(handler.isMarkerSet());
122 
123         handle.createNewFile();
124         assertTrue(handler.isMarkerSet());
125 
126         handler.clearMarker();
127         assertFalse(handle.exists());
128     }
129 
130     public void testMarkerTimeStamp() throws MojoExecutionException, IOException, InterruptedException {
131         File theFile = new File(outputFolder, "theFile.jar");
132         outputFolder.mkdirs();
133         theFile.createNewFile();
134         ArtifactItem theArtifactItem = artifactItems.get(0);
135         Artifact theArtifact = theArtifactItem.getArtifact();
136         theArtifact.setFile(theFile);
137         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(theArtifactItem, 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         assertTrue(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         // this stub wraps the file with an object to throw exceptions
155         StubUnpackFileMarkerHandler handler = new StubUnpackFileMarkerHandler(artifactItems.get(0), this.outputFolder);
156         try {
157             handler.setMarker();
158             fail("Expected an Exception here");
159         } catch (MojoExecutionException e) {
160 
161         }
162     }
163 
164     public void testGetterSetter() {
165         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(null, null);
166         assertNull(handler.getArtifactItem());
167         assertNull(handler.getArtifact());
168         handler.setArtifactItem(artifactItems.get(0));
169         assertSame(artifactItems.get(0), handler.getArtifactItem());
170         assertSame(artifactItems.get(0).getArtifact(), handler.getArtifact());
171 
172         assertNull(handler.getMarkerFilesDirectory());
173         handler.setMarkerFilesDirectory(outputFolder);
174         assertSame(outputFolder, handler.getMarkerFilesDirectory());
175     }
176 
177     public void testNullParent() throws MojoExecutionException {
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         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(null, null);
182         handler.setArtifactItem(artifactItems.get(0));
183         handler.setMarker();
184         assertTrue(handler.isMarkerSet());
185         handler.clearMarker();
186         assertFalse(handler.isMarkerSet());
187     }
188 
189     public void testIncludesMarker() throws MojoExecutionException, IOException {
190         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(1), outputFolder);
191         File handle = handler.getMarkerFile();
192         assertFalse(handle.exists());
193         assertFalse(handler.isMarkerSet());
194 
195         handler.setMarker();
196         assertTrue(handler.isMarkerSet());
197         assertTrue(handle.exists());
198         String hashCode = "" + ("**/*.xml".hashCode());
199         assertTrue(handle.getName().contains(hashCode));
200 
201         handle.delete();
202         assertFalse(handler.isMarkerSet());
203 
204         handle.createNewFile();
205         assertTrue(handler.isMarkerSet());
206 
207         handler.clearMarker();
208         assertFalse(handle.exists());
209     }
210 
211     public void testExcludesMarker() throws MojoExecutionException, IOException {
212         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(2), outputFolder);
213         File handle = handler.getMarkerFile();
214         assertFalse(handle.exists());
215         assertFalse(handler.isMarkerSet());
216 
217         handler.setMarker();
218         assertTrue(handler.isMarkerSet());
219         assertTrue(handle.exists());
220         String hashCode = "" + ("**/*.class".hashCode());
221         assertTrue(handle.getName().contains(hashCode));
222 
223         handle.delete();
224         assertFalse(handler.isMarkerSet());
225 
226         handle.createNewFile();
227         assertTrue(handler.isMarkerSet());
228 
229         handler.clearMarker();
230         assertFalse(handle.exists());
231     }
232 
233     public void testIncludesExcludesMarker() throws MojoExecutionException, IOException {
234         UnpackFileMarkerHandler handler = new UnpackFileMarkerHandler(artifactItems.get(3), outputFolder);
235         File handle = handler.getMarkerFile();
236         assertFalse(handle.exists());
237         assertFalse(handler.isMarkerSet());
238 
239         handler.setMarker();
240         assertTrue(handler.isMarkerSet());
241         assertTrue(handle.exists());
242         String hashCode = "" + (0 + "**/*.class".hashCode() + "**/*.xml".hashCode());
243         assertTrue(handle.getName().contains(hashCode));
244 
245         handle.delete();
246         assertFalse(handler.isMarkerSet());
247 
248         handle.createNewFile();
249         assertTrue(handler.isMarkerSet());
250 
251         handler.clearMarker();
252         assertFalse(handle.exists());
253     }
254 }