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.IOException; 022import java.nio.file.Files; 023import java.nio.file.Path; 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 implements LocalRepositoryManager { 046 047 private LocalRepository localRepository; 048 049 private final Set<Artifact> unavailableArtifacts = new HashSet<>(); 050 051 private final Set<Artifact> artifactRegistrations = new HashSet<>(); 052 053 private final Set<Metadata> metadataRegistrations = new HashSet<>(); 054 055 public TestLocalRepositoryManager() { 056 try { 057 Path tempDirectory = Files.createTempDirectory("test-local-repo"); 058 tempDirectory.toFile().deleteOnExit(); 059 localRepository = new LocalRepository(tempDirectory); 060 } catch (IOException e) { 061 throw new IllegalStateException(e); 062 } 063 } 064 065 public LocalRepository getRepository() { 066 return localRepository; 067 } 068 069 @Deprecated 070 @Override 071 public String getPathForLocalArtifact(Artifact artifact) { 072 requireNonNull(artifact, "artifact cannot be null"); 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( 081 "%s/%s/%s/%s-%s-%s%s.%s", 082 groupId, artifactId, version, groupId, artifactId, version, classifier, extension); 083 } 084 085 @Deprecated 086 @Override 087 public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) { 088 requireNonNull(artifact, "artifact cannot be null"); 089 requireNonNull(repository, "repository cannot be null"); 090 091 return getPathForLocalArtifact(artifact); 092 } 093 094 @Deprecated 095 @Override 096 public String getPathForLocalMetadata(Metadata metadata) { 097 requireNonNull(metadata, "metadata cannot be null"); 098 099 String artifactId = metadata.getArtifactId(); 100 String groupId = metadata.getGroupId(); 101 String version = metadata.getVersion(); 102 return String.format("%s/%s/%s/%s-%s-%s.xml", groupId, artifactId, version, groupId, artifactId, version); 103 } 104 105 @Deprecated 106 @Override 107 public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) { 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 requireNonNull(session, "session cannot be null"); 116 requireNonNull(request, "request cannot be null"); 117 118 Artifact artifact = request.getArtifact(); 119 120 LocalArtifactResult result = new LocalArtifactResult(request); 121 Path file = getAbsolutePathForLocalArtifact(artifact); 122 boolean isFile = Files.isRegularFile(file); 123 result.setPath(isFile ? file : null); 124 result.setAvailable(isFile && !unavailableArtifacts.contains(artifact)); 125 126 return result; 127 } 128 129 public void add(RepositorySystemSession session, LocalArtifactRegistration request) { 130 requireNonNull(session, "session cannot be null"); 131 requireNonNull(request, "request cannot be null"); 132 133 artifactRegistrations.add(request.getArtifact()); 134 } 135 136 public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) { 137 requireNonNull(session, "session cannot be null"); 138 requireNonNull(request, "request cannot be null"); 139 140 Metadata metadata = request.getMetadata(); 141 142 LocalMetadataResult result = new LocalMetadataResult(request); 143 Path file = getAbsolutePathForLocalMetadata(metadata); 144 result.setPath(Files.isRegularFile(file) ? file : null); 145 146 return result; 147 } 148 149 public void add(RepositorySystemSession session, LocalMetadataRegistration request) { 150 requireNonNull(session, "session cannot be null"); 151 requireNonNull(request, "request cannot be null"); 152 153 metadataRegistrations.add(request.getMetadata()); 154 } 155 156 public Set<Artifact> getArtifactRegistration() { 157 return artifactRegistrations; 158 } 159 160 public Set<Metadata> getMetadataRegistration() { 161 return metadataRegistrations; 162 } 163 164 public void setArtifactAvailability(Artifact artifact, boolean available) { 165 if (available) { 166 unavailableArtifacts.remove(artifact); 167 } else { 168 unavailableArtifacts.add(artifact); 169 } 170 } 171}