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.fest.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.fest.assertions.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 ), new LinkedHashSet<String>( repositories ) );
387 public void testUpdateProjectVersionMetadataIncomplete()
390 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
391 metadata.setId( TEST_PROJECT_VERSION );
392 metadata.setIncomplete( true );
393 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
395 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
396 assertEquals( true, metadata.isIncomplete() );
397 assertNull( metadata.getCiManagement() );
398 assertNull( metadata.getScm() );
399 assertNull( metadata.getIssueManagement() );
400 assertNull( metadata.getOrganization() );
401 assertNull( metadata.getDescription() );
402 assertNull( metadata.getName() );
403 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
404 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
405 assertTrue( metadata.getMailingLists().isEmpty() );
406 assertTrue( metadata.getLicenses().isEmpty() );
407 assertTrue( metadata.getDependencies().isEmpty() );
411 public void testUpdateProjectVersionMetadataWithExistingFacets()
414 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
415 metadata.setId( TEST_PROJECT_VERSION );
416 MetadataFacet facet = new TestMetadataFacet( "baz" );
417 metadata.addFacet( facet );
418 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
420 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
421 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
423 metadata = new ProjectVersionMetadata();
424 metadata.setId( TEST_PROJECT_VERSION );
425 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
427 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
428 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
429 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
430 assertEquals( "baz", testFacet.getValue() );
434 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
437 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
438 metadata.setId( TEST_PROJECT_VERSION );
439 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
441 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
443 assertThat( metadata.getFacetIds() ).isNotNull().isEmpty();
445 metadata = new ProjectVersionMetadata();
446 metadata.setId( TEST_PROJECT_VERSION );
447 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
449 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
450 assertThat( metadata.getFacetIds() ).isNotNull().isEmpty();
455 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
458 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
459 metadata.setId( TEST_PROJECT_VERSION );
461 Map<String, String> additionalProps = new HashMap<String, String>();
462 additionalProps.put( "deleteKey", "deleteValue" );
464 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
465 metadata.addFacet( facet );
466 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
468 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
470 assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
472 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
473 Map<String, String> facetProperties = testFacet.toProperties();
475 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
477 facetProperties.remove( "deleteKey" );
479 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
480 metadata.addFacet( newTestFacet );
482 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
484 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
486 assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
487 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
488 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
492 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
495 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
496 versionMetadata.setId( TEST_PROJECT_VERSION );
498 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
499 versionMetadata.addFacet( facet );
500 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
502 ArtifactMetadata artifactMetadata = createArtifact();
503 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
506 Collection<ArtifactMetadata> artifacts =
507 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
508 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
510 artifacts = repository.getArtifacts( TEST_REPO_ID );
511 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
513 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
514 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
516 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
517 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
519 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
520 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
524 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
527 ArtifactMetadata metadata = createArtifact();
529 Map<String, String> additionalProps = new HashMap<String, String>();
530 additionalProps.put( "deleteKey", "deleteValue" );
532 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
533 metadata.addFacet( facet );
534 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
536 Collection<ArtifactMetadata> artifacts =
537 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
539 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
540 metadata = artifacts.iterator().next();
542 Collection<String> ids = metadata.getFacetIds();
543 assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
545 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
546 Map<String, String> facetProperties = testFacet.toProperties();
548 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
550 facetProperties.remove( "deleteKey" );
552 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
553 metadata.addFacet( newTestFacet );
555 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
557 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
559 assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
560 metadata = artifacts.iterator().next();
562 ids = metadata.getFacetIds();
563 assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
565 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
567 Map<String, String> props = testFacet.toProperties();
568 assertThat( props ).isNotNull().doesNotContainKey( "deleteKey" );
572 public void testUpdateArtifactMetadataWithExistingFacets()
575 ArtifactMetadata metadata = createArtifact();
576 MetadataFacet facet = new TestMetadataFacet( "baz" );
577 metadata.addFacet( facet );
578 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
580 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
581 TEST_PROJECT_VERSION ).iterator().next();
582 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
584 metadata = createArtifact();
585 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
587 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
588 TEST_PROJECT_VERSION ).iterator().next();
589 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
590 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
591 assertEquals( "baz", testFacet.getValue() );
595 public void testUpdateArtifactMetadataWithNoExistingFacets()
598 ArtifactMetadata metadata = createArtifact();
599 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
601 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
602 TEST_PROJECT_VERSION ).iterator().next();
603 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
605 metadata = createArtifact();
606 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
608 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
609 TEST_PROJECT_VERSION ).iterator().next();
610 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
614 public void testGetMetadataFacet()
617 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
619 TestMetadataFacet test =
620 (TestMetadataFacet) repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
622 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
626 public void testGetMetadataFacetWhenEmpty()
629 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
633 public void testGetMetadataFacetWhenUnknownName()
636 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
638 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
642 public void testGetMetadataFacetWhenDefaultValue()
645 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
647 MetadataFacet metadataFacet = repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
649 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), metadataFacet );
653 public void testGetMetadataFacetWhenUnknownFacetId()
656 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
660 public void testGetMetadataFacets()
663 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
665 assertEquals( Collections.singletonList( TEST_NAME ),
666 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
670 public void testGetMetadataFacetsWhenEmpty()
674 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
675 assertTrue( facets.isEmpty() );
679 public void testRemoveFacets()
682 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
684 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
685 assertFalse( facets.isEmpty() );
687 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
689 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
690 assertTrue( facets.isEmpty() );
694 public void testRemoveFacetsWhenEmpty()
697 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
698 assertTrue( facets.isEmpty() );
700 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
702 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
703 assertTrue( facets.isEmpty() );
707 public void testRemoveFacetsWhenUnknown()
710 // testing no exception
711 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
715 public void testRemoveFacetWhenUnknown()
718 // testing no exception
719 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
723 public void testRemoveFacet()
726 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
727 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
729 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
730 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
731 assertFalse( facets.isEmpty() );
733 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
735 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
736 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
737 assertTrue( facets.isEmpty() );
741 public void testRemoveFacetWhenEmpty()
744 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
745 assertTrue( facets.isEmpty() );
746 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
748 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
750 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
751 assertTrue( facets.isEmpty() );
752 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
756 public void hasMetadataFacetStart()
759 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
764 public void hasMetadataFacet()
768 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
770 Calendar cal = Calendar.getInstance();
772 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
774 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
776 cal.add( Calendar.MINUTE, 2 );
778 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
780 cal.add( Calendar.MINUTE, 2 );
782 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
784 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
786 assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
788 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
790 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
792 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
794 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
796 assertThat( facets ).isNotNull().isEmpty();
802 public void testGetArtifacts()
805 ArtifactMetadata artifact1 = createArtifact();
806 ArtifactMetadata artifact2 = createArtifact( "pom" );
807 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
808 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
810 Collection<ArtifactMetadata> artifacts =
811 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
812 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
813 Collections.sort( actual, new Comparator<ArtifactMetadata>()
815 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
817 return o1.getId().compareTo( o2.getId() );
820 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
824 public void testGetArtifactVersions()
827 ArtifactMetadata artifact1 = createArtifact();
828 String version1 = "1.0-20091212.012345-1";
829 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
830 artifact1.setVersion( version1 );
831 ArtifactMetadata artifact2 = createArtifact();
832 String version2 = "1.0-20091212.123456-2";
833 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
834 artifact2.setVersion( version2 );
835 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
836 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
838 List<String> versions = new ArrayList<String>(
839 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
840 Collections.sort( versions );
841 assertEquals( Arrays.asList( version1, version2 ), versions );
845 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
848 ArtifactMetadata artifact1 = createArtifact();
849 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
850 ArtifactMetadata artifact2 = createArtifact();
851 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
852 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
853 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
855 Collection<String> versions =
856 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
858 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).containsExactly( TEST_PROJECT_VERSION );
863 public void testGetArtifactsByDateRangeOpen()
866 ArtifactMetadata artifact = createArtifact();
867 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
870 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
872 assertEquals( Collections.singletonList( artifact ), artifacts );
876 public void testGetArtifactsByDateRangeSparseNamespace()
879 String namespace = "org.apache.archiva";
880 ArtifactMetadata artifact = createArtifact();
881 artifact.setNamespace( namespace );
882 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
885 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
887 assertEquals( Collections.singletonList( artifact ), artifacts );
891 public void testGetArtifactsByDateRangeLowerBound()
894 ArtifactMetadata artifact = createArtifact();
895 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
898 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
900 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
902 assertEquals( Collections.singletonList( artifact ), artifacts );
906 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
909 ArtifactMetadata artifact = createArtifact();
910 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
912 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
914 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
916 assertThat( artifacts ).isNotNull().isEmpty();
920 public void testGetArtifactsByDateRangeLowerAndUpperBound()
923 ArtifactMetadata artifact = createArtifact();
924 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
927 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
928 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
930 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper );
932 assertEquals( Collections.singletonList( artifact ), artifacts );
936 public void testGetArtifactsByDateRangeUpperBound()
939 ArtifactMetadata artifact = createArtifact();
940 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
943 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
945 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
947 assertEquals( Collections.singletonList( artifact ), artifacts );
951 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
954 ArtifactMetadata artifact = createArtifact();
955 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
958 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
960 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
962 assertThat( artifacts ).isNotNull().isEmpty();
966 public void testGetArtifactsByRepoId()
969 ArtifactMetadata artifact = createArtifact();
970 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
973 List<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID );
975 assertEquals( Collections.singletonList( artifact ), artifacts );
979 public void testGetArtifactsByRepoIdMultipleCopies()
982 ArtifactMetadata artifact = createArtifact();
983 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
985 ArtifactMetadata secondArtifact = createArtifact();
986 secondArtifact.setRepositoryId( OTHER_REPO_ID );
987 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
990 // test it restricts to the appropriate repository
991 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
992 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
996 public void testGetArtifactsByDateRangeMultipleCopies()
999 ArtifactMetadata artifact = createArtifact();
1000 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1002 ArtifactMetadata secondArtifact = createArtifact();
1003 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1004 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1007 // test it restricts to the appropriate repository
1008 assertEquals( Collections.singletonList( artifact ),
1009 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1010 assertEquals( Collections.singletonList( secondArtifact ),
1011 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
1015 public void testGetArtifactsByChecksumMultipleCopies()
1018 ArtifactMetadata artifact = createArtifact();
1019 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1021 ArtifactMetadata secondArtifact = createArtifact();
1022 secondArtifact.setRepositoryId( OTHER_REPO_ID );
1023 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
1026 // test it restricts to the appropriate repository
1027 assertEquals( Collections.singletonList( artifact ),
1028 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1029 assertEquals( Collections.singletonList( secondArtifact ), new ArrayList<ArtifactMetadata>(
1030 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) ) );
1031 assertEquals( Collections.singletonList( artifact ),
1032 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1033 assertEquals( Collections.singletonList( secondArtifact ),
1034 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) ) );
1038 public void testGetNamespacesWithSparseDepth()
1041 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1043 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
1045 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "org" );
1047 namespaces = repository.getNamespaces( TEST_REPO_ID, "org" );
1048 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "apache" );
1050 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache" );
1051 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "maven" );
1053 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" );
1054 assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "shared" );
1058 public void testGetNamespacesWithProjectsPresent()
1061 String namespace = "org.apache.maven.shared";
1062 repository.updateNamespace( TEST_REPO_ID, namespace );
1064 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1065 metadata.setId( TEST_PROJECT_VERSION );
1066 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1068 Collection<String> namespaces = repository.getNamespaces( TEST_REPO_ID, namespace );
1070 assertThat( namespaces ).isNotNull().isEmpty();
1075 public void testGetProjectsWithOtherNamespacesPresent()
1078 ProjectMetadata projectMetadata = new ProjectMetadata();
1079 projectMetadata.setId( TEST_PROJECT );
1080 projectMetadata.setNamespace( "org.apache.maven" );
1081 repository.updateProject( TEST_REPO_ID, projectMetadata );
1083 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1085 Collection<String> projects = repository.getProjects( TEST_REPO_ID, "org.apache.maven" );
1087 assertThat( projects ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_PROJECT );
1091 public void testGetProjectVersionsWithOtherNamespacesPresent()
1094 // an unusual case but technically possible where a project namespace matches another project's name
1096 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1097 versionMetadata.setId( TEST_PROJECT_VERSION );
1098 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1100 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1103 Collection<String> versions =
1104 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project" );
1105 assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1107 versions = repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT );
1109 assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1113 public void testGetArtifactsByChecksumSingleResultMd5()
1116 ArtifactMetadata artifact = createArtifact();
1117 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1120 assertEquals( Collections.singletonList( artifact ),
1121 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1125 public void testGetArtifactsByChecksumSingleResultSha1()
1128 ArtifactMetadata artifact = createArtifact();
1129 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1132 assertEquals( Collections.singletonList( artifact ),
1133 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1137 public void testGetArtifactsByChecksumDeepNamespace()
1140 ArtifactMetadata artifact = createArtifact();
1141 String namespace = "multi.level.ns";
1142 artifact.setNamespace( namespace );
1143 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1146 assertEquals( Collections.singletonList( artifact ),
1147 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) ) );
1148 assertEquals( Collections.singletonList( artifact ),
1149 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) ) );
1153 public void testGetArtifactsByChecksumMultipleResult()
1156 ArtifactMetadata artifact1 = createArtifact();
1157 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1159 String newProjectId = "another-project";
1160 ArtifactMetadata artifact2 = createArtifact();
1161 artifact2.setProject( newProjectId );
1162 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1165 List<ArtifactMetadata> artifacts =
1166 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1167 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1168 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1170 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1171 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1172 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1176 public void testGetArtifactsByChecksumNoResult()
1179 ArtifactMetadata artifact = createArtifact();
1180 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1182 Collection<ArtifactMetadata> artifactsByChecksum =
1183 repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1184 assertThat( artifactsByChecksum ).isNotNull().isEmpty();
1189 public void testDeleteRepository()
1192 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1194 ProjectMetadata project1 = new ProjectMetadata();
1195 project1.setNamespace( TEST_NAMESPACE );
1196 project1.setId( "project1" );
1197 repository.updateProject( TEST_REPO_ID, project1 );
1198 ProjectMetadata project2 = new ProjectMetadata();
1199 project2.setNamespace( TEST_NAMESPACE );
1200 project2.setId( "project2" );
1201 repository.updateProject( TEST_REPO_ID, project2 );
1203 ArtifactMetadata artifact1 = createArtifact();
1204 artifact1.setProject( "project1" );
1205 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1206 ArtifactMetadata artifact2 = createArtifact();
1207 artifact2.setProject( "project2" );
1208 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1211 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1212 Collections.sort( expected, new ArtifactMetadataComparator() );
1214 List<ArtifactMetadata> actual =
1215 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1216 Collections.sort( actual, new ArtifactMetadataComparator() );
1218 assertEquals( expected, actual );
1220 repository.removeRepository( TEST_REPO_ID );
1222 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1223 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1228 public void testDeleteArtifact()
1231 ArtifactMetadata artifact = createArtifact();
1232 artifact.addFacet( new TestMetadataFacet( "value" ) );
1234 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1236 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1237 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1239 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1241 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1243 log.info( "versions {}", versions );
1245 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1247 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1249 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1251 log.info( "versions {}", versions );
1253 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1255 assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1256 TEST_PROJECT_VERSION ) ).isNotNull().isEmpty();
1258 assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1259 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1263 public void deleteArtifact()
1266 ArtifactMetadata artifact = createArtifact();
1267 artifact.addFacet( new TestMetadataFacet( "value" ) );
1269 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1271 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1273 Collection<ArtifactMetadata> artifacts =
1274 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1276 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( artifacts ) );
1278 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1280 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1282 assertThat( artifacts ).isNotNull().isEmpty();
1286 public void deleteVersion()
1289 ArtifactMetadata artifact = createArtifact();
1290 artifact.addFacet( new TestMetadataFacet( "value" ) );
1292 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1294 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1296 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1298 assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 );
1300 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1302 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1304 assertThat( versions ).isNotNull().isEmpty();
1308 public void deleteProject()
1311 ArtifactMetadata artifact = createArtifact();
1312 artifact.addFacet( new TestMetadataFacet( "value" ) );
1314 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1316 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1318 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1320 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1322 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1324 assertThat( versions ).isNotNull().isEmpty();
1329 public void deleteSnapshotVersion()
1332 ArtifactMetadata artifactOne = createArtifact();
1333 artifactOne.setVersion( "2.0-20120618.214127-1" );
1334 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1335 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1336 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1338 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1340 ArtifactMetadata artifactTwo = createArtifact();
1341 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1342 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1343 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1344 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1346 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1348 Collection<ArtifactMetadata> artifactMetadatas =
1349 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1351 assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1353 log.info( "artifactMetadatas: {}", artifactMetadatas );
1355 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1357 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1359 assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1361 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1363 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1365 assertThat( artifactMetadatas ).isNotNull().isEmpty();
1369 private static ProjectMetadata createProject()
1371 return createProject( TEST_NAMESPACE );
1374 private static ProjectMetadata createProject( String ns )
1376 ProjectMetadata project = new ProjectMetadata();
1377 project.setId( TEST_PROJECT );
1378 project.setNamespace( ns );
1382 private static ArtifactMetadata createArtifact()
1384 return createArtifact( "jar" );
1387 private static ArtifactMetadata createArtifact( String type )
1389 ArtifactMetadata artifact = new ArtifactMetadata();
1390 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1391 artifact.setWhenGathered( new Date() );
1392 artifact.setNamespace( TEST_NAMESPACE );
1393 artifact.setProject( TEST_PROJECT );
1394 artifact.setRepositoryId( TEST_REPO_ID );
1395 artifact.setFileLastModified( System.currentTimeMillis() );
1396 artifact.setVersion( TEST_PROJECT_VERSION );
1397 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1398 artifact.setMd5( TEST_MD5 );
1399 artifact.setSha1( TEST_SHA1 );
1403 private static class ArtifactMetadataComparator
1404 implements Comparator<ArtifactMetadata>
1406 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1408 return a.getProject().compareTo( b.getProject() );
1412 private static class KindOfRepositoryStatistics
1413 implements MetadataFacet
1415 private String value;
1419 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1421 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1423 private KindOfRepositoryStatistics( String value, Date date )
1429 public String getName()
1431 return createNameFormat().format( date );
1434 private static SimpleDateFormat createNameFormat()
1436 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1437 fmt.setTimeZone( UTC_TIME_ZONE );
1441 public String getFacetId()
1443 return KindOfRepositoryStatistics.class.getName();
1446 public Map<String, String> toProperties()
1448 return Collections.emptyMap();
1451 public void fromProperties( Map<String, String> properties )
1457 private static class TestMetadataFacet
1458 implements MetadataFacet
1460 private String testFacetId;
1462 private Map<String, String> additionalProps;
1464 private String value;
1466 private TestMetadataFacet( String value )
1469 testFacetId = TEST_FACET_ID;
1472 private TestMetadataFacet( String facetId, String value )
1475 testFacetId = facetId;
1478 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1480 this( facetId, value );
1481 this.additionalProps = additionalProps;
1484 public String getFacetId()
1489 public String getName()
1494 public Map<String, String> toProperties()
1496 if ( value != null )
1498 if ( additionalProps == null )
1500 return Collections.singletonMap( "foo", value );
1504 Map<String, String> props = new HashMap<String, String>();
1505 props.put( "foo", value );
1507 for ( String key : additionalProps.keySet() )
1509 props.put( key, additionalProps.get( key ) );
1516 return Collections.emptyMap();
1520 public void fromProperties( Map<String, String> properties )
1522 String value = properties.get( "foo" );
1523 if ( value != null )
1528 properties.remove( "foo" );
1530 if ( additionalProps == null )
1532 additionalProps = new HashMap<String, String>();
1535 for ( String key : properties.keySet() )
1537 additionalProps.put( key, properties.get( key ) );
1541 public String getValue()
1547 public String toString()
1549 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1553 public boolean equals( Object o )
1559 if ( o == null || getClass() != o.getClass() )
1564 TestMetadataFacet that = (TestMetadataFacet) o;
1566 if ( value != null ? !value.equals( that.value ) : that.value != null )
1575 public int hashCode()
1577 return value != null ? value.hashCode() : 0;