001package org.eclipse.aether.internal.test.util;
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 java.io.File;
023import java.io.IOException;
024import java.util.HashSet;
025import java.util.Set;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.metadata.Metadata;
030import org.eclipse.aether.repository.LocalArtifactRegistration;
031import org.eclipse.aether.repository.LocalArtifactRequest;
032import org.eclipse.aether.repository.LocalArtifactResult;
033import org.eclipse.aether.repository.LocalMetadataRegistration;
034import org.eclipse.aether.repository.LocalMetadataRequest;
035import org.eclipse.aether.repository.LocalMetadataResult;
036import org.eclipse.aether.repository.LocalRepository;
037import org.eclipse.aether.repository.LocalRepositoryManager;
038import org.eclipse.aether.repository.RemoteRepository;
039
040/**
041 * A simplistic local repository manager that uses a temporary base directory.
042 */
043public class TestLocalRepositoryManager
044    implements LocalRepositoryManager
045{
046
047    private LocalRepository localRepository;
048
049    private Set<Artifact> unavailableArtifacts = new HashSet<Artifact>();
050
051    private Set<Artifact> artifactRegistrations = new HashSet<Artifact>();
052
053    private Set<Metadata> metadataRegistrations = new HashSet<Metadata>();
054
055    public TestLocalRepositoryManager()
056    {
057        try
058        {
059            localRepository = new LocalRepository( TestFileUtils.createTempDir( "test-local-repo" ) );
060        }
061        catch ( IOException e )
062        {
063            throw new IllegalStateException( e );
064        }
065    }
066
067    public LocalRepository getRepository()
068    {
069        return localRepository;
070    }
071
072    public String getPathForLocalArtifact( Artifact artifact )
073    {
074        String artifactId = artifact.getArtifactId();
075        String groupId = artifact.getGroupId();
076        String extension = artifact.getExtension();
077        String version = artifact.getVersion();
078        String classifier = artifact.getClassifier();
079
080        String path =
081            String.format( "%s/%s/%s/%s-%s-%s%s.%s", groupId, artifactId, version, groupId, artifactId, version,
082                           classifier, extension );
083        return path;
084    }
085
086    public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context )
087    {
088        return getPathForLocalArtifact( artifact );
089    }
090
091    public String getPathForLocalMetadata( Metadata metadata )
092    {
093        String artifactId = metadata.getArtifactId();
094        String groupId = metadata.getGroupId();
095        String version = metadata.getVersion();
096        return String.format( "%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version );
097    }
098
099    public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context )
100    {
101        return getPathForLocalMetadata( metadata );
102    }
103
104    public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request )
105    {
106        Artifact artifact = request.getArtifact();
107
108        LocalArtifactResult result = new LocalArtifactResult( request );
109        File file = new File( localRepository.getBasedir(), getPathForLocalArtifact( artifact ) );
110        result.setFile( file.isFile() ? file : null );
111        result.setAvailable( file.isFile() && !unavailableArtifacts.contains( artifact ) );
112
113        return result;
114    }
115
116    public void add( RepositorySystemSession session, LocalArtifactRegistration request )
117    {
118        artifactRegistrations.add( request.getArtifact() );
119    }
120
121    public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request )
122    {
123        Metadata metadata = request.getMetadata();
124
125        LocalMetadataResult result = new LocalMetadataResult( request );
126        File file = new File( localRepository.getBasedir(), getPathForLocalMetadata( metadata ) );
127        result.setFile( file.isFile() ? file : null );
128
129        return result;
130    }
131
132    public void add( RepositorySystemSession session, LocalMetadataRegistration request )
133    {
134        metadataRegistrations.add( request.getMetadata() );
135    }
136
137    public Set<Artifact> getArtifactRegistration()
138    {
139        return artifactRegistrations;
140    }
141
142    public Set<Metadata> getMetadataRegistration()
143    {
144        return metadataRegistrations;
145    }
146
147    public void setArtifactAvailability( Artifact artifact, boolean available )
148    {
149        if ( available )
150        {
151            unavailableArtifacts.remove( artifact );
152        }
153        else
154        {
155            unavailableArtifacts.add( artifact );
156        }
157    }
158
159}