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<Artifact>();
64  
65      private List<Metadata> actualGetMD = new ArrayList<Metadata>();
66  
67      private List<Artifact> actualPut = new ArrayList<Artifact>();
68  
69      private List<Metadata> actualPutMD = new ArrayList<Metadata>();
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         throws Exception
204     {
205         TransferListener listener = transfer.getListener();
206         if ( listener == null )
207         {
208             return;
209         }
210         TransferEvent.Builder event =
211             new TransferEvent.Builder( session, new TransferResource( null, null, null, null, transfer.getTrace() ) );
212         event.setException( transfer.getException() );
213         if ( transfer.getException() != null )
214         {
215             listener.transferFailed( event.setType( TransferEvent.EventType.FAILED ).build() );
216         }
217         else
218         {
219             listener.transferSucceeded( event.setType( TransferEvent.EventType.SUCCEEDED ).build() );
220         }
221     }
222 
223     public void close()
224     {
225     }
226 
227     public void assertSeenExpected()
228     {
229         assertSeenExpected( actualGet, expectGet );
230         assertSeenExpected( actualGetMD, expectGetMD );
231         assertSeenExpected( actualPut, expectPut );
232         assertSeenExpected( actualPutMD, expectPutMD );
233     }
234 
235     private void assertSeenExpected( List<? extends Object> actual, Object[] expected )
236     {
237         if ( expected == null )
238         {
239             expected = new Object[0];
240         }
241 
242         assertEquals( "different number of expected and actual elements:\n", expected.length, actual.size() );
243         int idx = 0;
244         for ( Object actualObject : actual )
245         {
246             assertEquals( "seen object differs", expected[idx++], actualObject );
247         }
248     }
249 
250     public List<Artifact> getActualArtifactGetRequests()
251     {
252         return actualGet;
253     }
254 
255     public List<Metadata> getActualMetadataGetRequests()
256     {
257         return actualGetMD;
258     }
259 
260     public List<Artifact> getActualArtifactPutRequests()
261     {
262         return actualPut;
263     }
264 
265     public List<Metadata> getActualMetadataPutRequests()
266     {
267         return actualPutMD;
268     }
269 
270     public void setExpectGet( Artifact... expectGet )
271     {
272         this.expectGet = expectGet;
273     }
274 
275     public void setExpectPut( Artifact... expectPut )
276     {
277         this.expectPut = expectPut;
278     }
279 
280     public void setExpectGet( Metadata... expectGetMD )
281     {
282         this.expectGetMD = expectGetMD;
283     }
284 
285     public void setExpectPut( Metadata... expectPutMD )
286     {
287         this.expectPutMD = expectPutMD;
288     }
289 
290     public void resetActual()
291     {
292         this.actualGet = new ArrayList<Artifact>();
293         this.actualGetMD = new ArrayList<Metadata>();
294         this.actualPut = new ArrayList<Artifact>();
295         this.actualPutMD = new ArrayList<Metadata>();
296     }
297 
298 }