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;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.artifact.DefaultArtifact;
030import org.eclipse.aether.internal.test.util.TestFileUtils;
031import org.eclipse.aether.internal.test.util.TestUtils;
032import org.eclipse.aether.repository.LocalArtifactRequest;
033import org.eclipse.aether.repository.LocalArtifactResult;
034import org.eclipse.aether.repository.RemoteRepository;
035import org.junit.After;
036import org.junit.Before;
037import org.junit.Test;
038
039/**
040 */
041public class SimpleLocalRepositoryManagerTest
042{
043
044    private File basedir;
045
046    private SimpleLocalRepositoryManager manager;
047
048    private RepositorySystemSession session;
049
050    @Before
051    public void setup()
052        throws IOException
053    {
054        basedir = TestFileUtils.createTempDir( "simple-repo" );
055        manager = new SimpleLocalRepositoryManager( basedir, "simple", new DefaultLocalPathComposer() );
056        session = TestUtils.newSession();
057    }
058
059    @After
060    public void tearDown()
061        throws Exception
062    {
063        TestFileUtils.deleteFile( basedir );
064        manager = null;
065        session = null;
066    }
067
068    @Test
069    public void testGetPathForLocalArtifact()
070    {
071        Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" );
072        assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
073        assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", manager.getPathForLocalArtifact( artifact ) );
074
075        artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-20110329.221805-4" );
076        assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
077        assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar", manager.getPathForLocalArtifact( artifact ) );
078
079        artifact = new DefaultArtifact( "g.i.d", "a.i.d", "", "", "1.0-SNAPSHOT" );
080        assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT", manager.getPathForLocalArtifact( artifact ) );
081    }
082
083    @Test
084    public void testGetPathForRemoteArtifact()
085    {
086        RemoteRepository remoteRepo = new RemoteRepository.Builder( "repo", "default", "ram:/void" ).build();
087
088        Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" );
089        assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
090        assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-SNAPSHOT.jar",
091                      manager.getPathForRemoteArtifact( artifact, remoteRepo, "" ) );
092
093        artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-20110329.221805-4" );
094        assertEquals( "1.0-SNAPSHOT", artifact.getBaseVersion() );
095        assertEquals( "g/i/d/a.i.d/1.0-SNAPSHOT/a.i.d-1.0-20110329.221805-4.jar",
096                      manager.getPathForRemoteArtifact( artifact, remoteRepo, "" ) );
097    }
098
099    @Test
100    public void testFindArtifactUsesTimestampedVersion()
101        throws Exception
102    {
103        Artifact artifact = new DefaultArtifact( "g.i.d:a.i.d:1.0-SNAPSHOT" );
104        File file = new File( basedir, manager.getPathForLocalArtifact( artifact ) );
105        TestFileUtils.writeString( file, "test" );
106
107        artifact = artifact.setVersion( "1.0-20110329.221805-4" );
108        LocalArtifactRequest request = new LocalArtifactRequest();
109        request.setArtifact( artifact );
110        LocalArtifactResult result = manager.find( session, request );
111        assertNull( result.toString(), result.getFile() );
112        assertFalse( result.toString(), result.isAvailable() );
113    }
114
115}