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 java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.LinkedHashSet;
26 import java.util.List;
30 import org.apache.archiva.metadata.model.ProjectMetadata;
31 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionReference;
33 import org.apache.archiva.metadata.repository.MetadataResolver;
35 public class TestMetadataResolver
36 implements MetadataResolver
38 private Map<String, ProjectVersionMetadata> projectVersions = new HashMap<String, ProjectVersionMetadata>();
40 private Map<String, List<String>> artifactVersions = new HashMap<String, List<String>>();
42 private Map<String, List<ProjectVersionReference>> references =
43 new HashMap<String, List<ProjectVersionReference>>();
45 private List<String> namespaces;
47 private Map<String, Collection<String>> projectsInNamespace = new HashMap<String, Collection<String>>();
49 private Map<String, Collection<String>> versionsInProject = new HashMap<String, Collection<String>>();
51 public ProjectMetadata getProject( String repoId, String namespace, String projectId )
53 ProjectMetadata metadata = new ProjectMetadata();
54 metadata.setNamespace( namespace );
55 metadata.setId( projectId );
59 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
60 String projectVersion )
62 return projectVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
65 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
66 String projectVersion )
68 List<String> versions = artifactVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
69 return ( versions != null ? versions : Collections.<String>emptyList() );
72 public Collection<ProjectVersionReference> getProjectReferences( String repoId, String namespace, String projectId,
73 String projectVersion )
75 return references.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
78 public Collection<String> getRootNamespaces( String repoId )
80 return getNamespaces( null );
83 private Collection<String> getNamespaces( String baseNamespace )
85 Set<String> namespaces = new LinkedHashSet<String>();
86 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
87 for ( String namespace : this.namespaces )
89 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
91 int i = namespace.indexOf( '.', fromIndex );
94 namespaces.add( namespace.substring( fromIndex, i ) );
98 namespaces.add( namespace.substring( fromIndex ) );
105 public Collection<String> getNamespaces( String repoId, String namespace )
107 return getNamespaces( namespace );
110 public Collection<String> getProjects( String repoId, String namespace )
112 Collection<String> list = projectsInNamespace.get( namespace );
113 return list != null ? list : Collections.<String>emptyList();
116 public Collection<String> getProjectVersions( String repoId, String namespace, String projectId )
118 Collection<String> list = versionsInProject.get( namespace + ":" + projectId );
119 return list != null ? list : Collections.<String>emptyList();
122 public void setProjectVersion( String repoId, String namespace, String projectId,
123 ProjectVersionMetadata versionMetadata )
125 projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
127 Collection<String> projects = projectsInNamespace.get( namespace );
128 if ( projects == null )
130 projects = new LinkedHashSet<String>();
131 projectsInNamespace.put( namespace, projects );
133 projects.add( projectId );
135 String key = namespace + ":" + projectId;
136 Collection<String> versions = versionsInProject.get( key );
137 if ( versions == null )
139 versions = new LinkedHashSet<String>();
140 versionsInProject.put( key, versions );
142 versions.add( versionMetadata.getId() );
145 public void setArtifactVersions( String repoId, String namespace, String projectId, String projectVersion,
146 List<String> versions )
148 artifactVersions.put( createMapKey( repoId, namespace, projectId, projectVersion ), versions );
151 private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
153 return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
156 public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
157 List<ProjectVersionReference> references )
159 this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
162 public void setNamespaces( List<String> namespaces )
164 this.namespaces = namespaces;