View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.index;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.lucene.search.Query;
25  import org.apache.maven.index.context.IndexingContext;
26  import org.apache.maven.index.context.UnsupportedExistingLuceneIndexException;
27  import org.codehaus.plexus.util.FileUtils;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  
33  public class UniqueArtifactFilterTest extends AbstractIndexCreatorHelper {
34      private IndexingContext context;
35  
36      @Test
37      public void testSearchIterator() throws Exception {
38          NexusIndexer indexer = prepare();
39  
40          Query q = indexer.constructQuery(MAVEN.GROUP_ID, "qdox", SearchType.SCORED);
41  
42          IteratorSearchRequest request = new IteratorSearchRequest(q);
43  
44          IteratorSearchResponse response = indexer.searchIterator(request);
45  
46          assertEquals(2, response.getTotalHits());
47  
48          for (ArtifactInfo ai : response.getResults()) {
49              assertEquals("GroupId must match \"qdox\"!", "qdox", ai.getGroupId());
50          }
51      }
52  
53      @Test
54      public void testSearchIteratorWithFilter() throws Exception {
55          NexusIndexer indexer = prepare();
56  
57          Query q = indexer.constructQuery(MAVEN.GROUP_ID, "commons", SearchType.SCORED);
58  
59          UniqueArtifactFilterPostprocessor filter = new UniqueArtifactFilterPostprocessor();
60          filter.addField(MAVEN.GROUP_ID);
61          filter.addField(MAVEN.ARTIFACT_ID);
62  
63          IteratorSearchRequest request = new IteratorSearchRequest(q, filter);
64  
65          IteratorSearchResponse response = indexer.searchIterator(request);
66  
67          assertEquals("15 total hits (before filtering!)", 15, response.getTotalHits());
68  
69          ArtifactInfo ai = response.getResults().next();
70          assertTrue("Iterator has to have next (2 should be returned)", ai != null);
71  
72          ai = response.getResults().next();
73          assertTrue("Iterator has to have next (2 should be returned)", ai != null);
74  
75          assertEquals(
76                  "Property that is not unique has to have \"COLLAPSED\" value!",
77                  UniqueArtifactFilterPostprocessor.COLLAPSED,
78                  ai.getVersion());
79          assertEquals(
80                  "Property that is not unique has to have \"COLLAPSED\" value!",
81                  UniqueArtifactFilterPostprocessor.COLLAPSED,
82                  ai.getPackaging());
83          assertEquals(
84                  "Property that is not unique has to have \"COLLAPSED\" value!",
85                  UniqueArtifactFilterPostprocessor.COLLAPSED,
86                  ai.getClassifier());
87      }
88  
89      // ==
90  
91      private NexusIndexer prepare() throws Exception, IOException, UnsupportedExistingLuceneIndexException {
92          NexusIndexer indexer = lookup(NexusIndexer.class);
93  
94          // Directory indexDir = new RAMDirectory();
95          File indexDir = new File(getBasedir(), "target/index/test-" + System.currentTimeMillis());
96          FileUtils.deleteDirectory(indexDir);
97  
98          File repo = new File(getBasedir(), "src/test/repo");
99  
100         context = indexer.addIndexingContext("test", "test", repo, indexDir, null, null, DEFAULT_CREATORS);
101 
102         indexer.scan(context);
103 
104         return indexer;
105     }
106 }