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 assertNotNull( projectMetadata );
192 assertEquals( TEST_PROJECT, projectMetadata.getId() );
193 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
197 public void testGetArtifactOnly()
200 assertEquals( Collections.<ArtifactMetadata>emptyList(), new ArrayList<ArtifactMetadata>(
201 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
202 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
203 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
205 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
207 ArtifactMetadata metadata = createArtifact();
209 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
211 Collection<ArtifactMetadata> artifacts =
212 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
213 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
215 // test that namespace, project and project version is also constructed
217 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
218 TEST_NAMESPACE ).hasSize( 1 );
220 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
221 assertEquals( TEST_PROJECT, projectMetadata.getId() );
222 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
224 ProjectVersionMetadata projectVersionMetadata =
225 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
226 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
230 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
233 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
234 metadata.setId( TEST_PROJECT_VERSION );
235 MailingList mailingList = new MailingList();
236 mailingList.setName( "Foo List" );
237 mailingList.setOtherArchives( Collections.<String>emptyList() );
238 metadata.setMailingLists( Collections.singletonList( mailingList ) );
239 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
241 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
242 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
243 assertEquals( 1, metadata.getMailingLists().size() );
244 mailingList = metadata.getMailingLists().get( 0 );
245 assertEquals( "Foo List", mailingList.getName() );
246 assertEquals( Collections.<String>emptyList(), mailingList.getOtherArchives() );
250 public void testUpdateProjectVersionMetadataWithAllElements()
253 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
254 metadata.setId( TEST_PROJECT_VERSION );
256 metadata.setName( "project name" );
257 metadata.setDescription( "project description" );
259 MailingList mailingList = new MailingList();
260 mailingList.setName( "Foo List" );
261 mailingList.setOtherArchives( Collections.singletonList( "other archive" ) );
262 metadata.setMailingLists( Collections.singletonList( mailingList ) );
265 scm.setConnection( "connection" );
266 scm.setDeveloperConnection( "dev conn" );
268 metadata.setScm( scm );
270 CiManagement ci = new CiManagement();
271 ci.setSystem( "system" );
272 ci.setUrl( "ci url" );
273 metadata.setCiManagement( ci );
275 IssueManagement tracker = new IssueManagement();
276 tracker.setSystem( "system" );
277 tracker.setUrl( "issue tracker url" );
278 metadata.setIssueManagement( tracker );
280 Organization org = new Organization();
281 org.setName( "org name" );
283 metadata.setOrganization( org );
285 License l = new License();
286 l.setName( "license name" );
287 l.setUrl( "license url" );
288 metadata.addLicense( l );
290 Dependency d = new Dependency();
291 d.setArtifactId( "artifactId" );
292 d.setClassifier( "classifier" );
293 d.setGroupId( "groupId" );
294 d.setScope( "scope" );
295 d.setSystemPath( "system path" );
297 d.setVersion( "version" );
298 d.setOptional( true );
299 metadata.addDependency( d );
301 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
303 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
304 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
305 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
306 assertEquals( "project name", metadata.getName() );
307 assertEquals( "project description", metadata.getDescription() );
309 assertEquals( 1, metadata.getMailingLists().size() );
310 mailingList = metadata.getMailingLists().get( 0 );
311 assertEquals( "Foo List", mailingList.getName() );
312 assertEquals( Collections.singletonList( "other archive" ), mailingList.getOtherArchives() );
314 assertEquals( "connection", metadata.getScm().getConnection() );
315 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
316 assertEquals( "url", metadata.getScm().getUrl() );
318 assertEquals( "system", metadata.getCiManagement().getSystem() );
319 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
321 assertEquals( "system", metadata.getIssueManagement().getSystem() );
322 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
324 assertEquals( "org name", metadata.getOrganization().getName() );
325 assertEquals( "url", metadata.getOrganization().getUrl() );
327 assertEquals( 1, metadata.getLicenses().size() );
328 l = metadata.getLicenses().get( 0 );
329 assertEquals( "license name", l.getName() );
330 assertEquals( "license url", l.getUrl() );
332 assertEquals( 1, metadata.getDependencies().size() );
333 d = metadata.getDependencies().get( 0 );
334 assertEquals( "artifactId", d.getArtifactId() );
335 assertEquals( "classifier", d.getClassifier() );
336 assertEquals( "groupId", d.getGroupId() );
337 assertEquals( "scope", d.getScope() );
338 assertEquals( "system path", d.getSystemPath() );
339 assertEquals( "type", d.getType() );
340 assertEquals( "version", d.getVersion() );
341 assertTrue( d.isOptional() );
345 public void testGetRepositories()
348 // currently set up this way so the behaviour of both the test and the mock config return the same repository
349 // set as the File implementation just uses the config rather than the content
350 repository.updateNamespace( TEST_REPO_ID, "namespace" );
351 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
353 Collection<String> repositories = repository.getRepositories();
355 assertEquals( "repository.getRepositories() -> " + repositories, Arrays.asList( TEST_REPO_ID, OTHER_REPO_ID ),
356 new ArrayList<String>( repositories ) );
360 public void testUpdateProjectVersionMetadataIncomplete()
363 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
364 metadata.setId( TEST_PROJECT_VERSION );
365 metadata.setIncomplete( true );
366 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
368 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
369 assertEquals( true, metadata.isIncomplete() );
370 assertNull( metadata.getCiManagement() );
371 assertNull( metadata.getScm() );
372 assertNull( metadata.getIssueManagement() );
373 assertNull( metadata.getOrganization() );
374 assertNull( metadata.getDescription() );
375 assertNull( metadata.getName() );
376 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
377 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
378 assertTrue( metadata.getMailingLists().isEmpty() );
379 assertTrue( metadata.getLicenses().isEmpty() );
380 assertTrue( metadata.getDependencies().isEmpty() );
384 public void testUpdateProjectVersionMetadataWithExistingFacets()
387 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
388 metadata.setId( TEST_PROJECT_VERSION );
389 MetadataFacet facet = new TestMetadataFacet( "baz" );
390 metadata.addFacet( facet );
391 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
393 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
394 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
396 metadata = new ProjectVersionMetadata();
397 metadata.setId( TEST_PROJECT_VERSION );
398 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
400 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
401 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
402 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
403 assertEquals( "baz", testFacet.getValue() );
407 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
410 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
411 metadata.setId( TEST_PROJECT_VERSION );
412 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
414 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
415 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
417 metadata = new ProjectVersionMetadata();
418 metadata.setId( TEST_PROJECT_VERSION );
419 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
421 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
422 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
426 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
429 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
430 metadata.setId( TEST_PROJECT_VERSION );
432 Map<String, String> additionalProps = new HashMap<String, String>();
433 additionalProps.put( "deleteKey", "deleteValue" );
435 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
436 metadata.addFacet( facet );
437 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
439 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
440 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
442 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
443 Map<String, String> facetProperties = testFacet.toProperties();
445 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
447 facetProperties.remove( "deleteKey" );
449 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
450 metadata.addFacet( newTestFacet );
452 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
454 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
455 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
456 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
457 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
461 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
464 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
465 versionMetadata.setId( TEST_PROJECT_VERSION );
467 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
468 versionMetadata.addFacet( facet );
469 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
471 ArtifactMetadata artifactMetadata = createArtifact();
472 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
475 Collection<ArtifactMetadata> artifacts =
476 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
477 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
479 artifacts = repository.getArtifacts( TEST_REPO_ID );
480 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
482 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
483 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
485 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
486 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
488 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
489 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
493 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
496 ArtifactMetadata metadata = createArtifact();
498 Map<String, String> additionalProps = new HashMap<String, String>();
499 additionalProps.put( "deleteKey", "deleteValue" );
501 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
502 metadata.addFacet( facet );
503 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
505 Collection<ArtifactMetadata> artifacts =
506 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
507 assertEquals( 1, artifacts.size() );
508 metadata = artifacts.iterator().next();
509 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
511 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
512 Map<String, String> facetProperties = testFacet.toProperties();
514 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
516 facetProperties.remove( "deleteKey" );
518 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
519 metadata.addFacet( newTestFacet );
521 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
523 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
524 assertEquals( 1, artifacts.size() );
525 metadata = artifacts.iterator().next();
526 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
527 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
528 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
532 public void testUpdateArtifactMetadataWithExistingFacets()
535 ArtifactMetadata metadata = createArtifact();
536 MetadataFacet facet = new TestMetadataFacet( "baz" );
537 metadata.addFacet( facet );
538 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
540 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
541 TEST_PROJECT_VERSION ).iterator().next();
542 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
544 metadata = createArtifact();
545 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
547 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
548 TEST_PROJECT_VERSION ).iterator().next();
549 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
550 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
551 assertEquals( "baz", testFacet.getValue() );
555 public void testUpdateArtifactMetadataWithNoExistingFacets()
558 ArtifactMetadata metadata = createArtifact();
559 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
561 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
562 TEST_PROJECT_VERSION ).iterator().next();
563 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
565 metadata = createArtifact();
566 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
568 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
569 TEST_PROJECT_VERSION ).iterator().next();
570 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
574 public void testGetMetadataFacet()
577 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
579 TestMetadataFacet test =
580 (TestMetadataFacet) repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
582 assertEquals( new TestMetadataFacet( TEST_VALUE ), test );
586 public void testGetMetadataFacetWhenEmpty()
589 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
593 public void testGetMetadataFacetWhenUnknownName()
596 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
598 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
602 public void testGetMetadataFacetWhenDefaultValue()
605 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
607 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ),
608 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
612 public void testGetMetadataFacetWhenUnknownFacetId()
615 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
619 public void testGetMetadataFacets()
622 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
624 assertEquals( Collections.singletonList( TEST_NAME ),
625 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
629 public void testGetMetadataFacetsWhenEmpty()
633 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
634 assertTrue( facets.isEmpty() );
638 public void testRemoveFacets()
641 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
643 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
644 assertFalse( facets.isEmpty() );
646 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
648 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
649 assertTrue( facets.isEmpty() );
653 public void testRemoveFacetsWhenEmpty()
656 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
657 assertTrue( facets.isEmpty() );
659 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
661 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
662 assertTrue( facets.isEmpty() );
666 public void testRemoveFacetsWhenUnknown()
669 // testing no exception
670 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
674 public void testRemoveFacetWhenUnknown()
677 // testing no exception
678 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
682 public void testRemoveFacet()
685 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
686 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
688 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
689 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
690 assertFalse( facets.isEmpty() );
692 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
694 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
695 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
696 assertTrue( facets.isEmpty() );
700 public void testRemoveFacetWhenEmpty()
703 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
704 assertTrue( facets.isEmpty() );
705 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
707 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
709 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
710 assertTrue( facets.isEmpty() );
711 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
715 public void hasMetadataFacetStart()
718 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
723 public void hasMetadataFacet()
727 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
729 Calendar cal = Calendar.getInstance();
731 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
733 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
735 cal.add( Calendar.MINUTE, 2 );
737 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
739 cal.add( Calendar.MINUTE, 2 );
741 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
743 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
745 Assertions.assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
747 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
749 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
751 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
753 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
755 Assertions.assertThat( facets ).isNotNull().isEmpty();
761 public void testGetArtifacts()
764 ArtifactMetadata artifact1 = createArtifact();
765 ArtifactMetadata artifact2 = createArtifact( "pom" );
766 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
767 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
769 Collection<ArtifactMetadata> artifacts =
770 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
771 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
772 Collections.sort( actual, new Comparator<ArtifactMetadata>()
774 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
776 return o1.getId().compareTo( o2.getId() );
779 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
783 public void testGetArtifactVersions()
786 ArtifactMetadata artifact1 = createArtifact();
787 String version1 = "1.0-20091212.012345-1";
788 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
789 artifact1.setVersion( version1 );
790 ArtifactMetadata artifact2 = createArtifact();
791 String version2 = "1.0-20091212.123456-2";
792 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
793 artifact2.setVersion( version2 );
794 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
795 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
797 List<String> versions = new ArrayList<String>(
798 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
799 Collections.sort( versions );
800 assertEquals( Arrays.asList( version1, version2 ), versions );
804 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
807 ArtifactMetadata artifact1 = createArtifact();
808 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
809 ArtifactMetadata artifact2 = createArtifact();
810 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
811 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
812 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
814 Collection<String> versions =
815 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
817 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ), versions );
821 public void testGetArtifactsByDateRangeOpen()
824 ArtifactMetadata artifact = createArtifact();
825 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
828 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
830 assertEquals( Collections.singletonList( artifact ), artifacts );
834 public void testGetArtifactsByDateRangeSparseNamespace()
837 String namespace = "org.apache.archiva";
838 ArtifactMetadata artifact = createArtifact();
839 artifact.setNamespace( namespace );
840 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
843 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
845 assertEquals( Collections.singletonList( artifact ), artifacts );
849 public void testGetArtifactsByDateRangeLowerBound()
852 ArtifactMetadata artifact = createArtifact();
853 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
856 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
858 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
860 assertEquals( Collections.singletonList( artifact ), artifacts );
864 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
867 ArtifactMetadata artifact = createArtifact();
868 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
870 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
872 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, date, null );
874 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
878 public void testGetArtifactsByDateRangeLowerAndUpperBound()
881 ArtifactMetadata artifact = createArtifact();
882 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
885 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
886 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
888 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper );
890 assertEquals( Collections.singletonList( artifact ), artifacts );
894 public void testGetArtifactsByDateRangeUpperBound()
897 ArtifactMetadata artifact = createArtifact();
898 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
901 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
903 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
905 assertEquals( Collections.singletonList( artifact ), artifacts );
909 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
912 ArtifactMetadata artifact = createArtifact();
913 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
916 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
918 List<ArtifactMetadata> artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper );
920 //assertTrue( .isEmpty() );
922 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
926 public void testGetArtifactsByRepoId()
929 ArtifactMetadata artifact = createArtifact();
930 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
933 List<ArtifactMetadata> artifacts = repository.getArtifacts( TEST_REPO_ID );
935 assertEquals( Collections.singletonList( artifact ), artifacts );
939 public void testGetArtifactsByRepoIdMultipleCopies()
942 ArtifactMetadata artifact = createArtifact();
943 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
945 ArtifactMetadata secondArtifact = createArtifact();
946 secondArtifact.setRepositoryId( OTHER_REPO_ID );
947 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
950 // test it restricts to the appropriate repository
951 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
952 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
956 public void testGetArtifactsByDateRangeMultipleCopies()
959 ArtifactMetadata artifact = createArtifact();
960 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
962 ArtifactMetadata secondArtifact = createArtifact();
963 secondArtifact.setRepositoryId( OTHER_REPO_ID );
964 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
967 // test it restricts to the appropriate repository
968 assertEquals( Collections.singletonList( artifact ),
969 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
970 assertEquals( Collections.singletonList( secondArtifact ),
971 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
975 public void testGetArtifactsByChecksumMultipleCopies()
978 ArtifactMetadata artifact = createArtifact();
979 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
981 ArtifactMetadata secondArtifact = createArtifact();
982 secondArtifact.setRepositoryId( OTHER_REPO_ID );
983 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
986 // test it restricts to the appropriate repository
987 assertEquals( Collections.singletonList( artifact ),
988 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
989 assertEquals( Collections.singletonList( secondArtifact ),
990 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) );
991 assertEquals( Collections.singletonList( artifact ),
992 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
993 assertEquals( Collections.singletonList( secondArtifact ),
994 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) );
998 public void testGetNamespacesWithSparseDepth()
1001 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1003 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
1004 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
1005 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
1006 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
1010 public void testGetNamespacesWithProjectsPresent()
1013 String namespace = "org.apache.maven.shared";
1014 repository.updateNamespace( TEST_REPO_ID, namespace );
1016 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
1017 metadata.setId( TEST_PROJECT_VERSION );
1018 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
1020 assertEquals( Collections.<String>emptyList(), repository.getNamespaces( TEST_REPO_ID, namespace ) );
1024 public void testGetProjectsWithOtherNamespacesPresent()
1027 ProjectMetadata projectMetadata = new ProjectMetadata();
1028 projectMetadata.setId( TEST_PROJECT );
1029 projectMetadata.setNamespace( "org.apache.maven" );
1030 repository.updateProject( TEST_REPO_ID, projectMetadata );
1032 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1034 assertEquals( Collections.singletonList( TEST_PROJECT ),
1035 repository.getProjects( TEST_REPO_ID, "org.apache.maven" ) );
1039 public void testGetProjectVersionsWithOtherNamespacesPresent()
1042 // an unusual case but technically possible where a project namespace matches another project's name
1044 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1045 versionMetadata.setId( TEST_PROJECT_VERSION );
1046 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1048 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1051 List<String> expectedVersions = Collections.singletonList( TEST_PROJECT_VERSION );
1052 assertEquals( expectedVersions, repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT,
1053 "other-project" ) );
1054 assertEquals( expectedVersions,
1055 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT ) );
1059 public void testGetArtifactsByChecksumSingleResultMd5()
1062 ArtifactMetadata artifact = createArtifact();
1063 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1066 assertEquals( Collections.singletonList( artifact ),
1067 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1071 public void testGetArtifactsByChecksumSingleResultSha1()
1074 ArtifactMetadata artifact = createArtifact();
1075 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1078 assertEquals( Collections.singletonList( artifact ),
1079 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1083 public void testGetArtifactsByChecksumDeepNamespace()
1086 ArtifactMetadata artifact = createArtifact();
1087 String namespace = "multi.level.ns";
1088 artifact.setNamespace( namespace );
1089 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1092 assertEquals( Collections.singletonList( artifact ),
1093 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1094 assertEquals( Collections.singletonList( artifact ),
1095 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1099 public void testGetArtifactsByChecksumMultipleResult()
1102 ArtifactMetadata artifact1 = createArtifact();
1103 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1105 String newProjectId = "another-project";
1106 ArtifactMetadata artifact2 = createArtifact();
1107 artifact2.setProject( newProjectId );
1108 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1111 List<ArtifactMetadata> artifacts =
1112 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1113 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1114 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1116 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1117 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1118 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1122 public void testGetArtifactsByChecksumNoResult()
1125 ArtifactMetadata artifact = createArtifact();
1126 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1128 List<ArtifactMetadata> artifactsByChecksum = repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1129 assertEquals( Collections.<ArtifactMetadata>emptyList(), artifactsByChecksum );
1134 public void testDeleteRepository()
1137 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1139 ProjectMetadata project1 = new ProjectMetadata();
1140 project1.setNamespace( TEST_NAMESPACE );
1141 project1.setId( "project1" );
1142 repository.updateProject( TEST_REPO_ID, project1 );
1143 ProjectMetadata project2 = new ProjectMetadata();
1144 project2.setNamespace( TEST_NAMESPACE );
1145 project2.setId( "project2" );
1146 repository.updateProject( TEST_REPO_ID, project2 );
1148 ArtifactMetadata artifact1 = createArtifact();
1149 artifact1.setProject( "project1" );
1150 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1151 ArtifactMetadata artifact2 = createArtifact();
1152 artifact2.setProject( "project2" );
1153 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1156 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1157 Collections.sort( expected, new ArtifactMetadataComparator() );
1159 List<ArtifactMetadata> actual =
1160 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1161 Collections.sort( actual, new ArtifactMetadataComparator() );
1163 assertEquals( expected, actual );
1165 repository.removeRepository( TEST_REPO_ID );
1167 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1168 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1173 public void testDeleteArtifact()
1176 ArtifactMetadata artifact = createArtifact();
1177 artifact.addFacet( new TestMetadataFacet( "value" ) );
1179 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1181 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1182 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1184 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1186 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1188 log.info( "versions {}", versions );
1190 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1192 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1194 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1196 log.info( "versions {}", versions );
1198 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1201 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
1203 Assertions.assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1204 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1208 public void deleteArtifact()
1211 ArtifactMetadata artifact = createArtifact();
1212 artifact.addFacet( new TestMetadataFacet( "value" ) );
1214 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1216 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1218 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1219 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1221 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1223 Collection<ArtifactMetadata> artifacts =
1224 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1226 Assertions.assertThat( artifacts ).isNotNull().isEmpty();
1230 public void deleteVersion()
1233 ArtifactMetadata artifact = createArtifact();
1234 artifact.addFacet( new TestMetadataFacet( "value" ) );
1236 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1238 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1240 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1242 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1244 assertEquals( 0, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1248 public void deleteProject()
1251 ArtifactMetadata artifact = createArtifact();
1252 artifact.addFacet( new TestMetadataFacet( "value" ) );
1254 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1256 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1258 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1260 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1262 assertEquals( 0, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1267 public void deleteSnapshotVersion()
1270 ArtifactMetadata artifactOne = createArtifact();
1271 artifactOne.setVersion( "2.0-20120618.214127-1" );
1272 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1273 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1274 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1276 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1278 ArtifactMetadata artifactTwo = createArtifact();
1279 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1280 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1281 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1282 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1284 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1286 Collection<ArtifactMetadata> artifactMetadatas =
1287 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1289 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1291 log.info( "artifactMetadatas: {}", artifactMetadatas );
1293 //assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1294 // repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" ) ) );
1296 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1298 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1300 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1302 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1304 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1306 Assertions.assertThat( artifactMetadatas ).isNotNull().isEmpty();
1310 private static ProjectMetadata createProject()
1312 return createProject( TEST_NAMESPACE );
1315 private static ProjectMetadata createProject( String ns )
1317 ProjectMetadata project = new ProjectMetadata();
1318 project.setId( TEST_PROJECT );
1319 project.setNamespace( ns );
1323 private static ArtifactMetadata createArtifact()
1325 return createArtifact( "jar" );
1328 private static ArtifactMetadata createArtifact( String type )
1330 ArtifactMetadata artifact = new ArtifactMetadata();
1331 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1332 artifact.setWhenGathered( new Date() );
1333 artifact.setNamespace( TEST_NAMESPACE );
1334 artifact.setProject( TEST_PROJECT );
1335 artifact.setRepositoryId( TEST_REPO_ID );
1336 artifact.setFileLastModified( System.currentTimeMillis() );
1337 artifact.setVersion( TEST_PROJECT_VERSION );
1338 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1339 artifact.setMd5( TEST_MD5 );
1340 artifact.setSha1( TEST_SHA1 );
1344 private static class ArtifactMetadataComparator
1345 implements Comparator<ArtifactMetadata>
1347 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1349 return a.getProject().compareTo( b.getProject() );
1353 private static class KindOfRepositoryStatistics
1354 implements MetadataFacet
1356 private String value;
1360 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1362 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1364 private KindOfRepositoryStatistics( String value, Date date )
1370 public String getName()
1372 return createNameFormat().format( date );
1375 private static SimpleDateFormat createNameFormat()
1377 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1378 fmt.setTimeZone( UTC_TIME_ZONE );
1382 public String getFacetId()
1384 return KindOfRepositoryStatistics.class.getName();
1387 public Map<String, String> toProperties()
1389 return Collections.emptyMap();
1392 public void fromProperties( Map<String, String> properties )
1398 private static class TestMetadataFacet
1399 implements MetadataFacet
1401 private String testFacetId;
1403 private Map<String, String> additionalProps;
1405 private String value;
1407 private TestMetadataFacet( String value )
1410 testFacetId = TEST_FACET_ID;
1413 private TestMetadataFacet( String facetId, String value )
1416 testFacetId = facetId;
1419 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1421 this( facetId, value );
1422 this.additionalProps = additionalProps;
1425 public String getFacetId()
1430 public String getName()
1435 public Map<String, String> toProperties()
1437 if ( value != null )
1439 if ( additionalProps == null )
1441 return Collections.singletonMap( "foo", value );
1445 Map<String, String> props = new HashMap<String, String>();
1446 props.put( "foo", value );
1448 for ( String key : additionalProps.keySet() )
1450 props.put( key, additionalProps.get( key ) );
1457 return Collections.emptyMap();
1461 public void fromProperties( Map<String, String> properties )
1463 String value = properties.get( "foo" );
1464 if ( value != null )
1469 properties.remove( "foo" );
1471 if ( additionalProps == null )
1473 additionalProps = new HashMap<String, String>();
1476 for ( String key : properties.keySet() )
1478 additionalProps.put( key, properties.get( key ) );
1482 public String getValue()
1488 public String toString()
1490 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1494 public boolean equals( Object o )
1500 if ( o == null || getClass() != o.getClass() )
1505 TestMetadataFacet that = (TestMetadataFacet) o;
1507 if ( value != null ? !value.equals( that.value ) : that.value != null )
1516 public int hashCode()
1518 return value != null ? value.hashCode() : 0;