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;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.LinkedHashSet;
31 import java.util.List;
35 public class TestMetadataResolver
36 implements MetadataResolver
38 private Map<String, ProjectVersionMetadata> projectVersions = new HashMap<String, ProjectVersionMetadata>();
40 private Map<String, List<ArtifactMetadata>> artifacts = new HashMap<String, List<ArtifactMetadata>>();
42 private Map<String, List<ProjectVersionReference>> references =
43 new HashMap<String, List<ProjectVersionReference>>();
45 private Map<String, List<String>> namespaces = new HashMap<String, List<String>>();
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 ProjectVersionMetadata resolveProjectVersion( String repoId, String namespace, String projectId,
52 String projectVersion )
54 return projectVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
57 public Collection<ProjectVersionReference> resolveProjectReferences( String repoId, String namespace,
58 String projectId, String projectVersion )
60 return references.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
63 public Collection<String> resolveRootNamespaces( String repoId )
65 return resolveNamespaces( repoId, null );
68 public Collection<String> resolveNamespaces( String repoId, String baseNamespace )
70 Set<String> namespaces = new LinkedHashSet<String>();
71 int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
72 for ( String namespace : this.namespaces.get( repoId ) )
74 if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
76 int i = namespace.indexOf( '.', fromIndex );
79 namespaces.add( namespace.substring( fromIndex, i ) );
83 namespaces.add( namespace.substring( fromIndex ) );
90 public Collection<String> resolveProjects( String repoId, String namespace )
92 Collection<String> list = projectsInNamespace.get( namespace );
93 return list != null ? list : Collections.<String>emptyList();
96 public Collection<String> resolveProjectVersions( String repoId, String namespace, String projectId )
98 Collection<String> list = versionsInProject.get( namespace + ":" + projectId );
99 return list != null ? list : Collections.<String>emptyList();
102 public Collection<ArtifactMetadata> resolveArtifacts( String repoId, String namespace, String projectId,
103 String projectVersion )
105 List<ArtifactMetadata> artifacts = this.artifacts.get( createMapKey( repoId, namespace, projectId,
107 return ( artifacts != null ? artifacts : Collections.<ArtifactMetadata>emptyList() );
110 public void setProjectVersion( String repoId, String namespace, String projectId,
111 ProjectVersionMetadata versionMetadata )
113 projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
115 Collection<String> projects = projectsInNamespace.get( namespace );
116 if ( projects == null )
118 projects = new LinkedHashSet<String>();
119 projectsInNamespace.put( namespace, projects );
121 projects.add( projectId );
123 String key = namespace + ":" + projectId;
124 Collection<String> versions = versionsInProject.get( key );
125 if ( versions == null )
127 versions = new LinkedHashSet<String>();
128 versionsInProject.put( key, versions );
130 versions.add( versionMetadata.getId() );
133 public void setArtifacts( String repoId, String namespace, String projectId, String projectVersion,
134 List<ArtifactMetadata> artifacts )
136 this.artifacts.put( createMapKey( repoId, namespace, projectId, projectVersion ), artifacts );
139 private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
141 return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
144 public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
145 List<ProjectVersionReference> references )
147 this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
150 public void setNamespaces( String repoId, List<String> namespaces )
152 this.namespaces.put( repoId, namespaces );