1 package org.apache.archiva.metadata.repository.file;
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
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.HashSet;
29 import java.util.List;
32 import org.apache.archiva.metadata.model.ArtifactMetadata;
33 import org.apache.archiva.metadata.model.MailingList;
34 import org.apache.archiva.metadata.model.MetadataFacet;
35 import org.apache.archiva.metadata.model.MetadataFacetFactory;
36 import org.apache.archiva.metadata.model.ProjectMetadata;
37 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
38 import org.apache.commons.io.FileUtils;
39 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
41 public class FileMetadataRepositoryTest
42 extends PlexusInSpringTestCase
44 private FileMetadataRepository repository;
46 private static final String TEST_REPO_ID = "test";
48 private static final String TEST_PROJECT = "projectId";
50 private static final String TEST_NAMESPACE = "namespace";
52 private static final String TEST_PROJECT_VERSION = "1.0";
54 private static final String TEST_FACET_ID = "test-facet-id";
56 private static final String TEST_NAME = "test-name";
58 private static final String TEST_VALUE = "test-value";
60 private static final String UNKNOWN = "unknown";
62 private static final String OTHER_REPO = "other-repo";
69 repository = new FileMetadataRepository();
70 File directory = getTestFile( "target/test-repository" );
71 FileUtils.deleteDirectory( directory );
72 repository.setDirectory( directory );
74 repository.setMetadataFacetFactories(
75 Collections.<String, MetadataFacetFactory>singletonMap( TEST_FACET_ID, new MetadataFacetFactory()
77 public MetadataFacet createMetadataFacet()
79 return new TestMetadataFacet( "test-metadata" );
84 public void testRootNamespaceWithNoMetadataRepository()
86 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
87 assertEquals( Collections.<String>emptyList(), namespaces );
90 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
92 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
93 metadata.setId( TEST_PROJECT_VERSION );
94 MailingList mailingList = new MailingList();
95 mailingList.setName( "Foo List" );
96 mailingList.setOtherArchives( Collections.<String>emptyList() );
97 metadata.setMailingLists( Collections.singletonList( mailingList ) );
98 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
101 public void testUpdateProjectVersionMetadataWithExistingFacets()
103 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
104 metadata.setId( TEST_PROJECT_VERSION );
105 MetadataFacet facet = new TestMetadataFacet( "baz" );
106 metadata.addFacet( facet );
107 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
109 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
110 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
112 metadata = new ProjectVersionMetadata();
113 metadata.setId( TEST_PROJECT_VERSION );
114 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
116 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
117 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
118 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
119 assertEquals( "baz", testFacet.getValue() );
122 public void testGetMetadataFacet()
124 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
126 assertEquals( new TestMetadataFacet( TEST_VALUE ),
127 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
130 public void testGetMetadataFacetWhenEmpty()
132 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
135 public void testGetMetadataFacetWhenUnknownName()
137 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
139 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
142 public void testGetMetadataFacetWhenDefaultValue()
144 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( null ) );
146 assertEquals( new TestMetadataFacet( "test-metadata" ),
147 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
150 public void testGetMetadataFacetWhenUnknownFacetId()
152 repository.addMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
154 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
157 public void testGetMetadataFacets()
159 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
161 assertEquals( Collections.singletonList( TEST_NAME ),
162 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
165 public void testGetMetadataFacetsWhenEmpty()
167 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
168 assertTrue( facets.isEmpty() );
171 public void testRemoveFacets()
173 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
174 assertTrue( facets.isEmpty() );
176 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
178 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
179 assertTrue( facets.isEmpty() );
182 public void testRemoveFacetsWhenEmpty()
184 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
186 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
187 assertFalse( facets.isEmpty() );
189 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
191 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
192 assertTrue( facets.isEmpty() );
195 public void testRemoveFacetsWhenUnknown()
197 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
200 public void testGetArtifacts()
202 ArtifactMetadata artifact1 = createArtifact();
203 ArtifactMetadata artifact2 = createArtifact( "pom" );
204 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
205 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
207 assertEquals( Arrays.asList( artifact2, artifact1 ), new ArrayList<ArtifactMetadata>(
208 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
211 public void testGetArtifactVersions()
213 ArtifactMetadata artifact1 = createArtifact();
214 String version1 = "1.0-20091212.012345-1";
215 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
216 artifact1.setVersion( version1 );
217 ArtifactMetadata artifact2 = createArtifact();
218 String version2 = "1.0-20091212.123456-2";
219 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
220 artifact2.setVersion( version2 );
221 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
222 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
224 assertEquals( new HashSet<String>( Arrays.asList( version2, version1 ) ),
225 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
226 TEST_PROJECT_VERSION ) );
229 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
231 ArtifactMetadata artifact1 = createArtifact();
232 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
233 ArtifactMetadata artifact2 = createArtifact();
234 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
235 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
236 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
238 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ),
239 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
240 TEST_PROJECT_VERSION ) );
243 public void testRepositories()
245 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
246 repository.addMetadataFacet( OTHER_REPO, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
248 assertEquals( Arrays.asList( OTHER_REPO, TEST_REPO_ID ), repository.getRepositories() );
251 public void testRepositoriesWhenEmpty()
253 assertTrue( repository.getRepositories().isEmpty() );
256 public void testGetArtifactsByDateRangeOpen()
258 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
259 repository.updateProject( TEST_REPO_ID, createProject() );
260 ArtifactMetadata artifact = createArtifact();
261 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
263 assertEquals( Collections.singletonList( artifact ),
264 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
267 public void testGetArtifactsByDateRangeSparseNamespace()
269 String namespace = "org.apache.archiva";
270 repository.updateNamespace( TEST_REPO_ID, namespace );
271 repository.updateProject( TEST_REPO_ID, createProject( namespace ) );
272 ArtifactMetadata artifact = createArtifact();
273 artifact.setNamespace( namespace );
274 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
276 assertEquals( Collections.singletonList( artifact ),
277 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
280 public void testGetArtifactsByDateRangeLowerBound()
282 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
283 repository.updateProject( TEST_REPO_ID, createProject() );
284 ArtifactMetadata artifact = createArtifact();
285 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
287 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
288 assertEquals( Collections.singletonList( artifact ),
289 repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ) );
292 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
294 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
295 repository.updateProject( TEST_REPO_ID, createProject() );
296 ArtifactMetadata artifact = createArtifact();
297 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
299 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
300 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ).isEmpty() );
303 public void testGetArtifactsByDateRangeLowerAndUpperBound()
305 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
306 repository.updateProject( TEST_REPO_ID, createProject() );
307 ArtifactMetadata artifact = createArtifact();
308 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
310 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
311 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
312 assertEquals( Collections.singletonList( artifact ),
313 repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper ) );
316 public void testGetArtifactsByDateRangeUpperBound()
318 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
319 repository.updateProject( TEST_REPO_ID, createProject() );
320 ArtifactMetadata artifact = createArtifact();
321 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
323 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
324 assertEquals( Collections.singletonList( artifact ),
325 repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ) );
328 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
330 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
331 repository.updateProject( TEST_REPO_ID, createProject() );
332 ArtifactMetadata artifact = createArtifact();
333 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
335 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
336 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ).isEmpty() );
339 public void testGetNamespacesWithSparseDepth()
341 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
343 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
344 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
345 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
346 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
349 private ProjectMetadata createProject()
351 return createProject( TEST_NAMESPACE );
354 private ProjectMetadata createProject( String ns )
356 ProjectMetadata project = new ProjectMetadata();
357 project.setId( TEST_PROJECT );
358 project.setNamespace( ns );
362 private ArtifactMetadata createArtifact()
364 return createArtifact( "jar" );
367 private ArtifactMetadata createArtifact( String type )
369 ArtifactMetadata artifact = new ArtifactMetadata();
370 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
371 artifact.setWhenGathered( new Date() );
372 artifact.setNamespace( TEST_NAMESPACE );
373 artifact.setProject( TEST_PROJECT );
374 artifact.setRepositoryId( TEST_REPO_ID );
375 artifact.setFileLastModified( System.currentTimeMillis() );
376 artifact.setVersion( TEST_PROJECT_VERSION );
380 private static class TestMetadataFacet
381 implements MetadataFacet
383 private TestMetadataFacet( String value )
388 private String value;
390 public String getFacetId()
392 return TEST_FACET_ID;
395 public Map<String, String> toProperties()
399 return Collections.singletonMap( TEST_FACET_ID + ":foo", value );
403 return Collections.emptyMap();
407 public void fromProperties( Map<String, String> properties )
409 String value = properties.get( TEST_FACET_ID + ":foo" );
416 public String getValue()
422 public String toString()
424 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
428 public boolean equals( Object o )
434 if ( o == null || getClass() != o.getClass() )
439 TestMetadataFacet that = (TestMetadataFacet) o;
441 if ( value != null ? !value.equals( that.value ) : that.value != null )
450 public int hashCode()
452 return value != null ? value.hashCode() : 0;