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