1 package org.apache.maven.archiva.discoverer;
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 org.apache.maven.artifact.Artifact;
23 import org.apache.maven.artifact.factory.ArtifactFactory;
24 import org.apache.maven.artifact.repository.ArtifactRepository;
25 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
26 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
27 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
29 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
30 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
31 import org.codehaus.plexus.PlexusTestCase;
34 import java.io.IOException;
35 import java.util.Iterator;
36 import java.util.List;
39 * This class tests the DefaultMetadataDiscoverer class.
41 public class DefaultMetadataDiscovererTest
42 extends PlexusTestCase
44 private MetadataDiscoverer discoverer;
46 private static final String TEST_OPERATION = "test";
48 private ArtifactRepository repository;
50 private ArtifactFactory factory;
60 discoverer = (MetadataDiscoverer) lookup( MetadataDiscoverer.ROLE, "default" );
62 factory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
64 repository = getRepository();
66 removeTimestampMetadata();
69 protected ArtifactRepository getRepository()
72 File basedir = getTestFile( "src/test/repository" );
74 ArtifactRepositoryFactory factory = (ArtifactRepositoryFactory) lookup( ArtifactRepositoryFactory.ROLE );
76 ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) lookup( ArtifactRepositoryLayout.ROLE, "default" );
78 return factory.createArtifactRepository( "discoveryRepo", "file://" + basedir, layout, null, null );
84 public void tearDown()
92 * Test if metadata file in wrong directory was added to the kickedOutPaths.
94 public void testKickoutWrongDirectory()
95 throws DiscovererException
97 discoverer.discoverMetadata( repository, null );
98 Iterator iter = discoverer.getKickedOutPathsIterator();
99 boolean found = false;
100 while ( iter.hasNext() && !found )
102 DiscovererPath dPath = (DiscovererPath) iter.next();
103 String dir = dPath.getPath();
105 String normalizedDir = dir.replace( '\\', '/' );
106 if ( "javax/maven-metadata.xml".equals( normalizedDir ) )
109 assertEquals( "Check reason for kickout", "Unable to build a repository metadata from path",
110 dPath.getComment() );
117 * Test if blank metadata file was added to the kickedOutPaths.
119 public void testKickoutBlankMetadata()
120 throws DiscovererException
122 discoverer.discoverMetadata( repository, null );
123 Iterator iter = discoverer.getKickedOutPathsIterator();
124 boolean found = false;
125 while ( iter.hasNext() && !found )
127 DiscovererPath dPath = (DiscovererPath) iter.next();
128 String dir = dPath.getPath();
130 String normalizedDir = dir.replace( '\\', '/' );
131 if ( "org/apache/maven/some-ejb/1.0/maven-metadata.xml".equals( normalizedDir ) )
134 assertTrue( "Check reason for kickout", dPath.getComment().matches(
135 "Error reading metadata file '(.*)': input contained no data" ) );
141 private void removeTimestampMetadata()
144 // remove the metadata that tracks time
145 File file = new File( repository.getBasedir(), "maven-metadata.xml" );
146 System.gc(); // for Windows
148 assertFalse( file.exists() );
151 public void testDiscoverMetadata()
152 throws DiscovererException
154 List metadataPaths = discoverer.discoverMetadata( repository, null );
155 assertNotNull( "Check metadata not null", metadataPaths );
157 RepositoryMetadata metadata =
158 new ArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery" ) );
159 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
162 new SnapshotArtifactRepositoryMetadata( createArtifact( "org.apache.testgroup", "discovery", "1.0" ) );
163 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
165 metadata = new GroupRepositoryMetadata( "org.apache.maven" );
166 assertTrue( "Check included", containsMetadata( metadataPaths, metadata ) );
169 protected Artifact createArtifact( String groupId, String artifactId )
171 return createArtifact( groupId, artifactId, "1.0" );
174 private Artifact createArtifact( String groupId, String artifactId, String version )
176 return factory.createArtifact( groupId, artifactId, version, null, "jar" );
179 private boolean containsMetadata( List metadataPaths, RepositoryMetadata metadata )
181 for ( Iterator i = metadataPaths.iterator(); i.hasNext(); )
183 RepositoryMetadata m = (RepositoryMetadata) i.next();
185 if ( m.getGroupId().equals( metadata.getGroupId() ) )
187 if ( m.getArtifactId() == null && metadata.getArtifactId() == null )
191 else if ( m.getArtifactId() != null && m.getArtifactId().equals( metadata.getArtifactId() ) )