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 testGetArtifactsByRepoId()
491 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
492 repository.updateProject( TEST_REPO_ID, createProject() );
493 ArtifactMetadata artifact = createArtifact();
494 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
495 assertFalse( repository.getArtifacts(TEST_REPO_ID).isEmpty());
500 public void testGetNamespacesWithSparseDepth()
502 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
504 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
505 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
506 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
507 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
510 public void testGetArtifactsByChecksumSingleResultMd5()
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 testGetArtifactsByChecksumSingleResultSha1()
523 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
524 repository.updateProject( TEST_REPO_ID, createProject() );
525 ArtifactMetadata artifact = createArtifact();
526 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
528 assertEquals( Collections.singletonList( artifact ), repository.getArtifactsByChecksum( TEST_REPO_ID,
532 public void testGetArtifactsByChecksumMultipleResult()
534 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
536 ProjectMetadata projectMetadata = createProject();
537 repository.updateProject( TEST_REPO_ID, projectMetadata );
538 ArtifactMetadata artifact1 = createArtifact();
539 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
541 projectMetadata = createProject();
542 String newProjectId = "another-project";
543 projectMetadata.setId( newProjectId );
544 repository.updateProject( TEST_REPO_ID, projectMetadata );
545 ArtifactMetadata artifact2 = createArtifact();
546 artifact2.setProject( newProjectId );
547 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
549 List<ArtifactMetadata> artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum(
550 TEST_REPO_ID, TEST_SHA1 ) );
551 Collections.sort( artifacts, new ArtifactMetadataComparator() );
552 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
555 public void testGetArtifactsByChecksumNoResult()
557 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
558 repository.updateProject( TEST_REPO_ID, createProject() );
559 ArtifactMetadata artifact = createArtifact();
560 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
562 assertEquals( Collections.<ArtifactMetadata>emptyList(), repository.getArtifactsByChecksum( TEST_REPO_ID,
563 "not a checksum" ) );
566 public void testDeleteArtifact()
568 ArtifactMetadata artifact = createArtifact();
569 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
571 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>( repository.getArtifacts(
572 TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
574 repository.deleteArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
576 assertTrue( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
577 TEST_PROJECT_VERSION ).isEmpty() );
580 public void testDeleteRepository()
582 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
584 ProjectMetadata project1 = new ProjectMetadata();
585 project1.setNamespace( TEST_NAMESPACE );
586 project1.setId( "project1" );
587 repository.updateProject( TEST_REPO_ID, project1 );
588 ProjectMetadata project2 = new ProjectMetadata();
589 project2.setNamespace( TEST_NAMESPACE );
590 project2.setId( "project2" );
591 repository.updateProject( TEST_REPO_ID, project2 );
593 ArtifactMetadata artifact1 = createArtifact();
594 artifact1.setProject( "project1" );
595 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
596 ArtifactMetadata artifact2 = createArtifact();
597 artifact2.setProject( "project2" );
598 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
600 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
601 Collections.sort( expected, new ArtifactMetadataComparator() );
603 List<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange(
604 TEST_REPO_ID, null, null ) );
605 Collections.sort( actual, new ArtifactMetadataComparator() );
607 assertEquals( expected, actual );
609 repository.deleteRepository( TEST_REPO_ID );
611 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ).isEmpty() );
614 private ProjectMetadata createProject()
616 return createProject( TEST_NAMESPACE );
619 private ProjectMetadata createProject( String ns )
621 ProjectMetadata project = new ProjectMetadata();
622 project.setId( TEST_PROJECT );
623 project.setNamespace( ns );
627 private ArtifactMetadata createArtifact()
629 return createArtifact( "jar" );
632 private ArtifactMetadata createArtifact( String type )
634 ArtifactMetadata artifact = new ArtifactMetadata();
635 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
636 artifact.setWhenGathered( new Date() );
637 artifact.setNamespace( TEST_NAMESPACE );
638 artifact.setProject( TEST_PROJECT );
639 artifact.setRepositoryId( TEST_REPO_ID );
640 artifact.setFileLastModified( System.currentTimeMillis() );
641 artifact.setVersion( TEST_PROJECT_VERSION );
642 artifact.setProjectVersion( TEST_PROJECT_VERSION );
643 artifact.setMd5( TEST_MD5 );
644 artifact.setSha1( TEST_SHA1 );
648 private class ArtifactMetadataComparator
649 implements Comparator<ArtifactMetadata>
651 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
653 return a.getProject().compareTo( b.getProject() );
657 private static class TestMetadataFacet
658 implements MetadataFacet
660 private String testFacetId;
662 private Map<String, String> additionalProps;
664 private String value;
666 private TestMetadataFacet( String value )
669 testFacetId = TEST_FACET_ID;
672 private TestMetadataFacet( String facetId, String value )
675 testFacetId = facetId;
678 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
680 this( facetId, value );
681 this.additionalProps = additionalProps;
684 public String getFacetId()
689 public String getName()
694 public Map<String, String> toProperties()
698 if( additionalProps == null )
700 return Collections.singletonMap( "foo", value );
704 Map<String, String> props = new HashMap<String, String>();
705 props.put( "foo", value );
707 for( String key : additionalProps.keySet() )
709 props.put( key, additionalProps.get( key ) );
716 return Collections.emptyMap();
720 public void fromProperties( Map<String, String> properties )
722 String value = properties.get( "foo" );
728 properties.remove( "foo" );
730 if( additionalProps == null )
732 additionalProps = new HashMap<String, String>();
735 for( String key: properties.keySet() )
737 additionalProps.put( key, properties.get( key ) );
741 public String getValue()
747 public String toString()
749 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
753 public boolean equals( Object o )
759 if ( o == null || getClass() != o.getClass() )
764 TestMetadataFacet that = (TestMetadataFacet) o;
766 if ( value != null ? !value.equals( that.value ) : that.value != null )
775 public int hashCode()
777 return value != null ? value.hashCode() : 0;