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.ArtifactMetadata;
31 import org.apache.archiva.metadata.model.ProjectMetadata;
32 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
33 import org.apache.archiva.metadata.model.ProjectVersionReference;
34 import org.apache.archiva.metadata.repository.MetadataResolver;
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 List<String> namespaces;
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 ProjectMetadata getProject( String repoId, String namespace, String projectId )
54 ProjectMetadata metadata = new ProjectMetadata();
55 metadata.setNamespace( namespace );
56 metadata.setId( projectId );
60 public ProjectVersionMetadata getProjectVersion( String repoId, String namespace, String projectId,
61 String projectVersion )
63 return projectVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
66 public Collection<String> getArtifactVersions( String repoId, String namespace, String projectId,
67 String projectVersion )
69 throw new UnsupportedOperationException();
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 Collection<ArtifactMetadata> getArtifacts( String repoId, String namespace, String projectId,
123 String projectVersion )
125 List<ArtifactMetadata> artifacts =
126 this.artifacts.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
127 return ( artifacts != null ? artifacts : Collections.<ArtifactMetadata>emptyList() );
130 public void setProjectVersion( String repoId, String namespace, String projectId,
131 ProjectVersionMetadata versionMetadata )
133 projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
135 Collection<String> projects = projectsInNamespace.get( namespace );
136 if ( projects == null )
138 projects = new LinkedHashSet<String>();
139 projectsInNamespace.put( namespace, projects );
141 projects.add( projectId );
143 String key = namespace + ":" + projectId;
144 Collection<String> versions = versionsInProject.get( key );
145 if ( versions == null )
147 versions = new LinkedHashSet<String>();
148 versionsInProject.put( key, versions );
150 versions.add( versionMetadata.getId() );
153 public void setArtifacts( String repoId, String namespace, String projectId, String projectVersion,
154 List<ArtifactMetadata> artifacts )
156 this.artifacts.put( createMapKey( repoId, namespace, projectId, projectVersion ), artifacts );
159 private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
161 return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
164 public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
165 List<ProjectVersionReference> references )
167 this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
170 public void setNamespaces( List<String> namespaces )
172 this.namespaces = namespaces;