1 package org.apache.archiva.metadata.repository.memory;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
24 import org.apache.archiva.metadata.model.ProjectVersionReference;
25 import org.apache.archiva.metadata.repository.MetadataResolver;
26 import org.apache.archiva.metadata.repository.RepositorySession;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.LinkedHashSet;
32 import java.util.List;
36 public class TestMetadataResolver
37 implements MetadataResolver
39 private Map<String, ProjectVersionMetadata> projectVersions = new HashMap<String, ProjectVersionMetadata>();
41 private Map<String, List<ArtifactMetadata>> artifacts = new HashMap<String, List<ArtifactMetadata>>();
43 private Map<String, List<ProjectVersionReference>> references =
44 new HashMap<String, List<ProjectVersionReference>>();
46 private Map<String, List<String>> namespaces = new HashMap<String, List<String>>();
48 private Map<String, Collection<String>> projectsInNamespace = new HashMap<String, Collection<String>>();
50 private Map<String, Collection<String>> versionsInProject = new HashMap<String, Collection<String>>();
52 public ProjectVersionMetadata resolveProjectVersion( RepositorySession repositorySession, String repoId,
53 String namespace, String projectId, String projectVersion )
55 return projectVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
58 public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession repositorySession,
59 String repoId, String namespace,
60 String projectId, String projectVersion )
62 return references.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
65 public Collection<String> resolveRootNamespaces( RepositorySession repositorySession, String repoId )
67 return resolveNamespaces( repositorySession, repoId, null );
70 public Collection<String> resolveNamespaces( RepositorySession repositorySession, String repoId,
71 String baseNamespace )
73 Set<String> namespaces = new LinkedHashSet<String>();
74 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
75 for ( String namespace : this.namespaces.get( repoId ) )
77 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
79 int i = namespace.indexOf( '.', fromIndex );
82 namespaces.add( namespace.substring( fromIndex, i ) );
86 namespaces.add( namespace.substring( fromIndex ) );
93 public Collection<String> resolveProjects( RepositorySession repositorySession, String repoId, String namespace )
95 Collection<String> list = projectsInNamespace.get( namespace );
96 return list != null ? list : Collections.<String>emptyList();
99 public Collection<String> resolveProjectVersions( RepositorySession repositorySession, String repoId,
100 String namespace, String projectId )
102 Collection<String> list = versionsInProject.get( namespace + ":" + projectId );
103 return list != null ? list : Collections.<String>emptyList();
106 public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession repositorySession, String repoId,
107 String namespace, String projectId, String projectVersion )
109 List<ArtifactMetadata> artifacts = this.artifacts.get( createMapKey( repoId, namespace, projectId,
111 return ( artifacts != null ? artifacts : Collections.<ArtifactMetadata>emptyList() );
114 public void setProjectVersion( String repoId, String namespace, String projectId,
115 ProjectVersionMetadata versionMetadata )
117 projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
119 Collection<String> projects = projectsInNamespace.get( namespace );
120 if ( projects == null )
122 projects = new LinkedHashSet<String>();
123 projectsInNamespace.put( namespace, projects );
125 projects.add( projectId );
127 String key = namespace + ":" + projectId;
128 Collection<String> versions = versionsInProject.get( key );
129 if ( versions == null )
131 versions = new LinkedHashSet<String>();
132 versionsInProject.put( key, versions );
134 versions.add( versionMetadata.getId() );
137 public void setArtifacts( String repoId, String namespace, String projectId, String projectVersion,
138 List<ArtifactMetadata> artifacts )
140 this.artifacts.put( createMapKey( repoId, namespace, projectId, projectVersion ), artifacts );
143 private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
145 return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
148 public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
149 List<ProjectVersionReference> references )
151 this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
154 public void setNamespaces( String repoId, List<String> namespaces )
156 this.namespaces.put( repoId, namespaces );