001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.test.util; 020 021import java.io.File; 022import java.io.IOException; 023import java.util.HashSet; 024import java.util.Set; 025 026import org.eclipse.aether.RepositorySystemSession; 027import org.eclipse.aether.artifact.Artifact; 028import org.eclipse.aether.metadata.Metadata; 029import org.eclipse.aether.repository.LocalArtifactRegistration; 030import org.eclipse.aether.repository.LocalArtifactRequest; 031import org.eclipse.aether.repository.LocalArtifactResult; 032import org.eclipse.aether.repository.LocalMetadataRegistration; 033import org.eclipse.aether.repository.LocalMetadataRequest; 034import org.eclipse.aether.repository.LocalMetadataResult; 035import org.eclipse.aether.repository.LocalRepository; 036import org.eclipse.aether.repository.LocalRepositoryManager; 037import org.eclipse.aether.repository.RemoteRepository; 038 039import static java.util.Objects.requireNonNull; 040 041/** 042 * A simplistic local repository manager that uses a temporary base directory. 043 */ 044public class TestLocalRepositoryManager implements LocalRepositoryManager { 045 046 private LocalRepository localRepository; 047 048 private final Set<Artifact> unavailableArtifacts = new HashSet<>(); 049 050 private final Set<Artifact> artifactRegistrations = new HashSet<>(); 051 052 private final Set<Metadata> metadataRegistrations = new HashSet<>(); 053 054 public TestLocalRepositoryManager() { 055 try { 056 localRepository = new LocalRepository(TestFileUtils.createTempDir("test-local-repo")); 057 } catch (IOException e) { 058 throw new IllegalStateException(e); 059 } 060 } 061 062 public LocalRepository getRepository() { 063 return localRepository; 064 } 065 066 public String getPathForLocalArtifact(Artifact artifact) { 067 requireNonNull(artifact, "artifact cannot be null"); 068 069 String artifactId = artifact.getArtifactId(); 070 String groupId = artifact.getGroupId(); 071 String extension = artifact.getExtension(); 072 String version = artifact.getVersion(); 073 String classifier = artifact.getClassifier(); 074 075 return String.format( 076 "%s/%s/%s/%s-%s-%s%s.%s", 077 groupId, artifactId, version, groupId, artifactId, version, classifier, extension); 078 } 079 080 public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) { 081 requireNonNull(artifact, "artifact cannot be null"); 082 requireNonNull(repository, "repository cannot be null"); 083 084 return getPathForLocalArtifact(artifact); 085 } 086 087 public String getPathForLocalMetadata(Metadata metadata) { 088 requireNonNull(metadata, "metadata cannot be null"); 089 090 String artifactId = metadata.getArtifactId(); 091 String groupId = metadata.getGroupId(); 092 String version = metadata.getVersion(); 093 return String.format("%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version); 094 } 095 096 public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) { 097 requireNonNull(metadata, "metadata cannot be null"); 098 requireNonNull(repository, "repository cannot be null"); 099 100 return getPathForLocalMetadata(metadata); 101 } 102 103 public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) { 104 requireNonNull(session, "session cannot be null"); 105 requireNonNull(request, "request cannot be null"); 106 107 Artifact artifact = request.getArtifact(); 108 109 LocalArtifactResult result = new LocalArtifactResult(request); 110 File file = new File(localRepository.getBasedir(), getPathForLocalArtifact(artifact)); 111 result.setFile(file.isFile() ? file : null); 112 result.setAvailable(file.isFile() && !unavailableArtifacts.contains(artifact)); 113 114 return result; 115 } 116 117 public void add(RepositorySystemSession session, LocalArtifactRegistration request) { 118 requireNonNull(session, "session cannot be null"); 119 requireNonNull(request, "request cannot be null"); 120 121 artifactRegistrations.add(request.getArtifact()); 122 } 123 124 public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) { 125 requireNonNull(session, "session cannot be null"); 126 requireNonNull(request, "request cannot be null"); 127 128 Metadata metadata = request.getMetadata(); 129 130 LocalMetadataResult result = new LocalMetadataResult(request); 131 File file = new File(localRepository.getBasedir(), getPathForLocalMetadata(metadata)); 132 result.setFile(file.isFile() ? file : null); 133 134 return result; 135 } 136 137 public void add(RepositorySystemSession session, LocalMetadataRegistration request) { 138 requireNonNull(session, "session cannot be null"); 139 requireNonNull(request, "request cannot be null"); 140 141 metadataRegistrations.add(request.getMetadata()); 142 } 143 144 public Set<Artifact> getArtifactRegistration() { 145 return artifactRegistrations; 146 } 147 148 public Set<Metadata> getMetadataRegistration() { 149 return metadataRegistrations; 150 } 151 152 public void setArtifactAvailability(Artifact artifact, boolean available) { 153 if (available) { 154 unavailableArtifacts.remove(artifact); 155 } else { 156 unavailableArtifacts.add(artifact); 157 } 158 } 159}