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