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.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.DefaultArtifact;
27  import org.apache.maven.artifact.handler.ArtifactHandler;
28  import org.apache.maven.artifact.handler.DefaultArtifactHandler;
29  import org.apache.maven.artifact.versioning.VersionRange;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.plugins.dependency.testUtils.stubs.StubDefaultFileMarkerHandler;
32  import org.junit.jupiter.api.BeforeEach;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  
36  import static org.junit.jupiter.api.Assertions.assertFalse;
37  import static org.junit.jupiter.api.Assertions.assertNull;
38  import static org.junit.jupiter.api.Assertions.assertSame;
39  import static org.junit.jupiter.api.Assertions.assertTrue;
40  import static org.junit.jupiter.api.Assertions.fail;
41  
42  /**
43   * @author brianf
44   */
45  class TestDefaultMarkerFileHandler {
46      List<Artifact> artifacts = new ArrayList<>();
47  
48      @TempDir
49      File outputFolder;
50  
51      @BeforeEach
52      void setUp() {
53          ArtifactHandler ah = new DefaultArtifactHandler();
54          VersionRange vr = VersionRange.createFromVersion("1.1");
55          Artifact artifact = new DefaultArtifact("test", "1", vr, Artifact.SCOPE_COMPILE, "jar", "", ah, false);
56          artifacts.add(artifact);
57          artifact = new DefaultArtifact("test", "2", vr, Artifact.SCOPE_PROVIDED, "war", "", ah, false);
58          artifacts.add(artifact);
59          artifact = new DefaultArtifact("test", "3", vr, Artifact.SCOPE_TEST, "sources", "", ah, false);
60          artifacts.add(artifact);
61          artifact = new DefaultArtifact("test", "4", vr, Artifact.SCOPE_RUNTIME, "zip", "", ah, false);
62          artifacts.add(artifact);
63      }
64  
65      @Test
66      void setMarker() throws Exception {
67          DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifacts.get(0), this.outputFolder);
68          assertFalse(handler.isMarkerSet());
69          handler.setMarker();
70          assertTrue(handler.isMarkerSet());
71          handler.clearMarker();
72          assertFalse(handler.isMarkerSet());
73  
74          handler.setMarker();
75          assertTrue(handler.isMarkerSet());
76          handler.setMarker();
77          assertTrue(handler.isMarkerSet());
78  
79          handler.clearMarker();
80          assertFalse(handler.isMarkerSet());
81          handler.clearMarker();
82          assertFalse(handler.isMarkerSet());
83      }
84  
85      @Test
86      void markerFile() throws Exception {
87          DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(artifacts.get(0), this.outputFolder);
88  
89          File handle = handler.getMarkerFile();
90          assertFalse(handle.exists());
91          assertFalse(handler.isMarkerSet());
92  
93          handler.setMarker();
94          assertTrue(handler.isMarkerSet());
95          assertTrue(handle.exists());
96  
97          handle.delete();
98          assertFalse(handler.isMarkerSet());
99  
100         handle.createNewFile();
101         assertTrue(handler.isMarkerSet());
102 
103         handler.clearMarker();
104         assertFalse(handle.exists());
105     }
106 
107     @Test
108     void markerTimeStamp() throws Exception {
109         File theFile = new File(outputFolder, "theFile.jar");
110         outputFolder.mkdirs();
111         theFile.createNewFile();
112         Artifact theArtifact = artifacts.get(0);
113         theArtifact.setFile(theFile);
114         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(theArtifact, this.outputFolder);
115         assertFalse(handler.isMarkerSet());
116         // if the marker is not set, assume it is infinately older than the
117         // artifact.
118         assertTrue(handler.isMarkerOlder(theArtifact));
119         handler.setMarker();
120         assertFalse(handler.isMarkerOlder(theArtifact));
121 
122         assertTrue(theFile.setLastModified(theFile.lastModified() + 60000));
123         assertTrue(handler.isMarkerOlder(theArtifact));
124 
125         theFile.delete();
126         handler.clearMarker();
127         assertFalse(handler.isMarkerSet());
128     }
129 
130     @Test
131     void markerFileException() {
132         // this stub wraps the file with an object to throw exceptions
133         StubDefaultFileMarkerHandler handler = new StubDefaultFileMarkerHandler(artifacts.get(0), this.outputFolder);
134         try {
135             handler.setMarker();
136             fail("Expected an Exception here");
137         } catch (MojoExecutionException e) {
138 
139         }
140     }
141 
142     @Test
143     void getterSetter() {
144         DefaultFileMarkerHandler handler = new DefaultFileMarkerHandler(null, null);
145         assertNull(handler.getArtifact());
146         handler.setArtifact(artifacts.get(0));
147         assertSame(artifacts.get(0), handler.getArtifact());
148 
149         assertNull(handler.getMarkerFilesDirectory());
150         handler.setMarkerFilesDirectory(outputFolder);
151         assertSame(outputFolder, handler.getMarkerFilesDirectory());
152     }
153 
154     @Test
155     void nullParent() throws Exception {
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 }