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 Collection<ProjectVersionReference> projectVersionReferences =
63 references.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
64 return projectVersionReferences;
67 public Collection<String> resolveRootNamespaces( RepositorySession repositorySession, String repoId )
69 return resolveNamespaces( repositorySession, repoId, null );
72 public Collection<String> resolveNamespaces( RepositorySession repositorySession, String repoId,
73 String baseNamespace )
75 Set<String> namespaces = new LinkedHashSet<String>();
76 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
77 for ( String namespace : this.namespaces.get( repoId ) )
79 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
81 int i = namespace.indexOf( '.', fromIndex );
84 namespaces.add( namespace.substring( fromIndex, i ) );
88 namespaces.add( namespace.substring( fromIndex ) );
95 public Collection<String> resolveProjects( RepositorySession repositorySession, String repoId, String namespace )
97 Collection<String> list = projectsInNamespace.get( namespace );
98 return list != null ? list : Collections.<String>emptyList();
101 public Collection<String> resolveProjectVersions( RepositorySession repositorySession, String repoId,
102 String namespace, String projectId )
104 Collection<String> list = versionsInProject.get( namespace + ":" + projectId );
105 return list != null ? list : Collections.<String>emptyList();
108 public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession repositorySession, String repoId,
109 String namespace, String projectId, String projectVersion )
111 List<ArtifactMetadata> artifacts =
112 this.artifacts.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
113 return ( artifacts != null ? artifacts : Collections.<ArtifactMetadata>emptyList() );
116 public void setProjectVersion( String repoId, String namespace, String projectId,
117 ProjectVersionMetadata versionMetadata )
119 projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
121 Collection<String> projects = projectsInNamespace.get( namespace );
122 if ( projects == null )
124 projects = new LinkedHashSet<String>();
125 projectsInNamespace.put( namespace, projects );
127 projects.add( projectId );
129 String key = namespace + ":" + projectId;
130 Collection<String> versions = versionsInProject.get( key );
131 if ( versions == null )
133 versions = new LinkedHashSet<String>();
134 versionsInProject.put( key, versions );
136 versions.add( versionMetadata.getId() );
139 public void setArtifacts( String repoId, String namespace, String projectId, String projectVersion,
140 List<ArtifactMetadata> artifacts )
142 this.artifacts.put( createMapKey( repoId, namespace, projectId, projectVersion ), artifacts );
145 private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
147 return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
150 public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
151 List<ProjectVersionReference> references )
153 this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
156 public void setNamespaces( String repoId, List<String> namespaces )
158 this.namespaces.put( repoId, namespaces );