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