1 package org.apache.archiva.consumers.lucene;
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
23 import java.util.Calendar;
24 import java.util.Date;
27 import org.apache.commons.io.FileUtils;
28 import org.apache.lucene.search.BooleanQuery;
29 import org.apache.lucene.search.IndexSearcher;
30 import org.apache.lucene.search.TopDocs;
31 import org.apache.lucene.search.BooleanClause.Occur;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
34 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
35 import org.sonatype.nexus.index.ArtifactInfo;
36 import org.sonatype.nexus.index.FlatSearchRequest;
37 import org.sonatype.nexus.index.FlatSearchResponse;
38 import org.sonatype.nexus.index.NexusIndexer;
39 import org.sonatype.nexus.index.context.IndexingContext;
40 import org.sonatype.nexus.index.IndexerEngine;
41 import org.sonatype.nexus.index.packer.IndexPacker;
43 public class NexusIndexerConsumerTest
44 extends PlexusInSpringTestCase
46 private KnownRepositoryContentConsumer nexusIndexerConsumer;
48 private ManagedRepositoryConfiguration repositoryConfig;
50 private NexusIndexer nexusIndexer;
52 private IndexPacker indexPacker;
54 private IndexerEngine indexerEngine;
57 protected void setUp()
62 nexusIndexer = ( NexusIndexer ) lookup( NexusIndexer.class );
64 indexPacker = ( IndexPacker ) lookup( IndexPacker.class );
66 indexerEngine = ( IndexerEngine ) lookup( IndexerEngine.class );
68 //nexusIndexerConsumer = new NexusIndexerConsumer( nexusIndexer, indexPacker, indexerEngine );
70 nexusIndexerConsumer = new NexusIndexerConsumer( indexPacker, indexerEngine );
72 repositoryConfig = new ManagedRepositoryConfiguration();
73 repositoryConfig.setId( "test-repo" );
74 repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
75 repositoryConfig.setLayout( "default" );
76 repositoryConfig.setName( "Test Repository" );
77 repositoryConfig.setScanned( true );
78 repositoryConfig.setSnapshots( false );
79 repositoryConfig.setReleases( true );
83 protected void tearDown()
86 // delete created index in the repository
87 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
88 FileUtils.deleteDirectory( indexDir );
89 assertFalse( indexDir.exists() );
91 indexDir = new File( repositoryConfig.getLocation(), ".index" );
92 FileUtils.deleteDirectory( indexDir );
93 assertFalse( indexDir.exists() );
98 public void testIndexerIndexArtifact()
102 Date now = Calendar.getInstance().getTime();
103 nexusIndexerConsumer.beginScan( repositoryConfig, now );
104 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
105 nexusIndexerConsumer.completeScan();
108 BooleanQuery q = new BooleanQuery();
109 q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
110 q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
112 IndexingContext context = nexusIndexer.addIndexingContext( repositoryConfig.getId(), repositoryConfig.getId(), new File( repositoryConfig.getLocation() ),
113 new File( repositoryConfig.getLocation(), ".indexer" ), null, null, NexusIndexer.FULL_INDEX );
114 context.setSearchable( true );
116 FlatSearchRequest request = new FlatSearchRequest( q );
117 FlatSearchResponse response = nexusIndexer.searchFlat( request );
119 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
120 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
121 assertEquals( 1, response.getTotalHits() );
123 Set<ArtifactInfo> results = response.getResults();
125 ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
126 assertEquals( "org.apache.archiva", artifactInfo.groupId );
127 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
128 assertEquals( "test-repo", artifactInfo.repository );
131 public void testIndexerArtifactAlreadyIndexed()
135 Date now = Calendar.getInstance().getTime();
136 nexusIndexerConsumer.beginScan( repositoryConfig, now );
137 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
138 nexusIndexerConsumer.completeScan();
140 // scan and index again
141 now = Calendar.getInstance().getTime();
142 nexusIndexerConsumer.beginScan( repositoryConfig, now );
143 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
144 nexusIndexerConsumer.completeScan();
147 BooleanQuery q = new BooleanQuery();
148 q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
149 q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
151 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
152 TopDocs topDocs = searcher.search( q, null, 10 );
154 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
155 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
157 // should return 2 hits - this will be filtered out by the NexusRespositorySearch when it returns the results!
158 assertEquals( 2, topDocs.totalHits );
161 public void testIndexerIndexArtifactThenPom()
165 Date now = Calendar.getInstance().getTime();
166 nexusIndexerConsumer.beginScan( repositoryConfig, now );
167 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
168 nexusIndexerConsumer.completeScan();
170 // scan and index again
171 now = Calendar.getInstance().getTime();
172 nexusIndexerConsumer.beginScan( repositoryConfig, now );
173 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/pom.xml" );
174 nexusIndexerConsumer.completeScan();
177 BooleanQuery q = new BooleanQuery();
178 q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
179 q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
181 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
182 TopDocs topDocs = searcher.search( q, null, 10 );
184 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
185 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
187 // should return only 1 hit
188 assertEquals( 1, topDocs.totalHits );