1 package org.apache.maven.archiva.discoverer;
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;
31 import java.io.IOException;
32 import java.util.Iterator;
33 import java.util.List;
36 * This class tests the DefaultMetadataDiscoverer class.
38 public class DefaultMetadataDiscovererTest
39 extends PlexusTestCase
41 private MetadataDiscoverer discoverer;
43 private static final String TEST_OPERATION = "test";
45 private ArtifactRepository repository;
47 private ArtifactFactory factory;
57 discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" );
59 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
61 repository = getRepository();
63 removeTimestampMetadata();
66 protected ArtifactRepository getRepository()
69 File basedir = getTestFile( "src/test/repository" );
71 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
73 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
75 return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
81 public void tearDown()
89 * Test if metadata file in wrong directory was added to the kickedOutPaths.
91 public void testKickoutWrongDirectory()
92 throws DiscovererException
94 discoverer.discoverMetadata( repository, TEST_OPERATION, null );
95 Iterator iter = discoverer.getKickedOutPathsIterator();
96 boolean found = false;
97 while ( iter.hasNext() && !found )
99 DiscovererPath dPath = (DiscovererPath) iter.next();
100 String dir = dPath.getPath();
102 String normalizedDir = dir.replace( '\\', '/' );
103 if ( "javax/maven-metadata.xml".equals( normalizedDir ) )
106 assertEquals( "Check reason for kickout", "Unable to build a repository metadata from path",
107 dPath.getComment() );
114 * Test if blank metadata file was added to the kickedOutPaths.
116 public void testKickoutBlankMetadata()
117 throws DiscovererException
119 discoverer.discoverMetadata( repository, TEST_OPERATION, null );
120 Iterator iter = discoverer.getKickedOutPathsIterator();
121 boolean found = false;
122 while ( iter.hasNext() && !found )
124 DiscovererPath dPath = (DiscovererPath) iter.next();
125 String dir = dPath.getPath();
127 String normalizedDir = dir.replace( '\\', '/' );
128 if ( "org/apache/maven/some-ejb/1.0/maven-metadata.xml".equals( normalizedDir ) )
131 assertTrue( "Check reason for kickout", dPath.getComment().matches(
132 "Error reading metadata file '(.*)': input contained no data" ) );
138 private void removeTimestampMetadata()
141 // remove the metadata that tracks time
142 File file = new File( repository.getBasedir(), "maven-metadata.xml" );
143 System.gc(); // for Windows
145 assertFalse( file.exists() );
148 public void testDiscoverMetadata()
149 throws DiscovererException
151 List metadataPaths = discoverer.discoverMetadata( repository, TEST_OPERATION, null );
152 assertNotNull( "Check metadata not null", metadataPaths );
154 RepositoryMetadata metadata =
155 new ArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery" ) );
156 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
159 new SnapshotArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery", "1.0" ) );
160 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
162 metadata = new GroupRepositoryMetadata( "org.apache.maven" );
163 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
166 protected Artifact createArtifact( String groupId, String artifactId )
168 return createArtifact( groupId, artifactId, "1.0" );
171 private Artifact createArtifact( String groupId, String artifactId, String version )
173 return factory.createArtifact( groupId, artifactId, version, null, "jar" );
176 private boolean containsMetadata( List metadataPaths, RepositoryMetadata metadata )
178 for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
180 RepositoryMetadata m = (RepositoryMetadata) i.next();
182 if ( m.getGroupId().equals( metadata.getGroupId() ) )
184 if ( m.getArtifactId() == null && metadata.getArtifactId() == null )
188 else if ( m.getArtifactId() != null && m.getArtifactId().equals( metadata.getArtifactId() ) )