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