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.util.Set;
23  
24  import org.apache.lucene.index.Term;
25  import org.apache.lucene.search.WildcardQuery;
26  import org.apache.lucene.store.ByteBuffersDirectory;
27  import org.apache.lucene.store.Directory;
28  import org.apache.maven.index.context.IndexingContext;
29  import org.junit.Test;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  public class Nexus3177HitLimitChecks extends AbstractNexusIndexerTest {
34      protected File repo = new File(getBasedir(), "src/test/repo");
35  
36      protected Directory secondIndexDir = new ByteBuffersDirectory();
37  
38      protected IndexingContext secondContext;
39  
40      @Override
41      protected void prepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
42          context = nexusIndexer.addIndexingContext(
43                  "nexus-3177", "nexus-3177", repo, indexDir, null, null, DEFAULT_CREATORS);
44  
45          secondContext = nexusIndexer.addIndexingContext(
46                  "nexus-3177b", "nexus-3177b", repo, secondIndexDir, null, null, DEFAULT_CREATORS);
47  
48          nexusIndexer.scan(context);
49          nexusIndexer.scan(secondContext);
50      }
51  
52      @Override
53      protected void unprepareNexusIndexer(NexusIndexer nexusIndexer) throws Exception {
54          super.unprepareNexusIndexer(nexusIndexer);
55  
56          nexusIndexer.removeIndexingContext(secondContext, false);
57      }
58  
59      // ===================================================================
60      // NOTE: This test, with testing search limits lost it's original meaning,
61      // since version 4.1.0 there is no notion of hit limit.
62      // See http://jira.codehaus.org/browse/MINDEXER-14
63  
64      // Hence, some of the tests, that can keep still original semantics were updated and left in place
65      // but the two test explicitly testing LIMIT_EXCEEDED were just removed/commented out.
66  
67      @Test
68      public void testHitLimitNotReachedSingleContext() throws Exception {
69          WildcardQuery q = new WildcardQuery(new Term(ArtifactInfo.UINFO, "*testng*"));
70  
71          FlatSearchRequest request = new FlatSearchRequest(q);
72          request.setCount(5);
73          // request.setResultHitLimit( 5 );
74          request.getContexts().add(context);
75  
76          FlatSearchResponse response = nexusIndexer.searchFlat(request);
77          Set<ArtifactInfo> r = response.getResults();
78          assertEquals(r.toString(), 4, r.size());
79          assertEquals(r.toString(), 4, response.getTotalHitsCount());
80      }
81  
82      @Test
83      public void testHitLimitEqualSingleContext() throws Exception {
84          WildcardQuery q = new WildcardQuery(new Term(ArtifactInfo.UINFO, "*testng*"));
85  
86          FlatSearchRequest request = new FlatSearchRequest(q);
87          request.setCount(4);
88          // request.setResultHitLimit( 4 );
89          request.getContexts().add(context);
90  
91          FlatSearchResponse response = nexusIndexer.searchFlat(request);
92          Set<ArtifactInfo> r = response.getResults();
93          assertEquals(r.toString(), 4, r.size());
94          assertEquals(r.toString(), 4, response.getTotalHitsCount());
95      }
96  
97      // See NOTE above
98      // public void testHitLimitExceededSingleContext()
99      // throws Exception
100     // {
101     // WildcardQuery q = new WildcardQuery( new Term( ArtifactInfo.UINFO, "*testng*" ) );
102     //
103     // FlatSearchRequest request = new FlatSearchRequest( q );
104     // request.setResultHitLimit( 3 );
105     // request.getContexts().add( context );
106     //
107     // FlatSearchResponse response = nexusIndexer.searchFlat( request );
108     // Set<ArtifactInfo> r = response.getResults();
109     // assertEquals( r.toString(), 0, r.size() );
110     // assertEquals( r.toString(), AbstractSearchResponse.LIMIT_EXCEEDED, response.getTotalHits() );
111     // }
112 
113     @Test
114     public void testHitLimitNotReachedMultipleContexts() throws Exception {
115         WildcardQuery q = new WildcardQuery(new Term(ArtifactInfo.UINFO, "*testng*"));
116 
117         FlatSearchRequest request = new FlatSearchRequest(q);
118         request.setCount(9);
119         // request.setResultHitLimit( 9 );
120         request.setArtifactInfoComparator(ArtifactInfo.REPOSITORY_VERSION_COMPARATOR);
121         request.getContexts().add(context);
122         request.getContexts().add(secondContext);
123 
124         FlatSearchResponse response = nexusIndexer.searchFlat(request);
125         Set<ArtifactInfo> r = response.getResults();
126         assertEquals(r.toString(), 8, r.size());
127         assertEquals(r.toString(), 8, response.getTotalHitsCount());
128     }
129 
130     @Test
131     public void testHitLimitEqualMultipleContexts() throws Exception {
132         WildcardQuery q = new WildcardQuery(new Term(ArtifactInfo.UINFO, "*testng*"));
133 
134         FlatSearchRequest request = new FlatSearchRequest(q);
135         request.setCount(8);
136         // request.setResultHitLimit( 8 );
137         request.setArtifactInfoComparator(ArtifactInfo.REPOSITORY_VERSION_COMPARATOR);
138         request.getContexts().add(context);
139         request.getContexts().add(secondContext);
140 
141         FlatSearchResponse response = nexusIndexer.searchFlat(request);
142         Set<ArtifactInfo> r = response.getResults();
143         assertEquals(r.toString(), 8, r.size());
144         assertEquals(r.toString(), 8, response.getTotalHitsCount());
145     }
146 
147     // See NOTE above
148     // public void testHitLimitExceededMultipleContexts()
149     // throws Exception
150     // {
151     // WildcardQuery q = new WildcardQuery( new Term( ArtifactInfo.UINFO, "*testng*" ) );
152     //
153     // FlatSearchRequest request = new FlatSearchRequest( q );
154     // request.setResultHitLimit( 7 );
155     // request.setArtifactInfoComparator( ArtifactInfo.REPOSITORY_VERSION_COMPARATOR );
156     // request.getContexts().add( context );
157     // request.getContexts().add( secondContext );
158     //
159     // FlatSearchResponse response = nexusIndexer.searchFlat( request );
160     // Set<ArtifactInfo> r = response.getResults();
161     // assertEquals( r.toString(), 0, r.size() );
162     // assertEquals( r.toString(), AbstractSearchResponse.LIMIT_EXCEEDED, response.getTotalHits() );
163     // }
164 }