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.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.internal.test.util.TestFileUtils;
28  import org.eclipse.aether.metadata.Metadata;
29  import org.eclipse.aether.spi.connector.ArtifactDownload;
30  import org.eclipse.aether.spi.connector.ArtifactUpload;
31  import org.eclipse.aether.spi.connector.MetadataDownload;
32  import org.eclipse.aether.spi.connector.MetadataUpload;
33  import org.eclipse.aether.spi.connector.RepositoryConnector;
34  import org.eclipse.aether.spi.connector.Transfer;
35  import org.eclipse.aether.transfer.ArtifactTransferException;
36  import org.eclipse.aether.transfer.MetadataTransferException;
37  import org.eclipse.aether.transfer.TransferEvent;
38  import org.eclipse.aether.transfer.TransferListener;
39  import org.eclipse.aether.transfer.TransferResource;
40  
41  import static org.junit.Assert.*;
42  
43  /**
44   * A repository connector recording all get/put-requests and faking the results.
45   */
46  class RecordingRepositoryConnector implements RepositoryConnector {
47  
48      RepositorySystemSession session;
49  
50      boolean fail;
51  
52      private Artifact[] expectGet;
53  
54      private Artifact[] expectPut;
55  
56      private Metadata[] expectGetMD;
57  
58      private Metadata[] expectPutMD;
59  
60      private List<Artifact> actualGet = new ArrayList<>();
61  
62      private List<Metadata> actualGetMD = new ArrayList<>();
63  
64      private List<Artifact> actualPut = new ArrayList<>();
65  
66      private List<Metadata> actualPutMD = new ArrayList<>();
67  
68      public RecordingRepositoryConnector(
69              RepositorySystemSession session,
70              Artifact[] expectGet,
71              Artifact[] expectPut,
72              Metadata[] expectGetMD,
73              Metadata[] expectPutMD) {
74          this.session = session;
75          this.expectGet = expectGet;
76          this.expectPut = expectPut;
77          this.expectGetMD = expectGetMD;
78          this.expectPutMD = expectPutMD;
79      }
80  
81      public RecordingRepositoryConnector(RepositorySystemSession session) {
82          this.session = session;
83      }
84  
85      public RecordingRepositoryConnector() {}
86  
87      public void get(
88              Collection<? extends ArtifactDownload> artifactDownloads,
89              Collection<? extends MetadataDownload> metadataDownloads) {
90          try {
91              if (artifactDownloads != null) {
92                  for (ArtifactDownload download : artifactDownloads) {
93                      fireInitiated(download);
94                      Artifact artifact = download.getArtifact();
95                      this.actualGet.add(artifact);
96                      if (fail) {
97                          download.setException(new ArtifactTransferException(artifact, null, "forced failure"));
98                      } else {
99                          TestFileUtils.writeString(download.getFile(), artifact.toString());
100                     }
101                     fireDone(download);
102                 }
103             }
104             if (metadataDownloads != null) {
105                 for (MetadataDownload download : metadataDownloads) {
106                     fireInitiated(download);
107                     Metadata metadata = download.getMetadata();
108                     this.actualGetMD.add(metadata);
109                     if (fail) {
110                         download.setException(new MetadataTransferException(metadata, null, "forced failure"));
111                     } else {
112                         TestFileUtils.writeString(download.getFile(), metadata.toString());
113                     }
114                     fireDone(download);
115                 }
116             }
117         } catch (Exception e) {
118             throw new IllegalStateException(e);
119         }
120     }
121 
122     public void put(
123             Collection<? extends ArtifactUpload> artifactUploads,
124             Collection<? extends MetadataUpload> metadataUploads) {
125         try {
126             if (artifactUploads != null) {
127                 for (ArtifactUpload upload : artifactUploads) {
128                     // mimic "real" connector
129                     fireInitiated(upload);
130                     if (upload.getFile() == null) {
131                         upload.setException(new ArtifactTransferException(upload.getArtifact(), null, "no file"));
132                     } else if (fail) {
133                         upload.setException(
134                                 new ArtifactTransferException(upload.getArtifact(), null, "forced failure"));
135                     }
136                     this.actualPut.add(upload.getArtifact());
137                     fireDone(upload);
138                 }
139             }
140             if (metadataUploads != null) {
141                 for (MetadataUpload upload : metadataUploads) {
142                     // mimic "real" connector
143                     fireInitiated(upload);
144                     if (upload.getFile() == null) {
145                         upload.setException(new MetadataTransferException(upload.getMetadata(), null, "no file"));
146                     } else if (fail) {
147                         upload.setException(
148                                 new MetadataTransferException(upload.getMetadata(), null, "forced failure"));
149                     }
150                     this.actualPutMD.add(upload.getMetadata());
151                     fireDone(upload);
152                 }
153             }
154         } catch (Exception e) {
155             throw new IllegalStateException(e);
156         }
157     }
158 
159     private void fireInitiated(Transfer transfer) throws Exception {
160         TransferListener listener = transfer.getListener();
161         if (listener == null) {
162             return;
163         }
164         TransferEvent.Builder event =
165                 new TransferEvent.Builder(session, new TransferResource(null, null, null, null, transfer.getTrace()));
166         event.setType(TransferEvent.EventType.INITIATED);
167         listener.transferInitiated(event.build());
168     }
169 
170     private void fireDone(Transfer transfer) {
171         TransferListener listener = transfer.getListener();
172         if (listener == null) {
173             return;
174         }
175         TransferEvent.Builder event =
176                 new TransferEvent.Builder(session, new TransferResource(null, null, null, null, transfer.getTrace()));
177         event.setException(transfer.getException());
178         if (transfer.getException() != null) {
179             listener.transferFailed(
180                     event.setType(TransferEvent.EventType.FAILED).build());
181         } else {
182             listener.transferSucceeded(
183                     event.setType(TransferEvent.EventType.SUCCEEDED).build());
184         }
185     }
186 
187     public void close() {}
188 
189     public void assertSeenExpected() {
190         assertSeenExpected(actualGet, expectGet);
191         assertSeenExpected(actualGetMD, expectGetMD);
192         assertSeenExpected(actualPut, expectPut);
193         assertSeenExpected(actualPutMD, expectPutMD);
194     }
195 
196     private void assertSeenExpected(List<?> actual, Object[] expected) {
197         if (expected == null) {
198             expected = new Object[0];
199         }
200 
201         assertEquals("different number of expected and actual elements:\n", expected.length, actual.size());
202         int idx = 0;
203         for (Object actualObject : actual) {
204             assertEquals("seen object differs", expected[idx++], actualObject);
205         }
206     }
207 
208     public List<Artifact> getActualArtifactGetRequests() {
209         return actualGet;
210     }
211 
212     public List<Metadata> getActualMetadataGetRequests() {
213         return actualGetMD;
214     }
215 
216     public List<Artifact> getActualArtifactPutRequests() {
217         return actualPut;
218     }
219 
220     public List<Metadata> getActualMetadataPutRequests() {
221         return actualPutMD;
222     }
223 
224     public void setExpectGet(Artifact... expectGet) {
225         this.expectGet = expectGet;
226     }
227 
228     public void setExpectPut(Artifact... expectPut) {
229         this.expectPut = expectPut;
230     }
231 
232     public void setExpectGet(Metadata... expectGetMD) {
233         this.expectGetMD = expectGetMD;
234     }
235 
236     public void setExpectPut(Metadata... expectPutMD) {
237         this.expectPutMD = expectPutMD;
238     }
239 
240     public void resetActual() {
241         this.actualGet = new ArrayList<>();
242         this.actualGetMD = new ArrayList<>();
243         this.actualPut = new ArrayList<>();
244         this.actualPutMD = new ArrayList<>();
245     }
246 }