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 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
168 TEST_NAMESPACE ).hasSize( 1 );
172 public void testGetProjectVersionOnly()
175 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
176 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
177 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
179 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
180 metadata.setId( TEST_PROJECT_VERSION );
182 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
184 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
185 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
187 // test that namespace and project is also constructed
188 assertEquals( Collections.singletonList( TEST_NAMESPACE ), repository.getRootNamespaces( TEST_REPO_ID ) );
189 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
190 assertEquals( TEST_PROJECT, projectMetadata.getId() );
191 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
195 public void testGetArtifactOnly()
198 assertEquals( Collections.<ArtifactMetadata>emptyList(), new ArrayList<ArtifactMetadata>(
199 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
200 assertNull( repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
201 assertNull( repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ) );
203 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isEmpty();
205 ArtifactMetadata metadata = createArtifact();
207 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
209 Collection<ArtifactMetadata> artifacts =
210 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
211 assertEquals( Collections.singletonList( metadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
213 // test that namespace, project and project version is also constructed
215 Assertions.assertThat( repository.getRootNamespaces( TEST_REPO_ID ) ).isNotNull().isNotEmpty().contains(
216 TEST_NAMESPACE ).hasSize( 1 );
218 ProjectMetadata projectMetadata = repository.getProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
219 assertEquals( TEST_PROJECT, projectMetadata.getId() );
220 assertEquals( TEST_NAMESPACE, projectMetadata.getNamespace() );
222 ProjectVersionMetadata projectVersionMetadata =
223 repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
224 assertEquals( TEST_PROJECT_VERSION, projectVersionMetadata.getId() );
228 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
231 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
232 metadata.setId( TEST_PROJECT_VERSION );
233 MailingList mailingList = new MailingList();
234 mailingList.setName( "Foo List" );
235 mailingList.setOtherArchives( Collections.<String>emptyList() );
236 metadata.setMailingLists( Collections.singletonList( mailingList ) );
237 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
239 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
240 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
241 assertEquals( 1, metadata.getMailingLists().size() );
242 mailingList = metadata.getMailingLists().get( 0 );
243 assertEquals( "Foo List", mailingList.getName() );
244 assertEquals( Collections.<String>emptyList(), mailingList.getOtherArchives() );
248 public void testUpdateProjectVersionMetadataWithAllElements()
251 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
252 metadata.setId( TEST_PROJECT_VERSION );
254 metadata.setName( "project name" );
255 metadata.setDescription( "project description" );
257 MailingList mailingList = new MailingList();
258 mailingList.setName( "Foo List" );
259 mailingList.setOtherArchives( Collections.singletonList( "other archive" ) );
260 metadata.setMailingLists( Collections.singletonList( mailingList ) );
263 scm.setConnection( "connection" );
264 scm.setDeveloperConnection( "dev conn" );
266 metadata.setScm( scm );
268 CiManagement ci = new CiManagement();
269 ci.setSystem( "system" );
270 ci.setUrl( "ci url" );
271 metadata.setCiManagement( ci );
273 IssueManagement tracker = new IssueManagement();
274 tracker.setSystem( "system" );
275 tracker.setUrl( "issue tracker url" );
276 metadata.setIssueManagement( tracker );
278 Organization org = new Organization();
279 org.setName( "org name" );
281 metadata.setOrganization( org );
283 License l = new License();
284 l.setName( "license name" );
285 l.setUrl( "license url" );
286 metadata.addLicense( l );
288 Dependency d = new Dependency();
289 d.setArtifactId( "artifactId" );
290 d.setClassifier( "classifier" );
291 d.setGroupId( "groupId" );
292 d.setScope( "scope" );
293 d.setSystemPath( "system path" );
295 d.setVersion( "version" );
296 d.setOptional( true );
297 metadata.addDependency( d );
299 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
301 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
302 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
303 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
304 assertEquals( "project name", metadata.getName() );
305 assertEquals( "project description", metadata.getDescription() );
307 assertEquals( 1, metadata.getMailingLists().size() );
308 mailingList = metadata.getMailingLists().get( 0 );
309 assertEquals( "Foo List", mailingList.getName() );
310 assertEquals( Collections.singletonList( "other archive" ), mailingList.getOtherArchives() );
312 assertEquals( "connection", metadata.getScm().getConnection() );
313 assertEquals( "dev conn", metadata.getScm().getDeveloperConnection() );
314 assertEquals( "url", metadata.getScm().getUrl() );
316 assertEquals( "system", metadata.getCiManagement().getSystem() );
317 assertEquals( "ci url", metadata.getCiManagement().getUrl() );
319 assertEquals( "system", metadata.getIssueManagement().getSystem() );
320 assertEquals( "issue tracker url", metadata.getIssueManagement().getUrl() );
322 assertEquals( "org name", metadata.getOrganization().getName() );
323 assertEquals( "url", metadata.getOrganization().getUrl() );
325 assertEquals( 1, metadata.getLicenses().size() );
326 l = metadata.getLicenses().get( 0 );
327 assertEquals( "license name", l.getName() );
328 assertEquals( "license url", l.getUrl() );
330 assertEquals( 1, metadata.getDependencies().size() );
331 d = metadata.getDependencies().get( 0 );
332 assertEquals( "artifactId", d.getArtifactId() );
333 assertEquals( "classifier", d.getClassifier() );
334 assertEquals( "groupId", d.getGroupId() );
335 assertEquals( "scope", d.getScope() );
336 assertEquals( "system path", d.getSystemPath() );
337 assertEquals( "type", d.getType() );
338 assertEquals( "version", d.getVersion() );
339 assertTrue( d.isOptional() );
343 public void testGetRepositories()
346 // currently set up this way so the behaviour of both the test and the mock config return the same repository
347 // set as the File implementation just uses the config rather than the content
348 repository.updateNamespace( TEST_REPO_ID, "namespace" );
349 repository.updateNamespace( OTHER_REPO_ID, "namespace" );
351 assertEquals( "repository.getRepositories() -> " + repository.getRepositories(),
352 Arrays.asList( TEST_REPO_ID, OTHER_REPO_ID ),
353 new ArrayList<String>( repository.getRepositories() ) );
357 public void testUpdateProjectVersionMetadataIncomplete()
360 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
361 metadata.setId( TEST_PROJECT_VERSION );
362 metadata.setIncomplete( true );
363 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
365 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
366 assertEquals( true, metadata.isIncomplete() );
367 assertNull( metadata.getCiManagement() );
368 assertNull( metadata.getScm() );
369 assertNull( metadata.getIssueManagement() );
370 assertNull( metadata.getOrganization() );
371 assertNull( metadata.getDescription() );
372 assertNull( metadata.getName() );
373 assertEquals( TEST_PROJECT_VERSION, metadata.getId() );
374 assertEquals( TEST_PROJECT_VERSION, metadata.getVersion() );
375 assertTrue( metadata.getMailingLists().isEmpty() );
376 assertTrue( metadata.getLicenses().isEmpty() );
377 assertTrue( metadata.getDependencies().isEmpty() );
381 public void testUpdateProjectVersionMetadataWithExistingFacets()
384 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
385 metadata.setId( TEST_PROJECT_VERSION );
386 MetadataFacet facet = new TestMetadataFacet( "baz" );
387 metadata.addFacet( facet );
388 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
390 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
391 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
393 metadata = new ProjectVersionMetadata();
394 metadata.setId( TEST_PROJECT_VERSION );
395 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
397 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
398 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
399 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
400 assertEquals( "baz", testFacet.getValue() );
404 public void testUpdateProjectVersionMetadataWithNoExistingFacets()
407 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
408 metadata.setId( TEST_PROJECT_VERSION );
409 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
411 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
412 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
414 metadata = new ProjectVersionMetadata();
415 metadata.setId( TEST_PROJECT_VERSION );
416 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
418 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
419 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
423 public void testUpdateProjectVersionMetadataWithExistingFacetsFacetPropertyWasRemoved()
426 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
427 metadata.setId( TEST_PROJECT_VERSION );
429 Map<String, String> additionalProps = new HashMap<String, String>();
430 additionalProps.put( "deleteKey", "deleteValue" );
432 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
433 metadata.addFacet( facet );
434 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
436 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
437 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
439 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
440 Map<String, String> facetProperties = testFacet.toProperties();
442 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
444 facetProperties.remove( "deleteKey" );
446 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
447 metadata.addFacet( newTestFacet );
449 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
451 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
452 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
453 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
454 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
458 public void testGetArtifactsDoesntReturnProjectVersionMetadataFacets()
461 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
462 versionMetadata.setId( TEST_PROJECT_VERSION );
464 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz" );
465 versionMetadata.addFacet( facet );
466 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, versionMetadata );
468 ArtifactMetadata artifactMetadata = createArtifact();
469 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifactMetadata );
472 Collection<ArtifactMetadata> artifacts =
473 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
474 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
476 artifacts = repository.getArtifacts( TEST_REPO_ID );
477 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
479 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 );
480 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
482 artifacts = repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 );
483 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
485 artifacts = repository.getArtifactsByDateRange( TEST_REPO_ID, null, null );
486 assertEquals( Collections.singletonList( artifactMetadata ), new ArrayList<ArtifactMetadata>( artifacts ) );
490 public void testUpdateArtifactMetadataWithExistingFacetsFacetPropertyWasRemoved()
493 ArtifactMetadata metadata = createArtifact();
495 Map<String, String> additionalProps = new HashMap<String, String>();
496 additionalProps.put( "deleteKey", "deleteValue" );
498 MetadataFacet facet = new TestMetadataFacet( TEST_FACET_ID, "baz", additionalProps );
499 metadata.addFacet( facet );
500 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
502 Collection<ArtifactMetadata> artifacts =
503 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
504 assertEquals( 1, artifacts.size() );
505 metadata = artifacts.iterator().next();
506 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
508 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
509 Map<String, String> facetProperties = testFacet.toProperties();
511 assertEquals( "deleteValue", facetProperties.get( "deleteKey" ) );
513 facetProperties.remove( "deleteKey" );
515 TestMetadataFacet newTestFacet = new TestMetadataFacet( TEST_FACET_ID, testFacet.getValue(), facetProperties );
516 metadata.addFacet( newTestFacet );
518 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
520 artifacts = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
521 assertEquals( 1, artifacts.size() );
522 metadata = artifacts.iterator().next();
523 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
524 testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
525 assertFalse( testFacet.toProperties().containsKey( "deleteKey" ) );
529 public void testUpdateArtifactMetadataWithExistingFacets()
532 ArtifactMetadata metadata = createArtifact();
533 MetadataFacet facet = new TestMetadataFacet( "baz" );
534 metadata.addFacet( facet );
535 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
537 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
538 TEST_PROJECT_VERSION ).iterator().next();
539 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
541 metadata = createArtifact();
542 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
544 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
545 TEST_PROJECT_VERSION ).iterator().next();
546 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
547 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
548 assertEquals( "baz", testFacet.getValue() );
552 public void testUpdateArtifactMetadataWithNoExistingFacets()
555 ArtifactMetadata metadata = createArtifact();
556 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
558 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
559 TEST_PROJECT_VERSION ).iterator().next();
560 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
562 metadata = createArtifact();
563 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, metadata );
565 metadata = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
566 TEST_PROJECT_VERSION ).iterator().next();
567 assertEquals( Collections.<String>emptyList(), new ArrayList<String>( metadata.getFacetIds() ) );
571 public void testGetMetadataFacet()
574 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
576 assertEquals( new TestMetadataFacet( TEST_VALUE ),
577 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
581 public void testGetMetadataFacetWhenEmpty()
584 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
588 public void testGetMetadataFacetWhenUnknownName()
591 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
593 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
597 public void testGetMetadataFacetWhenDefaultValue()
600 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( null ) );
602 assertEquals( new TestMetadataFacet( TEST_METADATA_VALUE ),
603 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
607 public void testGetMetadataFacetWhenUnknownFacetId()
610 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
614 public void testGetMetadataFacets()
617 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
619 assertEquals( Collections.singletonList( TEST_NAME ),
620 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
624 public void testGetMetadataFacetsWhenEmpty()
628 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
629 assertTrue( facets.isEmpty() );
633 public void testRemoveFacets()
636 repository.addMetadataFacet( TEST_REPO_ID, new TestMetadataFacet( TEST_VALUE ) );
638 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
639 assertFalse( facets.isEmpty() );
641 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
643 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
644 assertTrue( facets.isEmpty() );
648 public void testRemoveFacetsWhenEmpty()
651 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
652 assertTrue( facets.isEmpty() );
654 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
656 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
657 assertTrue( facets.isEmpty() );
661 public void testRemoveFacetsWhenUnknown()
664 // testing no exception
665 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
669 public void testRemoveFacetWhenUnknown()
672 // testing no exception
673 repository.removeMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME );
677 public void testRemoveFacet()
680 TestMetadataFacet metadataFacet = new TestMetadataFacet( TEST_VALUE );
681 repository.addMetadataFacet( TEST_REPO_ID, metadataFacet );
683 assertEquals( metadataFacet, repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
684 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
685 assertFalse( facets.isEmpty() );
687 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
689 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
690 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
691 assertTrue( facets.isEmpty() );
695 public void testRemoveFacetWhenEmpty()
698 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
699 assertTrue( facets.isEmpty() );
700 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
702 repository.removeMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME );
704 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
705 assertTrue( facets.isEmpty() );
706 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
710 public void hasMetadataFacetStart()
713 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
718 public void hasMetadataFacet()
722 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
724 Calendar cal = Calendar.getInstance();
726 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "first", cal.getTime() ) );
728 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
730 cal.add( Calendar.MINUTE, 2 );
732 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "second", cal.getTime() ) );
734 cal.add( Calendar.MINUTE, 2 );
736 repository.addMetadataFacet( TEST_REPO_ID, new KindOfRepositoryStatistics( "third", cal.getTime() ) );
738 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
740 Assertions.assertThat( facets ).isNotNull().isNotEmpty().hasSize( 3 );
742 assertTrue( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
744 repository.removeMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
746 assertFalse( repository.hasMetadataFacet( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() ) );
748 facets = repository.getMetadataFacets( TEST_REPO_ID, KindOfRepositoryStatistics.class.getName() );
750 Assertions.assertThat( facets ).isNotNull().isEmpty();
756 public void testGetArtifacts()
759 ArtifactMetadata artifact1 = createArtifact();
760 ArtifactMetadata artifact2 = createArtifact( "pom" );
761 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
762 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
764 Collection<ArtifactMetadata> artifacts =
765 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
766 ArrayList<ArtifactMetadata> actual = new ArrayList<ArtifactMetadata>( artifacts );
767 Collections.sort( actual, new Comparator<ArtifactMetadata>()
769 public int compare( ArtifactMetadata o1, ArtifactMetadata o2 )
771 return o1.getId().compareTo( o2.getId() );
774 assertEquals( Arrays.asList( artifact1, artifact2 ), actual );
778 public void testGetArtifactVersions()
781 ArtifactMetadata artifact1 = createArtifact();
782 String version1 = "1.0-20091212.012345-1";
783 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
784 artifact1.setVersion( version1 );
785 ArtifactMetadata artifact2 = createArtifact();
786 String version2 = "1.0-20091212.123456-2";
787 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
788 artifact2.setVersion( version2 );
789 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
790 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
792 List<String> versions = new ArrayList<String>(
793 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) );
794 Collections.sort( versions );
795 assertEquals( Arrays.asList( version1, version2 ), versions );
799 public void testGetArtifactVersionsMultipleArtifactsSingleVersion()
802 ArtifactMetadata artifact1 = createArtifact();
803 artifact1.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + ".jar" );
804 ArtifactMetadata artifact2 = createArtifact();
805 artifact2.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "-sources.jar" );
806 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
807 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
809 assertEquals( Collections.singleton( TEST_PROJECT_VERSION ),
810 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
811 TEST_PROJECT_VERSION ) );
815 public void testGetArtifactsByDateRangeOpen()
818 ArtifactMetadata artifact = createArtifact();
819 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
822 assertEquals( Collections.singletonList( artifact ),
823 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
827 public void testGetArtifactsByDateRangeSparseNamespace()
830 String namespace = "org.apache.archiva";
831 ArtifactMetadata artifact = createArtifact();
832 artifact.setNamespace( namespace );
833 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
836 assertEquals( Collections.singletonList( artifact ),
837 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
841 public void testGetArtifactsByDateRangeLowerBound()
844 ArtifactMetadata artifact = createArtifact();
845 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
848 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
849 assertEquals( Collections.singletonList( artifact ),
850 repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ) );
854 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
857 ArtifactMetadata artifact = createArtifact();
858 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
860 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
861 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ).isEmpty() );
865 public void testGetArtifactsByDateRangeLowerAndUpperBound()
868 ArtifactMetadata artifact = createArtifact();
869 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
872 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
873 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
874 assertEquals( Collections.singletonList( artifact ),
875 repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper ) );
879 public void testGetArtifactsByDateRangeUpperBound()
882 ArtifactMetadata artifact = createArtifact();
883 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
886 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
887 assertEquals( Collections.singletonList( artifact ),
888 repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ) );
892 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
895 ArtifactMetadata artifact = createArtifact();
896 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
899 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
900 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ).isEmpty() );
904 public void testGetArtifactsByRepoId()
907 ArtifactMetadata artifact = createArtifact();
908 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
911 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
915 public void testGetArtifactsByRepoIdMultipleCopies()
918 ArtifactMetadata artifact = createArtifact();
919 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
921 ArtifactMetadata secondArtifact = createArtifact();
922 secondArtifact.setRepositoryId( OTHER_REPO_ID );
923 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
926 // test it restricts to the appropriate repository
927 assertEquals( Collections.singletonList( artifact ), repository.getArtifacts( TEST_REPO_ID ) );
928 assertEquals( Collections.singletonList( secondArtifact ), repository.getArtifacts( OTHER_REPO_ID ) );
932 public void testGetArtifactsByDateRangeMultipleCopies()
935 ArtifactMetadata artifact = createArtifact();
936 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
938 ArtifactMetadata secondArtifact = createArtifact();
939 secondArtifact.setRepositoryId( OTHER_REPO_ID );
940 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
943 // test it restricts to the appropriate repository
944 assertEquals( Collections.singletonList( artifact ),
945 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
946 assertEquals( Collections.singletonList( secondArtifact ),
947 repository.getArtifactsByDateRange( OTHER_REPO_ID, null, null ) );
951 public void testGetArtifactsByChecksumMultipleCopies()
954 ArtifactMetadata artifact = createArtifact();
955 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
957 ArtifactMetadata secondArtifact = createArtifact();
958 secondArtifact.setRepositoryId( OTHER_REPO_ID );
959 repository.updateArtifact( OTHER_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, secondArtifact );
962 // test it restricts to the appropriate repository
963 assertEquals( Collections.singletonList( artifact ),
964 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
965 assertEquals( Collections.singletonList( secondArtifact ),
966 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_SHA1 ) );
967 assertEquals( Collections.singletonList( artifact ),
968 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
969 assertEquals( Collections.singletonList( secondArtifact ),
970 repository.getArtifactsByChecksum( OTHER_REPO_ID, TEST_MD5 ) );
974 public void testGetNamespacesWithSparseDepth()
977 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
979 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
980 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
981 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
982 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
986 public void testGetNamespacesWithProjectsPresent()
989 String namespace = "org.apache.maven.shared";
990 repository.updateNamespace( TEST_REPO_ID, namespace );
992 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
993 metadata.setId( TEST_PROJECT_VERSION );
994 repository.updateProjectVersion( TEST_REPO_ID, namespace, TEST_PROJECT, metadata );
996 assertEquals( Collections.<String>emptyList(), repository.getNamespaces( TEST_REPO_ID, namespace ) );
1000 public void testGetProjectsWithOtherNamespacesPresent()
1003 ProjectMetadata projectMetadata = new ProjectMetadata();
1004 projectMetadata.setId( TEST_PROJECT );
1005 projectMetadata.setNamespace( "org.apache.maven" );
1006 repository.updateProject( TEST_REPO_ID, projectMetadata );
1008 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
1010 assertEquals( Collections.singletonList( TEST_PROJECT ),
1011 repository.getProjects( TEST_REPO_ID, "org.apache.maven" ) );
1015 public void testGetProjectVersionsWithOtherNamespacesPresent()
1018 // an unusual case but technically possible where a project namespace matches another project's name
1020 ProjectVersionMetadata versionMetadata = new ProjectVersionMetadata();
1021 versionMetadata.setId( TEST_PROJECT_VERSION );
1022 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT, versionMetadata );
1024 repository.updateProjectVersion( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT, "other-project",
1027 List<String> expectedVersions = Collections.singletonList( TEST_PROJECT_VERSION );
1028 assertEquals( expectedVersions, repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven." + TEST_PROJECT,
1029 "other-project" ) );
1030 assertEquals( expectedVersions,
1031 repository.getProjectVersions( TEST_REPO_ID, "org.apache.maven", TEST_PROJECT ) );
1035 public void testGetArtifactsByChecksumSingleResultMd5()
1038 ArtifactMetadata artifact = createArtifact();
1039 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1042 assertEquals( Collections.singletonList( artifact ),
1043 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1047 public void testGetArtifactsByChecksumSingleResultSha1()
1050 ArtifactMetadata artifact = createArtifact();
1051 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1054 assertEquals( Collections.singletonList( artifact ),
1055 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1059 public void testGetArtifactsByChecksumDeepNamespace()
1062 ArtifactMetadata artifact = createArtifact();
1063 String namespace = "multi.level.ns";
1064 artifact.setNamespace( namespace );
1065 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1068 assertEquals( Collections.singletonList( artifact ),
1069 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1070 assertEquals( Collections.singletonList( artifact ),
1071 repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1075 public void testGetArtifactsByChecksumMultipleResult()
1078 ArtifactMetadata artifact1 = createArtifact();
1079 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
1081 String newProjectId = "another-project";
1082 ArtifactMetadata artifact2 = createArtifact();
1083 artifact2.setProject( newProjectId );
1084 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, newProjectId, TEST_PROJECT_VERSION, artifact2 );
1087 List<ArtifactMetadata> artifacts =
1088 new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_SHA1 ) );
1089 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1090 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1092 artifacts = new ArrayList<ArtifactMetadata>( repository.getArtifactsByChecksum( TEST_REPO_ID, TEST_MD5 ) );
1093 Collections.sort( artifacts, new ArtifactMetadataComparator() );
1094 assertEquals( Arrays.asList( artifact2, artifact1 ), artifacts );
1098 public void testGetArtifactsByChecksumNoResult()
1101 ArtifactMetadata artifact = createArtifact();
1102 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1104 List<ArtifactMetadata> artifactsByChecksum = repository.getArtifactsByChecksum( TEST_REPO_ID, "not checksum" );
1105 assertEquals( Collections.<ArtifactMetadata>emptyList(), artifactsByChecksum );
1110 public void testDeleteRepository()
1113 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
1115 ProjectMetadata project1 = new ProjectMetadata();
1116 project1.setNamespace( TEST_NAMESPACE );
1117 project1.setId( "project1" );
1118 repository.updateProject( TEST_REPO_ID, project1 );
1119 ProjectMetadata project2 = new ProjectMetadata();
1120 project2.setNamespace( TEST_NAMESPACE );
1121 project2.setId( "project2" );
1122 repository.updateProject( TEST_REPO_ID, project2 );
1124 ArtifactMetadata artifact1 = createArtifact();
1125 artifact1.setProject( "project1" );
1126 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project1", TEST_PROJECT_VERSION, artifact1 );
1127 ArtifactMetadata artifact2 = createArtifact();
1128 artifact2.setProject( "project2" );
1129 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, "project2", TEST_PROJECT_VERSION, artifact2 );
1132 List<ArtifactMetadata> expected = Arrays.asList( artifact1, artifact2 );
1133 Collections.sort( expected, new ArtifactMetadataComparator() );
1135 List<ArtifactMetadata> actual =
1136 new ArrayList<ArtifactMetadata>( repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
1137 Collections.sort( actual, new ArtifactMetadataComparator() );
1139 assertEquals( expected, actual );
1141 repository.removeRepository( TEST_REPO_ID );
1143 assertTrue( repository.getArtifacts( TEST_REPO_ID ).isEmpty() );
1144 assertTrue( repository.getRootNamespaces( TEST_REPO_ID ).isEmpty() );
1149 public void testDeleteArtifact()
1152 ArtifactMetadata artifact = createArtifact();
1153 artifact.addFacet( new TestMetadataFacet( "value" ) );
1155 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1157 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1158 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1160 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION_2_0, artifact );
1162 Collection<String> versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1164 log.info( "versions {}", versions );
1166 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 2 ).contains( "1.0", "2.0" );
1168 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1170 versions = repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1172 log.info( "versions {}", versions );
1174 Assertions.assertThat( versions ).isNotNull().isNotEmpty().hasSize( 1 ).contains( "2.0" );
1177 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
1179 Assertions.assertThat( repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
1180 TEST_PROJECT_VERSION_2_0 ) ).isNotEmpty().hasSize( 1 );
1184 public void deleteArtifact()
1187 ArtifactMetadata artifact = createArtifact();
1188 artifact.addFacet( new TestMetadataFacet( "value" ) );
1190 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1192 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1194 assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1195 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
1197 repository.removeArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact.getId() );
1200 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ).isEmpty() );
1204 public void deleteVersion()
1207 ArtifactMetadata artifact = createArtifact();
1208 artifact.addFacet( new TestMetadataFacet( "value" ) );
1210 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1212 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1214 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1216 repository.removeProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
1218 assertEquals( 0, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1222 public void deleteProject()
1225 ArtifactMetadata artifact = createArtifact();
1226 artifact.addFacet( new TestMetadataFacet( "value" ) );
1228 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1230 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
1232 assertEquals( 1, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1234 repository.removeProject( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT );
1236 assertEquals( 0, repository.getProjectVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT ).size() );
1241 public void deleteSnapshotVersion()
1244 ArtifactMetadata artifactOne = createArtifact();
1245 artifactOne.setVersion( "2.0-20120618.214127-1" );
1246 artifactOne.setProjectVersion( "2.0-SNAPSHOT" );
1247 artifactOne.addFacet( new TestMetadataFacet( "value" ) );
1248 artifactOne.setId( TEST_PROJECT + "-" + "2.0-20120618.214127-1" + "." + "jar" );
1250 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactOne );
1252 ArtifactMetadata artifactTwo = createArtifact();
1253 artifactTwo.setVersion( "2.0-20120618.214135-2" );
1254 artifactTwo.setProjectVersion( "2.0-SNAPSHOT" );
1255 artifactTwo.addFacet( new TestMetadataFacet( "value" ) );
1256 artifactTwo.setId( TEST_PROJECT + "-" + "2.0-20120618.214135-2" + "." + "jar" );
1258 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT", artifactTwo );
1260 Collection<ArtifactMetadata> artifactMetadatas =
1261 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1263 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 2 );
1265 log.info( "artifactMetadatas: {}", artifactMetadatas );
1267 //assertEquals( Collections.singletonList( artifact ), new ArrayList<ArtifactMetadata>(
1268 // repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" ) ) );
1270 repository.removeArtifact( artifactOne, "2.0-SNAPSHOT" );
1272 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1274 Assertions.assertThat( artifactMetadatas ).isNotNull().isNotEmpty().hasSize( 1 );
1276 repository.removeArtifact( artifactTwo, "2.0-SNAPSHOT" );
1278 artifactMetadatas = repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, "2.0-SNAPSHOT" );
1280 Assertions.assertThat( artifactMetadatas ).isNotNull().isEmpty();
1284 private static ProjectMetadata createProject()
1286 return createProject( TEST_NAMESPACE );
1289 private static ProjectMetadata createProject( String ns )
1291 ProjectMetadata project = new ProjectMetadata();
1292 project.setId( TEST_PROJECT );
1293 project.setNamespace( ns );
1297 private static ArtifactMetadata createArtifact()
1299 return createArtifact( "jar" );
1302 private static ArtifactMetadata createArtifact( String type )
1304 ArtifactMetadata artifact = new ArtifactMetadata();
1305 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
1306 artifact.setWhenGathered( new Date() );
1307 artifact.setNamespace( TEST_NAMESPACE );
1308 artifact.setProject( TEST_PROJECT );
1309 artifact.setRepositoryId( TEST_REPO_ID );
1310 artifact.setFileLastModified( System.currentTimeMillis() );
1311 artifact.setVersion( TEST_PROJECT_VERSION );
1312 artifact.setProjectVersion( TEST_PROJECT_VERSION );
1313 artifact.setMd5( TEST_MD5 );
1314 artifact.setSha1( TEST_SHA1 );
1318 private static class ArtifactMetadataComparator
1319 implements Comparator<ArtifactMetadata>
1321 public final int compare( ArtifactMetadata a, ArtifactMetadata b )
1323 return a.getProject().compareTo( b.getProject() );
1327 private static class KindOfRepositoryStatistics
1328 implements MetadataFacet
1330 private String value;
1334 static final String SCAN_TIMESTAMP_FORMAT = "yyyy/MM/dd/HHmmss.SSS";
1336 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
1338 private KindOfRepositoryStatistics( String value, Date date )
1344 public String getName()
1346 return createNameFormat().format( date );
1349 private static SimpleDateFormat createNameFormat()
1351 SimpleDateFormat fmt = new SimpleDateFormat( SCAN_TIMESTAMP_FORMAT );
1352 fmt.setTimeZone( UTC_TIME_ZONE );
1356 public String getFacetId()
1358 return KindOfRepositoryStatistics.class.getName();
1361 public Map<String, String> toProperties()
1363 return Collections.emptyMap();
1366 public void fromProperties( Map<String, String> properties )
1372 private static class TestMetadataFacet
1373 implements MetadataFacet
1375 private String testFacetId;
1377 private Map<String, String> additionalProps;
1379 private String value;
1381 private TestMetadataFacet( String value )
1384 testFacetId = TEST_FACET_ID;
1387 private TestMetadataFacet( String facetId, String value )
1390 testFacetId = facetId;
1393 private TestMetadataFacet( String facetId, String value, Map<String, String> additionalProps )
1395 this( facetId, value );
1396 this.additionalProps = additionalProps;
1399 public String getFacetId()
1404 public String getName()
1409 public Map<String, String> toProperties()
1411 if ( value != null )
1413 if ( additionalProps == null )
1415 return Collections.singletonMap( "foo", value );
1419 Map<String, String> props = new HashMap<String, String>();
1420 props.put( "foo", value );
1422 for ( String key : additionalProps.keySet() )
1424 props.put( key, additionalProps.get( key ) );
1431 return Collections.emptyMap();
1435 public void fromProperties( Map<String, String> properties )
1437 String value = properties.get( "foo" );
1438 if ( value != null )
1443 properties.remove( "foo" );
1445 if ( additionalProps == null )
1447 additionalProps = new HashMap<String, String>();
1450 for ( String key : properties.keySet() )
1452 additionalProps.put( key, properties.get( key ) );
1456 public String getValue()
1462 public String toString()
1464 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
1468 public boolean equals( Object o )
1474 if ( o == null || getClass() != o.getClass() )
1479 TestMetadataFacet that = (TestMetadataFacet) o;
1481 if ( value != null ? !value.equals( that.value ) : that.value != null )
1490 public int hashCode()
1492 return value != null ? value.hashCode() : 0;