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.FileInputStream;
24  import java.io.InputStream;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.lucene.index.Term;
32  import org.apache.lucene.search.Query;
33  import org.apache.lucene.search.TermQuery;
34  import org.apache.lucene.store.Directory;
35  import org.apache.lucene.store.RAMDirectory;
36  import org.apache.maven.index.ArtifactInfo;
37  import org.apache.maven.index.ArtifactInfoGroup;
38  import org.apache.maven.index.FlatSearchRequest;
39  import org.apache.maven.index.FlatSearchResponse;
40  import org.apache.maven.index.GroupedSearchRequest;
41  import org.apache.maven.index.GroupedSearchResponse;
42  import org.apache.maven.index.NexusIndexer;
43  import org.apache.maven.index.search.grouping.GAGrouping;
44  import org.apache.maven.index.updater.DefaultIndexUpdater;
45  
46  /**
47   * @author Eugene Kuleshov
48   */
49  public class Index20081108RegressionTest
50      extends AbstractRepoNexusIndexerTest
51  {
52      @Override
53      protected void prepareNexusIndexer( NexusIndexer nexusIndexer )
54          throws Exception
55      {
56          context = nexusIndexer.addIndexingContextForced( "test", "test", null, indexDir, null, null, DEFAULT_CREATORS );
57  
58          InputStream is = new FileInputStream( getBasedir() + //
59              File.separator + "src" + //
60              File.separator + "test" + //
61              File.separator + "nexus-maven-repository-index.20081108.zip" );
62  
63          Directory tempDir = new RAMDirectory();
64          DefaultIndexUpdater.unpackIndexArchive( is, tempDir, context );
65          context.replace( tempDir );
66      }
67  
68      public void testExtension()
69          throws Exception
70      {
71          assertEquals( 31, context.getIndexReader().numDocs() );
72  
73          {
74              Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "qdox", SearchType.SCORED );
75              FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
76              assertEquals( response.getResults().toString(), 2, response.getTotalHits() );
77  
78              List<ArtifactInfo> list = new ArrayList<ArtifactInfo>( response.getResults() );
79              assertEquals( 2, list.size() );
80  
81              {
82                  ArtifactInfo ai = list.get( 0 );
83                  assertEquals( "1.6.1", ai.version );
84                  assertEquals( "jar", ai.fextension );
85                  assertEquals( "jar", ai.packaging );
86              }
87              {
88                  ArtifactInfo ai = list.get( 1 );
89                  assertEquals( "1.5", ai.version );
90                  assertEquals( "jar", ai.fextension );
91                  assertEquals( "jar", ai.packaging );
92              }
93          }
94          {
95              Query query = new TermQuery( new Term( ArtifactInfo.PACKAGING, "tar.gz" ) );
96              FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
97              assertEquals( response.getResults().toString(), 1, response.getTotalHits() );
98  
99              ArtifactInfo ai = response.getResults().iterator().next();
100             assertEquals( "tar.gz", ai.packaging );
101             assertEquals( "tar.gz", ai.fextension );
102         }
103         {
104             Query query = new TermQuery( new Term( ArtifactInfo.PACKAGING, "zip" ) );
105             FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( query ) );
106             assertEquals( response.getResults().toString(), 1, response.getTotalHits() );
107 
108             ArtifactInfo ai = response.getResults().iterator().next();
109             assertEquals( "zip", ai.packaging );
110             assertEquals( "zip", ai.fextension );
111         }
112     }
113 
114     @Override
115     public void testRootGroups()
116         throws Exception
117     {
118         Set<String> rootGroups = context.getRootGroups();
119         assertEquals( rootGroups.toString(), 8, rootGroups.size() );
120 
121         assertGroup( 2, "qdox", context );
122 
123         assertGroup( 1, "proptest", context );
124 
125         assertGroup( 1, "junit", context );
126 
127         assertGroup( 6, "commons-logging", context );
128 
129         assertGroup( 1, "regexp", context );
130 
131         assertGroup( 1, "commons-cli", context );
132 
133         assertGroup( 15, "org", context );
134 
135         assertGroup( 6, "org.slf4j", context );
136 
137         assertGroup( 3, "org.testng", context );
138 
139         assertGroup( 3, "org.apache", context );
140 
141         assertGroup( 1, "org.apache.directory", context );
142         assertGroup( 1, "org.apache.directory.server", context );
143 
144         assertGroup( 1, "org.apache.maven", context );
145         assertGroup( 1, "org.apache.maven.plugins", context );
146         assertGroup( 0, "org.apache.maven.plugins.maven-core-it-plugin", context );
147     }
148 
149     @Override
150     public void testSearchFlatPaged()
151         throws Exception
152     {
153         FlatSearchRequest request = new FlatSearchRequest( nexusIndexer.constructQuery( MAVEN.GROUP_ID, "org", SearchType.SCORED ) );
154 
155         // See MINDEXER-22
156         // Flat search is not pageable
157         // request.setStart( 0 );
158 
159         request.setCount( 50 );
160 
161         FlatSearchResponse response = nexusIndexer.searchFlat( request );
162 
163         assertEquals( response.getResults().toString(), 15, response.getTotalHits() );
164     }
165 
166     @Override
167     public void testSearchGroupedProblematicNames()
168         throws Exception
169     {
170 
171         // ----------------------------------------------------------------------------
172         // Artifacts with "problematic" names
173         // ----------------------------------------------------------------------------
174         {
175             // "-" in the name
176             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "commons-logg*", SearchType.SCORED );
177 
178             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
179 
180             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
181 
182             Map<String, ArtifactInfoGroup> r = response.getResults();
183 
184             assertEquals( 1, r.size() );
185 
186             ArtifactInfoGroup ig = r.values().iterator().next();
187 
188             assertEquals( "commons-logging : commons-logging", ig.getGroupKey() );
189 
190             assertEquals( ig.getArtifactInfos().toString(), 6, ig.getArtifactInfos().size() );
191         }
192 
193         {
194             // numbers and "-" in the name
195             Query q = nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, "jcl104-over-slf4*", SearchType.SCORED );
196 
197             GroupedSearchRequest request = new GroupedSearchRequest( q, new GAGrouping() );
198 
199             GroupedSearchResponse response = nexusIndexer.searchGrouped( request );
200             Map<String, ArtifactInfoGroup> r = response.getResults();
201 
202             assertEquals( 1, r.size() );
203 
204             ArtifactInfoGroup ig = r.values().iterator().next();
205 
206             assertEquals( ig.getArtifactInfos().toString(), 1, ig.getArtifactInfos().size() );
207 
208             assertEquals( "org.slf4j : jcl104-over-slf4j", ig.getGroupKey() );
209         }
210     }
211 
212     // See MINDEXER-22
213     // Flat search is not pageable
214 //    @Override
215 //    public void donttestPaging()
216 //        throws Exception
217 //    {
218 //        // we have 15 artifact for this search
219 //        int total = 15;
220 //
221 //        int pageSize = 4;
222 //
223 //        Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "org", SearchType.SCORED );
224 //
225 //        FlatSearchRequest req = new FlatSearchRequest( q );
226 //
227 //        // have page size of 4, that will make us 4 pages
228 //        req.setCount( pageSize );
229 //
230 //        List<ArtifactInfo> constructedPageList = new ArrayList<ArtifactInfo>();
231 //
232 //        int offset = 0;
233 //
234 //        while ( true )
235 //        {
236 //            req.setStart( offset );
237 //
238 //            FlatSearchResponse resp = nexusIndexer.searchFlat( req );
239 //
240 //            Collection<ArtifactInfo> p = resp.getResults();
241 //
242 //            assertEquals( p.toString(), total, resp.getTotalHits() );
243 //
244 //            assertEquals( Math.min( pageSize, total - offset ), p.size() );
245 //
246 //            constructedPageList.addAll( p );
247 //
248 //            offset += pageSize;
249 //
250 //            if ( offset > total )
251 //            {
252 //                break;
253 //            }
254 //        }
255 //
256 //        //
257 //        FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( q ) );
258 //        Collection<ArtifactInfo> onePage = response.getResults();
259 //
260 //        List<ArtifactInfo> onePageList = new ArrayList<ArtifactInfo>( onePage );
261 //
262 //        // onePage and constructedPage should hold equal elems in the same order
263 //        assertTrue( resultsAreEqual( onePageList, constructedPageList ) );
264 //    }
265 
266     @Override
267     public void testPurge()
268         throws Exception
269     {
270         // we have 14 artifact for this search
271         Query q = nexusIndexer.constructQuery( MAVEN.GROUP_ID, "org", SearchType.SCORED );
272         FlatSearchRequest request = new FlatSearchRequest( q );
273 
274         FlatSearchResponse response1 = nexusIndexer.searchFlat( request );
275         Collection<ArtifactInfo> p1 = response1.getResults();
276 
277         assertEquals( 15, p1.size() );
278 
279         context.purge();
280 
281         FlatSearchResponse response2 = nexusIndexer.searchFlat( request );
282         Collection<ArtifactInfo> p2 = response2.getResults();
283 
284         assertEquals( 0, p2.size() );
285     }
286 
287     @Override
288     public void testIdentify()
289     {
290         // skip test (sha1 field wasn't stored in the old index format)
291     }
292 
293 }