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 assertThat( new ArrayList<ArtifactMetadata>(
207 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
208 TEST_PROJECT_VERSION ) ) ).isNotNull().isEmpty();
210 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ).isNull();
211 assertThat( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) ).isNull();
213 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
215 ArtifactMetadata metadata = createArtifact();
217 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
219 Collection<ArtifactMetadata> artifacts =
220 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
221 //assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
222 assertThat( artifacts ).containsExactly( metadata );
223 // test that namespace, project and project version is also constructed
225 assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
226 TEST_NAMESPACE ).hasSize( 1 );
228 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
229 assertEquals( TEST_PROJECT, projectMetadata.getId() );
230 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
232 ProjectVersionMetadata projectVersionMetadata =
233 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
234 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
238 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
241 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
242 metadata.setId( TEST_PROJECT_VERSION );
243 MailingList mailingList = new MailingList();
244 mailingList.setName( "Foo List" );
245 mailingList.setOtherArchives( Collections.<String>emptyList() );
246 metadata.setMailingLists( Arrays.asList( mailingList ) );
247 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
249 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
250 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
252 List<MailingList> mailingLists = metadata.getMailingLists();
254 assertThat( mailingLists ).isNotNull().isNotEmpty().hasSize( 1 );
256 mailingList = metadata.getMailingLists().get( 0 );
257 assertEquals( "Foo List", mailingList.getName() );
259 List<String> others = mailingList.getOtherArchives();
260 assertThat( others ).isNotNull().isEmpty();
264 public void testUpdateProjectVersionMetadataWithAllElements()
267 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
268 metadata.setId( TEST_PROJECT_VERSION );
270 metadata.setName( "project name" );
271 metadata.setDescription( "project description" );
272 metadata.setUrl( "the url" );
274 MailingList mailingList = new MailingList();
275 mailingList.setName( "Foo List" );
276 mailingList.setUnsubscribeAddress( "UnsubscribeAddress" );
277 mailingList.setSubscribeAddress( "SubscribeAddress" );
278 mailingList.setPostAddress( "PostAddress" );
279 mailingList.setMainArchiveUrl( "MainArchiveUrl" );
280 mailingList.setOtherArchives( Arrays.asList( "other archive" ) );
281 metadata.setMailingLists( Arrays.asList( mailingList ) );
284 scm.setConnection( "connection" );
285 scm.setDeveloperConnection( "dev conn" );
287 metadata.setScm( scm );
289 CiManagement ci = new CiManagement();
290 ci.setSystem( "system" );
291 ci.setUrl( "ci url" );
292 metadata.setCiManagement( ci );
294 IssueManagement tracker = new IssueManagement();
295 tracker.setSystem( "system" );
296 tracker.setUrl( "issue tracker url" );
297 metadata.setIssueManagement( tracker );
299 Organization org = new Organization();
300 org.setName( "org name" );
302 metadata.setOrganization( org );
304 License l = new License();
305 l.setName( "license name" );
306 l.setUrl( "license url" );
307 metadata.addLicense( l );
309 Dependency d = new Dependency();
310 d.setArtifactId( "artifactId" );
311 d.setClassifier( "classifier" );
312 d.setGroupId( "groupId" );
313 d.setScope( "scope" );
314 d.setSystemPath( "system path" );
316 d.setVersion( "version" );
317 d.setOptional( true );
318 metadata.addDependency( d );
320 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
322 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
323 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
324 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
325 assertEquals( "project name", metadata.getName() );
326 assertEquals( "project description", metadata.getDescription() );
327 assertEquals( "the url", metadata.getUrl() );
329 assertEquals( "connection", metadata.getScm().getConnection() );
330 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
331 assertEquals( "url", metadata.getScm().getUrl() );
333 assertEquals( "system", metadata.getCiManagement().getSystem() );
334 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
336 assertEquals( "system", metadata.getIssueManagement().getSystem() );
337 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
339 assertEquals( "org name", metadata.getOrganization().getName() );
340 assertEquals( "url", metadata.getOrganization().getUrl() );
342 assertEquals( 1, metadata.getMailingLists().size() );
343 MailingList retrievedMailingList = metadata.getMailingLists().get( 0 );
344 assertEquals( mailingList.getName(), retrievedMailingList.getName() );
345 assertEquals( mailingList.getMainArchiveUrl(), retrievedMailingList.getMainArchiveUrl() );
346 assertEquals( mailingList.getPostAddress(), retrievedMailingList.getPostAddress() );
347 assertEquals( mailingList.getSubscribeAddress(), retrievedMailingList.getSubscribeAddress() );
348 assertEquals( mailingList.getUnsubscribeAddress(), retrievedMailingList.getUnsubscribeAddress() );
349 assertThat( retrievedMailingList.getOtherArchives() ) //
353 .contains( "other archive" );
355 assertEquals( 1, metadata.getLicenses().size() );
356 l = metadata.getLicenses().get( 0 );
357 assertEquals( "license name", l.getName() );
358 assertEquals( "license url", l.getUrl() );
360 assertEquals( 1, metadata.getDependencies().size() );
361 d = metadata.getDependencies().get( 0 );
362 assertEquals( "artifactId", d.getArtifactId() );
363 assertEquals( "classifier", d.getClassifier() );
364 assertEquals( "groupId", d.getGroupId() );
365 assertEquals( "scope", d.getScope() );
366 assertEquals( "system path", d.getSystemPath() );
367 assertEquals( "type", d.getType() );
368 assertEquals( "version", d.getVersion() );
369 assertTrue( d.isOptional() );
373 public void testGetRepositories()
376 // currently set up this way so the behaviour of both the test and the mock config return the same repository
377 // set as the File implementation just uses the config rather than the content
378 repository.updateNamespace( TEST_REPO_ID, "namespace" );
379 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
381 Collection<String> repositories = repository.getRepositories();
383 assertEquals( "repository.getRepositories() -> " + repositories, //
384 Sets.newLinkedHashSet( TEST_REPO_ID, OTHER_REPO_ID ), //
385 new LinkedHashSet<String>( repositories ) ); //
389 public void testUpdateProjectVersionMetadataIncomplete()
392 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
393 metadata.setId( TEST_PROJECT_VERSION );
394 metadata.setIncomplete( true );
395 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
397 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
398 assertEquals( true, metadata.isIncomplete() );
399 assertNull( metadata.getCiManagement() );
400 assertNull( metadata.getScm() );
401 assertNull( metadata.getIssueManagement() );
402 assertNull( metadata.getOrganization() );
403 assertNull( metadata.getDescription() );
404 assertNull( metadata.getName() );
405 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
406 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
407 assertTrue( metadata.getMailingLists().isEmpty() );
408 assertTrue( metadata.getLicenses().isEmpty() );
409 assertTrue( metadata.getDependencies().isEmpty() );
413 public void testUpdateProjectVersionMetadataWithExistingFacets()
416 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
417 metadata.setId( TEST_PROJECT_VERSION );
418 MetadataFacet facet = new TestMetadataFacet( "baz" );
419 metadata.addFacet( facet );
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() );
425 metadata = new ProjectVersionMetadata();
426 metadata.setId( TEST_PROJECT_VERSION );
427 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
429 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
430 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
431 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
432 assertEquals( "baz", testFacet.getValue() );
436 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
439 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
440 metadata.setId( TEST_PROJECT_VERSION );
441 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
443 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
445 assertThat( metadata.getFacetIds() ).isNotNull().isEmpty();
447 metadata = new ProjectVersionMetadata();
448 metadata.setId( TEST_PROJECT_VERSION );
449 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
451 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
452 assertThat( metadata.getFacetIds() ).isNotNull().isEmpty();
457 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
460 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
461 metadata.setId( TEST_PROJECT_VERSION );
463 Map<String, String> additionalProps = new HashMap<String, String>();
464 additionalProps.put( "deleteKey", "deleteValue" );
466 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
467 metadata.addFacet( facet );
468 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
470 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
472 assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
474 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
475 Map<String, String> facetProperties = testFacet.toProperties();
477 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
479 facetProperties.remove( "deleteKey" );
481 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
482 metadata.addFacet( newTestFacet );
484 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
486 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
488 assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
489 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
490 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
494 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
497 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
498 versionMetadata.setId( TEST_PROJECT_VERSION );
500 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
501 versionMetadata.addFacet( facet );
502 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
504 ArtifactMetadata artifactMetadata = createArtifact();
505 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
508 Collection<ArtifactMetadata> artifacts =
509 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
510 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
512 artifacts = repository.getArtifacts( TEST_REPO_ID );
513 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
515 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
516 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
518 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
519 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
521 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
522 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
526 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
529 ArtifactMetadata metadata = createArtifact();
531 Map<String, String> additionalProps = new HashMap<String, String>();
532 additionalProps.put( "deleteKey", "deleteValue" );
534 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
535 metadata.addFacet( facet );
536 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
538 Collection<ArtifactMetadata> artifacts =
539 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
541 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
542 metadata = artifacts.iterator().next();
544 Collection<String> ids = metadata.getFacetIds();
545 assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
547 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
548 Map<String, String> facetProperties = testFacet.toProperties();
550 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
552 facetProperties.remove( "deleteKey" );
554 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
555 metadata.addFacet( newTestFacet );
557 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
559 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
561 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
562 metadata = artifacts.iterator().next();
564 ids = metadata.getFacetIds();
565 assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
567 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
569 Map<String, String> props = testFacet.toProperties();
570 assertThat( props ).isNotNull().doesNotContainKey( "deleteKey" );
574 public void testUpdateArtifactMetadataWithExistingFacets()
577 ArtifactMetadata metadata = createArtifact();
578 MetadataFacet facet = new TestMetadataFacet( "baz" );
579 metadata.addFacet( facet );
580 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
582 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
583 TEST_PROJECT_VERSION ).iterator().next();
584 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
586 metadata = createArtifact();
587 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
589 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
590 TEST_PROJECT_VERSION ).iterator().next();
591 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
592 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
593 assertEquals( "baz", testFacet.getValue() );
597 public void testUpdateArtifactMetadataWithNoExistingFacets()
600 ArtifactMetadata metadata = createArtifact();
601 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
603 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
604 TEST_PROJECT_VERSION ).iterator().next();
605 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
607 metadata = createArtifact();
608 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
610 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
611 TEST_PROJECT_VERSION ).iterator().next();
612 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
616 public void testGetMetadataFacet()
619 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
621 TestMetadataFacet test =
622 (TestMetadataFacet) repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
624 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
628 public void testGetMetadataFacetWhenEmpty()
631 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
635 public void testGetMetadataFacetWhenUnknownName()
638 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
640 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
644 public void testGetMetadataFacetWhenDefaultValue()
647 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
649 MetadataFacet metadataFacet = repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
651 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), metadataFacet );
655 public void testGetMetadataFacetWhenUnknownFacetId()
658 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
662 public void testGetMetadataFacets()
665 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
667 assertEquals( Collections.singletonList( TEST_NAME ),
668 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
672 public void testGetMetadataFacetsWhenEmpty()
676 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
677 assertTrue( facets.isEmpty() );
681 public void testRemoveFacets()
684 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
686 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
687 assertFalse( facets.isEmpty() );
689 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
691 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
692 assertTrue( facets.isEmpty() );
696 public void testRemoveFacetsWhenEmpty()
699 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
700 assertTrue( facets.isEmpty() );
702 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
704 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
705 assertTrue( facets.isEmpty() );
709 public void testRemoveFacetsWhenUnknown()
712 // testing no exception
713 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
717 public void testRemoveFacetWhenUnknown()
720 // testing no exception
721 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
725 public void testRemoveFacet()
728 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
729 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
731 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
732 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
733 assertFalse( facets.isEmpty() );
735 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
737 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
738 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
739 assertTrue( facets.isEmpty() );
743 public void testRemoveFacetWhenEmpty()
746 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
747 assertThat( facets ).isNotNull().isEmpty();
748 assertThat( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull();
750 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
752 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
753 assertThat( facets ).isNotNull().isEmpty();
754 assertThat( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) ).isNull();
758 public void hasMetadataFacetStart()
761 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
766 public void hasMetadataFacet()
770 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
772 Calendar cal = Calendar.getInstance();
774 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
776 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
778 cal.add( Calendar.MINUTE, 2 );
780 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
782 cal.add( Calendar.MINUTE, 2 );
784 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
786 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
788 assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
790 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
792 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
794 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
796 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
798 assertThat( facets ).isNotNull().isEmpty();
804 public void testGetArtifacts()
807 ArtifactMetadata artifact1 = createArtifact();
808 ArtifactMetadata artifact2 = createArtifact( "pom" );
809 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
810 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
812 Collection<ArtifactMetadata> artifacts =
813 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
814 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
815 Collections.sort( actual, new Comparator<ArtifactMetadata>()
817 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
819 return o1.getId().compareTo( o2.getId() );
822 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
826 public void testGetArtifactVersions()
829 ArtifactMetadata artifact1 = createArtifact();
830 String version1 = "1.0-20091212.012345-1";
831 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
832 artifact1.setVersion( version1 );
833 ArtifactMetadata artifact2 = createArtifact();
834 String version2 = "1.0-20091212.123456-2";
835 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
836 artifact2.setVersion( version2 );
837 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
838 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
840 List<String> versions = new ArrayList<String>(
841 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
842 Collections.sort( versions );
843 assertEquals( Arrays.asList( version1, version2 ), versions );
847 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
850 ArtifactMetadata artifact1 = createArtifact();
851 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
852 ArtifactMetadata artifact2 = createArtifact();
853 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
854 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
855 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
857 Collection<String> versions =
858 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
860 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).containsExactly( TEST_PROJECT_VERSION );
865 public void testGetArtifactsByDateRangeOpen()
868 ArtifactMetadata artifact = createArtifact();
869 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
872 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
874 assertEquals( Collections.singletonList( artifact ), artifacts );
878 public void testGetArtifactsByDateRangeSparseNamespace()
881 String namespace = "org.apache.archiva";
882 ArtifactMetadata artifact = createArtifact();
883 artifact.setNamespace( namespace );
884 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
887 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
889 assertEquals( Collections.singletonList( artifact ), artifacts );
893 public void testGetArtifactsByDateRangeLowerBound()
896 ArtifactMetadata artifact = createArtifact();
897 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
900 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
902 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
904 assertEquals( Collections.singletonList( artifact ), artifacts );
908 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
911 ArtifactMetadata artifact = createArtifact();
912 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
914 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
916 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
918 assertThat( artifacts ).isNotNull().isEmpty();
922 public void testGetArtifactsByDateRangeLowerAndUpperBound()
925 ArtifactMetadata artifact = createArtifact();
926 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
929 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
930 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
932 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper );
934 assertEquals( Collections.singletonList( artifact ), artifacts );
938 public void testGetArtifactsByDateRangeUpperBound()
941 ArtifactMetadata artifact = createArtifact();
942 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
945 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
947 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
949 assertEquals( Collections.singletonList( artifact ), artifacts );
953 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
956 ArtifactMetadata artifact = createArtifact();
957 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
960 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
962 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
964 assertThat( artifacts ).isNotNull().isEmpty();
968 public void testGetArtifactsByRepoId()
971 ArtifactMetadata artifact = createArtifact();
972 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
975 List<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID );
977 assertEquals( Collections.singletonList( artifact ), artifacts );
981 public void testGetArtifactsByRepoIdMultipleCopies()
984 ArtifactMetadata artifact = createArtifact();
985 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
987 ArtifactMetadata secondArtifact = createArtifact();
988 secondArtifact.setRepositoryId( OTHER_REPO_ID );
989 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
992 // test it restricts to the appropriate repository
993 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
994 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
998 public void testGetArtifactsByDateRangeMultipleCopies()
1001 ArtifactMetadata artifact = createArtifact();
1002 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1004 ArtifactMetadata secondArtifact = createArtifact();
1005 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1006 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1009 // test it restricts to the appropriate repository
1010 assertEquals( Collections.singletonList( artifact ),
1011 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1012 assertEquals( Collections.singletonList( secondArtifact ),
1013 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
1017 public void testGetArtifactsByChecksumMultipleCopies()
1020 ArtifactMetadata artifact = createArtifact();
1021 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1023 ArtifactMetadata secondArtifact = createArtifact();
1024 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1025 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1028 // test it restricts to the appropriate repository
1029 assertEquals( Collections.singletonList( artifact ),
1030 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1031 assertEquals( Collections.singletonList( secondArtifact ), new ArrayList<ArtifactMetadata>(
1032 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) ) );
1033 assertEquals( Collections.singletonList( artifact ),
1034 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1035 assertEquals( Collections.singletonList( secondArtifact ),
1036 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) ) );
1040 public void testGetNamespacesWithSparseDepth()
1043 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1045 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
1047 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "org" );
1049 namespaces = repository.getNamespaces( TEST_REPO_ID, "org" );
1050 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "apache" );
1052 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache" );
1053 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "maven" );
1055 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" );
1056 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "shared" );
1060 public void testGetNamespacesWithProjectsPresent()
1063 String namespace = "org.apache.maven.shared";
1064 repository.updateNamespace( TEST_REPO_ID, namespace );
1066 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1067 metadata.setId( TEST_PROJECT_VERSION );
1068 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1070 Collection<String> namespaces = repository.getNamespaces( TEST_REPO_ID, namespace );
1072 assertThat( namespaces ).isNotNull().isEmpty();
1077 public void testGetProjectsWithOtherNamespacesPresent()
1080 ProjectMetadata projectMetadata = new ProjectMetadata();
1081 projectMetadata.setId( TEST_PROJECT );
1082 projectMetadata.setNamespace( "org.apache.maven" );
1083 repository.updateProject( TEST_REPO_ID, projectMetadata );
1085 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1087 Collection<String> projects = repository.getProjects( TEST_REPO_ID, "org.apache.maven" );
1089 assertThat( projects ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_PROJECT );
1093 public void testGetProjectVersionsWithOtherNamespacesPresent()
1096 // an unusual case but technically possible where a project namespace matches another project's name
1098 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1099 versionMetadata.setId( TEST_PROJECT_VERSION );
1100 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1102 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1105 Collection<String> versions =
1106 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project" );
1107 assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1109 versions = repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT );
1111 assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1115 public void testGetArtifactsByChecksumSingleResultMd5()
1118 ArtifactMetadata artifact = createArtifact();
1119 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1122 assertEquals( Collections.singletonList( artifact ),
1123 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1127 public void testGetArtifactsByChecksumSingleResultSha1()
1130 ArtifactMetadata artifact = createArtifact();
1131 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1134 assertEquals( Collections.singletonList( artifact ),
1135 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1139 public void testGetArtifactsByChecksumDeepNamespace()
1142 ArtifactMetadata artifact = createArtifact();
1143 String namespace = "multi.level.ns";
1144 artifact.setNamespace( namespace );
1145 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1148 assertEquals( Collections.singletonList( artifact ),
1149 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1150 assertEquals( Collections.singletonList( artifact ),
1151 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1155 public void testGetArtifactsByChecksumMultipleResult()
1158 ArtifactMetadata artifact1 = createArtifact();
1159 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1161 String newProjectId = "another-project";
1162 ArtifactMetadata artifact2 = createArtifact();
1163 artifact2.setProject( newProjectId );
1164 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1167 List<ArtifactMetadata> artifacts =
1168 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1169 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1170 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1172 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1173 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1174 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1178 public void testGetArtifactsByChecksumNoResult()
1181 ArtifactMetadata artifact = createArtifact();
1182 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1184 Collection<ArtifactMetadata> artifactsByChecksum =
1185 repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1186 assertThat( artifactsByChecksum ).isNotNull().isEmpty();
1191 public void testDeleteRepository()
1194 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1196 ProjectMetadata project1 = new ProjectMetadata();
1197 project1.setNamespace( TEST_NAMESPACE );
1198 project1.setId( "project1" );
1199 repository.updateProject( TEST_REPO_ID, project1 );
1200 ProjectMetadata project2 = new ProjectMetadata();
1201 project2.setNamespace( TEST_NAMESPACE );
1202 project2.setId( "project2" );
1203 repository.updateProject( TEST_REPO_ID, project2 );
1205 ArtifactMetadata artifact1 = createArtifact();
1206 artifact1.setProject( "project1" );
1207 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1208 ArtifactMetadata artifact2 = createArtifact();
1209 artifact2.setProject( "project2" );
1210 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1213 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1214 Collections.sort( expected, new ArtifactMetadataComparator() );
1216 List<ArtifactMetadata> actual =
1217 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1218 Collections.sort( actual, new ArtifactMetadataComparator() );
1220 assertEquals( expected, actual );
1222 repository.removeRepository( TEST_REPO_ID );
1224 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1225 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1230 public void testDeleteArtifact()
1233 ArtifactMetadata artifact = createArtifact();
1234 artifact.addFacet( new TestMetadataFacet( "value" ) );
1236 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1238 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1239 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1241 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1243 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1245 log.info( "versions {}", versions );
1247 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1249 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1251 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1253 log.info( "versions {}", versions );
1255 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1257 assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1258 TEST_PROJECT_VERSION ) ).isNotNull().isEmpty();
1260 assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1261 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1265 public void deleteArtifact()
1268 ArtifactMetadata artifact = createArtifact();
1269 artifact.addFacet( new TestMetadataFacet( "value" ) );
1271 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1273 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1275 Collection<ArtifactMetadata> artifacts =
1276 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1278 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( artifacts ) );
1280 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1282 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1284 assertThat( artifacts ).isNotNull().isEmpty();
1288 public void deleteVersion()
1291 ArtifactMetadata artifact = createArtifact();
1292 artifact.addFacet( new TestMetadataFacet( "value" ) );
1294 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1296 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1298 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1300 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 );
1302 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1304 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1306 assertThat( versions ).isNotNull().isEmpty();
1310 public void deleteProject()
1313 ArtifactMetadata artifact = createArtifact();
1314 artifact.addFacet( new TestMetadataFacet( "value" ) );
1316 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1318 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1320 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1322 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1324 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1326 assertThat( versions ).isNotNull().isEmpty();
1331 public void deleteSnapshotVersion()
1334 ArtifactMetadata artifactOne = createArtifact();
1335 artifactOne.setVersion( "2.0-20120618.214127-1" );
1336 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1337 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1338 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1340 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1342 ArtifactMetadata artifactTwo = createArtifact();
1343 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1344 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1345 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1346 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1348 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1350 Collection<ArtifactMetadata> artifactMetadatas =
1351 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1353 assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1355 log.info( "artifactMetadatas: {}", artifactMetadatas );
1357 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1359 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1361 assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1363 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1365 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1367 assertThat( artifactMetadatas ).isNotNull().isEmpty();
1371 private static ProjectMetadata createProject()
1373 return createProject( TEST_NAMESPACE );
1376 private static ProjectMetadata createProject( String ns )
1378 ProjectMetadata project = new ProjectMetadata();
1379 project.setId( TEST_PROJECT );
1380 project.setNamespace( ns );
1384 private static ArtifactMetadata createArtifact()
1386 return createArtifact( "jar" );
1389 private static ArtifactMetadata createArtifact( String type )
1391 ArtifactMetadata artifact = new ArtifactMetadata();
1392 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1393 artifact.setWhenGathered( new Date() );
1394 artifact.setNamespace( TEST_NAMESPACE );
1395 artifact.setProject( TEST_PROJECT );
1396 artifact.setRepositoryId( TEST_REPO_ID );
1397 artifact.setFileLastModified( System.currentTimeMillis() );
1398 artifact.setVersion( TEST_PROJECT_VERSION );
1399 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1400 artifact.setMd5( TEST_MD5 );
1401 artifact.setSha1( TEST_SHA1 );
1405 private static class ArtifactMetadataComparator
1406 implements Comparator<ArtifactMetadata>
1408 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1410 return a.getProject().compareTo( b.getProject() );
1414 private static class KindOfRepositoryStatistics
1415 implements MetadataFacet
1417 private String value;
1421 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1423 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1425 private KindOfRepositoryStatistics( String value, Date date )
1431 public String getName()
1433 return createNameFormat().format( date );
1436 private static SimpleDateFormat createNameFormat()
1438 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1439 fmt.setTimeZone( UTC_TIME_ZONE );
1443 public String getFacetId()
1445 return KindOfRepositoryStatistics.class.getName();
1448 public Map<String, String> toProperties()
1450 return Collections.emptyMap();
1453 public void fromProperties( Map<String, String> properties )
1459 private static class TestMetadataFacet
1460 implements MetadataFacet
1462 private String testFacetId;
1464 private Map<String, String> additionalProps;
1466 private String value;
1468 private TestMetadataFacet( String value )
1471 testFacetId = TEST_FACET_ID;
1474 private TestMetadataFacet( String facetId, String value )
1477 testFacetId = facetId;
1480 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1482 this( facetId, value );
1483 this.additionalProps = additionalProps;
1486 public String getFacetId()
1491 public String getName()
1496 public Map<String, String> toProperties()
1498 if ( value != null )
1500 if ( additionalProps == null )
1502 return Collections.singletonMap( "foo", value );
1506 Map<String, String> props = new HashMap<String, String>();
1507 props.put( "foo", value );
1509 for ( String key : additionalProps.keySet() )
1511 props.put( key, additionalProps.get( key ) );
1518 return Collections.emptyMap();
1522 public void fromProperties( Map<String, String> properties )
1524 String value = properties.get( "foo" );
1525 if ( value != null )
1530 properties.remove( "foo" );
1532 if ( additionalProps == null )
1534 additionalProps = new HashMap<String, String>();
1537 for ( String key : properties.keySet() )
1539 additionalProps.put( key, properties.get( key ) );
1543 public String getValue()
1549 public String toString()
1551 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1555 public boolean equals( Object o )
1561 if ( o == null || getClass() != o.getClass() )
1566 TestMetadataFacet that = (TestMetadataFacet) o;
1568 if ( value != null ? !value.equals( that.value ) : that.value != null )
1577 public int hashCode()
1579 return value != null ? value.hashCode() : 0;