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.eclipse.aether.internal.impl;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.file.Path;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import org.eclipse.aether.DefaultRepositorySystemSession;
31  import org.eclipse.aether.RepositoryEvent;
32  import org.eclipse.aether.RepositoryEvent.EventType;
33  import org.eclipse.aether.RepositoryException;
34  import org.eclipse.aether.artifact.Artifact;
35  import org.eclipse.aether.artifact.DefaultArtifact;
36  import org.eclipse.aether.deployment.DeployRequest;
37  import org.eclipse.aether.deployment.DeploymentException;
38  import org.eclipse.aether.internal.test.util.TestFileUtils;
39  import org.eclipse.aether.internal.test.util.TestUtils;
40  import org.eclipse.aether.metadata.DefaultMetadata;
41  import org.eclipse.aether.metadata.MergeableMetadata;
42  import org.eclipse.aether.metadata.Metadata;
43  import org.eclipse.aether.metadata.Metadata.Nature;
44  import org.eclipse.aether.repository.RemoteRepository;
45  import org.eclipse.aether.spi.connector.ArtifactDownload;
46  import org.eclipse.aether.spi.connector.ArtifactUpload;
47  import org.eclipse.aether.spi.connector.MetadataDownload;
48  import org.eclipse.aether.spi.connector.MetadataUpload;
49  import org.eclipse.aether.spi.connector.RepositoryConnector;
50  import org.eclipse.aether.spi.io.PathProcessorSupport;
51  import org.eclipse.aether.transfer.MetadataNotFoundException;
52  import org.junit.jupiter.api.AfterEach;
53  import org.junit.jupiter.api.BeforeEach;
54  import org.junit.jupiter.api.Test;
55  
56  import static java.util.Objects.requireNonNull;
57  import static org.junit.jupiter.api.Assertions.*;
58  
59  public class DefaultDeployerTest {
60  
61      private Artifact artifact;
62  
63      private DefaultMetadata metadata;
64  
65      private DefaultRepositorySystemSession session;
66  
67      private StubRepositoryConnectorProvider connectorProvider;
68  
69      private DefaultDeployer deployer;
70  
71      private DeployRequest request;
72  
73      private RecordingRepositoryConnector connector;
74  
75      private RecordingRepositoryListener listener;
76  
77      @BeforeEach
78      void setup() throws IOException {
79          artifact = new DefaultArtifact("gid", "aid", "jar", "ver");
80          artifact = artifact.setFile(TestFileUtils.createTempFile("artifact"));
81          metadata = new DefaultMetadata(
82                  "gid", "aid", "ver", "type", Nature.RELEASE_OR_SNAPSHOT, TestFileUtils.createTempFile("metadata"));
83  
84          session = TestUtils.newSession();
85          connectorProvider = new StubRepositoryConnectorProvider();
86  
87          deployer = new DefaultDeployer(
88                  new PathProcessorSupport(),
89                  new StubRepositoryEventDispatcher(),
90                  connectorProvider,
91                  new StubRemoteRepositoryManager(),
92                  new StaticUpdateCheckManager(true),
93                  Collections.emptyMap(),
94                  Collections.emptyMap(),
95                  Collections.emptyMap(),
96                  new StubSyncContextFactory(),
97                  new DefaultOfflineController());
98  
99          request = new DeployRequest();
100         request.setRepository(new RemoteRepository.Builder("id", "default", "file:///").build());
101         connector = new RecordingRepositoryConnector(session);
102         connectorProvider.setConnector(connector);
103 
104         listener = new RecordingRepositoryListener();
105         session.setRepositoryListener(listener);
106     }
107 
108     @AfterEach
109     void teardown() throws Exception {
110         if (session.getLocalRepository() != null) {
111             TestFileUtils.deleteFile(session.getLocalRepository().getBasedir());
112         }
113         session = null;
114         listener = null;
115         connector = null;
116         connectorProvider = null;
117         deployer = null;
118     }
119 
120     @Test
121     void testSuccessfulDeploy() throws DeploymentException {
122 
123         connector.setExpectPut(artifact);
124         connector.setExpectPut(metadata);
125 
126         request.addArtifact(artifact);
127         request.addMetadata(metadata);
128 
129         deployer.deploy(session, request);
130 
131         connector.assertSeenExpected();
132     }
133 
134     @Test
135     void testNullArtifactFile() {
136         request.addArtifact(artifact.setFile(null));
137         assertThrows(DeploymentException.class, () -> deployer.deploy(session, request));
138     }
139 
140     @Test
141     void testNullMetadataFile() {
142         request.addMetadata(metadata.setFile(null));
143         assertThrows(DeploymentException.class, () -> deployer.deploy(session, request));
144     }
145 
146     @Test
147     void testSuccessfulArtifactEvents() throws DeploymentException {
148         request.addArtifact(artifact);
149 
150         deployer.deploy(session, request);
151 
152         List<RepositoryEvent> events = listener.getEvents();
153         assertEquals(2, events.size());
154 
155         RepositoryEvent event = events.get(0);
156         assertEquals(EventType.ARTIFACT_DEPLOYING, event.getType());
157         assertEquals(artifact, event.getArtifact());
158         assertNull(event.getException());
159 
160         event = events.get(1);
161         assertEquals(EventType.ARTIFACT_DEPLOYED, event.getType());
162         assertEquals(artifact, event.getArtifact());
163         assertNull(event.getException());
164     }
165 
166     @Test
167     void testFailingArtifactEvents() {
168         connector.fail = true;
169 
170         request.addArtifact(artifact);
171 
172         try {
173             deployer.deploy(session, request);
174             fail("expected exception");
175         } catch (DeploymentException e) {
176             List<RepositoryEvent> events = listener.getEvents();
177             assertEquals(2, events.size());
178 
179             RepositoryEvent event = events.get(0);
180             assertEquals(EventType.ARTIFACT_DEPLOYING, event.getType());
181             assertEquals(artifact, event.getArtifact());
182             assertNull(event.getException());
183 
184             event = events.get(1);
185             assertEquals(EventType.ARTIFACT_DEPLOYED, event.getType());
186             assertEquals(artifact, event.getArtifact());
187             assertNotNull(event.getException());
188         }
189     }
190 
191     @Test
192     void testSuccessfulMetadataEvents() throws DeploymentException {
193         request.addMetadata(metadata);
194 
195         deployer.deploy(session, request);
196 
197         List<RepositoryEvent> events = listener.getEvents();
198         assertEquals(2, events.size());
199 
200         RepositoryEvent event = events.get(0);
201         assertEquals(EventType.METADATA_DEPLOYING, event.getType());
202         assertEquals(metadata, event.getMetadata());
203         assertNull(event.getException());
204 
205         event = events.get(1);
206         assertEquals(EventType.METADATA_DEPLOYED, event.getType());
207         assertEquals(metadata, event.getMetadata());
208         assertNull(event.getException());
209     }
210 
211     @Test
212     void testFailingMetdataEvents() {
213         connector.fail = true;
214 
215         request.addMetadata(metadata);
216 
217         try {
218             deployer.deploy(session, request);
219             fail("expected exception");
220         } catch (DeploymentException e) {
221             List<RepositoryEvent> events = listener.getEvents();
222             assertEquals(2, events.size());
223 
224             RepositoryEvent event = events.get(0);
225             assertEquals(EventType.METADATA_DEPLOYING, event.getType());
226             assertEquals(metadata, event.getMetadata());
227             assertNull(event.getException());
228 
229             event = events.get(1);
230             assertEquals(EventType.METADATA_DEPLOYED, event.getType());
231             assertEquals(metadata, event.getMetadata());
232             assertNotNull(event.getException());
233         }
234     }
235 
236     @Test
237     void testStaleLocalMetadataCopyGetsDeletedBeforeMergeWhenMetadataIsNotCurrentlyPresentInRemoteRepo()
238             throws Exception {
239         MergeableMetadata metadata = new MergeableMetadata() {
240 
241             public Metadata setFile(File file) {
242                 return this;
243             }
244 
245             public Metadata setPath(Path path) {
246                 return this;
247             }
248 
249             public String getVersion() {
250                 return "";
251             }
252 
253             public String getType() {
254                 return "test.properties";
255             }
256 
257             public Nature getNature() {
258                 return Nature.RELEASE;
259             }
260 
261             public String getGroupId() {
262                 return "org";
263             }
264 
265             public File getFile() {
266                 return null;
267             }
268 
269             public Path getPath() {
270                 return null;
271             }
272 
273             public String getArtifactId() {
274                 return "aether";
275             }
276 
277             public Metadata setProperties(Map<String, String> properties) {
278                 return this;
279             }
280 
281             public Map<String, String> getProperties() {
282                 return Collections.emptyMap();
283             }
284 
285             public String getProperty(String key, String defaultValue) {
286                 return defaultValue;
287             }
288 
289             public void merge(Path current, Path result) throws RepositoryException {
290                 merge(current != null ? current.toFile() : null, result != null ? result.toFile() : null);
291             }
292 
293             public void merge(File current, File result) throws RepositoryException {
294                 requireNonNull(current, "current cannot be null");
295                 requireNonNull(result, "result cannot be null");
296                 Properties props = new Properties();
297 
298                 try {
299                     if (current.isFile()) {
300                         TestFileUtils.readProps(current, props);
301                     }
302 
303                     props.setProperty("new", "value");
304 
305                     TestFileUtils.writeProps(result, props);
306                 } catch (IOException e) {
307                     throw new RepositoryException(e.getMessage(), e);
308                 }
309             }
310 
311             public boolean isMerged() {
312                 return false;
313             }
314         };
315 
316         connectorProvider.setConnector(new RepositoryConnector() {
317 
318             public void put(
319                     Collection<? extends ArtifactUpload> artifactUploads,
320                     Collection<? extends MetadataUpload> metadataUploads) {}
321 
322             public void get(
323                     Collection<? extends ArtifactDownload> artifactDownloads,
324                     Collection<? extends MetadataDownload> metadataDownloads) {
325                 if (metadataDownloads != null) {
326                     for (MetadataDownload download : metadataDownloads) {
327                         download.setException(new MetadataNotFoundException(download.getMetadata(), null, null));
328                     }
329                 }
330             }
331 
332             public void close() {}
333         });
334 
335         request.addMetadata(metadata);
336 
337         File metadataFile = new File(
338                 session.getLocalRepository().getBasedir(),
339                 session.getLocalRepositoryManager().getPathForRemoteMetadata(metadata, request.getRepository(), ""));
340         Properties props = new Properties();
341         props.setProperty("old", "value");
342         TestFileUtils.writeProps(metadataFile, props);
343 
344         deployer.deploy(session, request);
345 
346         props = new Properties();
347         TestFileUtils.readProps(metadataFile, props);
348         assertNull(props.get("old"), props.toString());
349     }
350 }