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.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.test.context.ContextConfiguration;
43 import java.text.SimpleDateFormat;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Calendar;
47 import java.util.Collection;
48 import java.util.Collections;
49 import java.util.Comparator;
50 import java.util.Date;
51 import java.util.HashMap;
52 import java.util.List;
54 import java.util.TimeZone;
56 @RunWith(ArchivaSpringJUnit4ClassRunner.class)
57 @ContextConfiguration(locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" })
58 public abstract class AbstractMetadataRepositoryTest
61 protected static final String OTHER_REPO_ID = "other-repo";
63 protected MetadataRepository repository;
65 protected static final String TEST_REPO_ID = "test";
67 private static final String TEST_PROJECT = "projectId";
69 private static final String TEST_NAMESPACE = "namespace";
71 private static final String TEST_PROJECT_VERSION = "1.0";
73 private static final String TEST_PROJECT_VERSION_2_0 = "2.0";
75 private static final String TEST_FACET_ID = "test-facet-id";
77 private static final String TEST_NAME = "test/name";
79 private static final String TEST_VALUE = "test-value";
81 private static final String UNKNOWN = "unknown";
83 private static final String TEST_MD5 = "bd4a9b642562547754086de2dab26b7d";
85 private static final String TEST_SHA1 = "2e5daf0201ddeb068a62d5e08da18657ab2c6be9";
87 private static final String TEST_METADATA_VALUE = "test-metadata";
89 protected Logger log = LoggerFactory.getLogger( getClass() );
91 protected static Map<String, MetadataFacetFactory> createTestMetadataFacetFactories()
93 Map<String, MetadataFacetFactory> factories = new HashMap<String, MetadataFacetFactory>();
94 factories.put( TEST_FACET_ID, new MetadataFacetFactory()
96 public MetadataFacet createMetadataFacet()
98 return new TestMetadataFacet( TEST_METADATA_VALUE );
101 public MetadataFacet createMetadataFacet( String repositoryId, String name )
103 return new TestMetadataFacet( TEST_METADATA_VALUE );
107 // add to ensure we don't accidentally create an empty facet ID.
108 factories.put( "", new MetadataFacetFactory()
110 public MetadataFacet createMetadataFacet()
112 return new TestMetadataFacet( "", TEST_VALUE );
115 public MetadataFacet createMetadataFacet( String repositoryId, String name )
117 return new TestMetadataFacet( "", TEST_VALUE );
124 public void testRootNamespaceWithNoMetadataRepository()
127 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
128 Assertions.assertThat( namespaces ).isNotNull().isEmpty();
132 public void testGetNamespaceOnly()
135 assertEquals( Collections.emptyList(), repository.getRootNamespaces( TEST_REPO_ID ) );
137 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
139 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
140 TEST_NAMESPACE ).hasSize( 1 );
142 repository.removeNamespace( TEST_REPO_ID, TEST_NAMESPACE );
144 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
149 public void testGetProjectOnly()
152 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
153 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
155 ProjectMetadata project = new ProjectMetadata();
156 project.setId( TEST_PROJECT );
157 project.setNamespace( TEST_NAMESPACE );
159 repository.updateProject( TEST_REPO_ID, project );
161 project = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
162 assertEquals( TEST_PROJECT, project.getId() );
163 assertEquals( TEST_NAMESPACE, project.getNamespace() );
165 // test that namespace is also constructed
167 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
169 Assertions.assertThat( namespaces ).isNotNull().isNotEmpty().contains( TEST_NAMESPACE ).hasSize( 1 );
173 public void testGetProjectVersionOnly()
176 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
177 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
178 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
180 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
181 metadata.setId( TEST_PROJECT_VERSION );
183 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
185 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
186 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
188 // test that namespace and project is also constructed
189 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
190 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
191 assertEquals( TEST_PROJECT, projectMetadata.getId() );
192 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
196 public void testGetArtifactOnly()
199 assertEquals( Collections.<ArtifactMetadata>emptyList(), new ArrayList<ArtifactMetadata>(
200 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
201 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
202 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
204 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
206 ArtifactMetadata metadata = createArtifact();
208 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
210 Collection<ArtifactMetadata> artifacts =
211 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
212 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
214 // test that namespace, project and project version is also constructed
216 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
217 TEST_NAMESPACE ).hasSize( 1 );
219 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
220 assertEquals( TEST_PROJECT, projectMetadata.getId() );
221 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
223 ProjectVersionMetadata projectVersionMetadata =
224 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
225 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
229 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
232 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
233 metadata.setId( TEST_PROJECT_VERSION );
234 MailingList mailingList = new MailingList();
235 mailingList.setName( "Foo List" );
236 mailingList.setOtherArchives( Collections.<String>emptyList() );
237 metadata.setMailingLists( Collections.singletonList( mailingList ) );
238 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
240 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
241 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
242 assertEquals( 1, metadata.getMailingLists().size() );
243 mailingList = metadata.getMailingLists().get( 0 );
244 assertEquals( "Foo List", mailingList.getName() );
245 assertEquals( Collections.<String>emptyList(), mailingList.getOtherArchives() );
249 public void testUpdateProjectVersionMetadataWithAllElements()
252 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
253 metadata.setId( TEST_PROJECT_VERSION );
255 metadata.setName( "project name" );
256 metadata.setDescription( "project description" );
258 MailingList mailingList = new MailingList();
259 mailingList.setName( "Foo List" );
260 mailingList.setOtherArchives( Collections.singletonList( "other archive" ) );
261 metadata.setMailingLists( Collections.singletonList( mailingList ) );
264 scm.setConnection( "connection" );
265 scm.setDeveloperConnection( "dev conn" );
267 metadata.setScm( scm );
269 CiManagement ci = new CiManagement();
270 ci.setSystem( "system" );
271 ci.setUrl( "ci url" );
272 metadata.setCiManagement( ci );
274 IssueManagement tracker = new IssueManagement();
275 tracker.setSystem( "system" );
276 tracker.setUrl( "issue tracker url" );
277 metadata.setIssueManagement( tracker );
279 Organization org = new Organization();
280 org.setName( "org name" );
282 metadata.setOrganization( org );
284 License l = new License();
285 l.setName( "license name" );
286 l.setUrl( "license url" );
287 metadata.addLicense( l );
289 Dependency d = new Dependency();
290 d.setArtifactId( "artifactId" );
291 d.setClassifier( "classifier" );
292 d.setGroupId( "groupId" );
293 d.setScope( "scope" );
294 d.setSystemPath( "system path" );
296 d.setVersion( "version" );
297 d.setOptional( true );
298 metadata.addDependency( d );
300 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
302 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
303 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
304 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
305 assertEquals( "project name", metadata.getName() );
306 assertEquals( "project description", metadata.getDescription() );
308 assertEquals( 1, metadata.getMailingLists().size() );
309 mailingList = metadata.getMailingLists().get( 0 );
310 assertEquals( "Foo List", mailingList.getName() );
311 assertEquals( Collections.singletonList( "other archive" ), mailingList.getOtherArchives() );
313 assertEquals( "connection", metadata.getScm().getConnection() );
314 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
315 assertEquals( "url", metadata.getScm().getUrl() );
317 assertEquals( "system", metadata.getCiManagement().getSystem() );
318 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
320 assertEquals( "system", metadata.getIssueManagement().getSystem() );
321 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
323 assertEquals( "org name", metadata.getOrganization().getName() );
324 assertEquals( "url", metadata.getOrganization().getUrl() );
326 assertEquals( 1, metadata.getLicenses().size() );
327 l = metadata.getLicenses().get( 0 );
328 assertEquals( "license name", l.getName() );
329 assertEquals( "license url", l.getUrl() );
331 assertEquals( 1, metadata.getDependencies().size() );
332 d = metadata.getDependencies().get( 0 );
333 assertEquals( "artifactId", d.getArtifactId() );
334 assertEquals( "classifier", d.getClassifier() );
335 assertEquals( "groupId", d.getGroupId() );
336 assertEquals( "scope", d.getScope() );
337 assertEquals( "system path", d.getSystemPath() );
338 assertEquals( "type", d.getType() );
339 assertEquals( "version", d.getVersion() );
340 assertTrue( d.isOptional() );
344 public void testGetRepositories()
347 // currently set up this way so the behaviour of both the test and the mock config return the same repository
348 // set as the File implementation just uses the config rather than the content
349 repository.updateNamespace( TEST_REPO_ID, "namespace" );
350 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
352 Collection<String> repositories = repository.getRepositories();
354 assertEquals( "repository.getRepositories() -> " + repositories, Arrays.asList( TEST_REPO_ID, OTHER_REPO_ID ),
355 new ArrayList<String>( repositories ) );
359 public void testUpdateProjectVersionMetadataIncomplete()
362 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
363 metadata.setId( TEST_PROJECT_VERSION );
364 metadata.setIncomplete( true );
365 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
367 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
368 assertEquals( true, metadata.isIncomplete() );
369 assertNull( metadata.getCiManagement() );
370 assertNull( metadata.getScm() );
371 assertNull( metadata.getIssueManagement() );
372 assertNull( metadata.getOrganization() );
373 assertNull( metadata.getDescription() );
374 assertNull( metadata.getName() );
375 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
376 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
377 assertTrue( metadata.getMailingLists().isEmpty() );
378 assertTrue( metadata.getLicenses().isEmpty() );
379 assertTrue( metadata.getDependencies().isEmpty() );
383 public void testUpdateProjectVersionMetadataWithExistingFacets()
386 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
387 metadata.setId( TEST_PROJECT_VERSION );
388 MetadataFacet facet = new TestMetadataFacet( "baz" );
389 metadata.addFacet( facet );
390 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
392 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
393 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
395 metadata = new ProjectVersionMetadata();
396 metadata.setId( TEST_PROJECT_VERSION );
397 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
399 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
400 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
401 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
402 assertEquals( "baz", testFacet.getValue() );
406 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
409 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
410 metadata.setId( TEST_PROJECT_VERSION );
411 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
413 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
414 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
416 metadata = new ProjectVersionMetadata();
417 metadata.setId( TEST_PROJECT_VERSION );
418 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
420 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
421 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
425 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
428 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
429 metadata.setId( TEST_PROJECT_VERSION );
431 Map<String, String> additionalProps = new HashMap<String, String>();
432 additionalProps.put( "deleteKey", "deleteValue" );
434 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
435 metadata.addFacet( facet );
436 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
438 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
439 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
441 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
442 Map<String, String> facetProperties = testFacet.toProperties();
444 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
446 facetProperties.remove( "deleteKey" );
448 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
449 metadata.addFacet( newTestFacet );
451 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
453 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
454 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
455 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
456 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
460 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
463 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
464 versionMetadata.setId( TEST_PROJECT_VERSION );
466 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
467 versionMetadata.addFacet( facet );
468 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
470 ArtifactMetadata artifactMetadata = createArtifact();
471 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
474 Collection<ArtifactMetadata> artifacts =
475 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
476 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
478 artifacts = repository.getArtifacts( TEST_REPO_ID );
479 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
481 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
482 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
484 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
485 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
487 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
488 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
492 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
495 ArtifactMetadata metadata = createArtifact();
497 Map<String, String> additionalProps = new HashMap<String, String>();
498 additionalProps.put( "deleteKey", "deleteValue" );
500 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
501 metadata.addFacet( facet );
502 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
504 Collection<ArtifactMetadata> artifacts =
505 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
506 assertEquals( 1, artifacts.size() );
507 metadata = artifacts.iterator().next();
508 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
510 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
511 Map<String, String> facetProperties = testFacet.toProperties();
513 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
515 facetProperties.remove( "deleteKey" );
517 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
518 metadata.addFacet( newTestFacet );
520 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
522 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
523 assertEquals( 1, artifacts.size() );
524 metadata = artifacts.iterator().next();
525 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
526 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
527 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
531 public void testUpdateArtifactMetadataWithExistingFacets()
534 ArtifactMetadata metadata = createArtifact();
535 MetadataFacet facet = new TestMetadataFacet( "baz" );
536 metadata.addFacet( facet );
537 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
539 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
540 TEST_PROJECT_VERSION ).iterator().next();
541 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
543 metadata = createArtifact();
544 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
546 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
547 TEST_PROJECT_VERSION ).iterator().next();
548 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
549 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
550 assertEquals( "baz", testFacet.getValue() );
554 public void testUpdateArtifactMetadataWithNoExistingFacets()
557 ArtifactMetadata metadata = createArtifact();
558 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
560 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
561 TEST_PROJECT_VERSION ).iterator().next();
562 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
564 metadata = createArtifact();
565 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
567 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
568 TEST_PROJECT_VERSION ).iterator().next();
569 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
573 public void testGetMetadataFacet()
576 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
578 TestMetadataFacet test =
579 (TestMetadataFacet) repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
581 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
585 public void testGetMetadataFacetWhenEmpty()
588 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
592 public void testGetMetadataFacetWhenUnknownName()
595 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
597 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
601 public void testGetMetadataFacetWhenDefaultValue()
604 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
606 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ),
607 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
611 public void testGetMetadataFacetWhenUnknownFacetId()
614 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
618 public void testGetMetadataFacets()
621 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
623 assertEquals( Collections.singletonList( TEST_NAME ),
624 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
628 public void testGetMetadataFacetsWhenEmpty()
632 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
633 assertTrue( facets.isEmpty() );
637 public void testRemoveFacets()
640 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
642 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
643 assertFalse( facets.isEmpty() );
645 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
647 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
648 assertTrue( facets.isEmpty() );
652 public void testRemoveFacetsWhenEmpty()
655 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
656 assertTrue( facets.isEmpty() );
658 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
660 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
661 assertTrue( facets.isEmpty() );
665 public void testRemoveFacetsWhenUnknown()
668 // testing no exception
669 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
673 public void testRemoveFacetWhenUnknown()
676 // testing no exception
677 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
681 public void testRemoveFacet()
684 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
685 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
687 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
688 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
689 assertFalse( facets.isEmpty() );
691 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
693 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
694 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
695 assertTrue( facets.isEmpty() );
699 public void testRemoveFacetWhenEmpty()
702 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
703 assertTrue( facets.isEmpty() );
704 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
706 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
708 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
709 assertTrue( facets.isEmpty() );
710 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
714 public void hasMetadataFacetStart()
717 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
722 public void hasMetadataFacet()
726 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
728 Calendar cal = Calendar.getInstance();
730 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
732 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
734 cal.add( Calendar.MINUTE, 2 );
736 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
738 cal.add( Calendar.MINUTE, 2 );
740 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
742 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
744 Assertions.assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
746 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
748 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
750 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
752 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
754 Assertions.assertThat( facets ).isNotNull().isEmpty();
760 public void testGetArtifacts()
763 ArtifactMetadata artifact1 = createArtifact();
764 ArtifactMetadata artifact2 = createArtifact( "pom" );
765 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
766 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
768 Collection<ArtifactMetadata> artifacts =
769 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
770 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
771 Collections.sort( actual, new Comparator<ArtifactMetadata>()
773 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
775 return o1.getId().compareTo( o2.getId() );
778 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
782 public void testGetArtifactVersions()
785 ArtifactMetadata artifact1 = createArtifact();
786 String version1 = "1.0-20091212.012345-1";
787 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
788 artifact1.setVersion( version1 );
789 ArtifactMetadata artifact2 = createArtifact();
790 String version2 = "1.0-20091212.123456-2";
791 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
792 artifact2.setVersion( version2 );
793 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
794 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
796 List<String> versions = new ArrayList<String>(
797 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
798 Collections.sort( versions );
799 assertEquals( Arrays.asList( version1, version2 ), versions );
803 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
806 ArtifactMetadata artifact1 = createArtifact();
807 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
808 ArtifactMetadata artifact2 = createArtifact();
809 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
810 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
811 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
813 Collection<String> versions =
814 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
816 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ), versions );
820 public void testGetArtifactsByDateRangeOpen()
823 ArtifactMetadata artifact = createArtifact();
824 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
827 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
829 assertEquals( Collections.singletonList( artifact ), artifacts );
833 public void testGetArtifactsByDateRangeSparseNamespace()
836 String namespace = "org.apache.archiva";
837 ArtifactMetadata artifact = createArtifact();
838 artifact.setNamespace( namespace );
839 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
842 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
844 assertEquals( Collections.singletonList( artifact ), artifacts );
848 public void testGetArtifactsByDateRangeLowerBound()
851 ArtifactMetadata artifact = createArtifact();
852 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
855 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
857 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
859 assertEquals( Collections.singletonList( artifact ), artifacts );
863 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
866 ArtifactMetadata artifact = createArtifact();
867 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
869 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
871 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
873 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
877 public void testGetArtifactsByDateRangeLowerAndUpperBound()
880 ArtifactMetadata artifact = createArtifact();
881 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
884 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
885 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
887 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper );
889 assertEquals( Collections.singletonList( artifact ), artifacts );
893 public void testGetArtifactsByDateRangeUpperBound()
896 ArtifactMetadata artifact = createArtifact();
897 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
900 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
902 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
904 assertEquals( Collections.singletonList( artifact ), artifacts );
908 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
911 ArtifactMetadata artifact = createArtifact();
912 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
915 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
917 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
919 //assertTrue( .isEmpty() );
921 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
925 public void testGetArtifactsByRepoId()
928 ArtifactMetadata artifact = createArtifact();
929 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
932 List<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID );
934 assertEquals( Collections.singletonList( artifact ), artifacts );
938 public void testGetArtifactsByRepoIdMultipleCopies()
941 ArtifactMetadata artifact = createArtifact();
942 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
944 ArtifactMetadata secondArtifact = createArtifact();
945 secondArtifact.setRepositoryId( OTHER_REPO_ID );
946 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
949 // test it restricts to the appropriate repository
950 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
951 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
955 public void testGetArtifactsByDateRangeMultipleCopies()
958 ArtifactMetadata artifact = createArtifact();
959 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
961 ArtifactMetadata secondArtifact = createArtifact();
962 secondArtifact.setRepositoryId( OTHER_REPO_ID );
963 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
966 // test it restricts to the appropriate repository
967 assertEquals( Collections.singletonList( artifact ),
968 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
969 assertEquals( Collections.singletonList( secondArtifact ),
970 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
974 public void testGetArtifactsByChecksumMultipleCopies()
977 ArtifactMetadata artifact = createArtifact();
978 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
980 ArtifactMetadata secondArtifact = createArtifact();
981 secondArtifact.setRepositoryId( OTHER_REPO_ID );
982 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
985 // test it restricts to the appropriate repository
986 assertEquals( Collections.singletonList( artifact ),
987 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
988 assertEquals( Collections.singletonList( secondArtifact ),
989 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) );
990 assertEquals( Collections.singletonList( artifact ),
991 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
992 assertEquals( Collections.singletonList( secondArtifact ),
993 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) );
997 public void testGetNamespacesWithSparseDepth()
1000 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1002 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
1003 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
1004 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
1005 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
1009 public void testGetNamespacesWithProjectsPresent()
1012 String namespace = "org.apache.maven.shared";
1013 repository.updateNamespace( TEST_REPO_ID, namespace );
1015 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1016 metadata.setId( TEST_PROJECT_VERSION );
1017 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1019 assertEquals( Collections.<String>emptyList(), repository.getNamespaces( TEST_REPO_ID, namespace ) );
1023 public void testGetProjectsWithOtherNamespacesPresent()
1026 ProjectMetadata projectMetadata = new ProjectMetadata();
1027 projectMetadata.setId( TEST_PROJECT );
1028 projectMetadata.setNamespace( "org.apache.maven" );
1029 repository.updateProject( TEST_REPO_ID, projectMetadata );
1031 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1033 assertEquals( Collections.singletonList( TEST_PROJECT ),
1034 repository.getProjects( TEST_REPO_ID, "org.apache.maven" ) );
1038 public void testGetProjectVersionsWithOtherNamespacesPresent()
1041 // an unusual case but technically possible where a project namespace matches another project's name
1043 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1044 versionMetadata.setId( TEST_PROJECT_VERSION );
1045 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1047 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1050 List<String> expectedVersions = Collections.singletonList( TEST_PROJECT_VERSION );
1051 assertEquals( expectedVersions, repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT,
1052 "other-project" ) );
1053 assertEquals( expectedVersions,
1054 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT ) );
1058 public void testGetArtifactsByChecksumSingleResultMd5()
1061 ArtifactMetadata artifact = createArtifact();
1062 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1065 assertEquals( Collections.singletonList( artifact ),
1066 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1070 public void testGetArtifactsByChecksumSingleResultSha1()
1073 ArtifactMetadata artifact = createArtifact();
1074 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1077 assertEquals( Collections.singletonList( artifact ),
1078 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1082 public void testGetArtifactsByChecksumDeepNamespace()
1085 ArtifactMetadata artifact = createArtifact();
1086 String namespace = "multi.level.ns";
1087 artifact.setNamespace( namespace );
1088 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1091 assertEquals( Collections.singletonList( artifact ),
1092 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1093 assertEquals( Collections.singletonList( artifact ),
1094 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1098 public void testGetArtifactsByChecksumMultipleResult()
1101 ArtifactMetadata artifact1 = createArtifact();
1102 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1104 String newProjectId = "another-project";
1105 ArtifactMetadata artifact2 = createArtifact();
1106 artifact2.setProject( newProjectId );
1107 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1110 List<ArtifactMetadata> artifacts =
1111 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1112 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1113 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1115 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1116 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1117 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1121 public void testGetArtifactsByChecksumNoResult()
1124 ArtifactMetadata artifact = createArtifact();
1125 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1127 List<ArtifactMetadata> artifactsByChecksum = repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1128 assertEquals( Collections.<ArtifactMetadata>emptyList(), artifactsByChecksum );
1133 public void testDeleteRepository()
1136 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1138 ProjectMetadata project1 = new ProjectMetadata();
1139 project1.setNamespace( TEST_NAMESPACE );
1140 project1.setId( "project1" );
1141 repository.updateProject( TEST_REPO_ID, project1 );
1142 ProjectMetadata project2 = new ProjectMetadata();
1143 project2.setNamespace( TEST_NAMESPACE );
1144 project2.setId( "project2" );
1145 repository.updateProject( TEST_REPO_ID, project2 );
1147 ArtifactMetadata artifact1 = createArtifact();
1148 artifact1.setProject( "project1" );
1149 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1150 ArtifactMetadata artifact2 = createArtifact();
1151 artifact2.setProject( "project2" );
1152 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1155 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1156 Collections.sort( expected, new ArtifactMetadataComparator() );
1158 List<ArtifactMetadata> actual =
1159 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1160 Collections.sort( actual, new ArtifactMetadataComparator() );
1162 assertEquals( expected, actual );
1164 repository.removeRepository( TEST_REPO_ID );
1166 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1167 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1172 public void testDeleteArtifact()
1175 ArtifactMetadata artifact = createArtifact();
1176 artifact.addFacet( new TestMetadataFacet( "value" ) );
1178 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1180 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1181 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1183 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1185 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1187 log.info( "versions {}", versions );
1189 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1191 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1193 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1195 log.info( "versions {}", versions );
1197 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1200 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
1202 Assertions.assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1203 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1207 public void deleteArtifact()
1210 ArtifactMetadata artifact = createArtifact();
1211 artifact.addFacet( new TestMetadataFacet( "value" ) );
1213 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1215 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1217 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1218 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1220 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1223 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
1227 public void deleteVersion()
1230 ArtifactMetadata artifact = createArtifact();
1231 artifact.addFacet( new TestMetadataFacet( "value" ) );
1233 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1235 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1237 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1239 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1241 assertEquals( 0, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1245 public void deleteProject()
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 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1257 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1259 assertEquals( 0, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1264 public void deleteSnapshotVersion()
1267 ArtifactMetadata artifactOne = createArtifact();
1268 artifactOne.setVersion( "2.0-20120618.214127-1" );
1269 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1270 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1271 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1273 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1275 ArtifactMetadata artifactTwo = createArtifact();
1276 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1277 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1278 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1279 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1281 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1283 Collection<ArtifactMetadata> artifactMetadatas =
1284 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1286 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1288 log.info( "artifactMetadatas: {}", artifactMetadatas );
1290 //assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1291 // repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" ) ) );
1293 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1295 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1297 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1299 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1301 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1303 Assertions.assertThat( artifactMetadatas ).isNotNull().isEmpty();
1307 private static ProjectMetadata createProject()
1309 return createProject( TEST_NAMESPACE );
1312 private static ProjectMetadata createProject( String ns )
1314 ProjectMetadata project = new ProjectMetadata();
1315 project.setId( TEST_PROJECT );
1316 project.setNamespace( ns );
1320 private static ArtifactMetadata createArtifact()
1322 return createArtifact( "jar" );
1325 private static ArtifactMetadata createArtifact( String type )
1327 ArtifactMetadata artifact = new ArtifactMetadata();
1328 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1329 artifact.setWhenGathered( new Date() );
1330 artifact.setNamespace( TEST_NAMESPACE );
1331 artifact.setProject( TEST_PROJECT );
1332 artifact.setRepositoryId( TEST_REPO_ID );
1333 artifact.setFileLastModified( System.currentTimeMillis() );
1334 artifact.setVersion( TEST_PROJECT_VERSION );
1335 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1336 artifact.setMd5( TEST_MD5 );
1337 artifact.setSha1( TEST_SHA1 );
1341 private static class ArtifactMetadataComparator
1342 implements Comparator<ArtifactMetadata>
1344 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1346 return a.getProject().compareTo( b.getProject() );
1350 private static class KindOfRepositoryStatistics
1351 implements MetadataFacet
1353 private String value;
1357 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1359 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1361 private KindOfRepositoryStatistics( String value, Date date )
1367 public String getName()
1369 return createNameFormat().format( date );
1372 private static SimpleDateFormat createNameFormat()
1374 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1375 fmt.setTimeZone( UTC_TIME_ZONE );
1379 public String getFacetId()
1381 return KindOfRepositoryStatistics.class.getName();
1384 public Map<String, String> toProperties()
1386 return Collections.emptyMap();
1389 public void fromProperties( Map<String, String> properties )
1395 private static class TestMetadataFacet
1396 implements MetadataFacet
1398 private String testFacetId;
1400 private Map<String, String> additionalProps;
1402 private String value;
1404 private TestMetadataFacet( String value )
1407 testFacetId = TEST_FACET_ID;
1410 private TestMetadataFacet( String facetId, String value )
1413 testFacetId = facetId;
1416 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1418 this( facetId, value );
1419 this.additionalProps = additionalProps;
1422 public String getFacetId()
1427 public String getName()
1432 public Map<String, String> toProperties()
1434 if ( value != null )
1436 if ( additionalProps == null )
1438 return Collections.singletonMap( "foo", value );
1442 Map<String, String> props = new HashMap<String, String>();
1443 props.put( "foo", value );
1445 for ( String key : additionalProps.keySet() )
1447 props.put( key, additionalProps.get( key ) );
1454 return Collections.emptyMap();
1458 public void fromProperties( Map<String, String> properties )
1460 String value = properties.get( "foo" );
1461 if ( value != null )
1466 properties.remove( "foo" );
1468 if ( additionalProps == null )
1470 additionalProps = new HashMap<String, String>();
1473 for ( String key : properties.keySet() )
1475 additionalProps.put( key, properties.get( key ) );
1479 public String getValue()
1485 public String toString()
1487 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1491 public boolean equals( Object o )
1497 if ( o == null || getClass() != o.getClass() )
1502 TestMetadataFacet that = (TestMetadataFacet) o;
1504 if ( value != null ? !value.equals( that.value ) : that.value != null )
1513 public int hashCode()
1515 return value != null ? value.hashCode() : 0;