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.List;
027
028import org.eclipse.aether.DefaultRepositorySystemSession;
029import org.eclipse.aether.RepositoryEvent;
030import org.eclipse.aether.RepositoryEvent.EventType;
031import org.eclipse.aether.artifact.Artifact;
032import org.eclipse.aether.artifact.DefaultArtifact;
033import org.eclipse.aether.installation.InstallRequest;
034import org.eclipse.aether.installation.InstallResult;
035import org.eclipse.aether.installation.InstallationException;
036import org.eclipse.aether.internal.impl.DefaultFileProcessor;
037import org.eclipse.aether.internal.impl.DefaultInstaller;
038import org.eclipse.aether.internal.test.util.TestFileProcessor;
039import org.eclipse.aether.internal.test.util.TestFileUtils;
040import org.eclipse.aether.internal.test.util.TestLocalRepositoryManager;
041import org.eclipse.aether.internal.test.util.TestUtils;
042import org.eclipse.aether.metadata.DefaultMetadata;
043import org.eclipse.aether.metadata.Metadata;
044import org.eclipse.aether.metadata.Metadata.Nature;
045import org.junit.After;
046import org.junit.Before;
047import org.junit.Test;
048
049public class DefaultInstallerTest
050{
051
052    private Artifact artifact;
053
054    private Metadata metadata;
055
056    private DefaultRepositorySystemSession session;
057
058    private String localArtifactPath;
059
060    private String localMetadataPath;
061
062    private DefaultInstaller installer;
063
064    private InstallRequest request;
065
066    private RecordingRepositoryListener listener;
067
068    private File localArtifactFile;
069
070    private TestLocalRepositoryManager lrm;
071
072    @Before
073    public void setup()
074        throws IOException
075    {
076        artifact = new DefaultArtifact( "gid", "aid", "jar", "ver" );
077        artifact = artifact.setFile( TestFileUtils.createTempFile( "artifact".getBytes(), 1 ) );
078        metadata =
079            new DefaultMetadata( "gid", "aid", "ver", "type", Nature.RELEASE_OR_SNAPSHOT,
080                                 TestFileUtils.createTempFile( "metadata".getBytes(), 1 ) );
081
082        session = TestUtils.newSession();
083        localArtifactPath = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
084        localMetadataPath = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
085
086        localArtifactFile = new File( session.getLocalRepository().getBasedir(), localArtifactPath );
087
088        installer = new DefaultInstaller();
089        installer.setFileProcessor( new TestFileProcessor() );
090        installer.setRepositoryEventDispatcher( new StubRepositoryEventDispatcher() );
091        installer.setSyncContextFactory( new StubSyncContextFactory() );
092        request = new InstallRequest();
093        listener = new RecordingRepositoryListener();
094        session.setRepositoryListener( listener );
095
096        lrm = (TestLocalRepositoryManager) session.getLocalRepositoryManager();
097
098        TestFileUtils.deleteFile( session.getLocalRepository().getBasedir() );
099    }
100
101    @After
102    public void teardown()
103        throws Exception
104    {
105        TestFileUtils.deleteFile( session.getLocalRepository().getBasedir() );
106    }
107
108    @Test
109    public void testSuccessfulInstall()
110        throws InstallationException, IOException
111    {
112        File artifactFile =
113            new File( session.getLocalRepositoryManager().getRepository().getBasedir(), localArtifactPath );
114        File metadataFile =
115            new File( session.getLocalRepositoryManager().getRepository().getBasedir(), localMetadataPath );
116
117        artifactFile.delete();
118        metadataFile.delete();
119
120        request.addArtifact( artifact );
121        request.addMetadata( metadata );
122
123        InstallResult result = installer.install( session, request );
124
125        assertTrue( artifactFile.exists() );
126        assertEquals( "artifact", TestFileUtils.readString( artifactFile ) );
127
128        assertTrue( metadataFile.exists() );
129        assertEquals( "metadata", TestFileUtils.readString( metadataFile ) );
130
131        assertEquals( result.getRequest(), request );
132
133        assertEquals( result.getArtifacts().size(), 1 );
134        assertTrue( result.getArtifacts().contains( artifact ) );
135
136        assertEquals( result.getMetadata().size(), 1 );
137        assertTrue( result.getMetadata().contains( metadata ) );
138
139        assertEquals( 1, lrm.getMetadataRegistration().size() );
140        assertTrue( lrm.getMetadataRegistration().contains( metadata ) );
141        assertEquals( 1, lrm.getArtifactRegistration().size() );
142        assertTrue( lrm.getArtifactRegistration().contains( artifact ) );
143    }
144
145    @Test( expected = InstallationException.class )
146    public void testNullArtifactFile()
147        throws InstallationException
148    {
149        InstallRequest request = new InstallRequest();
150        request.addArtifact( artifact.setFile( null ) );
151
152        installer.install( session, request );
153    }
154
155    @Test( expected = InstallationException.class )
156    public void testNullMetadataFile()
157        throws InstallationException
158    {
159        InstallRequest request = new InstallRequest();
160        request.addMetadata( metadata.setFile( null ) );
161
162        installer.install( session, request );
163    }
164
165    @Test( expected = InstallationException.class )
166    public void testNonExistentArtifactFile()
167        throws InstallationException
168    {
169        InstallRequest request = new InstallRequest();
170        request.addArtifact( artifact.setFile( new File( "missing.txt" ) ) );
171
172        installer.install( session, request );
173    }
174
175    @Test( expected = InstallationException.class )
176    public void testNonExistentMetadataFile()
177        throws InstallationException
178    {
179        InstallRequest request = new InstallRequest();
180        request.addMetadata( metadata.setFile( new File( "missing.xml" ) ) );
181
182        installer.install( session, request );
183    }
184
185    @Test( expected = InstallationException.class )
186    public void testArtifactExistsAsDir()
187        throws InstallationException
188    {
189        String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
190        File file = new File( session.getLocalRepository().getBasedir(), path );
191        assertFalse( file.getAbsolutePath() + " is a file, not directory", file.isFile() );
192        assertFalse( file.getAbsolutePath() + " already exists", file.exists() );
193        assertTrue( "failed to setup test: could not create " + file.getAbsolutePath(),
194                    file.mkdirs() || file.isDirectory() );
195
196        request.addArtifact( artifact );
197        installer.install( session, request );
198    }
199
200    @Test( expected = InstallationException.class )
201    public void testMetadataExistsAsDir()
202        throws InstallationException
203    {
204        String path = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
205        assertTrue( "failed to setup test: could not create " + path,
206                    new File( session.getLocalRepository().getBasedir(), path ).mkdirs() );
207
208        request.addMetadata( metadata );
209        installer.install( session, request );
210    }
211
212    @Test( expected = InstallationException.class )
213    public void testArtifactDestinationEqualsSource()
214        throws Exception
215    {
216        String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
217        File file = new File( session.getLocalRepository().getBasedir(), path );
218        artifact = artifact.setFile( file );
219        TestFileUtils.writeString( file, "test" );
220
221        request.addArtifact( artifact );
222        installer.install( session, request );
223    }
224
225    @Test( expected = InstallationException.class )
226    public void testMetadataDestinationEqualsSource()
227        throws Exception
228    {
229        String path = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
230        File file = new File( session.getLocalRepository().getBasedir(), path );
231        metadata = metadata.setFile( file );
232        TestFileUtils.writeString( file, "test" );
233
234        request.addMetadata( metadata );
235        installer.install( session, request );
236    }
237
238    @Test
239    public void testSuccessfulArtifactEvents()
240        throws InstallationException
241    {
242        InstallRequest request = new InstallRequest();
243        request.addArtifact( artifact );
244
245        installer.install( session, request );
246        checkEvents( "Repository Event problem", artifact, false );
247    }
248
249    @Test
250    public void testSuccessfulMetadataEvents()
251        throws InstallationException
252    {
253        InstallRequest request = new InstallRequest();
254        request.addMetadata( metadata );
255
256        installer.install( session, request );
257        checkEvents( "Repository Event problem", metadata, false );
258    }
259
260    @Test
261    public void testFailingEventsNullArtifactFile()
262    {
263        checkFailedEvents( "null artifact file", this.artifact.setFile( null ) );
264    }
265
266    @Test
267    public void testFailingEventsNullMetadataFile()
268    {
269        checkFailedEvents( "null metadata file", this.metadata.setFile( null ) );
270    }
271
272    @Test
273    public void testFailingEventsArtifactExistsAsDir()
274    {
275        String path = session.getLocalRepositoryManager().getPathForLocalArtifact( artifact );
276        assertTrue( "failed to setup test: could not create " + path,
277                    new File( session.getLocalRepository().getBasedir(), path ).mkdirs() );
278        checkFailedEvents( "target exists as dir", artifact );
279    }
280
281    @Test
282    public void testFailingEventsMetadataExistsAsDir()
283    {
284        String path = session.getLocalRepositoryManager().getPathForLocalMetadata( metadata );
285        assertTrue( "failed to setup test: could not create " + path,
286                    new File( session.getLocalRepository().getBasedir(), path ).mkdirs() );
287        checkFailedEvents( "target exists as dir", metadata );
288    }
289
290    private void checkFailedEvents( String msg, Metadata metadata )
291    {
292        InstallRequest request = new InstallRequest().addMetadata( metadata );
293        msg = "Repository events problem (case: " + msg + ")";
294
295        try
296        {
297            installer.install( session, request );
298            fail( "expected exception" );
299        }
300        catch ( InstallationException e )
301        {
302            checkEvents( msg, metadata, true );
303        }
304
305    }
306
307    private void checkEvents( String msg, Metadata metadata, boolean failed )
308    {
309        List<RepositoryEvent> events = listener.getEvents();
310        assertEquals( msg, 2, events.size() );
311        RepositoryEvent event = events.get( 0 );
312        assertEquals( msg, EventType.METADATA_INSTALLING, event.getType() );
313        assertEquals( msg, metadata, event.getMetadata() );
314        assertNull( msg, event.getException() );
315
316        event = events.get( 1 );
317        assertEquals( msg, EventType.METADATA_INSTALLED, event.getType() );
318        assertEquals( msg, metadata, event.getMetadata() );
319        if ( failed )
320        {
321            assertNotNull( msg, event.getException() );
322        }
323        else
324        {
325            assertNull( msg, event.getException() );
326        }
327    }
328
329    private void checkFailedEvents( String msg, Artifact artifact )
330    {
331        InstallRequest request = new InstallRequest().addArtifact( artifact );
332        msg = "Repository events problem (case: " + msg + ")";
333
334        try
335        {
336            installer.install( session, request );
337            fail( "expected exception" );
338        }
339        catch ( InstallationException e )
340        {
341            checkEvents( msg, artifact, true );
342        }
343    }
344
345    private void checkEvents( String msg, Artifact artifact, boolean failed )
346    {
347        List<RepositoryEvent> events = listener.getEvents();
348        assertEquals( msg, 2, events.size() );
349        RepositoryEvent event = events.get( 0 );
350        assertEquals( msg, EventType.ARTIFACT_INSTALLING, event.getType() );
351        assertEquals( msg, artifact, event.getArtifact() );
352        assertNull( msg, event.getException() );
353        
354        event = events.get( 1 );
355        assertEquals( msg, EventType.ARTIFACT_INSTALLED, event.getType() );
356        assertEquals( msg, artifact, event.getArtifact() );
357        if ( failed )
358        {
359            assertNotNull( msg + " > expected exception", event.getException() );
360        }
361        else
362        {
363            assertNull( msg + " > " + event.getException(), event.getException() );
364        }
365    }
366
367    @Test
368    public void testDoNotUpdateUnchangedArtifact()
369        throws InstallationException
370    {
371        request.addArtifact( artifact );
372        installer.install( session, request );
373
374        installer.setFileProcessor( new DefaultFileProcessor()
375        {
376            @Override
377            public long copy( File src, File target, ProgressListener listener )
378                throws IOException
379            {
380                throw new IOException( "copy called" );
381            }
382        } );
383
384        request = new InstallRequest();
385        request.addArtifact( artifact );
386        installer.install( session, request );
387    }
388
389    @Test
390    public void testSetArtifactTimestamps()
391        throws InstallationException
392    {
393        artifact.getFile().setLastModified( artifact.getFile().lastModified() - 60000 );
394
395        request.addArtifact( artifact );
396
397        installer.install( session, request );
398
399        assertEquals( "artifact timestamp was not set to src file", artifact.getFile().lastModified(),
400                      localArtifactFile.lastModified() );
401
402        request = new InstallRequest();
403
404        request.addArtifact( artifact );
405
406        artifact.getFile().setLastModified( artifact.getFile().lastModified() - 60000 );
407
408        installer.install( session, request );
409
410        assertEquals( "artifact timestamp was not set to src file", artifact.getFile().lastModified(),
411                      localArtifactFile.lastModified() );
412    }
413}