]> source.dussan.org Git - archiva.git/blob
56d6d60ec56bb8fc208a64a39ea36336c9d88455
[archiva.git] /
1 package org.apache.maven.archiva.indexer;
2
3 /*
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
11  *
12  *  http://www.apache.org/licenses/LICENSE-2.0
13  *
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
19  * under the License.
20  */
21
22 import org.apache.commons.io.FileUtils;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.index.IndexWriter;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.indexer.lucene.LuceneIndexHandlers;
28 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
29 import org.apache.maven.archiva.model.ArchivaArtifact;
30 import org.codehaus.plexus.PlexusTestCase;
31
32 import java.io.File;
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;
39 import java.util.Map;
40
41 /**
42  * AbstractIndexerTestCase
43  *
44  * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
45  * @version $Id$
46  */
47 public abstract class AbstractIndexerTestCase
48     extends PlexusTestCase
49 {
50     protected RepositoryContentIndex index;
51
52     protected LuceneIndexHandlers indexHandlers;
53
54     private static final String TEST_DEFAULT_REPOSITORY_NAME = "Test Default Repository";
55
56     private static final String TEST_DEFAULT_REPO_ID = "testDefaultRepo";
57
58     public abstract String getIndexName();
59
60     protected void assertRecord( LuceneRepositoryContentRecord expectedRecord, Document luceneDocument )
61         throws ParseException
62     {
63         LuceneRepositoryContentRecord actualRecord = indexHandlers.getConverter().convert( luceneDocument );
64         assertRecord( expectedRecord, actualRecord );
65     }
66
67     protected void assertRecord( LuceneRepositoryContentRecord expectedRecord,
68                                  LuceneRepositoryContentRecord actualRecord )
69     {
70         assertEquals( expectedRecord, actualRecord );
71     }
72
73     public abstract RepositoryContentIndex createIndex( RepositoryContentIndexFactory indexFactory,
74                                                         ManagedRepositoryConfiguration repository );
75
76     public abstract LuceneIndexHandlers getIndexHandler();
77
78     protected void setUp()
79         throws Exception
80     {
81         super.setUp();
82
83         RepositoryContentIndexFactory indexFactory =
84             (RepositoryContentIndexFactory) lookup( RepositoryContentIndexFactory.class
85                 .getName(), "lucene" );
86
87         ManagedRepositoryConfiguration repository = createTestIndex( getIndexName() );
88
89         index = createIndex( indexFactory, repository );
90
91         indexHandlers = getIndexHandler();
92     }
93
94     private ManagedRepositoryConfiguration createTestIndex( String indexName )
95         throws Exception
96     {
97         File repoDir = new File( getBasedir(), "src/test/managed-repository" );
98         File testIndexesDir = new File( getBasedir(), "target/test-indexes" );
99
100         if ( !testIndexesDir.exists() )
101         {
102             testIndexesDir.mkdirs();
103         }
104
105         assertTrue( "Default Test Repository should exist.", repoDir.exists() && repoDir.isDirectory() );
106
107         ManagedRepositoryConfiguration repository = createRepository( TEST_DEFAULT_REPO_ID,
108                                                                       TEST_DEFAULT_REPOSITORY_NAME, repoDir );
109
110         File indexLocation = new File( testIndexesDir, "/index-" + indexName + "-" + getName() + "/" );
111
112         MockConfiguration config = (MockConfiguration) lookup( ArchivaConfiguration.class.getName(), "mock" );
113
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() );
119
120         if ( indexLocation.exists() )
121         {
122             FileUtils.deleteDirectory( indexLocation );
123         }
124
125         config.getConfiguration().addManagedRepository( repoConfig );
126         return repository;
127     }
128
129     protected Map getArchivaArtifactDumpMap()
130     {
131         Map dumps = new HashMap();
132
133         // archiva-common-1.0.jar.txt
134         dumps.put( "archiva-common",
135                    createArchivaArtifact( "org.apache.maven.archiva", "archiva-common", "1.0", "", "jar" ) );
136
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" ) );
140
141         // daytrader-ear-1.1.ear.txt
142         dumps.put( "daytrader-ear", createArchivaArtifact( "org.apache.geronimo", "daytrader-ear", "1.1", "", "ear" ) );
143
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" ) );
147
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" ) );
151
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" ) );
156
157         // testng-5.1-jdk15.jar.txt
158         dumps.put( "testng", createArchivaArtifact( "org.testng", "testng", "5.1", "jdk15", "jar" ) );
159
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" ) );
163
164         return dumps;
165     }
166
167     protected File getDumpFile( ArchivaArtifact artifact )
168     {
169         File dumpDir = new File( getBasedir(), "src/test/artifact-dumps" );
170         StringBuffer filename = new StringBuffer();
171
172         filename.append( artifact.getArtifactId() ).append( "-" ).append( artifact.getVersion() );
173
174         if ( artifact.hasClassifier() )
175         {
176             filename.append( "-" ).append( artifact.getClassifier() );
177         }
178
179         filename.append( "." );
180
181         // TODO: use the ArtifactExtensionMapping object
182         if ( "maven-plugin".equals( artifact.getType() ) || "maven-archetype".equals( artifact.getType() ) )
183         {
184             filename.append( "jar" );
185         }
186         else
187         {
188             filename.append( artifact.getType() );
189         }
190         filename.append( ".txt" );
191
192         File dumpFile = new File( dumpDir, filename.toString() );
193
194         if ( !dumpFile.exists() )
195         {
196             fail( "Dump file " + dumpFile.getAbsolutePath() + " does not exist (should it?)." );
197         }
198
199         return dumpFile;
200     }
201
202     private ArchivaArtifact createArchivaArtifact( String groupId, String artifactId, String version, String classifier,
203                                                    String type )
204     {
205         ArchivaArtifact artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type );
206         return artifact;
207     }
208
209     protected void createEmptyIndex()
210         throws IOException
211     {
212         createIndex( Collections.EMPTY_LIST );
213     }
214
215     protected void createIndex( List documents )
216         throws IOException
217     {
218         IndexWriter writer = new IndexWriter( index.getIndexDirectory(), indexHandlers.getAnalyzer(), true );
219         for ( Iterator i = documents.iterator(); i.hasNext(); )
220         {
221             Document document = (Document) i.next();
222             writer.addDocument( document );
223         }
224         writer.optimize();
225         writer.close();
226     }
227     
228     protected ManagedRepositoryConfiguration createRepository( String id, String name, File location )
229     {
230         ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
231         repo.setId( id );
232         repo.setName( name );
233         repo.setLocation( location.getAbsolutePath() );
234         return repo;
235     }
236 }