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.creator.IndexerEngine;
40 import org.sonatype.nexus.index.packer.IndexPacker;
42 public class NexusIndexerConsumerTest
43 extends PlexusInSpringTestCase
45 private KnownRepositoryContentConsumer nexusIndexerConsumer;
47 private ManagedRepositoryConfiguration repositoryConfig;
49 private NexusIndexer nexusIndexer;
51 private IndexPacker indexPacker;
53 private IndexerEngine indexerEngine;
56 protected void setUp()
61 nexusIndexer = ( NexusIndexer ) lookup( NexusIndexer.class );
63 indexPacker = ( IndexPacker ) lookup( IndexPacker.class );
65 indexerEngine = ( IndexerEngine ) lookup( IndexerEngine.class );
67 nexusIndexerConsumer = new NexusIndexerConsumer( nexusIndexer, indexPacker, indexerEngine );
69 repositoryConfig = new ManagedRepositoryConfiguration();
70 repositoryConfig.setId( "test-repo" );
71 repositoryConfig.setLocation( getBasedir() + "/target/test-classes/test-repo" );
72 repositoryConfig.setLayout( "default" );
73 repositoryConfig.setName( "Test Repository" );
74 repositoryConfig.setScanned( true );
75 repositoryConfig.setSnapshots( false );
76 repositoryConfig.setReleases( true );
80 protected void tearDown()
83 // delete created index in the repository
84 File indexDir = new File( repositoryConfig.getLocation(), ".indexer" );
85 FileUtils.deleteDirectory( indexDir );
86 assertFalse( indexDir.exists() );
88 indexDir = new File( repositoryConfig.getLocation(), ".index" );
89 FileUtils.deleteDirectory( indexDir );
90 assertFalse( indexDir.exists() );
95 public void testIndexerIndexArtifact()
99 Date now = Calendar.getInstance().getTime();
100 nexusIndexerConsumer.beginScan( repositoryConfig, now );
101 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
102 nexusIndexerConsumer.completeScan();
105 BooleanQuery q = new BooleanQuery();
106 q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
107 q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
109 FlatSearchRequest request = new FlatSearchRequest( q );
110 FlatSearchResponse response = nexusIndexer.searchFlat( request );
112 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
113 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
114 assertEquals( 1, response.getTotalHits() );
116 Set<ArtifactInfo> results = response.getResults();
118 ArtifactInfo artifactInfo = (ArtifactInfo) results.iterator().next();
119 assertEquals( "org.apache.archiva", artifactInfo.groupId );
120 assertEquals( "archiva-index-methods-jar-test", artifactInfo.artifactId );
121 assertEquals( "test-repo", artifactInfo.repository );
124 public void testIndexerArtifactAlreadyIndexed()
128 Date now = Calendar.getInstance().getTime();
129 nexusIndexerConsumer.beginScan( repositoryConfig, now );
130 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
131 nexusIndexerConsumer.completeScan();
133 // scan and index again
134 now = Calendar.getInstance().getTime();
135 nexusIndexerConsumer.beginScan( repositoryConfig, now );
136 nexusIndexerConsumer.processFile( "org/apache/archiva/archiva-index-methods-jar-test/1.0/archiva-index-methods-jar-test-1.0.jar" );
137 nexusIndexerConsumer.completeScan();
140 BooleanQuery q = new BooleanQuery();
141 q.add( nexusIndexer.constructQuery( ArtifactInfo.GROUP_ID, "org.apache.archiva" ), Occur.SHOULD );
142 q.add( nexusIndexer.constructQuery( ArtifactInfo.ARTIFACT_ID, "archiva-index-methods-jar-test" ), Occur.SHOULD );
144 IndexSearcher searcher = new IndexSearcher( repositoryConfig.getLocation() + "/.indexer" );
145 TopDocs topDocs = searcher.search( q, null, 10 );
147 assertTrue( new File( repositoryConfig.getLocation(), ".indexer" ).exists() );
148 assertTrue( new File( repositoryConfig.getLocation(), ".index" ).exists() );
150 // should return only 1 hit - artifact should have just been updated and not added as a separate doc
151 assertEquals( 1, topDocs.totalHits );
155 public void testIndexerIndexArtifactThenPom()
159 Date now = Calendar.getInstance().getTime();
160 nexusIndexerConsumer.beginScan( repositoryConfig, now );
163 //nexusIndexerConsumer.processFile( )