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