1 package org.apache.maven.archiva.indexer;
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.lucene.document.Document;
23 import org.apache.lucene.index.IndexWriter;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.indexer.lucene.LuceneIndexHandlers;
27 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
28 import org.apache.maven.archiva.model.ArchivaArtifact;
29 import org.codehaus.plexus.PlexusTestCase;
30 import org.codehaus.plexus.util.FileUtils;
33 import java.io.IOException;
34 import java.text.ParseException;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
42 * AbstractIndexerTestCase
44 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
47 public abstract class AbstractIndexerTestCase
48 extends PlexusTestCase
50 protected RepositoryContentIndex index;
52 protected LuceneIndexHandlers indexHandlers;
54 private static final String TEST_DEFAULT_REPOSITORY_NAME = "Test Default Repository";
56 private static final String TEST_DEFAULT_REPO_ID = "testDefaultRepo";
58 public abstract String getIndexName();
60 protected void assertRecord( LuceneRepositoryContentRecord expectedRecord, Document luceneDocument )
63 LuceneRepositoryContentRecord actualRecord = indexHandlers.getConverter().convert( luceneDocument );
64 assertRecord( expectedRecord, actualRecord );
67 protected void assertRecord( LuceneRepositoryContentRecord expectedRecord,
68 LuceneRepositoryContentRecord actualRecord )
70 assertEquals( expectedRecord, actualRecord );
73 public abstract RepositoryContentIndex createIndex( RepositoryContentIndexFactory indexFactory,
74 ManagedRepositoryConfiguration repository );
76 public abstract LuceneIndexHandlers getIndexHandler();
78 protected void setUp()
83 RepositoryContentIndexFactory indexFactory =
84 (RepositoryContentIndexFactory) lookup( RepositoryContentIndexFactory.class
85 .getName(), "lucene" );
87 ManagedRepositoryConfiguration repository = createTestIndex( getIndexName() );
89 index = createIndex( indexFactory, repository );
91 indexHandlers = getIndexHandler();
94 private ManagedRepositoryConfiguration createTestIndex( String indexName )
97 File repoDir = new File( getBasedir(), "src/test/managed-repository" );
98 File testIndexesDir = new File( getBasedir(), "target/test-indexes" );
100 if ( !testIndexesDir.exists() )
102 testIndexesDir.mkdirs();
105 assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
107 ManagedRepositoryConfiguration repository = createRepository( TEST_DEFAULT_REPO_ID,
108 TEST_DEFAULT_REPOSITORY_NAME, repoDir );
110 File indexLocation = new File( testIndexesDir, "/index-" + indexName + "-" + getName() + "/" );
112 MockConfiguration config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
114 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
115 repoConfig.setId( TEST_DEFAULT_REPO_ID );
116 repoConfig.setName( TEST_DEFAULT_REPOSITORY_NAME );
117 repoConfig.setLocation( repoDir.getAbsolutePath() );
118 repoConfig.setIndexDir( indexLocation.getAbsolutePath() );
120 if ( indexLocation.exists() )
122 FileUtils.deleteDirectory( indexLocation );
125 config.getConfiguration().addManagedRepository( repoConfig );
129 protected Map getArchivaArtifactDumpMap()
131 Map dumps = new HashMap();
133 // archiva-common-1.0.jar.txt
134 dumps.put( "archiva-common",
135 createArchivaArtifact( "org.apache.maven.archiva", "archiva-common", "1.0", "", "jar" ) );
137 // continuum-webapp-1.0.3-SNAPSHOT.war.txt
138 dumps.put( "continuum-webapp", createArchivaArtifact( "org.apache.maven.continuum", "continuum-webapp",
139 "1.0.3-SNAPSHOT", "", "war" ) );
141 // daytrader-ear-1.1.ear.txt
142 dumps.put( "daytrader-ear", createArchivaArtifact( "org.apache.geronimo", "daytrader-ear", "1.1", "", "ear" ) );
144 // maven-archetype-simple-1.0-alpha-4.jar.txt
145 dumps.put( "maven-archetype-simple", createArchivaArtifact( "org.apache.maven", "maven-archetype-simple",
146 "1.0-alpha-4", "", "maven-archetype" ) );
148 // maven-help-plugin-2.0.2-20070119.121239-2.jar.txt
149 dumps.put( "maven-help-plugin", createArchivaArtifact( "org.apache.maven.plugins", "maven-help-plugin",
150 "2.0.2-20070119.121239-2", "", "maven-plugin" ) );
152 // redback-authorization-open-1.0-alpha-1-SNAPSHOT.jar.txt
153 dumps.put( "redback-authorization-open", createArchivaArtifact( "org.codehaus.plexus.redback",
154 "redback-authorization-open",
155 "1.0-alpha-1-SNAPSHOT", "", "jar" ) );
157 // testng-5.1-jdk15.jar.txt
158 dumps.put( "testng", createArchivaArtifact( "org.testng", "testng", "5.1", "jdk15", "jar" ) );
160 // wagon-provider-api-1.0-beta-3-20070209.213958-2.jar.txt
161 dumps.put( "wagon-provider-api", createArchivaArtifact( "org.apache.maven.wagon", "wagon-provider-api",
162 "1.0-beta-3-20070209.213958-2", "", "jar" ) );
167 protected File getDumpFile( ArchivaArtifact artifact )
169 File dumpDir = new File( getBasedir(), "src/test/artifact-dumps" );
170 StringBuffer filename = new StringBuffer();
172 filename.append( artifact.getArtifactId() ).append( "-" ).append( artifact.getVersion() );
174 if ( artifact.hasClassifier() )
176 filename.append( "-" ).append( artifact.getClassifier() );
179 filename.append( "." );
181 // TODO: use the ArtifactExtensionMapping object
182 if ( "maven-plugin".equals( artifact.getType() ) || "maven-archetype".equals( artifact.getType() ) )
184 filename.append( "jar" );
188 filename.append( artifact.getType() );
190 filename.append( ".txt" );
192 File dumpFile = new File( dumpDir, filename.toString() );
194 if ( !dumpFile.exists() )
196 fail( "Dump file " + dumpFile.getAbsolutePath() + " does not exist (should it?)." );
202 private ArchivaArtifact createArchivaArtifact( String groupId, String artifactId, String version, String classifier,
205 ArchivaArtifact artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type );
209 protected void createEmptyIndex()
212 createIndex( Collections.EMPTY_LIST );
215 protected void createIndex( List documents )
218 IndexWriter writer = new IndexWriter( index.getIndexDirectory(), indexHandlers.getAnalyzer(), true );
219 for ( Iterator i = documents.iterator(); i.hasNext(); )
221 Document document = (Document) i.next();
222 writer.addDocument( document );
228 protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
230 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
232 repo.setName( name );
233 repo.setLocation( location.getAbsolutePath() );