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