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.Collection;
24 import java.util.Collections;
25 import java.util.List;
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.ProjectVersionMetadata;
32 import org.apache.commons.io.FileUtils;
33 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 public class FileMetadataRepositoryTest
36 extends PlexusInSpringTestCase
38 private FileMetadataRepository repository;
40 private static final String TEST_REPO_ID = "test";
42 private static final String TEST_PROJECT = "projectId";
44 private static final String TEST_NAMESPACE = "namespace";
46 private static final String TEST_PROJECT_VERSION = "1.0";
48 private static final String TEST_FACET_ID = "test-facet-id";
50 private static final String TEST_NAME = "test-name";
52 private static final String TEST_VALUE = "test-value";
54 private static final String UNKNOWN = "unknown";
61 repository = new FileMetadataRepository();
62 File directory = getTestFile( "target/test-repository" );
63 FileUtils.deleteDirectory( directory );
64 repository.setDirectory( directory );
66 repository.setMetadataFacetFactories(
67 Collections.<String, MetadataFacetFactory>singletonMap( TEST_FACET_ID, new MetadataFacetFactory()
69 public MetadataFacet createMetadataFacet()
71 return new TestMetadataFacet( "test-metadata" );
76 public void testRootNamespaceWithNoMetadataRepository()
78 Collection<String> namespaces = repository.getRootNamespaces( TEST_REPO_ID );
79 assertEquals( Collections.<String>emptyList(), namespaces );
82 public void testUpdateProjectVersionMetadataWithNoOtherArchives()
84 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
85 metadata.setId( TEST_PROJECT_VERSION );
86 MailingList mailingList = new MailingList();
87 mailingList.setName( "Foo List" );
88 mailingList.setOtherArchives( Collections.<String>emptyList() );
89 metadata.setMailingLists( Collections.singletonList( mailingList ) );
90 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
93 public void testUpdateProjectVersionMetadataWithExistingFacets()
95 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
96 metadata.setId( TEST_PROJECT_VERSION );
97 MetadataFacet facet = new TestMetadataFacet( "baz" );
98 metadata.addFacet( facet );
99 repository.updateProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, metadata );
101 metadata = repository.getProjectVersion( TEST_REPO_ID, TEST_NAMESPACE, TEST_PROJECT, TEST_PROJECT_VERSION );
102 assertEquals( Collections.singleton( TEST_FACET_ID ), metadata.getFacetIds() );
104 metadata = new ProjectVersionMetadata();
105 metadata.setId( TEST_PROJECT_VERSION );
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() );
110 TestMetadataFacet testFacet = (TestMetadataFacet) metadata.getFacet( TEST_FACET_ID );
111 assertEquals( "baz", testFacet.getValue() );
114 public void testGetMetadataFacet()
116 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
118 assertEquals( new TestMetadataFacet( TEST_VALUE ),
119 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
122 public void testGetMetadataFacetWhenEmpty()
124 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
127 public void testGetMetadataFacetWhenUnknownName()
129 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
131 assertNull( repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, UNKNOWN ) );
134 public void testGetMetadataFacetWhenDefaultValue()
136 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( null ) );
138 assertEquals( new TestMetadataFacet( "test-metadata" ),
139 repository.getMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME ) );
142 public void testGetMetadataFacetWhenUnknownFacetId()
144 repository.addMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
146 assertNull( repository.getMetadataFacet( TEST_REPO_ID, UNKNOWN, TEST_NAME ) );
149 public void testGetMetadataFacets()
151 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
153 assertEquals( Collections.singletonList( TEST_NAME ),
154 repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID ) );
157 public void testGetMetadataFacetsWhenEmpty()
159 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
160 assertTrue( facets.isEmpty() );
163 public void testRemoveFacets()
165 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
166 assertTrue( facets.isEmpty() );
168 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
170 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
171 assertTrue( facets.isEmpty() );
174 public void testRemoveFacetsWhenEmpty()
176 repository.addMetadataFacet( TEST_REPO_ID, TEST_FACET_ID, TEST_NAME, new TestMetadataFacet( TEST_VALUE ) );
178 List<String> facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
179 assertFalse( facets.isEmpty() );
181 repository.removeMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
183 facets = repository.getMetadataFacets( TEST_REPO_ID, TEST_FACET_ID );
184 assertTrue( facets.isEmpty() );
187 public void testRemoveFacetsWhenUnknown()
189 repository.removeMetadataFacets( TEST_REPO_ID, UNKNOWN );
192 private static class TestMetadataFacet
193 implements MetadataFacet
195 private TestMetadataFacet( String value )
200 private String value;
202 public String getFacetId()
204 return TEST_FACET_ID;
207 public Map<String, String> toProperties()
211 return Collections.singletonMap( TEST_FACET_ID + ":foo", value );
215 return Collections.emptyMap();
219 public void fromProperties( Map<String, String> properties )
221 String value = properties.get( TEST_FACET_ID + ":foo" );
228 public String getValue()
234 public String toString()
236 return "TestMetadataFacet{" + "value='" + value + '\'' + '}';
240 public boolean equals( Object o )
246 if ( o == null || getClass() != o.getClass() )
251 TestMetadataFacet that = (TestMetadataFacet) o;
253 if ( value != null ? !value.equals( that.value ) : that.value != null )
262 public int hashCode()
264 return value != null ? value.hashCode() : 0;