You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestMetadataResolver.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package org.apache.archiva.web.webtest.memory;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import org.apache.archiva.metadata.model.ArtifactMetadata;
  20. import org.apache.archiva.metadata.model.ProjectVersionMetadata;
  21. import org.apache.archiva.metadata.model.ProjectVersionReference;
  22. import org.apache.archiva.metadata.repository.MetadataResolver;
  23. import org.apache.archiva.metadata.repository.RepositorySession;
  24. import java.util.Collection;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.LinkedHashSet;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.Set;
  31. public class TestMetadataResolver
  32. implements MetadataResolver
  33. {
  34. private Map<String, ProjectVersionMetadata> projectVersions = new HashMap<>();
  35. private Map<String, List<ArtifactMetadata>> artifacts = new HashMap<>();
  36. private Map<String, List<ProjectVersionReference>> references =
  37. new HashMap<String, List<ProjectVersionReference>>();
  38. private Map<String, List<String>> namespaces = new HashMap<>();
  39. private Map<String, Collection<String>> projectsInNamespace = new HashMap<>();
  40. private Map<String, Collection<String>> versionsInProject = new HashMap<>();
  41. @Override
  42. public ProjectVersionMetadata resolveProjectVersion( RepositorySession repositorySession, String repoId,
  43. String namespace, String projectId, String projectVersion )
  44. {
  45. return projectVersions.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
  46. }
  47. @Override
  48. public Collection<ProjectVersionReference> resolveProjectReferences( RepositorySession repositorySession,
  49. String repoId, String namespace,
  50. String projectId, String projectVersion )
  51. {
  52. Collection<ProjectVersionReference> projectVersionReferences =
  53. references.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
  54. return projectVersionReferences;
  55. }
  56. @Override
  57. public Collection<String> resolveRootNamespaces( RepositorySession repositorySession, String repoId )
  58. {
  59. return resolveNamespaces( repositorySession, repoId, null );
  60. }
  61. @Override
  62. public Collection<String> resolveNamespaces( RepositorySession repositorySession, String repoId,
  63. String baseNamespace )
  64. {
  65. Set<String> namespaces = new LinkedHashSet<String>();
  66. int fromIndex = baseNamespace != null ? baseNamespace.length() + 1 : 0;
  67. for ( String namespace : this.namespaces.get( repoId ) )
  68. {
  69. if ( baseNamespace == null || namespace.startsWith( baseNamespace + "." ) )
  70. {
  71. int i = namespace.indexOf( '.', fromIndex );
  72. if ( i >= 0 )
  73. {
  74. namespaces.add( namespace.substring( fromIndex, i ) );
  75. }
  76. else
  77. {
  78. namespaces.add( namespace.substring( fromIndex ) );
  79. }
  80. }
  81. }
  82. return namespaces;
  83. }
  84. @Override
  85. public Collection<String> resolveProjects( RepositorySession repositorySession, String repoId, String namespace )
  86. {
  87. Collection<String> list = projectsInNamespace.get( namespace );
  88. return list != null ? list : Collections.<String>emptyList();
  89. }
  90. @Override
  91. public Collection<String> resolveProjectVersions( RepositorySession repositorySession, String repoId,
  92. String namespace, String projectId )
  93. {
  94. Collection<String> list = versionsInProject.get( namespace + ":" + projectId );
  95. return list != null ? list : Collections.<String>emptyList();
  96. }
  97. @Override
  98. public Collection<ArtifactMetadata> resolveArtifacts( RepositorySession repositorySession, String repoId,
  99. String namespace, String projectId, String projectVersion )
  100. {
  101. List<ArtifactMetadata> artifacts =
  102. this.artifacts.get( createMapKey( repoId, namespace, projectId, projectVersion ) );
  103. return ( artifacts != null ? artifacts : Collections.<ArtifactMetadata>emptyList() );
  104. }
  105. public void setProjectVersion( String repoId, String namespace, String projectId,
  106. ProjectVersionMetadata versionMetadata )
  107. {
  108. projectVersions.put( createMapKey( repoId, namespace, projectId, versionMetadata.getId() ), versionMetadata );
  109. Collection<String> projects = projectsInNamespace.get( namespace );
  110. if ( projects == null )
  111. {
  112. projects = new LinkedHashSet<String>();
  113. projectsInNamespace.put( namespace, projects );
  114. }
  115. projects.add( projectId );
  116. String key = namespace + ":" + projectId;
  117. Collection<String> versions = versionsInProject.get( key );
  118. if ( versions == null )
  119. {
  120. versions = new LinkedHashSet<String>();
  121. versionsInProject.put( key, versions );
  122. }
  123. versions.add( versionMetadata.getId() );
  124. }
  125. public void setArtifacts( String repoId, String namespace, String projectId, String projectVersion,
  126. List<ArtifactMetadata> artifacts )
  127. {
  128. this.artifacts.put( createMapKey( repoId, namespace, projectId, projectVersion ), artifacts );
  129. }
  130. private String createMapKey( String repoId, String namespace, String projectId, String projectVersion )
  131. {
  132. return repoId + ":" + namespace + ":" + projectId + ":" + projectVersion;
  133. }
  134. public void setProjectReferences( String repoId, String namespace, String projectId, String projectVersion,
  135. List<ProjectVersionReference> references )
  136. {
  137. this.references.put( createMapKey( repoId, namespace, projectId, projectVersion ), references );
  138. }
  139. public void setNamespaces( String repoId, List<String> namespaces )
  140. {
  141. this.namespaces.put( repoId, namespaces );
  142. }
  143. }