View Javadoc

1   package org.apache.maven.index;
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 java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.lucene.search.Query;
26  import org.apache.maven.index.ArtifactInfo;
27  import org.apache.maven.index.IteratorSearchRequest;
28  import org.apache.maven.index.IteratorSearchResponse;
29  import org.apache.maven.index.MAVEN;
30  import org.apache.maven.index.NexusIndexer;
31  import org.apache.maven.index.SearchType;
32  import org.apache.maven.index.UniqueArtifactFilterPostprocessor;
33  import org.apache.maven.index.context.IndexingContext;
34  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
35  import org.codehaus.plexus.util.FileUtils;
36  
37  public class UniqueArtifactFilterTest
38      extends AbstractIndexCreatorHelper
39  {
40      private IndexingContext context;
41  
42      public void testSearchIterator()
43          throws Exception
44      {
45          NexusIndexer indexer = prepare();
46  
47          Query q = indexer.constructQuery( MAVEN.GROUP_ID, "qdox", SearchType.SCORED );
48  
49          IteratorSearchRequest request = new IteratorSearchRequest( q );
50  
51          IteratorSearchResponse response = indexer.searchIterator( request );
52  
53          assertEquals( 2, response.getTotalHits() );
54  
55          for ( ArtifactInfo ai : response.getResults() )
56          {
57              assertEquals( "GroupId must match \"qdox\"!", "qdox", ai.groupId );
58          }
59      }
60  
61      public void testSearchIteratorWithFilter()
62          throws Exception
63      {
64          NexusIndexer indexer = prepare();
65  
66          Query q = indexer.constructQuery( MAVEN.GROUP_ID, "commons", SearchType.SCORED );
67  
68          UniqueArtifactFilterPostprocessor filter = new UniqueArtifactFilterPostprocessor();
69          filter.addField( MAVEN.GROUP_ID );
70          filter.addField( MAVEN.ARTIFACT_ID );
71  
72          IteratorSearchRequest request = new IteratorSearchRequest( q, filter );
73  
74          IteratorSearchResponse response = indexer.searchIterator( request );
75  
76          assertEquals( "15 total hits (before filtering!)", 15, response.getTotalHits() );
77  
78          ArtifactInfo ai = response.getResults().next();
79          assertTrue( "Iterator has to have next (2 should be returned)", ai != null );
80  
81          ai = response.getResults().next();
82          assertTrue( "Iterator has to have next (2 should be returned)", ai != null );
83  
84          assertEquals( "Property that is not unique has to have \"COLLAPSED\" value!",
85              UniqueArtifactFilterPostprocessor.COLLAPSED, ai.version );
86          assertEquals( "Property that is not unique has to have \"COLLAPSED\" value!",
87              UniqueArtifactFilterPostprocessor.COLLAPSED, ai.packaging );
88          assertEquals( "Property that is not unique has to have \"COLLAPSED\" value!",
89              UniqueArtifactFilterPostprocessor.COLLAPSED, ai.classifier );
90      }
91  
92      // ==
93  
94      private NexusIndexer prepare()
95          throws Exception, IOException, UnsupportedExistingLuceneIndexException
96      {
97          NexusIndexer indexer = lookup( NexusIndexer.class );
98  
99          // Directory indexDir = new RAMDirectory();
100         File indexDir = new File( getBasedir(), "target/index/test-" + Long.toString( System.currentTimeMillis() ) );
101         FileUtils.deleteDirectory( indexDir );
102 
103         File repo = new File( getBasedir(), "src/test/repo" );
104 
105         context = indexer.addIndexingContext( "test", "test", repo, indexDir, null, null, DEFAULT_CREATORS );
106 
107         indexer.scan( context );
108 
109         return indexer;
110     }
111 }