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.assertions.api.Assertions;
37 import org.fest.util.Sets;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.test.context.ContextConfiguration;
44 import java.text.SimpleDateFormat;
45 import java.util.ArrayList;
46 import java.util.Arrays;
47 import java.util.Calendar;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.Comparator;
51 import java.util.Date;
52 import java.util.HashMap;
53 import java.util.LinkedHashSet;
54 import java.util.List;
56 import java.util.TimeZone;
58 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
59 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
60 public abstract class AbstractMetadataRepositoryTest
63 protected static final String OTHER_REPO_ID = "other-repo";
65 protected MetadataRepository repository;
67 protected static final String TEST_REPO_ID = "test";
69 private static final String TEST_PROJECT = "projectId";
71 private static final String TEST_NAMESPACE = "namespace";
73 private static final String TEST_PROJECT_VERSION = "1.0";
75 private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
77 private static final String TEST_FACET_ID = "test-facet-id";
79 private static final String TEST_NAME = "test/name";
81 private static final String TEST_VALUE = "test-value";
83 private static final String UNKNOWN = "unknown";
85 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
87 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
89 private static final String TEST_METADATA_VALUE = "test-metadata";
91 protected Logger log = LoggerFactory.getLogger( getClass() );
93 protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
95 Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
96 factories.put( TEST_FACET_ID, new MetadataFacetFactory()
98 public MetadataFacet createMetadataFacet()
100 return new TestMetadataFacet( TEST_METADATA_VALUE );
103 public MetadataFacet createMetadataFacet( String repositoryId, String name )
105 return new TestMetadataFacet( TEST_METADATA_VALUE );
109 // add to ensure we don't accidentally create an empty facet ID.
110 factories.put( "", new MetadataFacetFactory()
112 public MetadataFacet createMetadataFacet()
114 return new TestMetadataFacet( "", TEST_VALUE );
117 public MetadataFacet createMetadataFacet( String repositoryId, String name )
119 return new TestMetadataFacet( "", TEST_VALUE );
126 public void testRootNamespaceWithNoMetadataRepository()
129 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
130 Assertions.assertThat( namespaces ).isNotNull().isEmpty();
134 public void testGetNamespaceOnly()
137 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
139 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
141 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
142 TEST_NAMESPACE ).hasSize( 1 );
144 repository.removeNamespace( TEST_REPO_ID, TEST_NAMESPACE );
146 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
151 public void testGetProjectOnly()
154 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
155 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
157 ProjectMetadata project = new ProjectMetadata();
158 project.setId( TEST_PROJECT );
159 project.setNamespace( TEST_NAMESPACE );
161 repository.updateProject( TEST_REPO_ID, project );
163 project = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
164 assertEquals( TEST_PROJECT, project.getId() );
165 assertEquals( TEST_NAMESPACE, project.getNamespace() );
167 // test that namespace is also constructed
169 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
171 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().contains( TEST_NAMESPACE ).hasSize( 1 );
175 public void testGetProjectVersionOnly()
178 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
179 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
180 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
182 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
183 metadata.setId( TEST_PROJECT_VERSION );
185 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
187 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
188 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
190 // test that namespace and project is also constructed
191 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
193 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_NAMESPACE );
195 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
196 assertNotNull( projectMetadata );
197 assertEquals( TEST_PROJECT, projectMetadata.getId() );
198 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
202 public void testGetArtifactOnly()
205 assertEquals( Collections.<ArtifactMetadata>emptyList(), new ArrayList<ArtifactMetadata>(
206 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
207 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
208 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
210 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
212 ArtifactMetadata metadata = createArtifact();
214 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
216 Collection<ArtifactMetadata> artifacts =
217 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
218 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
220 // test that namespace, project and project version is also constructed
222 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
223 TEST_NAMESPACE ).hasSize( 1 );
225 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
226 assertEquals( TEST_PROJECT, projectMetadata.getId() );
227 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
229 ProjectVersionMetadata projectVersionMetadata =
230 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
231 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
235 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
238 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
239 metadata.setId( TEST_PROJECT_VERSION );
240 MailingList mailingList = new MailingList();
241 mailingList.setName( "Foo List" );
242 mailingList.setOtherArchives( Collections.<String>emptyList() );
243 metadata.setMailingLists( Arrays.asList( mailingList ) );
244 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
246 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
247 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
249 List<MailingList> mailingLists = metadata.getMailingLists();
251 Assertions.assertThat( mailingLists ).isNotNull().isNotEmpty().hasSize( 1 );
253 mailingList = metadata.getMailingLists().get( 0 );
254 assertEquals( "Foo List", mailingList.getName() );
256 List<String> others = mailingList.getOtherArchives();
257 Assertions.assertThat( others ).isNotNull().isEmpty();
261 public void testUpdateProjectVersionMetadataWithAllElements()
264 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
265 metadata.setId( TEST_PROJECT_VERSION );
267 metadata.setName( "project name" );
268 metadata.setDescription( "project description" );
270 MailingList mailingList = new MailingList();
271 mailingList.setName( "Foo List" );
272 mailingList.setOtherArchives( Arrays.asList( "other archive" ) );
273 metadata.setMailingLists( Arrays.asList( mailingList ) );
276 scm.setConnection( "connection" );
277 scm.setDeveloperConnection( "dev conn" );
279 metadata.setScm( scm );
281 CiManagement ci = new CiManagement();
282 ci.setSystem( "system" );
283 ci.setUrl( "ci url" );
284 metadata.setCiManagement( ci );
286 IssueManagement tracker = new IssueManagement();
287 tracker.setSystem( "system" );
288 tracker.setUrl( "issue tracker url" );
289 metadata.setIssueManagement( tracker );
291 Organization org = new Organization();
292 org.setName( "org name" );
294 metadata.setOrganization( org );
296 License l = new License();
297 l.setName( "license name" );
298 l.setUrl( "license url" );
299 metadata.addLicense( l );
301 Dependency d = new Dependency();
302 d.setArtifactId( "artifactId" );
303 d.setClassifier( "classifier" );
304 d.setGroupId( "groupId" );
305 d.setScope( "scope" );
306 d.setSystemPath( "system path" );
308 d.setVersion( "version" );
309 d.setOptional( true );
310 metadata.addDependency( d );
312 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
314 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
315 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
316 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
317 assertEquals( "project name", metadata.getName() );
318 assertEquals( "project description", metadata.getDescription() );
320 assertEquals( 1, metadata.getMailingLists().size() );
321 mailingList = metadata.getMailingLists().get( 0 );
322 assertEquals( "Foo List", mailingList.getName() );
323 //assertEquals( Collections.singletonList( "other archive" ), mailingList.getOtherArchives() );
324 Assertions.assertThat( mailingList.getOtherArchives() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "other archive" );
326 assertEquals( "connection", metadata.getScm().getConnection() );
327 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
328 assertEquals( "url", metadata.getScm().getUrl() );
330 assertEquals( "system", metadata.getCiManagement().getSystem() );
331 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
333 assertEquals( "system", metadata.getIssueManagement().getSystem() );
334 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
336 assertEquals( "org name", metadata.getOrganization().getName() );
337 assertEquals( "url", metadata.getOrganization().getUrl() );
339 assertEquals( 1, metadata.getLicenses().size() );
340 l = metadata.getLicenses().get( 0 );
341 assertEquals( "license name", l.getName() );
342 assertEquals( "license url", l.getUrl() );
344 assertEquals( 1, metadata.getDependencies().size() );
345 d = metadata.getDependencies().get( 0 );
346 assertEquals( "artifactId", d.getArtifactId() );
347 assertEquals( "classifier", d.getClassifier() );
348 assertEquals( "groupId", d.getGroupId() );
349 assertEquals( "scope", d.getScope() );
350 assertEquals( "system path", d.getSystemPath() );
351 assertEquals( "type", d.getType() );
352 assertEquals( "version", d.getVersion() );
353 assertTrue( d.isOptional() );
357 public void testGetRepositories()
360 // currently set up this way so the behaviour of both the test and the mock config return the same repository
361 // set as the File implementation just uses the config rather than the content
362 repository.updateNamespace( TEST_REPO_ID, "namespace" );
363 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
365 Collection<String> repositories = repository.getRepositories();
367 assertEquals( "repository.getRepositories() -> " + repositories,
368 Sets.newLinkedHashSet( TEST_REPO_ID, OTHER_REPO_ID ), new LinkedHashSet<String>( repositories ) );
372 public void testUpdateProjectVersionMetadataIncomplete()
375 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
376 metadata.setId( TEST_PROJECT_VERSION );
377 metadata.setIncomplete( true );
378 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
380 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
381 assertEquals( true, metadata.isIncomplete() );
382 assertNull( metadata.getCiManagement() );
383 assertNull( metadata.getScm() );
384 assertNull( metadata.getIssueManagement() );
385 assertNull( metadata.getOrganization() );
386 assertNull( metadata.getDescription() );
387 assertNull( metadata.getName() );
388 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
389 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
390 assertTrue( metadata.getMailingLists().isEmpty() );
391 assertTrue( metadata.getLicenses().isEmpty() );
392 assertTrue( metadata.getDependencies().isEmpty() );
396 public void testUpdateProjectVersionMetadataWithExistingFacets()
399 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
400 metadata.setId( TEST_PROJECT_VERSION );
401 MetadataFacet facet = new TestMetadataFacet( "baz" );
402 metadata.addFacet( facet );
403 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
405 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
406 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
408 metadata = new ProjectVersionMetadata();
409 metadata.setId( TEST_PROJECT_VERSION );
410 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
412 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
413 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
414 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
415 assertEquals( "baz", testFacet.getValue() );
419 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
422 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
423 metadata.setId( TEST_PROJECT_VERSION );
424 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
426 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
427 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
429 metadata = new ProjectVersionMetadata();
430 metadata.setId( TEST_PROJECT_VERSION );
431 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
433 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
434 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
438 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
441 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
442 metadata.setId( TEST_PROJECT_VERSION );
444 Map<String, String> additionalProps = new HashMap<String, String>();
445 additionalProps.put( "deleteKey", "deleteValue" );
447 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
448 metadata.addFacet( facet );
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 );
453 Assertions.assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
455 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
456 Map<String, String> facetProperties = testFacet.toProperties();
458 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
460 facetProperties.remove( "deleteKey" );
462 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
463 metadata.addFacet( newTestFacet );
465 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
467 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
469 Assertions.assertThat( metadata.getFacetIds() ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
470 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
471 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
475 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
478 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
479 versionMetadata.setId( TEST_PROJECT_VERSION );
481 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
482 versionMetadata.addFacet( facet );
483 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
485 ArtifactMetadata artifactMetadata = createArtifact();
486 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
489 Collection<ArtifactMetadata> artifacts =
490 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
491 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
493 artifacts = repository.getArtifacts( TEST_REPO_ID );
494 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
496 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
497 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
499 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
500 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
502 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
503 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
507 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
510 ArtifactMetadata metadata = createArtifact();
512 Map<String, String> additionalProps = new HashMap<String, String>();
513 additionalProps.put( "deleteKey", "deleteValue" );
515 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
516 metadata.addFacet( facet );
517 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
519 Collection<ArtifactMetadata> artifacts =
520 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
522 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
523 metadata = artifacts.iterator().next();
525 Collection<String> ids = metadata.getFacetIds();
526 Assertions.assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
528 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
529 Map<String, String> facetProperties = testFacet.toProperties();
531 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
533 facetProperties.remove( "deleteKey" );
535 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
536 metadata.addFacet( newTestFacet );
538 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
540 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
542 Assertions.assertThat( artifacts ).isNotNull().isNotEmpty().hasSize( 1 );
543 metadata = artifacts.iterator().next();
545 ids = metadata.getFacetIds();
546 Assertions.assertThat( ids ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_FACET_ID );
548 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
550 Map<String, String> props = testFacet.toProperties();
551 Assertions.assertThat( props ).isNotNull().doesNotContainKey( "deleteKey" );
555 public void testUpdateArtifactMetadataWithExistingFacets()
558 ArtifactMetadata metadata = createArtifact();
559 MetadataFacet facet = new TestMetadataFacet( "baz" );
560 metadata.addFacet( facet );
561 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
563 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
564 TEST_PROJECT_VERSION ).iterator().next();
565 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
567 metadata = createArtifact();
568 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
570 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
571 TEST_PROJECT_VERSION ).iterator().next();
572 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
573 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
574 assertEquals( "baz", testFacet.getValue() );
578 public void testUpdateArtifactMetadataWithNoExistingFacets()
581 ArtifactMetadata metadata = createArtifact();
582 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
584 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
585 TEST_PROJECT_VERSION ).iterator().next();
586 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
588 metadata = createArtifact();
589 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
591 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
592 TEST_PROJECT_VERSION ).iterator().next();
593 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
597 public void testGetMetadataFacet()
600 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
602 TestMetadataFacet test =
603 (TestMetadataFacet) repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
605 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
609 public void testGetMetadataFacetWhenEmpty()
612 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
616 public void testGetMetadataFacetWhenUnknownName()
619 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
621 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
625 public void testGetMetadataFacetWhenDefaultValue()
628 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
630 MetadataFacet metadataFacet = repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
632 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), metadataFacet );
636 public void testGetMetadataFacetWhenUnknownFacetId()
639 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
643 public void testGetMetadataFacets()
646 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
648 assertEquals( Collections.singletonList( TEST_NAME ),
649 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
653 public void testGetMetadataFacetsWhenEmpty()
657 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
658 assertTrue( facets.isEmpty() );
662 public void testRemoveFacets()
665 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
667 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
668 assertFalse( facets.isEmpty() );
670 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
672 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
673 assertTrue( facets.isEmpty() );
677 public void testRemoveFacetsWhenEmpty()
680 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
681 assertTrue( facets.isEmpty() );
683 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
685 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
686 assertTrue( facets.isEmpty() );
690 public void testRemoveFacetsWhenUnknown()
693 // testing no exception
694 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
698 public void testRemoveFacetWhenUnknown()
701 // testing no exception
702 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
706 public void testRemoveFacet()
709 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
710 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
712 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
713 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
714 assertFalse( facets.isEmpty() );
716 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
718 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
719 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
720 assertTrue( facets.isEmpty() );
724 public void testRemoveFacetWhenEmpty()
727 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
728 assertTrue( facets.isEmpty() );
729 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
731 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
733 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
734 assertTrue( facets.isEmpty() );
735 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
739 public void hasMetadataFacetStart()
742 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
747 public void hasMetadataFacet()
751 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
753 Calendar cal = Calendar.getInstance();
755 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
757 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
759 cal.add( Calendar.MINUTE, 2 );
761 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
763 cal.add( Calendar.MINUTE, 2 );
765 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
767 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
769 Assertions.assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
771 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
773 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
775 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
777 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
779 Assertions.assertThat( facets ).isNotNull().isEmpty();
785 public void testGetArtifacts()
788 ArtifactMetadata artifact1 = createArtifact();
789 ArtifactMetadata artifact2 = createArtifact( "pom" );
790 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
791 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
793 Collection<ArtifactMetadata> artifacts =
794 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
795 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
796 Collections.sort( actual, new Comparator<ArtifactMetadata>()
798 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
800 return o1.getId().compareTo( o2.getId() );
803 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
807 public void testGetArtifactVersions()
810 ArtifactMetadata artifact1 = createArtifact();
811 String version1 = "1.0-20091212.012345-1";
812 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
813 artifact1.setVersion( version1 );
814 ArtifactMetadata artifact2 = createArtifact();
815 String version2 = "1.0-20091212.123456-2";
816 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
817 artifact2.setVersion( version2 );
818 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
819 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
821 List<String> versions = new ArrayList<String>(
822 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
823 Collections.sort( versions );
824 assertEquals( Arrays.asList( version1, version2 ), versions );
828 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
831 ArtifactMetadata artifact1 = createArtifact();
832 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
833 ArtifactMetadata artifact2 = createArtifact();
834 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
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 Collection<String> versions =
839 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
841 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).containsExactly( TEST_PROJECT_VERSION );
846 public void testGetArtifactsByDateRangeOpen()
849 ArtifactMetadata artifact = createArtifact();
850 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
853 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
855 assertEquals( Collections.singletonList( artifact ), artifacts );
859 public void testGetArtifactsByDateRangeSparseNamespace()
862 String namespace = "org.apache.archiva";
863 ArtifactMetadata artifact = createArtifact();
864 artifact.setNamespace( namespace );
865 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
868 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
870 assertEquals( Collections.singletonList( artifact ), artifacts );
874 public void testGetArtifactsByDateRangeLowerBound()
877 ArtifactMetadata artifact = createArtifact();
878 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
881 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
883 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
885 assertEquals( Collections.singletonList( artifact ), artifacts );
889 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
892 ArtifactMetadata artifact = createArtifact();
893 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
895 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
897 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
899 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
903 public void testGetArtifactsByDateRangeLowerAndUpperBound()
906 ArtifactMetadata artifact = createArtifact();
907 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
910 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
911 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
913 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper );
915 assertEquals( Collections.singletonList( artifact ), artifacts );
919 public void testGetArtifactsByDateRangeUpperBound()
922 ArtifactMetadata artifact = createArtifact();
923 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
926 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
928 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
930 assertEquals( Collections.singletonList( artifact ), artifacts );
934 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
937 ArtifactMetadata artifact = createArtifact();
938 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
941 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
943 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
945 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
949 public void testGetArtifactsByRepoId()
952 ArtifactMetadata artifact = createArtifact();
953 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
956 List<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID );
958 assertEquals( Collections.singletonList( artifact ), artifacts );
962 public void testGetArtifactsByRepoIdMultipleCopies()
965 ArtifactMetadata artifact = createArtifact();
966 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
968 ArtifactMetadata secondArtifact = createArtifact();
969 secondArtifact.setRepositoryId( OTHER_REPO_ID );
970 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
973 // test it restricts to the appropriate repository
974 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
975 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
979 public void testGetArtifactsByDateRangeMultipleCopies()
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 ),
992 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
993 assertEquals( Collections.singletonList( secondArtifact ),
994 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
998 public void testGetArtifactsByChecksumMultipleCopies()
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.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1012 assertEquals( Collections.singletonList( secondArtifact ),
1013 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) );
1014 assertEquals( Collections.singletonList( artifact ),
1015 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1016 assertEquals( Collections.singletonList( secondArtifact ),
1017 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) );
1021 public void testGetNamespacesWithSparseDepth()
1024 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1026 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
1028 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "org" );
1030 namespaces = repository.getNamespaces( TEST_REPO_ID, "org" );
1031 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "apache" );
1033 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache" );
1034 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "maven" );
1036 namespaces = repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" );
1037 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "shared" );
1041 public void testGetNamespacesWithProjectsPresent()
1044 String namespace = "org.apache.maven.shared";
1045 repository.updateNamespace( TEST_REPO_ID, namespace );
1047 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1048 metadata.setId( TEST_PROJECT_VERSION );
1049 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1051 Collection<String> namespaces = repository.getNamespaces( TEST_REPO_ID, namespace );
1053 Assertions.assertThat( namespaces ).isNotNull().isEmpty();
1058 public void testGetProjectsWithOtherNamespacesPresent()
1061 ProjectMetadata projectMetadata = new ProjectMetadata();
1062 projectMetadata.setId( TEST_PROJECT );
1063 projectMetadata.setNamespace( "org.apache.maven" );
1064 repository.updateProject( TEST_REPO_ID, projectMetadata );
1066 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1068 Collection<String> projects = repository.getProjects( TEST_REPO_ID, "org.apache.maven" );
1070 Assertions.assertThat( projects ).isNotNull().isNotEmpty().hasSize( 1 ).contains( TEST_PROJECT );
1074 public void testGetProjectVersionsWithOtherNamespacesPresent()
1077 // an unusual case but technically possible where a project namespace matches another project's name
1079 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1080 versionMetadata.setId( TEST_PROJECT_VERSION );
1081 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1083 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1086 Collection<String> versions =
1087 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project" );
1088 Assertions.assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1090 versions = repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT );
1092 Assertions.assertThat( versions ).isNotNull().isNotEmpty().contains( TEST_PROJECT_VERSION );
1096 public void testGetArtifactsByChecksumSingleResultMd5()
1099 ArtifactMetadata artifact = createArtifact();
1100 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1103 assertEquals( Collections.singletonList( artifact ),
1104 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1108 public void testGetArtifactsByChecksumSingleResultSha1()
1111 ArtifactMetadata artifact = createArtifact();
1112 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1115 assertEquals( Collections.singletonList( artifact ),
1116 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1120 public void testGetArtifactsByChecksumDeepNamespace()
1123 ArtifactMetadata artifact = createArtifact();
1124 String namespace = "multi.level.ns";
1125 artifact.setNamespace( namespace );
1126 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1129 assertEquals( Collections.singletonList( artifact ),
1130 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1131 assertEquals( Collections.singletonList( artifact ),
1132 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1136 public void testGetArtifactsByChecksumMultipleResult()
1139 ArtifactMetadata artifact1 = createArtifact();
1140 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1142 String newProjectId = "another-project";
1143 ArtifactMetadata artifact2 = createArtifact();
1144 artifact2.setProject( newProjectId );
1145 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1148 List<ArtifactMetadata> artifacts =
1149 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1150 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1151 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1153 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1154 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1155 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1159 public void testGetArtifactsByChecksumNoResult()
1162 ArtifactMetadata artifact = createArtifact();
1163 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1165 List<ArtifactMetadata> artifactsByChecksum = repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1166 assertEquals( Collections.<ArtifactMetadata>emptyList(), artifactsByChecksum );
1171 public void testDeleteRepository()
1174 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1176 ProjectMetadata project1 = new ProjectMetadata();
1177 project1.setNamespace( TEST_NAMESPACE );
1178 project1.setId( "project1" );
1179 repository.updateProject( TEST_REPO_ID, project1 );
1180 ProjectMetadata project2 = new ProjectMetadata();
1181 project2.setNamespace( TEST_NAMESPACE );
1182 project2.setId( "project2" );
1183 repository.updateProject( TEST_REPO_ID, project2 );
1185 ArtifactMetadata artifact1 = createArtifact();
1186 artifact1.setProject( "project1" );
1187 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1188 ArtifactMetadata artifact2 = createArtifact();
1189 artifact2.setProject( "project2" );
1190 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1193 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1194 Collections.sort( expected, new ArtifactMetadataComparator() );
1196 List<ArtifactMetadata> actual =
1197 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1198 Collections.sort( actual, new ArtifactMetadataComparator() );
1200 assertEquals( expected, actual );
1202 repository.removeRepository( TEST_REPO_ID );
1204 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1205 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1210 public void testDeleteArtifact()
1213 ArtifactMetadata artifact = createArtifact();
1214 artifact.addFacet( new TestMetadataFacet( "value" ) );
1216 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1218 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1219 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1221 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1223 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1225 log.info( "versions {}", versions );
1227 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1229 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1231 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1233 log.info( "versions {}", versions );
1235 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1238 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
1240 Assertions.assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1241 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1245 public void deleteArtifact()
1248 ArtifactMetadata artifact = createArtifact();
1249 artifact.addFacet( new TestMetadataFacet( "value" ) );
1251 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1253 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1255 Collection<ArtifactMetadata> artifacts =
1256 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1258 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( artifacts ) );
1260 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1262 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1264 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
1268 public void deleteVersion()
1271 ArtifactMetadata artifact = createArtifact();
1272 artifact.addFacet( new TestMetadataFacet( "value" ) );
1274 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1276 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1278 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1280 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 );
1282 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1284 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1286 Assertions.assertThat( versions ).isNotNull().isEmpty();
1290 public void deleteProject()
1293 ArtifactMetadata artifact = createArtifact();
1294 artifact.addFacet( new TestMetadataFacet( "value" ) );
1296 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1298 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1300 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1302 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1304 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1306 Assertions.assertThat( versions ).isNotNull().isEmpty();
1311 public void deleteSnapshotVersion()
1314 ArtifactMetadata artifactOne = createArtifact();
1315 artifactOne.setVersion( "2.0-20120618.214127-1" );
1316 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1317 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1318 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1320 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1322 ArtifactMetadata artifactTwo = createArtifact();
1323 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1324 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1325 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1326 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1328 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1330 Collection<ArtifactMetadata> artifactMetadatas =
1331 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1333 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1335 log.info( "artifactMetadatas: {}", artifactMetadatas );
1337 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1339 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1341 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1343 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1345 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1347 Assertions.assertThat( artifactMetadatas ).isNotNull().isEmpty();
1351 private static ProjectMetadata createProject()
1353 return createProject( TEST_NAMESPACE );
1356 private static ProjectMetadata createProject( String ns )
1358 ProjectMetadata project = new ProjectMetadata();
1359 project.setId( TEST_PROJECT );
1360 project.setNamespace( ns );
1364 private static ArtifactMetadata createArtifact()
1366 return createArtifact( "jar" );
1369 private static ArtifactMetadata createArtifact( String type )
1371 ArtifactMetadata artifact = new ArtifactMetadata();
1372 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1373 artifact.setWhenGathered( new Date() );
1374 artifact.setNamespace( TEST_NAMESPACE );
1375 artifact.setProject( TEST_PROJECT );
1376 artifact.setRepositoryId( TEST_REPO_ID );
1377 artifact.setFileLastModified( System.currentTimeMillis() );
1378 artifact.setVersion( TEST_PROJECT_VERSION );
1379 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1380 artifact.setMd5( TEST_MD5 );
1381 artifact.setSha1( TEST_SHA1 );
1385 private static class ArtifactMetadataComparator
1386 implements Comparator<ArtifactMetadata>
1388 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1390 return a.getProject().compareTo( b.getProject() );
1394 private static class KindOfRepositoryStatistics
1395 implements MetadataFacet
1397 private String value;
1401 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1403 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1405 private KindOfRepositoryStatistics( String value, Date date )
1411 public String getName()
1413 return createNameFormat().format( date );
1416 private static SimpleDateFormat createNameFormat()
1418 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1419 fmt.setTimeZone( UTC_TIME_ZONE );
1423 public String getFacetId()
1425 return KindOfRepositoryStatistics.class.getName();
1428 public Map<String, String> toProperties()
1430 return Collections.emptyMap();
1433 public void fromProperties( Map<String, String> properties )
1439 private static class TestMetadataFacet
1440 implements MetadataFacet
1442 private String testFacetId;
1444 private Map<String, String> additionalProps;
1446 private String value;
1448 private TestMetadataFacet( String value )
1451 testFacetId = TEST_FACET_ID;
1454 private TestMetadataFacet( String facetId, String value )
1457 testFacetId = facetId;
1460 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1462 this( facetId, value );
1463 this.additionalProps = additionalProps;
1466 public String getFacetId()
1471 public String getName()
1476 public Map<String, String> toProperties()
1478 if ( value != null )
1480 if ( additionalProps == null )
1482 return Collections.singletonMap( "foo", value );
1486 Map<String, String> props = new HashMap<String, String>();
1487 props.put( "foo", value );
1489 for ( String key : additionalProps.keySet() )
1491 props.put( key, additionalProps.get( key ) );
1498 return Collections.emptyMap();
1502 public void fromProperties( Map<String, String> properties )
1504 String value = properties.get( "foo" );
1505 if ( value != null )
1510 properties.remove( "foo" );
1512 if ( additionalProps == null )
1514 additionalProps = new HashMap<String, String>();
1517 for ( String key : properties.keySet() )
1519 additionalProps.put( key, properties.get( key ) );
1523 public String getValue()
1529 public String toString()
1531 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1535 public boolean equals( Object o )
1541 if ( o == null || getClass() != o.getClass() )
1546 TestMetadataFacet that = (TestMetadataFacet) o;
1548 if ( value != null ? !value.equals( that.value ) : that.value != null )
1557 public int hashCode()
1559 return value != null ? value.hashCode() : 0;