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<>(); 050 051 private Set<Artifact> artifactRegistrations = new HashSet<>(); 052 053 private Set<Metadata> metadataRegistrations = new HashSet<>(); 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 return String.format( "%s/%s/%s/%s-%s-%s%s.%s", groupId, artifactId, version, groupId, artifactId, version, 081 classifier, extension ); 082 } 083 084 public String getPathForRemoteArtifact( Artifact artifact, RemoteRepository repository, String context ) 085 { 086 return getPathForLocalArtifact( artifact ); 087 } 088 089 public String getPathForLocalMetadata( Metadata metadata ) 090 { 091 String artifactId = metadata.getArtifactId(); 092 String groupId = metadata.getGroupId(); 093 String version = metadata.getVersion(); 094 return String.format( "%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version ); 095 } 096 097 public String getPathForRemoteMetadata( Metadata metadata, RemoteRepository repository, String context ) 098 { 099 return getPathForLocalMetadata( metadata ); 100 } 101 102 public LocalArtifactResult find( RepositorySystemSession session, LocalArtifactRequest request ) 103 { 104 Artifact artifact = request.getArtifact(); 105 106 LocalArtifactResult result = new LocalArtifactResult( request ); 107 File file = new File( localRepository.getBasedir(), getPathForLocalArtifact( artifact ) ); 108 result.setFile( file.isFile() ? file : null ); 109 result.setAvailable( file.isFile() && !unavailableArtifacts.contains( artifact ) ); 110 111 return result; 112 } 113 114 public void add( RepositorySystemSession session, LocalArtifactRegistration request ) 115 { 116 artifactRegistrations.add( request.getArtifact() ); 117 } 118 119 public LocalMetadataResult find( RepositorySystemSession session, LocalMetadataRequest request ) 120 { 121 Metadata metadata = request.getMetadata(); 122 123 LocalMetadataResult result = new LocalMetadataResult( request ); 124 File file = new File( localRepository.getBasedir(), getPathForLocalMetadata( metadata ) ); 125 result.setFile( file.isFile() ? file : null ); 126 127 return result; 128 } 129 130 public void add( RepositorySystemSession session, LocalMetadataRegistration request ) 131 { 132 metadataRegistrations.add( request.getMetadata() ); 133 } 134 135 public Set<Artifact> getArtifactRegistration() 136 { 137 return artifactRegistrations; 138 } 139 140 public Set<Metadata> getMetadataRegistration() 141 { 142 return metadataRegistrations; 143 } 144 145 public void setArtifactAvailability( Artifact artifact, boolean available ) 146 { 147 if ( available ) 148 { 149 unavailableArtifacts.remove( artifact ); 150 } 151 else 152 { 153 unavailableArtifacts.add( artifact ); 154 } 155 } 156 157}