001package org.eclipse.aether.internal.impl;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.*;
023
024import java.io.File;
025import java.io.IOException;
026import java.util.Collection;
027import java.util.Collections;
028import java.util.List;
029import java.util.Map;
030import java.util.Properties;
031
032import org.eclipse.aether.DefaultRepositorySystemSession;
033import org.eclipse.aether.RepositoryEvent;
034import org.eclipse.aether.RepositoryEvent.EventType;
035import org.eclipse.aether.RepositoryException;
036import org.eclipse.aether.artifact.Artifact;
037import org.eclipse.aether.artifact.DefaultArtifact;
038import org.eclipse.aether.deployment.DeployRequest;
039import org.eclipse.aether.deployment.DeploymentException;
040import org.eclipse.aether.internal.impl.DefaultDeployer;
041import org.eclipse.aether.internal.test.util.TestFileProcessor;
042import org.eclipse.aether.internal.test.util.TestFileUtils;
043import org.eclipse.aether.internal.test.util.TestUtils;
044import org.eclipse.aether.metadata.DefaultMetadata;
045import org.eclipse.aether.metadata.MergeableMetadata;
046import org.eclipse.aether.metadata.Metadata;
047import org.eclipse.aether.metadata.Metadata.Nature;
048import org.eclipse.aether.repository.RemoteRepository;
049import org.eclipse.aether.spi.connector.ArtifactDownload;
050import org.eclipse.aether.spi.connector.ArtifactUpload;
051import org.eclipse.aether.spi.connector.MetadataDownload;
052import org.eclipse.aether.spi.connector.MetadataUpload;
053import org.eclipse.aether.spi.connector.RepositoryConnector;
054import org.eclipse.aether.transfer.MetadataNotFoundException;
055import org.junit.After;
056import org.junit.Before;
057import org.junit.Test;
058
059public class DefaultDeployerTest
060{
061
062    private Artifact artifact;
063
064    private DefaultMetadata metadata;
065
066    private DefaultRepositorySystemSession session;
067
068    private StubRepositoryConnectorProvider connectorProvider;
069
070    private DefaultDeployer deployer;
071
072    private DeployRequest request;
073
074    private RecordingRepositoryConnector connector;
075
076    private RecordingRepositoryListener listener;
077
078    @Before
079    public void setup()
080        throws IOException
081    {
082        artifact = new DefaultArtifact( "gid", "aid", "jar", "ver" );
083        artifact = artifact.setFile( TestFileUtils.createTempFile( "artifact" ) );
084        metadata =
085            new DefaultMetadata( "gid", "aid", "ver", "type", Nature.RELEASE_OR_SNAPSHOT,
086                                 TestFileUtils.createTempFile( "metadata" ) );
087
088        session = TestUtils.newSession();
089        connectorProvider = new StubRepositoryConnectorProvider();
090
091        deployer = new DefaultDeployer();
092        deployer.setRepositoryConnectorProvider( connectorProvider );
093        deployer.setRemoteRepositoryManager( new StubRemoteRepositoryManager() );
094        deployer.setRepositoryEventDispatcher( new StubRepositoryEventDispatcher() );
095        deployer.setUpdateCheckManager( new StaticUpdateCheckManager( true ) );
096        deployer.setFileProcessor( new TestFileProcessor() );
097        deployer.setSyncContextFactory( new StubSyncContextFactory() );
098        deployer.setOfflineController( new DefaultOfflineController() );
099
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}