1 package org.apache.archiva.metadata.repository;
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.CiManagement;
24 import org.apache.archiva.metadata.model.Dependency;
25 import org.apache.archiva.metadata.model.IssueManagement;
26 import org.apache.archiva.metadata.model.License;
27 import org.apache.archiva.metadata.model.MailingList;
28 import org.apache.archiva.metadata.model.MetadataFacet;
29 import org.apache.archiva.metadata.model.MetadataFacetFactory;
30 import org.apache.archiva.metadata.model.Organization;
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.model.Scm;
35 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.Date;
43 import java.util.HashMap;
44 import java.util.List;
47 public abstract class AbstractMetadataRepositoryTest
48 extends PlexusInSpringTestCase
50 protected static final String OTHER_REPO_ID = "other-repo";
52 protected MetadataRepository repository;
54 protected static final String TEST_REPO_ID = "test";
56 private static final String TEST_PROJECT = "projectId";
58 private static final String TEST_NAMESPACE = "namespace";
60 private static final String TEST_PROJECT_VERSION = "1.0";
62 private static final String TEST_FACET_ID = "test-facet-id";
64 private static final String TEST_NAME = "test/name";
66 private static final String TEST_VALUE = "test-value";
68 private static final String UNKNOWN = "unknown";
70 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
72 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
74 private static final String TEST_METADATA_VALUE = "test-metadata";
76 protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
78 Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
79 factories.put( TEST_FACET_ID, new MetadataFacetFactory()
81 public MetadataFacet createMetadataFacet()
83 return new TestMetadataFacet( TEST_METADATA_VALUE );
86 public MetadataFacet createMetadataFacet( String repositoryId, String name )
88 return new TestMetadataFacet( TEST_METADATA_VALUE );
92 // add to ensure we don't accidentally create an empty facet ID.
93 factories.put( "", new MetadataFacetFactory()
95 public MetadataFacet createMetadataFacet()
97 return new TestMetadataFacet( "", TEST_VALUE );
100 public MetadataFacet createMetadataFacet( String repositoryId, String name )
102 return new TestMetadataFacet( "", TEST_VALUE );
108 public void testRootNamespaceWithNoMetadataRepository()
111 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
112 assertEquals( Collections.<String>emptyList(), namespaces );
115 public void testGetNamespaceOnly()
118 assertEquals( Collections.emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
120 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
122 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
125 public void testGetProjectOnly()
128 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
129 assertEquals( Collections.emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
131 ProjectMetadata project = new ProjectMetadata();
132 project.setId( TEST_PROJECT );
133 project.setNamespace( TEST_NAMESPACE );
135 repository.updateProject( TEST_REPO_ID, project );
137 project = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
138 assertEquals( TEST_PROJECT, project.getId() );
139 assertEquals( TEST_NAMESPACE, project.getNamespace() );
141 // test that namespace is also constructed
142 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
145 public void testGetProjectVersionOnly()
148 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
149 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
150 assertEquals( Collections.<String>emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
152 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
153 metadata.setId( TEST_PROJECT_VERSION );
155 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
157 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
158 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
160 // test that namespace and project is also constructed
161 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
162 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
163 assertEquals( TEST_PROJECT, projectMetadata.getId() );
164 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
167 public void testGetArtifactOnly()
170 assertEquals( Collections.<ArtifactMetadata>emptyList(), new ArrayList<ArtifactMetadata>(
171 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
172 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
173 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
174 assertEquals( Collections.<String>emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
176 ArtifactMetadata metadata = createArtifact();
178 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
180 Collection<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
181 TEST_PROJECT_VERSION );
182 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
184 // test that namespace, project and project version is also constructed
185 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
187 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
188 assertEquals( TEST_PROJECT, projectMetadata.getId() );
189 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
191 ProjectVersionMetadata projectVersionMetadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE,
193 TEST_PROJECT_VERSION );
194 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
197 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
200 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
201 metadata.setId( TEST_PROJECT_VERSION );
202 MailingList mailingList = new MailingList();
203 mailingList.setName( "Foo List" );
204 mailingList.setOtherArchives( Collections.<String>emptyList() );
205 metadata.setMailingLists( Collections.singletonList( mailingList ) );
206 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
208 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
209 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
210 assertEquals( 1, metadata.getMailingLists().size() );
211 mailingList = metadata.getMailingLists().get( 0 );
212 assertEquals( "Foo List", mailingList.getName() );
213 assertEquals( Collections.<String>emptyList(), mailingList.getOtherArchives() );
216 public void testUpdateProjectVersionMetadataWithAllElements()
219 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
220 metadata.setId( TEST_PROJECT_VERSION );
222 metadata.setName( "project name" );
223 metadata.setDescription( "project description" );
225 MailingList mailingList = new MailingList();
226 mailingList.setName( "Foo List" );
227 mailingList.setOtherArchives( Collections.singletonList( "other archive" ) );
228 metadata.setMailingLists( Collections.singletonList( mailingList ) );
231 scm.setConnection( "connection" );
232 scm.setDeveloperConnection( "dev conn" );
234 metadata.setScm( scm );
236 CiManagement ci = new CiManagement();
237 ci.setSystem( "system" );
238 ci.setUrl( "ci url" );
239 metadata.setCiManagement( ci );
241 IssueManagement tracker = new IssueManagement();
242 tracker.setSystem( "system" );
243 tracker.setUrl( "issue tracker url" );
244 metadata.setIssueManagement( tracker );
246 Organization org = new Organization();
247 org.setName( "org name" );
249 metadata.setOrganization( org );
251 License l = new License();
252 l.setName( "license name" );
253 l.setUrl( "license url" );
254 metadata.addLicense( l );
256 Dependency d = new Dependency();
257 d.setArtifactId( "artifactId" );
258 d.setClassifier( "classifier" );
259 d.setGroupId( "groupId" );
260 d.setScope( "scope" );
261 d.setSystemPath( "system path" );
263 d.setVersion( "version" );
264 metadata.addDependency( d );
266 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
268 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
269 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
270 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
271 assertEquals( "project name", metadata.getName() );
272 assertEquals( "project description", metadata.getDescription() );
274 assertEquals( 1, metadata.getMailingLists().size() );
275 mailingList = metadata.getMailingLists().get( 0 );
276 assertEquals( "Foo List", mailingList.getName() );
277 assertEquals( Collections.singletonList( "other archive" ), mailingList.getOtherArchives() );
279 assertEquals( "connection", metadata.getScm().getConnection() );
280 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
281 assertEquals( "url", metadata.getScm().getUrl() );
283 assertEquals( "system", metadata.getCiManagement().getSystem() );
284 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
286 assertEquals( "system", metadata.getIssueManagement().getSystem() );
287 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
289 assertEquals( "org name", metadata.getOrganization().getName() );
290 assertEquals( "url", metadata.getOrganization().getUrl() );
292 assertEquals( 1, metadata.getLicenses().size() );
293 l = metadata.getLicenses().get( 0 );
294 assertEquals( "license name", l.getName() );
295 assertEquals( "license url", l.getUrl() );
297 assertEquals( 1, metadata.getDependencies().size() );
298 d = metadata.getDependencies().get( 0 );
299 assertEquals( "artifactId", d.getArtifactId() );
300 assertEquals( "classifier", d.getClassifier() );
301 assertEquals( "groupId", d.getGroupId() );
302 assertEquals( "scope", d.getScope() );
303 assertEquals( "system path", d.getSystemPath() );
304 assertEquals( "type", d.getType() );
305 assertEquals( "version", d.getVersion() );
308 public void testUpdateProjectReference()
311 ProjectVersionReference reference = new ProjectVersionReference();
312 reference.setNamespace( "another.namespace" );
313 reference.setProjectId( "another-project-id" );
314 reference.setProjectVersion( "1.1" );
315 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
317 repository.updateProjectReference( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION,
320 Collection<ProjectVersionReference> references = repository.getProjectReferences( TEST_REPO_ID, TEST_NAMESPACE,
322 TEST_PROJECT_VERSION );
323 assertEquals( 1, references.size() );
324 reference = references.iterator().next();
325 assertEquals( "another.namespace", reference.getNamespace() );
326 assertEquals( "another-project-id", reference.getProjectId() );
327 assertEquals( "1.1", reference.getProjectVersion() );
328 assertEquals( ProjectVersionReference.ReferenceType.DEPENDENCY, reference.getReferenceType() );
331 public void testGetRepositories()
334 // currently set up this way so the behaviour of both the test and the mock config return the same repository
335 // set as the File implementation just uses the config rather than the content
336 repository.updateNamespace( TEST_REPO_ID, "namespace" );
337 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
339 assertEquals( Arrays.asList( TEST_REPO_ID, OTHER_REPO_ID ), new ArrayList<String>(
340 repository.getRepositories() ) );
343 public void testUpdateProjectVersionMetadataIncomplete()
346 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
347 metadata.setId( TEST_PROJECT_VERSION );
348 metadata.setIncomplete( true );
349 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
351 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
352 assertEquals( true, metadata.isIncomplete() );
353 assertNull( metadata.getCiManagement() );
354 assertNull( metadata.getScm() );
355 assertNull( metadata.getIssueManagement() );
356 assertNull( metadata.getOrganization() );
357 assertNull( metadata.getDescription() );
358 assertNull( metadata.getName() );
359 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
360 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
361 assertTrue( metadata.getMailingLists().isEmpty() );
362 assertTrue( metadata.getLicenses().isEmpty() );
363 assertTrue( metadata.getDependencies().isEmpty() );
366 public void testUpdateProjectVersionMetadataWithExistingFacets()
369 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
370 metadata.setId( TEST_PROJECT_VERSION );
371 MetadataFacet facet = new TestMetadataFacet( "baz" );
372 metadata.addFacet( facet );
373 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
375 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
376 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
378 metadata = new ProjectVersionMetadata();
379 metadata.setId( TEST_PROJECT_VERSION );
380 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
382 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
383 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
384 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
385 assertEquals( "baz", testFacet.getValue() );
388 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
391 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
392 metadata.setId( TEST_PROJECT_VERSION );
393 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
395 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
396 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
398 metadata = new ProjectVersionMetadata();
399 metadata.setId( TEST_PROJECT_VERSION );
400 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
402 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
403 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
406 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
409 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
410 metadata.setId( TEST_PROJECT_VERSION );
412 Map<String, String> additionalProps = new HashMap<String, String>();
413 additionalProps.put( "deleteKey", "deleteValue" );
415 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
416 metadata.addFacet( facet );
417 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
419 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
420 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
422 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
423 Map<String, String> facetProperties = testFacet.toProperties();
425 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
427 facetProperties.remove( "deleteKey" );
429 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
430 metadata.addFacet( newTestFacet );
432 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
434 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
435 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
436 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
437 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
440 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
443 ArtifactMetadata metadata = createArtifact();
445 Map<String, String> additionalProps = new HashMap<String, String>();
446 additionalProps.put( "deleteKey", "deleteValue" );
448 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
449 metadata.addFacet( facet );
450 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
452 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
453 TEST_PROJECT_VERSION ).iterator().next();
454 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
456 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
457 Map<String, String> facetProperties = testFacet.toProperties();
459 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
461 facetProperties.remove( "deleteKey" );
463 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
464 metadata.addFacet( newTestFacet );
466 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
468 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
469 TEST_PROJECT_VERSION ).iterator().next();
470 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
471 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
472 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
475 public void testUpdateArtifactMetadataWithExistingFacets()
478 ArtifactMetadata metadata = createArtifact();
479 MetadataFacet facet = new TestMetadataFacet( "baz" );
480 metadata.addFacet( facet );
481 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
483 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
484 TEST_PROJECT_VERSION ).iterator().next();
485 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
487 metadata = createArtifact();
488 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
490 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
491 TEST_PROJECT_VERSION ).iterator().next();
492 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
493 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
494 assertEquals( "baz", testFacet.getValue() );
497 public void testUpdateArtifactMetadataWithNoExistingFacets()
500 ArtifactMetadata metadata = createArtifact();
501 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
503 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
504 TEST_PROJECT_VERSION ).iterator().next();
505 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
507 metadata = createArtifact();
508 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
510 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
511 TEST_PROJECT_VERSION ).iterator().next();
512 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
515 public void testGetMetadataFacet()
518 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
520 assertEquals( new TestMetadataFacet( TEST_VALUE ), repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID,
524 public void testGetMetadataFacetWhenEmpty()
528 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
531 public void testGetMetadataFacetWhenUnknownName()
535 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
537 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
540 public void testGetMetadataFacetWhenDefaultValue()
544 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
546 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), repository.getMetadataFacet( TEST_REPO_ID,
551 public void testGetMetadataFacetWhenUnknownFacetId()
555 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
558 public void testGetMetadataFacets()
562 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
564 assertEquals( Collections.singletonList( TEST_NAME ), repository.getMetadataFacets( TEST_REPO_ID,
568 public void testGetMetadataFacetsWhenEmpty()
572 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
573 assertTrue( facets.isEmpty() );
576 public void testRemoveFacets()
580 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
582 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
583 assertFalse( facets.isEmpty() );
585 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
587 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
588 assertTrue( facets.isEmpty() );
591 public void testRemoveFacetsWhenEmpty()
595 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
596 assertTrue( facets.isEmpty() );
598 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
600 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
601 assertTrue( facets.isEmpty() );
604 public void testRemoveFacetsWhenUnknown()
608 // testing no exception
609 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
612 public void testRemoveFacetWhenUnknown()
616 // testing no exception
617 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
620 public void testRemoveFacet()
624 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
625 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
627 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
628 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
629 assertFalse( facets.isEmpty() );
631 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
633 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
634 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
635 assertTrue( facets.isEmpty() );
638 public void testRemoveFacetWhenEmpty()
642 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
643 assertTrue( facets.isEmpty() );
644 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
646 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
648 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
649 assertTrue( facets.isEmpty() );
650 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
653 public void testGetArtifacts()
656 ArtifactMetadata artifact1 = createArtifact();
657 ArtifactMetadata artifact2 = createArtifact( "pom" );
658 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
659 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
661 Collection<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
662 TEST_PROJECT_VERSION );
663 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
664 Collections.sort( actual, new Comparator<ArtifactMetadata>()
666 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
668 return o1.getId().compareTo( o2.getId() );
671 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
674 public void testGetArtifactVersions()
677 ArtifactMetadata artifact1 = createArtifact();
678 String version1 = "1.0-20091212.012345-1";
679 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
680 artifact1.setVersion( version1 );
681 ArtifactMetadata artifact2 = createArtifact();
682 String version2 = "1.0-20091212.123456-2";
683 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
684 artifact2.setVersion( version2 );
685 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
686 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
688 List<String> versions = new ArrayList<String>( repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE,
690 TEST_PROJECT_VERSION ) );
691 Collections.sort( versions );
692 assertEquals( Arrays.asList( version1, version2 ), versions );
695 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
698 ArtifactMetadata artifact1 = createArtifact();
699 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
700 ArtifactMetadata artifact2 = createArtifact();
701 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
702 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
703 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
705 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ), repository.getArtifactVersions( TEST_REPO_ID,
708 TEST_PROJECT_VERSION ) );
711 public void testGetArtifactsByDateRangeOpen()
714 ArtifactMetadata artifact = createArtifact();
715 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
717 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
721 public void testGetArtifactsByDateRangeSparseNamespace()
724 String namespace = "org.apache.archiva";
725 ArtifactMetadata artifact = createArtifact();
726 artifact.setNamespace( namespace );
727 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
729 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
733 public void testGetArtifactsByDateRangeLowerBound()
736 ArtifactMetadata artifact = createArtifact();
737 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
739 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
740 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, date,
744 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
747 ArtifactMetadata artifact = createArtifact();
748 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
750 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
751 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ).isEmpty() );
754 public void testGetArtifactsByDateRangeLowerAndUpperBound()
757 ArtifactMetadata artifact = createArtifact();
758 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
760 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
761 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
762 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, lower,
766 public void testGetArtifactsByDateRangeUpperBound()
769 ArtifactMetadata artifact = createArtifact();
770 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
772 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
773 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
777 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
780 ArtifactMetadata artifact = createArtifact();
781 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
783 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
784 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ).isEmpty() );
787 public void testGetArtifactsByRepoId()
790 ArtifactMetadata artifact = createArtifact();
791 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
792 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
795 public void testGetNamespacesWithSparseDepth()
798 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
800 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
801 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
802 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
803 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
806 public void testGetArtifactsByChecksumSingleResultMd5()
809 ArtifactMetadata artifact = createArtifact();
810 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
812 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
816 public void testGetArtifactsByChecksumSingleResultSha1()
819 ArtifactMetadata artifact = createArtifact();
820 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
822 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
826 public void testGetArtifactsByChecksumDeepNamespace()
829 ArtifactMetadata artifact = createArtifact();
830 String namespace = "multi.level.ns";
831 artifact.setNamespace( namespace );
832 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
834 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
838 public void testGetArtifactsByChecksumMultipleResult()
841 ArtifactMetadata artifact1 = createArtifact();
842 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
844 String newProjectId = "another-project";
845 ArtifactMetadata artifact2 = createArtifact();
846 artifact2.setProject( newProjectId );
847 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
849 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum(
850 TEST_REPO_ID, TEST_SHA1 ) );
851 Collections.sort( artifacts, new ArtifactMetadataComparator() );
852 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
855 public void testGetArtifactsByChecksumNoResult()
858 ArtifactMetadata artifact = createArtifact();
859 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
861 assertEquals( Collections.<ArtifactMetadata>emptyList(), repository.getArtifactsByChecksum( TEST_REPO_ID,
862 "not a checksum" ) );
865 public void testDeleteArtifact()
868 ArtifactMetadata artifact = createArtifact();
869 artifact.addFacet( new TestMetadataFacet( "value" ) );
871 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
873 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( repository.getArtifacts(
874 TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
876 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
878 assertTrue( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
879 TEST_PROJECT_VERSION ).isEmpty() );
882 public void testDeleteRepository()
885 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
887 ProjectMetadata project1 = new ProjectMetadata();
888 project1.setNamespace( TEST_NAMESPACE );
889 project1.setId( "project1" );
890 repository.updateProject( TEST_REPO_ID, project1 );
891 ProjectMetadata project2 = new ProjectMetadata();
892 project2.setNamespace( TEST_NAMESPACE );
893 project2.setId( "project2" );
894 repository.updateProject( TEST_REPO_ID, project2 );
896 ArtifactMetadata artifact1 = createArtifact();
897 artifact1.setProject( "project1" );
898 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
899 ArtifactMetadata artifact2 = createArtifact();
900 artifact2.setProject( "project2" );
901 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
903 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
904 Collections.sort( expected, new ArtifactMetadataComparator() );
906 List<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange(
907 TEST_REPO_ID, null, null ) );
908 Collections.sort( actual, new ArtifactMetadataComparator() );
910 assertEquals( expected, actual );
912 repository.removeRepository( TEST_REPO_ID );
914 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
915 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
918 private static ProjectMetadata createProject()
920 return createProject( TEST_NAMESPACE );
923 private static ProjectMetadata createProject( String ns )
925 ProjectMetadata project = new ProjectMetadata();
926 project.setId( TEST_PROJECT );
927 project.setNamespace( ns );
931 private static ArtifactMetadata createArtifact()
933 return createArtifact( "jar" );
936 private static ArtifactMetadata createArtifact( String type )
938 ArtifactMetadata artifact = new ArtifactMetadata();
939 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
940 artifact.setWhenGathered( new Date() );
941 artifact.setNamespace( TEST_NAMESPACE );
942 artifact.setProject( TEST_PROJECT );
943 artifact.setRepositoryId( TEST_REPO_ID );
944 artifact.setFileLastModified( System.currentTimeMillis() );
945 artifact.setVersion( TEST_PROJECT_VERSION );
946 artifact.setProjectVersion( TEST_PROJECT_VERSION );
947 artifact.setMd5( TEST_MD5 );
948 artifact.setSha1( TEST_SHA1 );
952 private static class ArtifactMetadataComparator
953 implements Comparator<ArtifactMetadata>
955 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
957 return a.getProject().compareTo( b.getProject() );
961 private static class TestMetadataFacet
962 implements MetadataFacet
964 private String testFacetId;
966 private Map<String, String> additionalProps;
968 private String value;
970 private TestMetadataFacet( String value )
973 testFacetId = TEST_FACET_ID;
976 private TestMetadataFacet( String facetId, String value )
979 testFacetId = facetId;
982 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
984 this( facetId, value );
985 this.additionalProps = additionalProps;
988 public String getFacetId()
993 public String getName()
998 public Map<String, String> toProperties()
1000 if ( value != null )
1002 if ( additionalProps == null )
1004 return Collections.singletonMap( "foo", value );
1008 Map<String, String> props = new HashMap<String, String>();
1009 props.put( "foo", value );
1011 for ( String key : additionalProps.keySet() )
1013 props.put( key, additionalProps.get( key ) );
1020 return Collections.emptyMap();
1024 public void fromProperties( Map<String, String> properties )
1026 String value = properties.get( "foo" );
1027 if ( value != null )
1032 properties.remove( "foo" );
1034 if ( additionalProps == null )
1036 additionalProps = new HashMap<String, String>();
1039 for ( String key : properties.keySet() )
1041 additionalProps.put( key, properties.get( key ) );
1045 public String getValue()
1051 public String toString()
1053 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1057 public boolean equals( Object o )
1063 if ( o == null || getClass() != o.getClass() )
1068 TestMetadataFacet that = (TestMetadataFacet) o;
1070 if ( value != null ? !value.equals( that.value ) : that.value != null )
1079 public int hashCode()
1081 return value != null ? value.hashCode() : 0;