View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.*;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.List;
27  
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.internal.test.util.TestFileUtils;
31  import org.eclipse.aether.metadata.Metadata;
32  import org.eclipse.aether.spi.connector.ArtifactDownload;
33  import org.eclipse.aether.spi.connector.ArtifactUpload;
34  import org.eclipse.aether.spi.connector.MetadataDownload;
35  import org.eclipse.aether.spi.connector.MetadataUpload;
36  import org.eclipse.aether.spi.connector.RepositoryConnector;
37  import org.eclipse.aether.spi.connector.Transfer;
38  import org.eclipse.aether.transfer.ArtifactTransferException;
39  import org.eclipse.aether.transfer.MetadataTransferException;
40  import org.eclipse.aether.transfer.TransferEvent;
41  import org.eclipse.aether.transfer.TransferListener;
42  import org.eclipse.aether.transfer.TransferResource;
43  
44  /**
45   * A repository connector recording all get/put-requests and faking the results.
46   */
47  class RecordingRepositoryConnector
48      implements RepositoryConnector
49  {
50  
51      RepositorySystemSession session;
52  
53      boolean fail;
54  
55      private Artifact[] expectGet;
56  
57      private Artifact[] expectPut;
58  
59      private Metadata[] expectGetMD;
60  
61      private Metadata[] expectPutMD;
62  
63      private List<Artifact> actualGet = new ArrayList<>();
64  
65      private List<Metadata> actualGetMD = new ArrayList<>();
66  
67      private List<Artifact> actualPut = new ArrayList<>();
68  
69      private List<Metadata> actualPutMD = new ArrayList<>();
70  
71      public RecordingRepositoryConnector( RepositorySystemSession session, Artifact[] expectGet, Artifact[] expectPut,
72                                           Metadata[] expectGetMD, Metadata[] expectPutMD )
73      {
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      {
83          this.session = session;
84      }
85  
86      public RecordingRepositoryConnector()
87      {
88      }
89  
90      public void get( Collection<? extends ArtifactDownload> artifactDownloads,
91                       Collection<? extends MetadataDownload> metadataDownloads )
92      {
93          try
94          {
95              if ( artifactDownloads != null )
96              {
97                  for ( ArtifactDownload download : artifactDownloads )
98                  {
99                      fireInitiated( download );
100                     Artifact artifact = download.getArtifact();
101                     this.actualGet.add( artifact );
102                     if ( fail )
103                     {
104                         download.setException( new ArtifactTransferException( artifact, null, "forced failure" ) );
105                     }
106                     else
107                     {
108                         TestFileUtils.writeString( download.getFile(), artifact.toString() );
109                     }
110                     fireDone( download );
111                 }
112             }
113             if ( metadataDownloads != null )
114             {
115                 for ( MetadataDownload download : metadataDownloads )
116                 {
117                     fireInitiated( download );
118                     Metadata metadata = download.getMetadata();
119                     this.actualGetMD.add( metadata );
120                     if ( fail )
121                     {
122                         download.setException( new MetadataTransferException( metadata, null, "forced failure" ) );
123                     }
124                     else
125                     {
126                         TestFileUtils.writeString( download.getFile(), metadata.toString() );
127                     }
128                     fireDone( download );
129                 }
130             }
131         }
132         catch ( Exception e )
133         {
134             throw new IllegalStateException( e );
135         }
136     }
137 
138     public void put( Collection<? extends ArtifactUpload> artifactUploads,
139                      Collection<? extends MetadataUpload> metadataUploads )
140     {
141         try
142         {
143             if ( artifactUploads != null )
144             {
145                 for ( ArtifactUpload upload : artifactUploads )
146                 {
147                     // mimic "real" connector
148                     fireInitiated( upload );
149                     if ( upload.getFile() == null )
150                     {
151                         upload.setException( new ArtifactTransferException( upload.getArtifact(), null, "no file" ) );
152                     }
153                     else if ( fail )
154                     {
155                         upload.setException( new ArtifactTransferException( upload.getArtifact(), null,
156                                                                             "forced failure" ) );
157                     }
158                     this.actualPut.add( upload.getArtifact() );
159                     fireDone( upload );
160                 }
161             }
162             if ( metadataUploads != null )
163             {
164                 for ( MetadataUpload upload : metadataUploads )
165                 {
166                     // mimic "real" connector
167                     fireInitiated( upload );
168                     if ( upload.getFile() == null )
169                     {
170                         upload.setException( new MetadataTransferException( upload.getMetadata(), null, "no file" ) );
171                     }
172                     else if ( fail )
173                     {
174                         upload.setException( new MetadataTransferException( upload.getMetadata(), null,
175                                                                             "forced failure" ) );
176                     }
177                     this.actualPutMD.add( upload.getMetadata() );
178                     fireDone( upload );
179                 }
180             }
181         }
182         catch ( Exception e )
183         {
184             throw new IllegalStateException( e );
185         }
186     }
187 
188     private void fireInitiated( Transfer transfer )
189         throws Exception
190     {
191         TransferListener listener = transfer.getListener();
192         if ( listener == null )
193         {
194             return;
195         }
196         TransferEvent.Builder event =
197             new TransferEvent.Builder( session, new TransferResource( null, null, null, null, transfer.getTrace() ) );
198         event.setType( TransferEvent.EventType.INITIATED );
199         listener.transferInitiated( event.build() );
200     }
201 
202     private void fireDone( Transfer transfer )
203     {
204         TransferListener listener = transfer.getListener();
205         if ( listener == null )
206         {
207             return;
208         }
209         TransferEvent.Builder event =
210             new TransferEvent.Builder( session, new TransferResource( null, null, null, null, transfer.getTrace() ) );
211         event.setException( transfer.getException() );
212         if ( transfer.getException() != null )
213         {
214             listener.transferFailed( event.setType( TransferEvent.EventType.FAILED ).build() );
215         }
216         else
217         {
218             listener.transferSucceeded( event.setType( TransferEvent.EventType.SUCCEEDED ).build() );
219         }
220     }
221 
222     public void close()
223     {
224     }
225 
226     public void assertSeenExpected()
227     {
228         assertSeenExpected( actualGet, expectGet );
229         assertSeenExpected( actualGetMD, expectGetMD );
230         assertSeenExpected( actualPut, expectPut );
231         assertSeenExpected( actualPutMD, expectPutMD );
232     }
233 
234     private void assertSeenExpected( List<?> actual, Object[] expected )
235     {
236         if ( expected == null )
237         {
238             expected = new Object[0];
239         }
240 
241         assertEquals( "different number of expected and actual elements:\n", expected.length, actual.size() );
242         int idx = 0;
243         for ( Object actualObject : actual )
244         {
245             assertEquals( "seen object differs", expected[idx++], actualObject );
246         }
247     }
248 
249     public List<Artifact> getActualArtifactGetRequests()
250     {
251         return actualGet;
252     }
253 
254     public List<Metadata> getActualMetadataGetRequests()
255     {
256         return actualGetMD;
257     }
258 
259     public List<Artifact> getActualArtifactPutRequests()
260     {
261         return actualPut;
262     }
263 
264     public List<Metadata> getActualMetadataPutRequests()
265     {
266         return actualPutMD;
267     }
268 
269     public void setExpectGet( Artifact... expectGet )
270     {
271         this.expectGet = expectGet;
272     }
273 
274     public void setExpectPut( Artifact... expectPut )
275     {
276         this.expectPut = expectPut;
277     }
278 
279     public void setExpectGet( Metadata... expectGetMD )
280     {
281         this.expectGetMD = expectGetMD;
282     }
283 
284     public void setExpectPut( Metadata... expectPutMD )
285     {
286         this.expectPutMD = expectPutMD;
287     }
288 
289     public void resetActual()
290     {
291         this.actualGet = new ArrayList<>();
292         this.actualGetMD = new ArrayList<>();
293         this.actualPut = new ArrayList<>();
294         this.actualPutMD = new ArrayList<>();
295     }
296 
297 }