1 package org.apache.archiva.metadata.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.metadata.model.ArtifactMetadata;
23 import org.apache.archiva.metadata.model.MailingList;
24 import org.apache.archiva.metadata.model.MetadataFacet;
25 import org.apache.archiva.metadata.model.MetadataFacetFactory;
26 import org.apache.archiva.metadata.model.ProjectMetadata;
27 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
28 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.Comparator;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.List;
40 public abstract class AbstractMetadataRepositoryTest
41 extends PlexusInSpringTestCase
43 protected MetadataRepository repository;
45 protected static final String TEST_REPO_ID = "test";
47 private static final String TEST_PROJECT = "projectId";
49 private static final String TEST_NAMESPACE = "namespace";
51 private static final String TEST_PROJECT_VERSION = "1.0";
53 private static final String TEST_FACET_ID = "test-facet-id";
55 private static final String TEST_NAME = "test/name";
57 private static final String TEST_VALUE = "test-value";
59 private static final String UNKNOWN = "unknown";
61 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
63 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
65 private static final String TEST_METADATA_VALUE = "test-metadata";
67 protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
69 Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
70 factories.put( TEST_FACET_ID, new MetadataFacetFactory()
72 public MetadataFacet createMetadataFacet()
74 return new TestMetadataFacet( TEST_METADATA_VALUE );
77 public MetadataFacet createMetadataFacet( String repositoryId, String name )
79 return new TestMetadataFacet( TEST_METADATA_VALUE );
83 // add to ensure we don't accidentally create an empty facet ID.
84 factories.put( "", new MetadataFacetFactory()
86 public MetadataFacet createMetadataFacet()
88 return new TestMetadataFacet( "", TEST_VALUE );
91 public MetadataFacet createMetadataFacet( String repositoryId, String name )
93 return new TestMetadataFacet( "", TEST_VALUE );
99 public void testRootNamespaceWithNoMetadataRepository()
101 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
102 assertEquals( Collections.<String>emptyList(), namespaces );
105 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
107 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
108 metadata.setId( TEST_PROJECT_VERSION );
109 MailingList mailingList = new MailingList();
110 mailingList.setName( "Foo List" );
111 mailingList.setOtherArchives( Collections.<String>emptyList() );
112 metadata.setMailingLists( Collections.singletonList( mailingList ) );
113 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
116 public void testUpdateProjectVersionMetadataIncomplete()
117 throws MetadataResolutionException
119 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
120 metadata.setId( TEST_PROJECT_VERSION );
121 metadata.setIncomplete( true );
122 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
124 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
125 assertEquals( true, metadata.isIncomplete() );
128 public void testUpdateProjectVersionMetadataWithExistingFacets()
129 throws MetadataResolutionException
131 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
132 metadata.setId( TEST_PROJECT_VERSION );
133 MetadataFacet facet = new TestMetadataFacet( "baz" );
134 metadata.addFacet( facet );
135 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
137 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
138 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
140 metadata = new ProjectVersionMetadata();
141 metadata.setId( TEST_PROJECT_VERSION );
142 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
144 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
145 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
146 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
147 assertEquals( "baz", testFacet.getValue() );
150 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
151 throws MetadataResolutionException
153 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
154 metadata.setId( TEST_PROJECT_VERSION );
155 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
157 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
158 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
160 metadata = new ProjectVersionMetadata();
161 metadata.setId( TEST_PROJECT_VERSION );
162 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
164 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
165 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
168 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
169 throws MetadataResolutionException
171 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
172 metadata.setId( TEST_PROJECT_VERSION );
174 Map<String, String> additionalProps = new HashMap<String, String>();
175 additionalProps.put( "deleteKey", "deleteValue" );
177 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
178 metadata.addFacet( facet );
179 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
181 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
182 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
184 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
185 Map<String, String> facetProperties = testFacet.toProperties();
187 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
189 facetProperties.remove( "deleteKey" );
191 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
192 metadata.addFacet( newTestFacet );
194 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
196 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
197 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
198 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
199 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
202 public void testUpdateArtifactMetadataWithExistingFacets()
204 ArtifactMetadata metadata = createArtifact();
205 MetadataFacet facet = new TestMetadataFacet( "baz" );
206 metadata.addFacet( facet );
207 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
209 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
210 TEST_PROJECT_VERSION ).iterator().next();
211 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
213 metadata = createArtifact();
214 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
216 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
217 TEST_PROJECT_VERSION ).iterator().next();
218 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
219 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
220 assertEquals( "baz", testFacet.getValue() );
223 public void testUpdateArtifactMetadataWithNoExistingFacets()
225 ArtifactMetadata metadata = createArtifact();
226 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
228 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
229 TEST_PROJECT_VERSION ).iterator().next();
230 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
232 metadata = createArtifact();
233 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
235 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
236 TEST_PROJECT_VERSION ).iterator().next();
237 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
240 public void testGetMetadataFacet()
242 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
244 assertEquals( new TestMetadataFacet( TEST_VALUE ), repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID,
248 public void testGetMetadataFacetWhenEmpty()
250 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
253 public void testGetMetadataFacetWhenUnknownName()
255 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
257 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
260 public void testGetMetadataFacetWhenDefaultValue()
262 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
264 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ), repository.getMetadataFacet( TEST_REPO_ID,
269 public void testGetMetadataFacetWhenUnknownFacetId()
271 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
274 public void testGetMetadataFacets()
276 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
278 assertEquals( Collections.singletonList( TEST_NAME ), repository.getMetadataFacets( TEST_REPO_ID,
282 public void testGetMetadataFacetsWhenEmpty()
284 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
285 assertTrue( facets.isEmpty() );
288 public void testRemoveFacets()
290 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
292 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
293 assertFalse( facets.isEmpty() );
295 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
297 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
298 assertTrue( facets.isEmpty() );
301 public void testRemoveFacetsWhenEmpty()
303 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
304 assertTrue( facets.isEmpty() );
306 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
308 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
309 assertTrue( facets.isEmpty() );
312 public void testRemoveFacetsWhenUnknown()
314 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
317 public void testRemoveFacet()
319 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
320 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
322 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
323 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
324 assertFalse( facets.isEmpty() );
326 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
328 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
329 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
330 assertTrue( facets.isEmpty() );
333 public void testRemoveFacetWhenEmpty()
335 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
336 assertTrue( facets.isEmpty() );
337 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
339 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
341 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
342 assertTrue( facets.isEmpty() );
343 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
346 public void testRemoveFacetWhenUnknown()
348 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
351 public void testGetArtifacts()
353 ArtifactMetadata artifact1 = createArtifact();
354 ArtifactMetadata artifact2 = createArtifact( "pom" );
355 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
356 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
358 Collection<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
359 TEST_PROJECT_VERSION );
360 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
361 Collections.sort( actual, new Comparator<ArtifactMetadata>()
363 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
365 return o1.getId().compareTo( o2.getId() );
368 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
371 public void testGetArtifactVersions()
373 ArtifactMetadata artifact1 = createArtifact();
374 String version1 = "1.0-20091212.012345-1";
375 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
376 artifact1.setVersion( version1 );
377 ArtifactMetadata artifact2 = createArtifact();
378 String version2 = "1.0-20091212.123456-2";
379 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
380 artifact2.setVersion( version2 );
381 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
382 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
384 List<String> versions = new ArrayList<String>( repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE,
386 TEST_PROJECT_VERSION ) );
387 Collections.sort( versions );
388 assertEquals( Arrays.asList( version1, version2 ), versions );
391 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
393 ArtifactMetadata artifact1 = createArtifact();
394 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
395 ArtifactMetadata artifact2 = createArtifact();
396 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
397 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
398 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
400 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ), repository.getArtifactVersions( TEST_REPO_ID,
403 TEST_PROJECT_VERSION ) );
406 public void testGetArtifactsByDateRangeOpen()
408 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
409 repository.updateProject( TEST_REPO_ID, createProject() );
410 ArtifactMetadata artifact = createArtifact();
411 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
413 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
417 public void testGetArtifactsByDateRangeSparseNamespace()
419 String namespace = "org.apache.archiva";
420 repository.updateNamespace( TEST_REPO_ID, namespace );
421 repository.updateProject( TEST_REPO_ID, createProject( namespace ) );
422 ArtifactMetadata artifact = createArtifact();
423 artifact.setNamespace( namespace );
424 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
426 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
430 public void testGetArtifactsByDateRangeLowerBound()
432 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
433 repository.updateProject( TEST_REPO_ID, createProject() );
434 ArtifactMetadata artifact = createArtifact();
435 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
437 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
438 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, date,
442 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
444 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
445 repository.updateProject( TEST_REPO_ID, createProject() );
446 ArtifactMetadata artifact = createArtifact();
447 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
449 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
450 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ).isEmpty() );
453 public void testGetArtifactsByDateRangeLowerAndUpperBound()
455 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
456 repository.updateProject( TEST_REPO_ID, createProject() );
457 ArtifactMetadata artifact = createArtifact();
458 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
460 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
461 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
462 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, lower,
466 public void testGetArtifactsByDateRangeUpperBound()
468 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
469 repository.updateProject( TEST_REPO_ID, createProject() );
470 ArtifactMetadata artifact = createArtifact();
471 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
473 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
474 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByDateRange( TEST_REPO_ID, null,
478 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
480 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
481 repository.updateProject( TEST_REPO_ID, createProject() );
482 ArtifactMetadata artifact = createArtifact();
483 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
485 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
486 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ).isEmpty() );
489 public void testGetNamespacesWithSparseDepth()
491 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
493 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
494 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
495 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
496 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
499 public void testGetArtifactsByChecksumSingleResultMd5()
501 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
502 repository.updateProject( TEST_REPO_ID, createProject() );
503 ArtifactMetadata artifact = createArtifact();
504 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
506 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
510 public void testGetArtifactsByChecksumSingleResultSha1()
512 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
513 repository.updateProject( TEST_REPO_ID, createProject() );
514 ArtifactMetadata artifact = createArtifact();
515 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
517 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
521 public void testGetArtifactsByChecksumMultipleResult()
523 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
525 ProjectMetadata projectMetadata = createProject();
526 repository.updateProject( TEST_REPO_ID, projectMetadata );
527 ArtifactMetadata artifact1 = createArtifact();
528 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
530 projectMetadata = createProject();
531 String newProjectId = "another-project";
532 projectMetadata.setId( newProjectId );
533 repository.updateProject( TEST_REPO_ID, projectMetadata );
534 ArtifactMetadata artifact2 = createArtifact();
535 artifact2.setProject( newProjectId );
536 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
538 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum(
539 TEST_REPO_ID, TEST_SHA1 ) );
540 Collections.sort( artifacts, new ArtifactMetadataComparator() );
541 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
544 public void testGetArtifactsByChecksumNoResult()
546 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
547 repository.updateProject( TEST_REPO_ID, createProject() );
548 ArtifactMetadata artifact = createArtifact();
549 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
551 assertEquals( Collections.<ArtifactMetadata>emptyList(), repository.getArtifactsByChecksum( TEST_REPO_ID,
552 "not a checksum" ) );
555 public void testDeleteArtifact()
557 ArtifactMetadata artifact = createArtifact();
558 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
560 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( repository.getArtifacts(
561 TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
563 repository.deleteArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
565 assertTrue( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
566 TEST_PROJECT_VERSION ).isEmpty() );
569 public void testDeleteRepository()
571 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
573 ProjectMetadata project1 = new ProjectMetadata();
574 project1.setNamespace( TEST_NAMESPACE );
575 project1.setId( "project1" );
576 repository.updateProject( TEST_REPO_ID, project1 );
577 ProjectMetadata project2 = new ProjectMetadata();
578 project2.setNamespace( TEST_NAMESPACE );
579 project2.setId( "project2" );
580 repository.updateProject( TEST_REPO_ID, project2 );
582 ArtifactMetadata artifact1 = createArtifact();
583 artifact1.setProject( "project1" );
584 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
585 ArtifactMetadata artifact2 = createArtifact();
586 artifact2.setProject( "project2" );
587 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
589 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
590 Collections.sort( expected, new ArtifactMetadataComparator() );
592 List<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange(
593 TEST_REPO_ID, null, null ) );
594 Collections.sort( actual, new ArtifactMetadataComparator() );
596 assertEquals( expected, actual );
598 repository.deleteRepository( TEST_REPO_ID );
600 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ).isEmpty() );
603 private ProjectMetadata createProject()
605 return createProject( TEST_NAMESPACE );
608 private ProjectMetadata createProject( String ns )
610 ProjectMetadata project = new ProjectMetadata();
611 project.setId( TEST_PROJECT );
612 project.setNamespace( ns );
616 private ArtifactMetadata createArtifact()
618 return createArtifact( "jar" );
621 private ArtifactMetadata createArtifact( String type )
623 ArtifactMetadata artifact = new ArtifactMetadata();
624 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
625 artifact.setWhenGathered( new Date() );
626 artifact.setNamespace( TEST_NAMESPACE );
627 artifact.setProject( TEST_PROJECT );
628 artifact.setRepositoryId( TEST_REPO_ID );
629 artifact.setFileLastModified( System.currentTimeMillis() );
630 artifact.setVersion( TEST_PROJECT_VERSION );
631 artifact.setProjectVersion( TEST_PROJECT_VERSION );
632 artifact.setMd5( TEST_MD5 );
633 artifact.setSha1( TEST_SHA1 );
637 private class ArtifactMetadataComparator
638 implements Comparator<ArtifactMetadata>
640 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
642 return a.getProject().compareTo( b.getProject() );
646 private static class TestMetadataFacet
647 implements MetadataFacet
649 private String testFacetId;
651 private Map<String, String> additionalProps;
653 private String value;
655 private TestMetadataFacet( String value )
658 testFacetId = TEST_FACET_ID;
661 private TestMetadataFacet( String facetId, String value )
664 testFacetId = facetId;
667 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
669 this( facetId, value );
670 this.additionalProps = additionalProps;
673 public String getFacetId()
678 public String getName()
683 public Map<String, String> toProperties()
687 if( additionalProps == null )
689 return Collections.singletonMap( "foo", value );
693 Map<String, String> props = new HashMap<String, String>();
694 props.put( "foo", value );
696 for( String key : additionalProps.keySet() )
698 props.put( key, additionalProps.get( key ) );
705 return Collections.emptyMap();
709 public void fromProperties( Map<String, String> properties )
711 String value = properties.get( "foo" );
717 properties.remove( "foo" );
719 if( additionalProps == null )
721 additionalProps = new HashMap<String, String>();
724 for( String key: properties.keySet() )
726 additionalProps.put( key, properties.get( key ) );
730 public String getValue()
736 public String toString()
738 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
742 public boolean equals( Object o )
748 if ( o == null || getClass() != o.getClass() )
753 TestMetadataFacet that = (TestMetadataFacet) o;
755 if ( value != null ? !value.equals( that.value ) : that.value != null )
764 public int hashCode()
766 return value != null ? value.hashCode() : 0;