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