1 package org.apache.archiva.metadata.repository.file;
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
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.List;
31 import org.apache.archiva.metadata.model.ArtifactMetadata;
32 import org.apache.archiva.metadata.model.MailingList;
33 import org.apache.archiva.metadata.model.MetadataFacet;
34 import org.apache.archiva.metadata.model.MetadataFacetFactory;
35 import org.apache.archiva.metadata.model.ProjectMetadata;
36 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
37 import org.apache.commons.io.FileUtils;
38 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
40 public class FileMetadataRepositoryTest
41 extends PlexusInSpringTestCase
43 private FileMetadataRepository repository;
45 private static final String TEST_REPO_ID = "test";
47 private static final String TEST_PROJECT = "projectId";
49 private static final String TEST_NAMESPACE = "namespace";
51 private static final String TEST_PROJECT_VERSION = "1.0";
53 private static final String TEST_FACET_ID = "test-facet-id";
55 private static final String TEST_NAME = "test-name";
57 private static final String TEST_VALUE = "test-value";
59 private static final String UNKNOWN = "unknown";
61 private static final String OTHER_REPO = "other-repo";
68 repository = new FileMetadataRepository();
69 File directory = getTestFile( "target/test-repository" );
70 FileUtils.deleteDirectory( directory );
71 repository.setDirectory( directory );
73 repository.setMetadataFacetFactories(
74 Collections.<String, MetadataFacetFactory>singletonMap( TEST_FACET_ID, new MetadataFacetFactory()
76 public MetadataFacet createMetadataFacet()
78 return new TestMetadataFacet( "test-metadata" );
83 public void testRootNamespaceWithNoMetadataRepository()
85 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
86 assertEquals( Collections.<String>emptyList(), namespaces );
89 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
91 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
92 metadata.setId( TEST_PROJECT_VERSION );
93 MailingList mailingList = new MailingList();
94 mailingList.setName( "Foo List" );
95 mailingList.setOtherArchives( Collections.<String>emptyList() );
96 metadata.setMailingLists( Collections.singletonList( mailingList ) );
97 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
100 public void testUpdateProjectVersionMetadataWithExistingFacets()
102 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
103 metadata.setId( TEST_PROJECT_VERSION );
104 MetadataFacet facet = new TestMetadataFacet( "baz" );
105 metadata.addFacet( facet );
106 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
108 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
109 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
111 metadata = new ProjectVersionMetadata();
112 metadata.setId( TEST_PROJECT_VERSION );
113 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
115 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
116 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
117 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
118 assertEquals( "baz", testFacet.getValue() );
121 public void testGetMetadataFacet()
123 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
125 assertEquals( new TestMetadataFacet( TEST_VALUE ),
126 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
129 public void testGetMetadataFacetWhenEmpty()
131 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
134 public void testGetMetadataFacetWhenUnknownName()
136 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
138 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
141 public void testGetMetadataFacetWhenDefaultValue()
143 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( null ) );
145 assertEquals( new TestMetadataFacet( "test-metadata" ),
146 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
149 public void testGetMetadataFacetWhenUnknownFacetId()
151 repository.addMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
153 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
156 public void testGetMetadataFacets()
158 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
160 assertEquals( Collections.singletonList( TEST_NAME ),
161 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
164 public void testGetMetadataFacetsWhenEmpty()
166 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
167 assertTrue( facets.isEmpty() );
170 public void testRemoveFacets()
172 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
173 assertTrue( facets.isEmpty() );
175 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
177 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
178 assertTrue( facets.isEmpty() );
181 public void testRemoveFacetsWhenEmpty()
183 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
185 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
186 assertFalse( facets.isEmpty() );
188 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
190 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
191 assertTrue( facets.isEmpty() );
194 public void testRemoveFacetsWhenUnknown()
196 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
199 public void testGetArtifacts()
201 ArtifactMetadata artifact1 = createArtifact();
202 ArtifactMetadata artifact2 = createArtifact( "pom" );
203 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
204 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
206 assertEquals( Arrays.asList( artifact2, artifact1 ), new ArrayList<ArtifactMetadata>(
207 repository.getArtifacts( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION ) ) );
210 public void testGetArtifactVersions()
212 ArtifactMetadata artifact1 = createArtifact();
213 String version1 = "1.0-20091212.012345-1";
214 artifact1.setId( artifact1.getProject() + "-" + version1 + ".jar" );
215 artifact1.setVersion( version1 );
216 ArtifactMetadata artifact2 = createArtifact();
217 String version2 = "1.0-20091212.123456-2";
218 artifact2.setId( artifact2.getProject() + "-" + version2 + ".jar" );
219 artifact2.setVersion( version2 );
220 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact1 );
221 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact2 );
223 assertEquals( Arrays.asList( version2, version1 ),
224 repository.getArtifactVersions( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT,
225 TEST_PROJECT_VERSION ) );
228 public void testRepositories()
230 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
231 repository.addMetadataFacet( OTHER_REPO, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
233 assertEquals( Arrays.asList( OTHER_REPO, TEST_REPO_ID ), repository.getRepositories() );
236 public void testRepositoriesWhenEmpty()
238 assertTrue( repository.getRepositories().isEmpty() );
241 public void testGetArtifactsByDateRangeOpen()
243 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
244 repository.updateProject( TEST_REPO_ID, createProject() );
245 ArtifactMetadata artifact = createArtifact();
246 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
248 assertEquals( Collections.singletonList( artifact ),
249 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
252 public void testGetArtifactsByDateRangeSparseNamespace()
254 String namespace = "org.apache.archiva";
255 repository.updateNamespace( TEST_REPO_ID, namespace );
256 repository.updateProject( TEST_REPO_ID, createProject( namespace ) );
257 ArtifactMetadata artifact = createArtifact();
258 artifact.setNamespace( namespace );
259 repository.updateArtifact( TEST_REPO_ID, namespace, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
261 assertEquals( Collections.singletonList( artifact ),
262 repository.getArtifactsByDateRange( TEST_REPO_ID, null, null ) );
265 public void testGetArtifactsByDateRangeLowerBound()
267 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
268 repository.updateProject( TEST_REPO_ID, createProject() );
269 ArtifactMetadata artifact = createArtifact();
270 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
272 Date date = new Date( artifact.getWhenGathered().getTime() - 10000 );
273 assertEquals( Collections.singletonList( artifact ),
274 repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ) );
277 public void testGetArtifactsByDateRangeLowerBoundOutOfRange()
279 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
280 repository.updateProject( TEST_REPO_ID, createProject() );
281 ArtifactMetadata artifact = createArtifact();
282 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
284 Date date = new Date( artifact.getWhenGathered().getTime() + 10000 );
285 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, date, null ).isEmpty() );
288 public void testGetArtifactsByDateRangeLowerAndUpperBound()
290 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
291 repository.updateProject( TEST_REPO_ID, createProject() );
292 ArtifactMetadata artifact = createArtifact();
293 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
295 Date lower = new Date( artifact.getWhenGathered().getTime() - 10000 );
296 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
297 assertEquals( Collections.singletonList( artifact ),
298 repository.getArtifactsByDateRange( TEST_REPO_ID, lower, upper ) );
301 public void testGetArtifactsByDateRangeUpperBound()
303 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
304 repository.updateProject( TEST_REPO_ID, createProject() );
305 ArtifactMetadata artifact = createArtifact();
306 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
308 Date upper = new Date( artifact.getWhenGathered().getTime() + 10000 );
309 assertEquals( Collections.singletonList( artifact ),
310 repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ) );
313 public void testGetArtifactsByDateRangeUpperBoundOutOfRange()
315 repository.updateNamespace( TEST_REPO_ID, TEST_NAMESPACE );
316 repository.updateProject( TEST_REPO_ID, createProject() );
317 ArtifactMetadata artifact = createArtifact();
318 repository.updateArtifact( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION, artifact );
320 Date upper = new Date( artifact.getWhenGathered().getTime() - 10000 );
321 assertTrue( repository.getArtifactsByDateRange( TEST_REPO_ID, null, upper ).isEmpty() );
324 public void testGetNamespacesWithSparseDepth()
326 repository.updateNamespace( TEST_REPO_ID, "org.apache.maven.shared" );
328 assertEquals( Arrays.asList( "org" ), repository.getRootNamespaces( TEST_REPO_ID ) );
329 assertEquals( Arrays.asList( "apache" ), repository.getNamespaces( TEST_REPO_ID, "org" ) );
330 assertEquals( Arrays.asList( "maven" ), repository.getNamespaces( TEST_REPO_ID, "org.apache" ) );
331 assertEquals( Arrays.asList( "shared" ), repository.getNamespaces( TEST_REPO_ID, "org.apache.maven" ) );
334 private ProjectMetadata createProject()
336 return createProject( TEST_NAMESPACE );
339 private ProjectMetadata createProject( String ns )
341 ProjectMetadata project = new ProjectMetadata();
342 project.setId( TEST_PROJECT );
343 project.setNamespace( ns );
347 private ArtifactMetadata createArtifact()
349 return createArtifact( "jar" );
352 private ArtifactMetadata createArtifact( String type )
354 ArtifactMetadata artifact = new ArtifactMetadata();
355 artifact.setId( TEST_PROJECT + "-" + TEST_PROJECT_VERSION + "." + type );
356 artifact.setWhenGathered( new Date() );
357 artifact.setNamespace( TEST_NAMESPACE );
358 artifact.setProject( TEST_PROJECT );
359 artifact.setRepositoryId( TEST_REPO_ID );
360 artifact.setFileLastModified( System.currentTimeMillis() );
361 artifact.setVersion( TEST_PROJECT_VERSION );
365 private static class TestMetadataFacet
366 implements MetadataFacet
368 private TestMetadataFacet( String value )
373 private String value;
375 public String getFacetId()
377 return TEST_FACET_ID;
380 public Map<String, String> toProperties()
384 return Collections.singletonMap( TEST_FACET_ID + ":foo", value );
388 return Collections.emptyMap();
392 public void fromProperties( Map<String, String> properties )
394 String value = properties.get( TEST_FACET_ID + ":foo" );
401 public String getValue()
407 public String toString()
409 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
413 public boolean equals( Object o )
419 if ( o == null || getClass() != o.getClass() )
424 TestMetadataFacet that = (TestMetadataFacet) o;
426 if ( value != null ? !value.equals( that.value ) : that.value != null )
435 public int hashCode()
437 return value != null ? value.hashCode() : 0;