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 junit.framework.TestCase;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.CiManagement;
25 import org.apache.archiva.metadata.model.Dependency;
26 import org.apache.archiva.metadata.model.IssueManagement;
27 import org.apache.archiva.metadata.model.License;
28 import org.apache.archiva.metadata.model.MailingList;
29 import org.apache.archiva.metadata.model.MetadataFacet;
30 import org.apache.archiva.metadata.model.MetadataFacetFactory;
31 import org.apache.archiva.metadata.model.Organization;
32 import org.apache.archiva.metadata.model.ProjectMetadata;
33 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
34 import org.apache.archiva.metadata.model.Scm;
35 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
36 import org.assertj.core.util.Sets;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.test.context.ContextConfiguration;
43 import java.text.SimpleDateFormat;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Calendar;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.Comparator;
50 import java.util.Date;
51 import java.util.HashMap;
52 import java.util.LinkedHashSet;
53 import java.util.List;
55 import java.util.TimeZone;
57 import static org.assertj.core.api.Assertions.assertThat;
59 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
60 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
61 public abstract class AbstractMetadataRepositoryTest
64 protected static final String OTHER_REPO_ID = "other-repo";
66 protected MetadataRepository repository;
68 protected static final String TEST_REPO_ID = "test";
70 private static final String TEST_PROJECT = "projectId";
72 private static final String TEST_NAMESPACE = "namespace";
74 private static final String TEST_PROJECT_VERSION = "1.0";
76 private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
78 private static final String TEST_FACET_ID = "test-facet-id";
80 private static final String TEST_NAME = "test/name";
82 private static final String TEST_VALUE = "test-value";
84 private static final String UNKNOWN = "unknown";
86 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
88 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
90 private static final String TEST_METADATA_VALUE = "test-metadata";
92 protected Logger log = LoggerFactory.getLogger( getClass() );
94 protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
96 Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
97 factories.put( TEST_FACET_ID, new MetadataFacetFactory()
99 public MetadataFacet createMetadataFacet()
101 return new TestMetadataFacet( TEST_METADATA_VALUE );
104 public MetadataFacet createMetadataFacet( String repositoryId, String name )
106 return new TestMetadataFacet( TEST_METADATA_VALUE );
110 // add to ensure we don't accidentally create an empty facet ID.
111 factories.put( "", new MetadataFacetFactory()
113 public MetadataFacet createMetadataFacet()
115 return new TestMetadataFacet( "", TEST_VALUE );
118 public MetadataFacet createMetadataFacet( String repositoryId, String name )
120 return new TestMetadataFacet( "", TEST_VALUE );
127 public void testRootNamespaceWithNoMetadataRepository()
130 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
131 assertThat( namespaces ).isNotNull().isEmpty();
135 public void testGetNamespaceOnly()
138 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
140 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
142 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
143 TEST_NAMESPACE ).hasSize( 1 );
145 repository.removeNamespace( TEST_REPO_ID, TEST_NAMESPACE );
147 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
152 public void testGetProjectOnly()
155 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
156 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
158 ProjectMetadata project = new ProjectMetadata();
159 project.setId( TEST_PROJECT );
160 project.setNamespace( TEST_NAMESPACE );
162 repository.updateProject( TEST_REPO_ID, project );
164 project = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
165 assertEquals( TEST_PROJECT, project.getId() );
166 assertEquals( TEST_NAMESPACE, project.getNamespace() );
168 // test that namespace is also constructed
170 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
172 assertThat( namespaces ).isNotNull().isNotEmpty().contains( TEST_NAMESPACE ).hasSize( 1 );
176 public void testGetProjectVersionOnly()
179 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
180 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
181 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
183 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
184 metadata.setId( TEST_PROJECT_VERSION );
186 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
188 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
189 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
191 // test that namespace and project is also constructed
192 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
194 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_NAMESPACE );
196 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
197 assertNotNull( projectMetadata );
198 assertEquals( TEST_PROJECT, projectMetadata.getId() );
199 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
203 public void testGetArtifactOnly()
206 assertEquals( Collections.<ArtifactMetadata>emptyList(), new ArrayList<ArtifactMetadata>(
207 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
209 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
210 assertThat( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) ).isNull();
212 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
214 ArtifactMetadata metadata = createArtifact();
216 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
218 Collection<ArtifactMetadata> artifacts =
219 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
220 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
222 // test that namespace, project and project version is also constructed
224 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
225 TEST_NAMESPACE ).hasSize( 1 );
227 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
228 assertEquals( TEST_PROJECT, projectMetadata.getId() );
229 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
231 ProjectVersionMetadata projectVersionMetadata =
232 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
233 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
237 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
240 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
241 metadata.setId( TEST_PROJECT_VERSION );
242 MailingList mailingList = new MailingList();
243 mailingList.setName( "Foo List" );
244 mailingList.setOtherArchives( Collections.<String>emptyList() );
245 metadata.setMailingLists( Arrays.asList( mailingList ) );
246 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
248 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
249 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
251 List<MailingList> mailingLists = metadata.getMailingLists();
253 assertThat( mailingLists ).isNotNull().isNotEmpty().hasSize( 1 );
255 mailingList = metadata.getMailingLists().get( 0 );
256 assertEquals( "Foo List", mailingList.getName() );
258 List<String> others = mailingList.getOtherArchives();
259 assertThat( others ).isNotNull().isEmpty();
263 public void testUpdateProjectVersionMetadataWithAllElements()
266 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
267 metadata.setId( TEST_PROJECT_VERSION );
269 metadata.setName( "project name" );
270 metadata.setDescription( "project description" );
271 metadata.setUrl( "the url" );
273 MailingList mailingList = new MailingList();
274 mailingList.setName( "Foo List" );
275 mailingList.setUnsubscribeAddress( "UnsubscribeAddress" );
276 mailingList.setSubscribeAddress( "SubscribeAddress" );
277 mailingList.setPostAddress( "PostAddress" );
278 mailingList.setMainArchiveUrl( "MainArchiveUrl" );
279 mailingList.setOtherArchives( Arrays.asList( "other archive" ) );
280 metadata.setMailingLists( Arrays.asList( mailingList ) );
283 scm.setConnection( "connection" );
284 scm.setDeveloperConnection( "dev conn" );
286 metadata.setScm( scm );
288 CiManagement ci = new CiManagement();
289 ci.setSystem( "system" );
290 ci.setUrl( "ci url" );
291 metadata.setCiManagement( ci );
293 IssueManagement tracker = new IssueManagement();
294 tracker.setSystem( "system" );
295 tracker.setUrl( "issue tracker url" );
296 metadata.setIssueManagement( tracker );
298 Organization org = new Organization();
299 org.setName( "org name" );
301 metadata.setOrganization( org );
303 License l = new License();
304 l.setName( "license name" );
305 l.setUrl( "license url" );
306 metadata.addLicense( l );
308 Dependency d = new Dependency();
309 d.setArtifactId( "artifactId" );
310 d.setClassifier( "classifier" );
311 d.setGroupId( "groupId" );
312 d.setScope( "scope" );
313 d.setSystemPath( "system path" );
315 d.setVersion( "version" );
316 d.setOptional( true );
317 metadata.addDependency( d );
319 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
321 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
322 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
323 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
324 assertEquals( "project name", metadata.getName() );
325 assertEquals( "project description", metadata.getDescription() );
326 assertEquals( "the url", metadata.getUrl() );
328 assertEquals( "connection", metadata.getScm().getConnection() );
329 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
330 assertEquals( "url", metadata.getScm().getUrl() );
332 assertEquals( "system", metadata.getCiManagement().getSystem() );
333 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
335 assertEquals( "system", metadata.getIssueManagement().getSystem() );
336 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
338 assertEquals( "org name", metadata.getOrganization().getName() );
339 assertEquals( "url", metadata.getOrganization().getUrl() );
341 assertEquals( 1, metadata.getMailingLists().size() );
342 MailingList retrievedMailingList = metadata.getMailingLists().get( 0 );
343 assertEquals( mailingList.getName(), retrievedMailingList.getName() );
344 assertEquals( mailingList.getMainArchiveUrl(), retrievedMailingList.getMainArchiveUrl() );
345 assertEquals( mailingList.getPostAddress(), retrievedMailingList.getPostAddress() );
346 assertEquals( mailingList.getSubscribeAddress(), retrievedMailingList.getSubscribeAddress() );
347 assertEquals( mailingList.getUnsubscribeAddress(), retrievedMailingList.getUnsubscribeAddress() );
348 assertThat( retrievedMailingList.getOtherArchives() ) //
352 .contains( "other archive" );
354 assertEquals( 1, metadata.getLicenses().size() );
355 l = metadata.getLicenses().get( 0 );
356 assertEquals( "license name", l.getName() );
357 assertEquals( "license url", l.getUrl() );
359 assertEquals( 1, metadata.getDependencies().size() );
360 d = metadata.getDependencies().get( 0 );
361 assertEquals( "artifactId", d.getArtifactId() );
362 assertEquals( "classifier", d.getClassifier() );
363 assertEquals( "groupId", d.getGroupId() );
364 assertEquals( "scope", d.getScope() );
365 assertEquals( "system path", d.getSystemPath() );
366 assertEquals( "type", d.getType() );
367 assertEquals( "version", d.getVersion() );
368 assertTrue( d.isOptional() );
372 public void testGetRepositories()
375 // currently set up this way so the behaviour of both the test and the mock config return the same repository
376 // set as the File implementation just uses the config rather than the content
377 repository.updateNamespace( TEST_REPO_ID, "namespace" );
378 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
380 Collection<String> repositories = repository.getRepositories();
382 assertEquals( "repository.getRepositories() -> " + repositories, //
383 Sets.newLinkedHashSet( TEST_REPO_ID, OTHER_REPO_ID ), //
384 new LinkedHashSet<String>( repositories ) ); //
388 public void testUpdateProjectVersionMetadataIncomplete()
391 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
392 metadata.setId( TEST_PROJECT_VERSION );
393 metadata.setIncomplete( true );
394 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
396 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
397 assertEquals( true, metadata.isIncomplete() );
398 assertNull( metadata.getCiManagement() );
399 assertNull( metadata.getScm() );
400 assertNull( metadata.getIssueManagement() );
401 assertNull( metadata.getOrganization() );
402 assertNull( metadata.getDescription() );
403 assertNull( metadata.getName() );
404 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
405 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
406 assertTrue( metadata.getMailingLists().isEmpty() );
407 assertTrue( metadata.getLicenses().isEmpty() );
408 assertTrue( metadata.getDependencies().isEmpty() );
412 public void testUpdateProjectVersionMetadataWithExistingFacets()
415 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
416 metadata.setId( TEST_PROJECT_VERSION );
417 MetadataFacet facet = new TestMetadataFacet( "baz" );
418 metadata.addFacet( facet );
419 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
421 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
422 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
424 metadata = new ProjectVersionMetadata();
425 metadata.setId( TEST_PROJECT_VERSION );
426 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
428 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
429 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
430 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
431 assertEquals( "baz", testFacet.getValue() );
435 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
438 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
439 metadata.setId( TEST_PROJECT_VERSION );
440 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
442 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
444 assertThat( metadata.getFacetIds() ).isNotNull().isEmpty();
446 metadata = new ProjectVersionMetadata();
447 metadata.setId( TEST_PROJECT_VERSION );
448 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
450 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
451 assertThat( metadata.getFacetIds() ).isNotNull().isEmpty();
456 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
459 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
460 metadata.setId( TEST_PROJECT_VERSION );
462 Map<String, String> additionalProps = new HashMap<String, String>();
463 additionalProps.put( "deleteKey", "deleteValue" );
465 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
466 metadata.addFacet( facet );
467 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
469 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
471 assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
473 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
474 Map<String, String> facetProperties = testFacet.toProperties();
476 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
478 facetProperties.remove( "deleteKey" );
480 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
481 metadata.addFacet( newTestFacet );
483 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
485 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
487 assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
488 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
489 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
493 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
496 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
497 versionMetadata.setId( TEST_PROJECT_VERSION );
499 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
500 versionMetadata.addFacet( facet );
501 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
503 ArtifactMetadata artifactMetadata = createArtifact();
504 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
507 Collection<ArtifactMetadata> artifacts =
508 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
509 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
511 artifacts = repository.getArtifacts( TEST_REPO_ID );
512 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
514 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
515 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
517 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
518 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
520 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
521 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
525 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
528 ArtifactMetadata metadata = createArtifact();
530 Map<String, String> additionalProps = new HashMap<String, String>();
531 additionalProps.put( "deleteKey", "deleteValue" );
533 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
534 metadata.addFacet( facet );
535 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
537 Collection<ArtifactMetadata> artifacts =
538 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
540 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
541 metadata = artifacts.iterator().next();
543 Collection<String> ids = metadata.getFacetIds();
544 assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
546 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
547 Map<String, String> facetProperties = testFacet.toProperties();
549 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
551 facetProperties.remove( "deleteKey" );
553 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
554 metadata.addFacet( newTestFacet );
556 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
558 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
560 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
561 metadata = artifacts.iterator().next();
563 ids = metadata.getFacetIds();
564 assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
566 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
568 Map<String, String> props = testFacet.toProperties();
569 assertThat( props ).isNotNull().doesNotContainKey( "deleteKey" );
573 public void testUpdateArtifactMetadataWithExistingFacets()
576 ArtifactMetadata metadata = createArtifact();
577 MetadataFacet facet = new TestMetadataFacet( "baz" );
578 metadata.addFacet( facet );
579 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
581 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
582 TEST_PROJECT_VERSION ).iterator().next();
583 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
585 metadata = createArtifact();
586 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
588 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
589 TEST_PROJECT_VERSION ).iterator().next();
590 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
591 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
592 assertEquals( "baz", testFacet.getValue() );
596 public void testUpdateArtifactMetadataWithNoExistingFacets()
599 ArtifactMetadata metadata = createArtifact();
600 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
602 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
603 TEST_PROJECT_VERSION ).iterator().next();
604 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
606 metadata = createArtifact();
607 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
609 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
610 TEST_PROJECT_VERSION ).iterator().next();
611 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
615 public void testGetMetadataFacet()
618 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
620 TestMetadataFacet test =
621 (TestMetadataFacet) repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
623 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
627 public void testGetMetadataFacetWhenEmpty()
630 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
634 public void testGetMetadataFacetWhenUnknownName()
637 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
639 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
643 public void testGetMetadataFacetWhenDefaultValue()
646 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
648 MetadataFacet metadataFacet = repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
650 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), metadataFacet );
654 public void testGetMetadataFacetWhenUnknownFacetId()
657 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
661 public void testGetMetadataFacets()
664 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
666 assertEquals( Collections.singletonList( TEST_NAME ),
667 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
671 public void testGetMetadataFacetsWhenEmpty()
675 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
676 assertTrue( facets.isEmpty() );
680 public void testRemoveFacets()
683 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
685 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
686 assertFalse( facets.isEmpty() );
688 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
690 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
691 assertTrue( facets.isEmpty() );
695 public void testRemoveFacetsWhenEmpty()
698 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
699 assertTrue( facets.isEmpty() );
701 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
703 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
704 assertTrue( facets.isEmpty() );
708 public void testRemoveFacetsWhenUnknown()
711 // testing no exception
712 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
716 public void testRemoveFacetWhenUnknown()
719 // testing no exception
720 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
724 public void testRemoveFacet()
727 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
728 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
730 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
731 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
732 assertFalse( facets.isEmpty() );
734 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
736 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
737 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
738 assertTrue( facets.isEmpty() );
742 public void testRemoveFacetWhenEmpty()
745 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
746 assertThat( facets ).isNotNull().isEmpty();
747 assertThat( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull();
749 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
751 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
752 assertThat( facets ).isNotNull().isEmpty();
753 assertThat( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull();
757 public void hasMetadataFacetStart()
760 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
765 public void hasMetadataFacet()
769 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
771 Calendar cal = Calendar.getInstance();
773 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
775 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
777 cal.add( Calendar.MINUTE, 2 );
779 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
781 cal.add( Calendar.MINUTE, 2 );
783 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
785 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
787 assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
789 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
791 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
793 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
795 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
797 assertThat( facets ).isNotNull().isEmpty();
803 public void testGetArtifacts()
806 ArtifactMetadata artifact1 = createArtifact();
807 ArtifactMetadata artifact2 = createArtifact( "pom" );
808 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
809 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
811 Collection<ArtifactMetadata> artifacts =
812 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
813 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
814 Collections.sort( actual, new Comparator<ArtifactMetadata>()
816 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
818 return o1.getId().compareTo( o2.getId() );
821 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
825 public void testGetArtifactVersions()
828 ArtifactMetadata artifact1 = createArtifact();
829 String version1 = "1.0-20091212.012345-1";
830 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
831 artifact1.setVersion( version1 );
832 ArtifactMetadata artifact2 = createArtifact();
833 String version2 = "1.0-20091212.123456-2";
834 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
835 artifact2.setVersion( version2 );
836 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
837 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
839 List<String> versions = new ArrayList<String>(
840 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
841 Collections.sort( versions );
842 assertEquals( Arrays.asList( version1, version2 ), versions );
846 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
849 ArtifactMetadata artifact1 = createArtifact();
850 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
851 ArtifactMetadata artifact2 = createArtifact();
852 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
853 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
854 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
856 Collection<String> versions =
857 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
859 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).containsExactly( TEST_PROJECT_VERSION );
864 public void testGetArtifactsByDateRangeOpen()
867 ArtifactMetadata artifact = createArtifact();
868 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
871 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
873 assertEquals( Collections.singletonList( artifact ), artifacts );
877 public void testGetArtifactsByDateRangeSparseNamespace()
880 String namespace = "org.apache.archiva";
881 ArtifactMetadata artifact = createArtifact();
882 artifact.setNamespace( namespace );
883 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
886 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
888 assertEquals( Collections.singletonList( artifact ), artifacts );
892 public void testGetArtifactsByDateRangeLowerBound()
895 ArtifactMetadata artifact = createArtifact();
896 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
899 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
901 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
903 assertEquals( Collections.singletonList( artifact ), artifacts );
907 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
910 ArtifactMetadata artifact = createArtifact();
911 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
913 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
915 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
917 assertThat( artifacts ).isNotNull().isEmpty();
921 public void testGetArtifactsByDateRangeLowerAndUpperBound()
924 ArtifactMetadata artifact = createArtifact();
925 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
928 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
929 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
931 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper );
933 assertEquals( Collections.singletonList( artifact ), artifacts );
937 public void testGetArtifactsByDateRangeUpperBound()
940 ArtifactMetadata artifact = createArtifact();
941 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
944 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
946 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
948 assertEquals( Collections.singletonList( artifact ), artifacts );
952 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
955 ArtifactMetadata artifact = createArtifact();
956 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
959 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
961 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
963 assertThat( artifacts ).isNotNull().isEmpty();
967 public void testGetArtifactsByRepoId()
970 ArtifactMetadata artifact = createArtifact();
971 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
974 List<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID );
976 assertEquals( Collections.singletonList( artifact ), artifacts );
980 public void testGetArtifactsByRepoIdMultipleCopies()
983 ArtifactMetadata artifact = createArtifact();
984 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
986 ArtifactMetadata secondArtifact = createArtifact();
987 secondArtifact.setRepositoryId( OTHER_REPO_ID );
988 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
991 // test it restricts to the appropriate repository
992 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
993 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
997 public void testGetArtifactsByDateRangeMultipleCopies()
1000 ArtifactMetadata artifact = createArtifact();
1001 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1003 ArtifactMetadata secondArtifact = createArtifact();
1004 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1005 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1008 // test it restricts to the appropriate repository
1009 assertEquals( Collections.singletonList( artifact ),
1010 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1011 assertEquals( Collections.singletonList( secondArtifact ),
1012 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
1016 public void testGetArtifactsByChecksumMultipleCopies()
1019 ArtifactMetadata artifact = createArtifact();
1020 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1022 ArtifactMetadata secondArtifact = createArtifact();
1023 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1024 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1027 // test it restricts to the appropriate repository
1028 assertEquals( Collections.singletonList( artifact ),
1029 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1030 assertEquals( Collections.singletonList( secondArtifact ), new ArrayList<ArtifactMetadata>(
1031 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) ) );
1032 assertEquals( Collections.singletonList( artifact ),
1033 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1034 assertEquals( Collections.singletonList( secondArtifact ),
1035 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) ) );
1039 public void testGetNamespacesWithSparseDepth()
1042 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1044 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
1046 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "org" );
1048 namespaces = repository.getNamespaces( TEST_REPO_ID, "org" );
1049 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "apache" );
1051 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache" );
1052 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "maven" );
1054 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" );
1055 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "shared" );
1059 public void testGetNamespacesWithProjectsPresent()
1062 String namespace = "org.apache.maven.shared";
1063 repository.updateNamespace( TEST_REPO_ID, namespace );
1065 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1066 metadata.setId( TEST_PROJECT_VERSION );
1067 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1069 Collection<String> namespaces = repository.getNamespaces( TEST_REPO_ID, namespace );
1071 assertThat( namespaces ).isNotNull().isEmpty();
1076 public void testGetProjectsWithOtherNamespacesPresent()
1079 ProjectMetadata projectMetadata = new ProjectMetadata();
1080 projectMetadata.setId( TEST_PROJECT );
1081 projectMetadata.setNamespace( "org.apache.maven" );
1082 repository.updateProject( TEST_REPO_ID, projectMetadata );
1084 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1086 Collection<String> projects = repository.getProjects( TEST_REPO_ID, "org.apache.maven" );
1088 assertThat( projects ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_PROJECT );
1092 public void testGetProjectVersionsWithOtherNamespacesPresent()
1095 // an unusual case but technically possible where a project namespace matches another project's name
1097 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1098 versionMetadata.setId( TEST_PROJECT_VERSION );
1099 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1101 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1104 Collection<String> versions =
1105 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project" );
1106 assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1108 versions = repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT );
1110 assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1114 public void testGetArtifactsByChecksumSingleResultMd5()
1117 ArtifactMetadata artifact = createArtifact();
1118 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1121 assertEquals( Collections.singletonList( artifact ),
1122 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1126 public void testGetArtifactsByChecksumSingleResultSha1()
1129 ArtifactMetadata artifact = createArtifact();
1130 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1133 assertEquals( Collections.singletonList( artifact ),
1134 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1138 public void testGetArtifactsByChecksumDeepNamespace()
1141 ArtifactMetadata artifact = createArtifact();
1142 String namespace = "multi.level.ns";
1143 artifact.setNamespace( namespace );
1144 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1147 assertEquals( Collections.singletonList( artifact ),
1148 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1149 assertEquals( Collections.singletonList( artifact ),
1150 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1154 public void testGetArtifactsByChecksumMultipleResult()
1157 ArtifactMetadata artifact1 = createArtifact();
1158 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1160 String newProjectId = "another-project";
1161 ArtifactMetadata artifact2 = createArtifact();
1162 artifact2.setProject( newProjectId );
1163 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1166 List<ArtifactMetadata> artifacts =
1167 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1168 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1169 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1171 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1172 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1173 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1177 public void testGetArtifactsByChecksumNoResult()
1180 ArtifactMetadata artifact = createArtifact();
1181 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1183 Collection<ArtifactMetadata> artifactsByChecksum =
1184 repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1185 assertThat( artifactsByChecksum ).isNotNull().isEmpty();
1190 public void testDeleteRepository()
1193 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1195 ProjectMetadata project1 = new ProjectMetadata();
1196 project1.setNamespace( TEST_NAMESPACE );
1197 project1.setId( "project1" );
1198 repository.updateProject( TEST_REPO_ID, project1 );
1199 ProjectMetadata project2 = new ProjectMetadata();
1200 project2.setNamespace( TEST_NAMESPACE );
1201 project2.setId( "project2" );
1202 repository.updateProject( TEST_REPO_ID, project2 );
1204 ArtifactMetadata artifact1 = createArtifact();
1205 artifact1.setProject( "project1" );
1206 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1207 ArtifactMetadata artifact2 = createArtifact();
1208 artifact2.setProject( "project2" );
1209 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1212 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1213 Collections.sort( expected, new ArtifactMetadataComparator() );
1215 List<ArtifactMetadata> actual =
1216 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1217 Collections.sort( actual, new ArtifactMetadataComparator() );
1219 assertEquals( expected, actual );
1221 repository.removeRepository( TEST_REPO_ID );
1223 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1224 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1229 public void testDeleteArtifact()
1232 ArtifactMetadata artifact = createArtifact();
1233 artifact.addFacet( new TestMetadataFacet( "value" ) );
1235 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1237 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1238 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1240 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1242 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1244 log.info( "versions {}", versions );
1246 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1248 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1250 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1252 log.info( "versions {}", versions );
1254 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1256 assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1257 TEST_PROJECT_VERSION ) ).isNotNull().isEmpty();
1259 assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1260 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1264 public void deleteArtifact()
1267 ArtifactMetadata artifact = createArtifact();
1268 artifact.addFacet( new TestMetadataFacet( "value" ) );
1270 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1272 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1274 Collection<ArtifactMetadata> artifacts =
1275 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1277 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( artifacts ) );
1279 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1281 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1283 assertThat( artifacts ).isNotNull().isEmpty();
1287 public void deleteVersion()
1290 ArtifactMetadata artifact = createArtifact();
1291 artifact.addFacet( new TestMetadataFacet( "value" ) );
1293 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1295 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1297 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1299 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 );
1301 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1303 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1305 assertThat( versions ).isNotNull().isEmpty();
1309 public void deleteProject()
1312 ArtifactMetadata artifact = createArtifact();
1313 artifact.addFacet( new TestMetadataFacet( "value" ) );
1315 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1317 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1319 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1321 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1323 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1325 assertThat( versions ).isNotNull().isEmpty();
1330 public void deleteSnapshotVersion()
1333 ArtifactMetadata artifactOne = createArtifact();
1334 artifactOne.setVersion( "2.0-20120618.214127-1" );
1335 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1336 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1337 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1339 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1341 ArtifactMetadata artifactTwo = createArtifact();
1342 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1343 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1344 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1345 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1347 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1349 Collection<ArtifactMetadata> artifactMetadatas =
1350 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1352 assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1354 log.info( "artifactMetadatas: {}", artifactMetadatas );
1356 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1358 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1360 assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1362 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1364 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1366 assertThat( artifactMetadatas ).isNotNull().isEmpty();
1370 private static ProjectMetadata createProject()
1372 return createProject( TEST_NAMESPACE );
1375 private static ProjectMetadata createProject( String ns )
1377 ProjectMetadata project = new ProjectMetadata();
1378 project.setId( TEST_PROJECT );
1379 project.setNamespace( ns );
1383 private static ArtifactMetadata createArtifact()
1385 return createArtifact( "jar" );
1388 private static ArtifactMetadata createArtifact( String type )
1390 ArtifactMetadata artifact = new ArtifactMetadata();
1391 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1392 artifact.setWhenGathered( new Date() );
1393 artifact.setNamespace( TEST_NAMESPACE );
1394 artifact.setProject( TEST_PROJECT );
1395 artifact.setRepositoryId( TEST_REPO_ID );
1396 artifact.setFileLastModified( System.currentTimeMillis() );
1397 artifact.setVersion( TEST_PROJECT_VERSION );
1398 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1399 artifact.setMd5( TEST_MD5 );
1400 artifact.setSha1( TEST_SHA1 );
1404 private static class ArtifactMetadataComparator
1405 implements Comparator<ArtifactMetadata>
1407 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1409 return a.getProject().compareTo( b.getProject() );
1413 private static class KindOfRepositoryStatistics
1414 implements MetadataFacet
1416 private String value;
1420 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1422 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1424 private KindOfRepositoryStatistics( String value, Date date )
1430 public String getName()
1432 return createNameFormat().format( date );
1435 private static SimpleDateFormat createNameFormat()
1437 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1438 fmt.setTimeZone( UTC_TIME_ZONE );
1442 public String getFacetId()
1444 return KindOfRepositoryStatistics.class.getName();
1447 public Map<String, String> toProperties()
1449 return Collections.emptyMap();
1452 public void fromProperties( Map<String, String> properties )
1458 private static class TestMetadataFacet
1459 implements MetadataFacet
1461 private String testFacetId;
1463 private Map<String, String> additionalProps;
1465 private String value;
1467 private TestMetadataFacet( String value )
1470 testFacetId = TEST_FACET_ID;
1473 private TestMetadataFacet( String facetId, String value )
1476 testFacetId = facetId;
1479 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1481 this( facetId, value );
1482 this.additionalProps = additionalProps;
1485 public String getFacetId()
1490 public String getName()
1495 public Map<String, String> toProperties()
1497 if ( value != null )
1499 if ( additionalProps == null )
1501 return Collections.singletonMap( "foo", value );
1505 Map<String, String> props = new HashMap<String, String>();
1506 props.put( "foo", value );
1508 for ( String key : additionalProps.keySet() )
1510 props.put( key, additionalProps.get( key ) );
1517 return Collections.emptyMap();
1521 public void fromProperties( Map<String, String> properties )
1523 String value = properties.get( "foo" );
1524 if ( value != null )
1529 properties.remove( "foo" );
1531 if ( additionalProps == null )
1533 additionalProps = new HashMap<String, String>();
1536 for ( String key : properties.keySet() )
1538 additionalProps.put( key, properties.get( key ) );
1542 public String getValue()
1548 public String toString()
1550 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1554 public boolean equals( Object o )
1560 if ( o == null || getClass() != o.getClass() )
1565 TestMetadataFacet that = (TestMetadataFacet) o;
1567 if ( value != null ? !value.equals( that.value ) : that.value != null )
1576 public int hashCode()
1578 return value != null ? value.hashCode() : 0;