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 org.apache.lucene.search.BooleanClause;
23  import org.apache.lucene.search.BooleanQuery;
24  import org.apache.maven.index.context.IndexCreator;
25  import org.apache.maven.index.context.IndexingContext;
26  import org.apache.maven.index.expr.StringSearchExpression;
27  import org.apache.maven.index.packer.IndexPacker;
28  import org.apache.maven.index.packer.IndexPackingRequest;
29  import org.codehaus.plexus.PlexusTestCase;
30  import org.codehaus.plexus.util.FileUtils;
31  
32  import java.io.File;
33  import java.util.ArrayList;
34  import java.util.Arrays;
35  import java.util.List;
36  
37  /**
38   * @author Olivier Lamy
39   */
40  public class SearchWithAnEmptyIndexTest
41      extends PlexusTestCase
42  {
43      protected List<IndexCreator> indexCreators;
44  
45      private NexusIndexer nexusIndexer;
46  
47      static final String INDEX_ID1 = "osgi-test1";
48  
49      static final String INDEX_ID2 = "empty-repo";
50  
51      private IndexPacker indexPacker;
52  
53      @Override
54      public void setUp()
55          throws Exception
56      {
57          super.setUp();
58  
59          indexCreators = this.getContainer().lookupList( IndexCreator.class );
60  
61          nexusIndexer = this.lookup( NexusIndexer.class );
62  
63          indexPacker = this.lookup( IndexPacker.class );
64  
65          if ( !nexusIndexer.getIndexingContexts().isEmpty() )
66          {
67              for ( IndexingContext context : nexusIndexer.getIndexingContexts().values() )
68              {
69                  nexusIndexer.removeIndexingContext( context, true );
70              }
71          }
72      }
73  
74      public void testWithTwoContextWithOneEmptyFirstInContextsListSearchFlat()
75          throws Exception
76      {
77  
78          String repoPath = "target/test/empty-repo-for-searchtest";
79  
80          File emptyRepo = new File( getBasedir(), repoPath );
81  
82          if ( emptyRepo.exists() )
83          {
84              FileUtils.deleteDirectory( emptyRepo );
85          }
86  
87          emptyRepo.mkdirs();
88  
89          //createIndex( "/src/test/repo", repoPath + "/.index", INDEX_ID2 );
90          createIndex( repoPath, repoPath, INDEX_ID2 );
91  
92          createIndex( "src/test/repo-with-osgi", "target/test/repo-with-osgi/", INDEX_ID1 );
93  
94          try
95          {
96              BooleanQuery q = new BooleanQuery();
97  
98              q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME,
99                                                  new StringSearchExpression( "org.apache.karaf.features.command" ) ),
100                    BooleanClause.Occur.MUST );
101 
102             FlatSearchRequest request = new FlatSearchRequest( q );
103             assertEquals( 2, nexusIndexer.getIndexingContexts().values().size() );
104             request.setContexts( Arrays.asList( nexusIndexer.getIndexingContexts().get( INDEX_ID2 ),
105                                                 nexusIndexer.getIndexingContexts().get( INDEX_ID1 ) ) );
106 
107             FlatSearchResponse response = nexusIndexer.searchFlat( request );
108 
109             assertEquals( 1, response.getResults().size() );
110 
111             q = new BooleanQuery();
112 
113             q.add( nexusIndexer.constructQuery( OSGI.SYMBOLIC_NAME,
114                                                 new StringSearchExpression( "org.apache.karaf.features.core" ) ),
115                    BooleanClause.Occur.MUST );
116 
117             request = new FlatSearchRequest( q );
118             request.setContexts( new ArrayList( nexusIndexer.getIndexingContexts().values() ) );
119 
120             response = nexusIndexer.searchFlat( request );
121 
122             assertEquals( 2, response.getResults().size() );
123 
124             String term = "org.apache.karaf.features";
125 
126             q = new BooleanQuery();
127 
128             q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( term ) ),
129                    BooleanClause.Occur.SHOULD );
130             q.add( nexusIndexer.constructQuery( MAVEN.ARTIFACT_ID, new StringSearchExpression( term ) ),
131                    BooleanClause.Occur.SHOULD );
132             q.add( nexusIndexer.constructQuery( MAVEN.VERSION, new StringSearchExpression( term ) ),
133                    BooleanClause.Occur.SHOULD );
134             q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( term ) ),
135                    BooleanClause.Occur.SHOULD );
136             q.add( nexusIndexer.constructQuery( MAVEN.CLASSNAMES, new StringSearchExpression( term ) ),
137                    BooleanClause.Occur.SHOULD );
138 
139             request = new FlatSearchRequest( q );
140             request.setContexts( new ArrayList( nexusIndexer.getIndexingContexts().values() ) );
141 
142             response = nexusIndexer.searchFlat( request );
143 
144             System.out.println( " result size with term usage " + response.getResults().size() );
145 
146             assertEquals( 3, response.getResults().size() );
147 
148         }
149         finally
150         {
151             closeAllIndexs();
152         }
153     }
154 
155     /**
156      * both repos contains commons-cli so ensure we don't return duplicates
157      */
158     public void testSearchNoDuplicateArtifactInfo()
159         throws Exception
160     {
161 
162         String repoPathIndex = "target/test/repo-for-searchdupe";
163 
164         File emptyRepo = new File( getBasedir(), repoPathIndex );
165 
166         if ( emptyRepo.exists() )
167         {
168             FileUtils.deleteDirectory( emptyRepo );
169         }
170 
171         emptyRepo.mkdirs();
172 
173         //createIndex( "/src/test/repo", repoPath + "/.index", INDEX_ID2 );
174         createIndex( "/src/test/repo", repoPathIndex, INDEX_ID2 );
175 
176         createIndex( "src/test/repo-with-osgi", "target/test/repo-with-osgi/", INDEX_ID1 );
177 
178         try
179         {
180             BooleanQuery q = new BooleanQuery();
181 
182             q.add( nexusIndexer.constructQuery( MAVEN.GROUP_ID, new StringSearchExpression( "commons-cli" ) ),
183                    BooleanClause.Occur.MUST );
184 
185             q.add( nexusIndexer.constructQuery( MAVEN.PACKAGING, new StringSearchExpression( "jar" ) ),
186                    BooleanClause.Occur.MUST );
187 
188             q.add( nexusIndexer.constructQuery( MAVEN.CLASSIFIER, new StringSearchExpression( "sources" ) ),
189                    BooleanClause.Occur.MUST );
190 
191             FlatSearchRequest request = new FlatSearchRequest( q );
192             assertEquals( 2, nexusIndexer.getIndexingContexts().values().size() );
193             request.setContexts( Arrays.asList( nexusIndexer.getIndexingContexts().get( INDEX_ID2 ),
194                                                 nexusIndexer.getIndexingContexts().get( INDEX_ID1 ) ) );
195 
196             FlatSearchResponse response = nexusIndexer.searchFlat( request );
197 
198             assertEquals( 1, response.getResults().size() );
199 
200         }
201         finally
202         {
203             closeAllIndexs();
204         }
205     }
206 
207     private void closeAllIndexs()
208         throws Exception
209     {
210         for ( IndexingContext context : nexusIndexer.getIndexingContexts().values() )
211         {
212             context.close( true );
213         }
214     }
215 
216     private void createIndex( String filePath, String repoIndex, String contextId )
217         throws Exception
218     {
219 
220         File repo = new File( getBasedir(), filePath );
221 
222         File repoIndexDir = new File( getBasedir(), repoIndex + "/.index" );
223 
224         if ( repoIndexDir.exists() )
225         {
226             FileUtils.deleteDirectory( repoIndexDir );
227         }
228 
229         repoIndexDir.mkdirs();
230 
231         System.out.println(
232             "creating Index with id " + contextId + " path : " + filePath + " , indexPath " + repoIndex );
233 
234         IndexingContext indexingContext =
235             nexusIndexer.addIndexingContext( contextId, contextId, repo, repoIndexDir, "http://www.apache.org",
236                                              "http://www.apache.org/.index", indexCreators );
237         indexingContext.setSearchable( true );
238         nexusIndexer.scan( indexingContext, false );
239 
240         indexingContext.optimize();
241 
242         File managedRepository = new File( repoIndex );
243         final File indexLocation = new File( managedRepository, ".index" );
244         IndexPackingRequest request = new IndexPackingRequest( indexingContext, indexLocation );
245         indexPacker.packIndex( request );
246 
247     }
248 }