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.IOException;
24  import java.util.Collection;
25  import org.apache.lucene.index.Term;
26  import org.apache.lucene.search.PrefixQuery;
27  import org.apache.lucene.store.Directory;
28  import org.apache.lucene.store.FSDirectory;
29  import org.apache.lucene.store.RAMDirectory;
30  import org.apache.lucene.store.SimpleFSDirectory;
31  import org.apache.maven.index.ArtifactInfo;
32  import org.apache.maven.index.FlatSearchRequest;
33  import org.apache.maven.index.FlatSearchResponse;
34  import org.apache.maven.index.NexusIndexer;
35  import org.apache.maven.index.context.IndexingContext;
36  
37  public abstract class AbstractNexusIndexerTest
38      extends AbstractIndexCreatorHelper
39  {
40      protected NexusIndexer nexusIndexer;
41  
42      protected Directory indexDir = new RAMDirectory();
43  
44      protected IndexingContext context;
45  
46      @Override
47      protected void setUp()
48          throws Exception
49      {
50  //        indexDir = new SimpleFSDirectory(new File("/tmp/nexus-test"));
51          super.setUp();
52          // FileUtils.deleteDirectory( indexDir );
53          nexusIndexer = lookup( NexusIndexer.class );
54          prepareNexusIndexer( nexusIndexer );
55      }
56  
57      @Override
58      protected void tearDown()
59          throws Exception
60      {
61          unprepareNexusIndexer( nexusIndexer );
62          super.tearDown();
63          // TODO: Brian reported, does not work on Windows because of left open files?
64          // FileUtils.deleteDirectory( indexDir );
65      }
66  
67      protected abstract void prepareNexusIndexer( NexusIndexer nexusIndexer )
68          throws Exception;
69  
70      protected void unprepareNexusIndexer( NexusIndexer nexusIndexer )
71          throws Exception
72      {
73          nexusIndexer.removeIndexingContext( context, false );
74      }
75  
76      protected void assertGroup( int expected, String group, IndexingContext context )
77          throws IOException
78      {
79          // ArtifactInfo.UINFO - UN_TOKENIZED
80          // ArtifactInfo.GROUP_ID - TOKENIZED
81  
82          Term term = new Term( ArtifactInfo.GROUP_ID, group );
83          PrefixQuery pq = new PrefixQuery( term );
84  
85          // new WildcardQuery( //
86          // SpanTermQuery pq = new SpanTermQuery( term );
87          // PhraseQuery pq = new PhraseQuery();
88          // pq.add( new Term( ArtifactInfo.UINFO, group + "*" ) );
89  
90          FlatSearchResponse response = nexusIndexer.searchFlat( new FlatSearchRequest( pq, context ) );
91          Collection<ArtifactInfo> artifacts = response.getResults();
92          assertEquals( artifacts.toString(), expected, artifacts.size() );
93      }
94  
95  }