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  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.lucene.search.IndexSearcher;
27  import org.apache.lucene.search.Query;
28  import org.apache.lucene.store.Directory;
29  import org.apache.maven.index.context.DefaultIndexingContext;
30  import org.apache.maven.index.context.ExistingLuceneIndexMismatchException;
31  import org.apache.maven.index.context.IndexCreator;
32  import org.apache.maven.index.util.IndexCreatorSorter;
33  import org.junit.Test;
34  
35  import static org.junit.Assert.assertEquals;
36  
37  public class DefaultSearchEngineTest extends AbstractNexusIndexerTest {
38  
39      private static class CountingIndexingContext extends DefaultIndexingContext {
40          public int count;
41  
42          public CountingIndexingContext(
43                  String id,
44                  String repositoryId,
45                  File repository,
46                  Directory indexDirectory,
47                  String repositoryUrl,
48                  String indexUpdateUrl,
49                  List<? extends IndexCreator> indexCreators,
50                  boolean reclaimIndex)
51                  throws IOException, ExistingLuceneIndexMismatchException {
52              super(
53                      id,
54                      repositoryId,
55                      repository,
56                      indexDirectory,
57                      repositoryUrl,
58                      indexUpdateUrl,
59                      indexCreators,
60                      reclaimIndex);
61          }
62  
63          public IndexSearcher acquireIndexSearcher() throws IOException {
64              try {
65                  return super.acquireIndexSearcher();
66              } finally {
67                  count++;
68              }
69          }
70  
71          @Override
72          public void releaseIndexSearcher(IndexSearcher is) throws IOException {
73              try {
74                  super.releaseIndexSearcher(is);
75              } finally {
76                  count--;
77              }
78          }
79      }
80  
81      @Override
82      protected void prepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
83          File repo = new File(getBasedir(), "src/test/repo");
84          context = new CountingIndexingContext(
85                  "test-minimal", "test", repo, indexDir, null, null, IndexCreatorSorter.sort(MIN_CREATORS), false);
86  
87          nexusIndexer.scan(context);
88      }
89  
90      private SearchEngine searchEngine;
91  
92      @Override
93      public void setUp() throws Exception {
94          super.setUp();
95  
96          searchEngine = lookup(SearchEngine.class);
97      }
98  
99      @Override
100     public void tearDown() throws Exception {
101         searchEngine = null;
102         super.tearDown();
103     }
104 
105     @Test
106     public void testExceptionInArtifactFilter() throws Exception {
107         Query q = nexusIndexer.constructQuery(MAVEN.GROUP_ID, "com.adobe.flexunit", SearchType.EXACT);
108         IteratorSearchRequest request = new IteratorSearchRequest(q);
109         request.setArtifactInfoFilter((ctx, ai) -> {
110             throw new RuntimeException();
111         });
112         request.setArtifactInfoPostprocessor((ctx, ai) -> {
113             throw new RuntimeException();
114         });
115 
116         try {
117             searchEngine.forceSearchIteratorPaged(request, Collections.singletonList(context));
118         } catch (RuntimeException e) {
119             // this is the point of this test
120         }
121 
122         assertEquals(0, ((CountingIndexingContext) context).count);
123     }
124 }