1 package org.apache.maven.archiva.discovery;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.factory.ArtifactFactory;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
23 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
24 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
28 import org.codehaus.plexus.PlexusTestCase;
29 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
32 import java.io.IOException;
33 import java.text.ParseException;
34 import java.text.SimpleDateFormat;
35 import java.util.Date;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Locale;
41 * This class tests the DefaultMetadataDiscoverer class.
43 public class DefaultMetadataDiscovererTest
44 extends PlexusTestCase
46 private MetadataDiscoverer discoverer;
48 private static final String TEST_OPERATION = "test";
50 private ArtifactRepository repository;
52 private ArtifactFactory factory;
62 discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" );
64 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
66 repository = getRepository();
68 removeTimestampMetadata();
71 protected ArtifactRepository getRepository()
74 File basedir = getTestFile( "src/test/repository" );
76 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
78 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
80 return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
86 public void tearDown()
94 * Test if metadata file in wrong directory was added to the kickedOutPaths.
96 public void testKickoutWrongDirectory()
97 throws DiscovererException
99 discoverer.discoverMetadata( repository, TEST_OPERATION, null );
100 Iterator iter = discoverer.getKickedOutPathsIterator();
101 boolean found = false;
102 while ( iter.hasNext() && !found )
104 DiscovererPath dPath = (DiscovererPath) iter.next();
105 String dir = dPath.getPath();
107 String normalizedDir = dir.replace( '\\', '/' );
108 if ( "javax/maven-metadata.xml".equals( normalizedDir ) )
111 assertEquals( "Check reason for kickout", "Unable to build a repository metadata from path",
112 dPath.getComment() );
119 * Test if blank metadata file was added to the kickedOutPaths.
121 public void testKickoutBlankMetadata()
122 throws DiscovererException
124 discoverer.discoverMetadata( repository, TEST_OPERATION, null );
125 Iterator iter = discoverer.getKickedOutPathsIterator();
126 boolean found = false;
127 while ( iter.hasNext() && !found )
129 DiscovererPath dPath = (DiscovererPath) iter.next();
130 String dir = dPath.getPath();
132 String normalizedDir = dir.replace( '\\', '/' );
133 if ( "org/apache/maven/some-ejb/1.0/maven-metadata.xml".equals( normalizedDir ) )
136 assertTrue( "Check reason for kickout", dPath.getComment().matches(
137 "Error reading metadata file '(.*)': input contained no data" ) );
143 private void removeTimestampMetadata()
146 // remove the metadata that tracks time
147 File file = new File( repository.getBasedir(), "maven-metadata.xml" );
148 System.gc(); // for Windows
150 assertFalse( file.exists() );
153 public void testDiscoverMetadata()
154 throws DiscovererException
156 List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
157 assertNotNull( "Check metadata not null", metadataPaths );
159 RepositoryMetadata metadata =
160 new ArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery" ) );
161 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
164 new SnapshotArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery", "1.0" ) );
165 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
167 metadata = new GroupRepositoryMetadata( "org.apache.maven" );
168 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
171 public void testUpdatedInRepository()
172 throws ComponentLookupException, DiscovererException, ParseException, IOException
174 // Set repository time to 1-1-2000, a time in the distant past so definitely updated
175 discoverer.setLastCheckedTime( repository, "update",
176 new SimpleDateFormat( "yyyy-MM-dd", Locale.US ).parse( "2000-01-01" ) );
178 List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
179 assertNotNull( "Check metadata not null", metadataPaths );
181 RepositoryMetadata metadata =
182 new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-updated" ) );
183 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
185 // try again with the updated timestamp
186 metadataPaths = discoverer.discoverMetadata( repository, "update", null );
187 assertNotNull( "Check metadata not null", metadataPaths );
189 assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
192 private boolean containsMetadata( List metadataPaths, RepositoryMetadata metadata )
194 for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
196 RepositoryMetadata m = (RepositoryMetadata) i.next();
198 if ( m.getGroupId().equals( metadata.getGroupId() ) )
200 if ( m.getArtifactId() == null && metadata.getArtifactId() == null )
204 else if ( m.getArtifactId() != null && m.getArtifactId().equals( metadata.getArtifactId() ) )
213 public void testNotUpdatedInRepository()
214 throws ComponentLookupException, DiscovererException, IOException
216 // Set repository time to now, which is after any artifacts, so definitely not updated
217 discoverer.setLastCheckedTime( repository, "update", new Date() );
219 List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
220 assertNotNull( "Check metadata not null", metadataPaths );
222 RepositoryMetadata metadata =
223 new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
224 assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
227 public void testNotUpdatedInRepositoryForcedDiscovery()
228 throws ComponentLookupException, DiscovererException, IOException
230 discoverer.resetLastCheckedTime( repository, "update" );
232 List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
233 assertNotNull( "Check metadata not null", metadataPaths );
235 RepositoryMetadata metadata =
236 new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
237 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
239 // try again with the updated timestamp
240 metadataPaths = discoverer.discoverMetadata( repository, "update", null );
241 assertNotNull( "Check metadata not null", metadataPaths );
243 assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
246 public void testNotUpdatedInRepositoryForcedDiscoveryMetadataAlreadyExists()
247 throws ComponentLookupException, DiscovererException, IOException
249 discoverer.setLastCheckedTime( repository, "update", new Date() );
251 discoverer.resetLastCheckedTime( repository, "update" );
253 List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
254 assertNotNull( "Check metadata not null", metadataPaths );
256 RepositoryMetadata metadata =
257 new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
258 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
260 // try again with the updated timestamp
261 metadataPaths = discoverer.discoverMetadata( repository, "update", null );
262 assertNotNull( "Check metadata not null", metadataPaths );
264 assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
267 public void testNotUpdatedInRepositoryForcedDiscoveryOtherMetadataAlreadyExists()
268 throws ComponentLookupException, DiscovererException, IOException
270 discoverer.setLastCheckedTime( repository, "test", new Date() );
272 discoverer.resetLastCheckedTime( repository, "update" );
274 List metadataPaths = discoverer.discoverMetadata( repository, "update", null );
275 assertNotNull( "Check metadata not null", metadataPaths );
277 RepositoryMetadata metadata =
278 new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-not-updated" ) );
279 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
281 // try again with the updated timestamp
282 metadataPaths = discoverer.discoverMetadata( repository, "update", null );
283 assertNotNull( "Check metadata not null", metadataPaths );
285 assertFalse( "Check not included", containsMetadata( metadataPaths, metadata ) );
288 public void testNoRepositoryMetadata()
289 throws ComponentLookupException, DiscovererException, ParseException, IOException
291 removeTimestampMetadata();
294 List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
295 assertNotNull( "Check metadata not null", metadataPaths );
297 RepositoryMetadata metadata =
298 new ArtifactRepositoryMetadata( createArtifact( "org.apache.maven.update", "test-updated" ) );
299 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
302 protected Artifact createArtifact( String groupId, String artifactId )
304 return createArtifact( groupId, artifactId, "1.0" );
307 private Artifact createArtifact( String groupId, String artifactId, String version )
309 return factory.createArtifact( groupId, artifactId, version, null, "jar" );