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 MetadataRepository repository;
52 protected static final String TEST_REPO_ID = "test";
54 private static final String TEST_PROJECT = "projectId";
56 private static final String TEST_NAMESPACE = "namespace";
58 private static final String TEST_PROJECT_VERSION = "1.0";
60 private static final String TEST_FACET_ID = "test-facet-id";
62 private static final String TEST_NAME = "test/name";
64 private static final String TEST_VALUE = "test-value";
66 private static final String UNKNOWN = "unknown";
68 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
70 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
72 private static final String TEST_METADATA_VALUE = "test-metadata";
74 protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
76 Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
77 factories.put( TEST_FACET_ID, new MetadataFacetFactory()
79 public MetadataFacet createMetadataFacet()
81 return new TestMetadataFacet( TEST_METADATA_VALUE );
84 public MetadataFacet createMetadataFacet( String repositoryId, String name )
86 return new TestMetadataFacet( TEST_METADATA_VALUE );
90 // add to ensure we don't accidentally create an empty facet ID.
91 factories.put( "", new MetadataFacetFactory()
93 public MetadataFacet createMetadataFacet()
95 return new TestMetadataFacet( "", TEST_VALUE );
98 public MetadataFacet createMetadataFacet( String repositoryId, String name )
100 return new TestMetadataFacet( "", TEST_VALUE );
106 public void testRootNamespaceWithNoMetadataRepository()
108 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
109 assertEquals( Collections.<String>emptyList(), namespaces );
112 public void testGetNamespaceOnly()
114 assertEquals( Collections.emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
116 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
118 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
121 public void testGetProjectOnly()
123 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
124 assertEquals( Collections.emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
126 ProjectMetadata project = new ProjectMetadata();
127 project.setId( TEST_PROJECT );
128 project.setNamespace( TEST_NAMESPACE );
130 repository.updateProject( TEST_REPO_ID, project );
132 project = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
133 assertEquals( TEST_PROJECT, project.getId() );
134 assertEquals( TEST_NAMESPACE, project.getNamespace() );
136 // test that namespace is also constructed
137 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
140 public void testGetProjectVersionOnly()
141 throws MetadataResolutionException
143 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
144 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
145 assertEquals( Collections.<String>emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
147 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
148 metadata.setId( TEST_PROJECT_VERSION );
150 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
152 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
153 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
155 // test that namespace and project is also constructed
156 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
157 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
158 assertEquals( TEST_PROJECT, projectMetadata.getId() );
159 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
162 public void testGetArtifactOnly()
163 throws MetadataResolutionException
165 assertEquals( Collections.<ArtifactMetadata>emptyList(),
166 new ArrayList<ArtifactMetadata>(
167 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
168 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
169 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
170 assertEquals( Collections.<String>emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
172 ArtifactMetadata metadata = createArtifact();
174 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
176 Collection<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
177 TEST_PROJECT_VERSION );
178 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
180 // test that namespace, project and project version is also constructed
181 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
183 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
184 assertEquals( TEST_PROJECT, projectMetadata.getId() );
185 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
187 ProjectVersionMetadata projectVersionMetadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
188 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
191 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
192 throws MetadataResolutionException
194 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
195 metadata.setId( TEST_PROJECT_VERSION );
196 MailingList mailingList = new MailingList();
197 mailingList.setName( "Foo List" );
198 mailingList.setOtherArchives( Collections.<String>emptyList() );
199 metadata.setMailingLists( Collections.singletonList( mailingList ) );
200 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
202 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
203 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
204 assertEquals( 1, metadata.getMailingLists().size() );
205 mailingList = metadata.getMailingLists().get( 0 );
206 assertEquals( "Foo List", mailingList.getName() );
207 assertEquals( Collections.<String>emptyList(), mailingList.getOtherArchives() );
210 public void testUpdateProjectVersionMetadataWithAllElements()
211 throws MetadataResolutionException
213 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
214 metadata.setId( TEST_PROJECT_VERSION );
216 metadata.setName( "project name" );
217 metadata.setDescription( "project description" );
219 MailingList mailingList = new MailingList();
220 mailingList.setName( "Foo List" );
221 mailingList.setOtherArchives( Collections.singletonList( "other archive" ) );
222 metadata.setMailingLists( Collections.singletonList( mailingList ) );
225 scm.setConnection( "connection" );
226 scm.setDeveloperConnection( "dev conn" );
228 metadata.setScm( scm );
230 CiManagement ci = new CiManagement();
231 ci.setSystem( "system" );
232 ci.setUrl( "ci url" );
233 metadata.setCiManagement( ci );
235 IssueManagement tracker = new IssueManagement();
236 tracker.setSystem( "system" );
237 tracker.setUrl( "issue tracker url" );
238 metadata.setIssueManagement( tracker );
240 Organization org = new Organization();
241 org.setName( "org name" );
243 metadata.setOrganization( org );
245 License l = new License();
246 l.setName( "license name" );
247 l.setUrl( "license url" );
248 metadata.addLicense( l );
250 Dependency d = new Dependency();
251 d.setArtifactId( "artifactId" );
252 d.setClassifier( "classifier" );
253 d.setGroupId( "groupId" );
254 d.setScope( "scope" );
255 d.setSystemPath( "system path" );
257 d.setVersion( "version" );
258 metadata.addDependency( d );
260 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
262 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
263 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
264 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
265 assertEquals( "project name", metadata.getName() );
266 assertEquals( "project description", metadata.getDescription() );
268 assertEquals( 1, metadata.getMailingLists().size() );
269 mailingList = metadata.getMailingLists().get( 0 );
270 assertEquals( "Foo List", mailingList.getName() );
271 assertEquals( Collections.singletonList( "other archive" ), mailingList.getOtherArchives() );
273 assertEquals( "connection", metadata.getScm().getConnection() );
274 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
275 assertEquals( "url", metadata.getScm().getUrl() );
277 assertEquals( "system", metadata.getCiManagement().getSystem() );
278 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
280 assertEquals( "system", metadata.getIssueManagement().getSystem() );
281 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
283 assertEquals( "org name", metadata.getOrganization().getName() );
284 assertEquals( "url", metadata.getOrganization().getUrl() );
286 assertEquals( 1, metadata.getLicenses().size() );
287 l = metadata.getLicenses().get( 0 );
288 assertEquals( "license name", l.getName() );
289 assertEquals( "license url", l.getUrl() );
291 assertEquals( 1, metadata.getDependencies().size() );
292 d = metadata.getDependencies().get( 0 );
293 assertEquals( "artifactId", d.getArtifactId() );
294 assertEquals( "classifier", d.getClassifier() );
295 assertEquals( "groupId", d.getGroupId() );
296 assertEquals( "scope", d.getScope() );
297 assertEquals( "system path", d.getSystemPath() );
298 assertEquals( "type", d.getType() );
299 assertEquals( "version", d.getVersion() );
302 public void testUpdateProjectReference()
304 ProjectVersionReference reference = new ProjectVersionReference();
305 reference.setNamespace( "another.namespace" );
306 reference.setProjectId( "another-project-id" );
307 reference.setProjectVersion( "1.1" );
308 reference.setReferenceType( ProjectVersionReference.ReferenceType.DEPENDENCY );
310 repository.updateProjectReference( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, reference );
312 Collection<ProjectVersionReference> references = repository.getProjectReferences( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
313 assertEquals( 1, references.size() );
314 reference = references.iterator().next();
315 assertEquals( "another.namespace", reference.getNamespace() );
316 assertEquals( "another-project-id", reference.getProjectId() );
317 assertEquals( "1.1", reference.getProjectVersion() );
318 assertEquals( ProjectVersionReference.ReferenceType.DEPENDENCY, reference.getReferenceType() );
321 public void testGetRepositories()
323 // currently set up this way so the behaviour of both the test and the mock config return the same repository
324 // set as the File implementation just uses the config rather than the content
325 repository.updateNamespace( TEST_REPO_ID, "namespace" );
326 repository.updateNamespace( "other-repo", "namespace" );
328 assertEquals( Arrays.asList( TEST_REPO_ID, "other-repo" ), new ArrayList<String>( repository.getRepositories() ) );
331 public void testUpdateProjectVersionMetadataIncomplete()
332 throws MetadataResolutionException
334 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
335 metadata.setId( TEST_PROJECT_VERSION );
336 metadata.setIncomplete( true );
337 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
339 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
340 assertEquals( true, metadata.isIncomplete() );
341 assertNull( metadata.getCiManagement() );
342 assertNull( metadata.getScm() );
343 assertNull( metadata.getIssueManagement() );
344 assertNull( metadata.getOrganization() );
345 assertNull( metadata.getDescription() );
346 assertNull( metadata.getName() );
347 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
348 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
349 assertTrue( metadata.getMailingLists().isEmpty() );
350 assertTrue( metadata.getLicenses().isEmpty() );
351 assertTrue( metadata.getDependencies().isEmpty() );
354 public void testUpdateProjectVersionMetadataWithExistingFacets()
355 throws MetadataResolutionException
357 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
358 metadata.setId( TEST_PROJECT_VERSION );
359 MetadataFacet facet = new TestMetadataFacet( "baz" );
360 metadata.addFacet( facet );
361 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
363 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
364 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
366 metadata = new ProjectVersionMetadata();
367 metadata.setId( TEST_PROJECT_VERSION );
368 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
370 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
371 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
372 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
373 assertEquals( "baz", testFacet.getValue() );
376 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
377 throws MetadataResolutionException
379 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
380 metadata.setId( TEST_PROJECT_VERSION );
381 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
383 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
384 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
386 metadata = new ProjectVersionMetadata();
387 metadata.setId( TEST_PROJECT_VERSION );
388 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
390 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
391 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
394 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
395 throws MetadataResolutionException
397 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
398 metadata.setId( TEST_PROJECT_VERSION );
400 Map<String, String> additionalProps = new HashMap<String, String>();
401 additionalProps.put( "deleteKey", "deleteValue" );
403 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
404 metadata.addFacet( facet );
405 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
407 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
408 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
410 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
411 Map<String, String> facetProperties = testFacet.toProperties();
413 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
415 facetProperties.remove( "deleteKey" );
417 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
418 metadata.addFacet( newTestFacet );
420 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
422 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
423 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
424 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
425 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
428 public void testUpdateArtifactMetadataWithExistingFacets()
430 ArtifactMetadata metadata = createArtifact();
431 MetadataFacet facet = new TestMetadataFacet( "baz" );
432 metadata.addFacet( facet );
433 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
435 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
436 TEST_PROJECT_VERSION ).iterator().next();
437 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
439 metadata = createArtifact();
440 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
442 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
443 TEST_PROJECT_VERSION ).iterator().next();
444 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
445 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
446 assertEquals( "baz", testFacet.getValue() );
449 public void testUpdateArtifactMetadataWithNoExistingFacets()
451 ArtifactMetadata metadata = createArtifact();
452 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
454 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
455 TEST_PROJECT_VERSION ).iterator().next();
456 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
458 metadata = createArtifact();
459 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
461 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
462 TEST_PROJECT_VERSION ).iterator().next();
463 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
466 public void testGetMetadataFacet()
468 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
470 assertEquals( new TestMetadataFacet( TEST_VALUE ), repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID,
474 public void testGetMetadataFacetWhenEmpty()
476 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
479 public void testGetMetadataFacetWhenUnknownName()
481 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
483 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
486 public void testGetMetadataFacetWhenDefaultValue()
488 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
490 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), repository.getMetadataFacet( TEST_REPO_ID,
495 public void testGetMetadataFacetWhenUnknownFacetId()
497 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
500 public void testGetMetadataFacets()
502 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
504 assertEquals( Collections.singletonList( TEST_NAME ), repository.getMetadataFacets( TEST_REPO_ID,
508 public void testGetMetadataFacetsWhenEmpty()
510 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
511 assertTrue( facets.isEmpty() );
514 public void testRemoveFacets()
516 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
518 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
519 assertFalse( facets.isEmpty() );
521 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
523 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
524 assertTrue( facets.isEmpty() );
527 public void testRemoveFacetsWhenEmpty()
529 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
530 assertTrue( facets.isEmpty() );
532 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
534 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
535 assertTrue( facets.isEmpty() );
538 public void testRemoveFacetsWhenUnknown()
540 // testing no exception
541 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
544 public void testRemoveFacetWhenUnknown()
546 // testing no exception
547 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
550 public void testRemoveFacet()
552 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
553 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
555 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
556 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
557 assertFalse( facets.isEmpty() );
559 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
561 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
562 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
563 assertTrue( facets.isEmpty() );
566 public void testRemoveFacetWhenEmpty()
568 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
569 assertTrue( facets.isEmpty() );
570 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
572 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
574 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
575 assertTrue( facets.isEmpty() );
576 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
579 public void testGetArtifacts()
581 ArtifactMetadata artifact1 = createArtifact();
582 ArtifactMetadata artifact2 = createArtifact( "pom" );
583 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
584 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
586 Collection<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
587 TEST_PROJECT_VERSION );
588 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
589 Collections.sort( actual, new Comparator<ArtifactMetadata>()
591 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
593 return o1.getId().compareTo( o2.getId() );
596 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
599 public void testGetArtifactVersions()
601 ArtifactMetadata artifact1 = createArtifact();
602 String version1 = "1.0-20091212.012345-1";
603 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
604 artifact1.setVersion( version1 );
605 ArtifactMetadata artifact2 = createArtifact();
606 String version2 = "1.0-20091212.123456-2";
607 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
608 artifact2.setVersion( version2 );
609 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
610 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
612 List<String> versions = new ArrayList<String>( repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE,
614 TEST_PROJECT_VERSION ) );
615 Collections.sort( versions );
616 assertEquals( Arrays.asList( version1, version2 ), versions );
619 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
621 ArtifactMetadata artifact1 = createArtifact();
622 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
623 ArtifactMetadata artifact2 = createArtifact();
624 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
625 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
626 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
628 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ), repository.getArtifactVersions( TEST_REPO_ID,
631 TEST_PROJECT_VERSION ) );
634 public void testGetArtifactsByDateRangeOpen()
636 ArtifactMetadata artifact = createArtifact();
637 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
639 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
643 public void testGetArtifactsByDateRangeSparseNamespace()
645 String namespace = "org.apache.archiva";
646 ArtifactMetadata artifact = createArtifact();
647 artifact.setNamespace( namespace );
648 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
650 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
654 public void testGetArtifactsByDateRangeLowerBound()
656 ArtifactMetadata artifact = createArtifact();
657 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
659 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
660 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, date,
664 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
666 ArtifactMetadata artifact = createArtifact();
667 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
669 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
670 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ).isEmpty() );
673 public void testGetArtifactsByDateRangeLowerAndUpperBound()
675 ArtifactMetadata artifact = createArtifact();
676 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
678 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
679 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
680 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, lower,
684 public void testGetArtifactsByDateRangeUpperBound()
686 ArtifactMetadata artifact = createArtifact();
687 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
689 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
690 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
694 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
696 ArtifactMetadata artifact = createArtifact();
697 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
699 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
700 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ).isEmpty() );
703 public void testGetArtifactsByRepoId()
705 ArtifactMetadata artifact = createArtifact();
706 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
707 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
710 public void testGetNamespacesWithSparseDepth()
712 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
714 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
715 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
716 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
717 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
720 public void testGetArtifactsByChecksumSingleResultMd5()
722 ArtifactMetadata artifact = createArtifact();
723 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
725 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
729 public void testGetArtifactsByChecksumSingleResultSha1()
731 ArtifactMetadata artifact = createArtifact();
732 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
734 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
738 public void testGetArtifactsByChecksumDeepNamespace()
740 ArtifactMetadata artifact = createArtifact();
741 String namespace = "multi.level.ns";
742 artifact.setNamespace( namespace );
743 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
745 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
749 public void testGetArtifactsByChecksumMultipleResult()
751 ArtifactMetadata artifact1 = createArtifact();
752 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
754 String newProjectId = "another-project";
755 ArtifactMetadata artifact2 = createArtifact();
756 artifact2.setProject( newProjectId );
757 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
759 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum(
760 TEST_REPO_ID, TEST_SHA1 ) );
761 Collections.sort( artifacts, new ArtifactMetadataComparator() );
762 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
765 public void testGetArtifactsByChecksumNoResult()
767 ArtifactMetadata artifact = createArtifact();
768 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
770 assertEquals( Collections.<ArtifactMetadata>emptyList(), repository.getArtifactsByChecksum( TEST_REPO_ID, "not a checksum" ) );
773 public void testDeleteArtifact()
775 ArtifactMetadata artifact = createArtifact();
776 artifact.addFacet( new TestMetadataFacet( "value" ) );
778 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
780 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( repository.getArtifacts(
781 TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
783 repository.deleteArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
785 assertTrue( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
786 TEST_PROJECT_VERSION ).isEmpty() );
789 public void testDeleteRepository()
791 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
793 ProjectMetadata project1 = new ProjectMetadata();
794 project1.setNamespace( TEST_NAMESPACE );
795 project1.setId( "project1" );
796 repository.updateProject( TEST_REPO_ID, project1 );
797 ProjectMetadata project2 = new ProjectMetadata();
798 project2.setNamespace( TEST_NAMESPACE );
799 project2.setId( "project2" );
800 repository.updateProject( TEST_REPO_ID, project2 );
802 ArtifactMetadata artifact1 = createArtifact();
803 artifact1.setProject( "project1" );
804 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
805 ArtifactMetadata artifact2 = createArtifact();
806 artifact2.setProject( "project2" );
807 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
809 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
810 Collections.sort( expected, new ArtifactMetadataComparator() );
812 List<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange(
813 TEST_REPO_ID, null, null ) );
814 Collections.sort( actual, new ArtifactMetadataComparator() );
816 assertEquals( expected, actual );
818 repository.deleteRepository( TEST_REPO_ID );
820 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
821 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
824 private static ProjectMetadata createProject()
826 return createProject( TEST_NAMESPACE );
829 private static ProjectMetadata createProject( String ns )
831 ProjectMetadata project = new ProjectMetadata();
832 project.setId( TEST_PROJECT );
833 project.setNamespace( ns );
837 private static ArtifactMetadata createArtifact()
839 return createArtifact( "jar" );
842 private static ArtifactMetadata createArtifact( String type )
844 ArtifactMetadata artifact = new ArtifactMetadata();
845 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
846 artifact.setWhenGathered( new Date() );
847 artifact.setNamespace( TEST_NAMESPACE );
848 artifact.setProject( TEST_PROJECT );
849 artifact.setRepositoryId( TEST_REPO_ID );
850 artifact.setFileLastModified( System.currentTimeMillis() );
851 artifact.setVersion( TEST_PROJECT_VERSION );
852 artifact.setProjectVersion( TEST_PROJECT_VERSION );
853 artifact.setMd5( TEST_MD5 );
854 artifact.setSha1( TEST_SHA1 );
858 private static class ArtifactMetadataComparator
859 implements Comparator<ArtifactMetadata>
861 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
863 return a.getProject().compareTo( b.getProject() );
867 private static class TestMetadataFacet
868 implements MetadataFacet
870 private String testFacetId;
872 private Map<String, String> additionalProps;
874 private String value;
876 private TestMetadataFacet( String value )
879 testFacetId = TEST_FACET_ID;
882 private TestMetadataFacet( String facetId, String value )
885 testFacetId = facetId;
888 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
890 this( facetId, value );
891 this.additionalProps = additionalProps;
894 public String getFacetId()
899 public String getName()
904 public Map<String, String> toProperties()
908 if( additionalProps == null )
910 return Collections.singletonMap( "foo", value );
914 Map<String, String> props = new HashMap<String, String>();
915 props.put( "foo", value );
917 for( String key : additionalProps.keySet() )
919 props.put( key, additionalProps.get( key ) );
926 return Collections.emptyMap();
930 public void fromProperties( Map<String, String> properties )
932 String value = properties.get( "foo" );
938 properties.remove( "foo" );
940 if( additionalProps == null )
942 additionalProps = new HashMap<String, String>();
945 for( String key: properties.keySet() )
947 additionalProps.put( key, properties.get( key ) );
951 public String getValue()
957 public String toString()
959 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
963 public boolean equals( Object o )
969 if ( o == null || getClass() != o.getClass() )
974 TestMetadataFacet that = (TestMetadataFacet) o;
976 if ( value != null ? !value.equals( that.value ) : that.value != null )
985 public int hashCode()
987 return value != null ? value.hashCode() : 0;